Tests for UnspacedList

This commit is contained in:
Peter Eckersley 2016-06-17 14:32:24 -07:00
parent efd1ff46c6
commit ba0a0e9c26
2 changed files with 65 additions and 9 deletions

View file

@ -164,16 +164,15 @@ spacey = lambda x: (isinstance(x, str) and x.isspace()) or x == ''
class UnspacedList(list):
"""Wrap a list [of lists], making any whitespace entries magically invisible"""
def __init__(self, list_source, top=False):
def __init__(self, list_source):
self.spaced = copy.deepcopy(list(list_source))
# Turn self into a version of the source list that has spaces removed
# and all sub-lists also UnspacedList()ed
list.__init__(self, list_source)
self.top = top if top else self
for i, entry in reversed(list(enumerate(self))):
if isinstance(entry, list):
sublist = UnspacedList(entry, top=self.top)
sublist = UnspacedList(entry)
list.__setitem__(self, i, sublist)
self.spaced[i] = sublist.spaced
elif spacey(entry):
@ -202,12 +201,9 @@ class UnspacedList(list):
list.extend(self, x)
def __add__(self, other):
if hasattr(other, "spaced"):
# If the thing added to us is an UnspacedList, use its spaced form
self.spaced.__add__(other.spaced)
else:
self.spaced.__add__(other)
list.__add__(self, other)
l = copy.deepcopy(self)
l.extend(other)
return l
def __setitem__(self, i, value):
if hasattr(value, "spaced"):
@ -220,6 +216,12 @@ class UnspacedList(list):
self.spaced.__delitem__(i + self._spaces_before(i))
list.__delitem__(self, i)
def __deepcopy__(self, memo):
l = UnspacedList(self[:])
l.spaced = copy.deepcopy(self.spaced)
return l
def _spaces_before(self, idx):
"Count the number of spaces in the spaced list before pos idx in the spaceless one"
spaces = 0

View file

@ -1,4 +1,5 @@
"""Test for certbot_nginx.nginxparser."""
import copy
import operator
import os
import unittest
@ -180,5 +181,58 @@ class TestRawNginxParser(unittest.TestCase):
[['set', '$webp "true"']]]
])
class TestUnspacedList(unittest.TestCase):
"""Test the raw low-level Nginx config parser."""
def setUp(self):
self.a = ["\n ", "things", " ", "quirk"]
self.b = ["y", " "]
self.l = self.a[:]
self.l2 = self.b[:]
self.ul = UnspacedList(self.l)
self.ul2 = UnspacedList(self.l2)
def test_construction(self):
self.assertEqual(self.ul, ["things", "quirk"])
self.assertEqual(self.ul2, ["y"])
def test_append(self):
ul3 = copy.deepcopy(self.ul)
ul3.append("wise")
self.assertEqual(ul3, ["things", "quirk", "wise"])
self.assertEqual(ul3.spaced, self.a + ["wise"])
def test_add(self):
ul3 = self.ul + self.ul2
self.assertEqual(ul3, ["things", "quirk", "y"])
self.assertEqual(ul3.spaced, self.a + self.b)
self.assertEqual(self.ul.spaced, self.a)
ul3 = self.ul + self.l2
self.assertEqual(ul3, ["things", "quirk", "y", " "])
self.assertEqual(ul3.spaced, self.a + self.b)
def test_extend(self):
ul3 = copy.deepcopy(self.ul)
ul3.extend(self.ul2)
self.assertEqual(ul3, ["things", "quirk", "y"])
self.assertEqual(ul3.spaced, self.a + self.b)
self.assertEqual(self.ul.spaced, self.a)
def test_set(self):
ul3 = copy.deepcopy(self.ul)
ul3[0] = "zither"
l = ["\n ", "zather", "zest"]
ul3[1] = UnspacedList(l)
self.assertEqual(ul3, ["zither", ["zather", "zest"]])
self.assertEqual(ul3.spaced, [self.a[0], "zither", " ", l])
def test_rawlists(self):
ul3 = copy.deepcopy(self.ul)
ul3.insert(0, "some")
ul3.append("why")
ul3.extend(["did", "whether"])
del ul3[2]
self.assertEqual(ul3, ["some", "things", "why", "did", "whether"])
if __name__ == '__main__':
unittest.main() # pragma: no cover