def test___init__with_tuples(self): l = KVL([('foo', 'hi'), ('bar', 666)]) self.assertEqual(2, len(l)) self.assertTrue(isinstance(l[0], KV)) self.assertEqual(KV('foo', 'hi'), l[0]) self.assertTrue(isinstance(l[1], KV)) self.assertEqual(KV('bar', 666), l[1])
def test_contains_key(self): l = KVL() l.append(KV('foo', 'hi')) l.append(KV('bar', '666')) self.assertTrue('foo' in l) self.assertTrue('bar' in l) self.assertFalse('not' in l)
def test___str__(self): self.assertEqual( 'foo: bar', str(SCE(KV('foo', 'bar'), SCO('<unittest>', 1), None))) self.assertEqual( 'foo[a=x,b=y]: bar', str( SCE(KV('foo', 'bar'), SCO('<unittest>', 1), KVL([('a', 'x'), ('b', 'y')]))))
def test_find_all_key(self): l = KVL() l.append(KV('foo', 'hi')) l.append(KV('bar', '666')) l.append(KV('foo', 'hi2')) found = l.find_all_key('foo') self.assertEqual([KV('foo', 'hi'), KV('foo', 'hi2')], found)
def test_find_annotation(self): self.assertEqual(('a', 'x'), SCE(KV('foo', 'bar'), SCO('<unittest>', 1), KVL([('a', 'x'), ('b', 'y')])).find_annotation('a')) self.assertEqual( None, SCE(KV('foo', 'bar'), SCO('<unittest>', 1), KVL([('a', 'x'), ('b', 'y')])).find_annotation('notthere'))
def test_has_annotation(self): self.assertEqual( True, SCE(KV('foo', 'bar'), SCO('<unittest>', 1), KVL([('a', 'x'), ('b', 'y')])).has_annotation('a')) self.assertEqual( False, SCE(KV('foo', 'bar'), SCO('<unittest>', 1), KVL([('a', 'x'), ('b', 'y')])).has_annotation('notthere'))
def test_find_by_key(self): l = KVL() l.append(KV('foo', 'hi')) l.append(KV('bar', '666')) found = l.find_by_key('foo') self.assertEqual(KV('foo', 'hi'), found) found = l.find_by_key('not') self.assertEqual(None, found)
def test_remove_key(self): l = KVL() l.append(KV('foo', 'hi')) l.append(KV('bar', '666')) self.assertEqual(2, len(l)) self.assertEqual(KV('foo', 'hi'), l[0]) self.assertEqual(KV('bar', '666'), l[1]) l.remove_key('bar') self.assertEqual(1, len(l)) self.assertEqual(KV('foo', 'hi'), l[0]) l.remove_key('foo') self.assertEqual(0, len(l))
def test___eq__(self): a = KVL() a.append(KV('foo', 'hi')) a.append(KV('bar', '666')) b = KVL() b.append(KV('apple', '6')) b.append(KV('kiwi', '7')) self.assertFalse(a == b) c = KVL() c.append(KV('foo', 'hi')) c.append(KV('bar', '666')) self.assertTrue(a == c) self.assertEqual(a, [KV('foo', 'hi'), KV('bar', '666')]) self.assertEqual([KV('foo', 'hi'), KV('bar', '666')], a)
def test_empty_value(self): self.maxDiff = None self.assertEqual([KV('foo', None)], self._parse('foo=')) self.assertEqual([KV('foo', None)], self._parse('foo= ')) self.assertEqual([KV('foo', None)], self._parse(' foo= ')) self.assertEqual([KV('foo', None), KV('bar', None)], self._parse('foo= bar=')) self.assertEqual([KV('foo', None), KV('bar', None)], self._parse('foo= bar='))
def test_empty_value_not_default(self): self.maxDiff = None self.assertEqual([KV('foo', 'X')], self._parse('foo=', empty_value='X')) self.assertEqual([KV('foo', 'X')], self._parse('foo= ', empty_value='X')) self.assertEqual([KV('foo', 'X')], self._parse(' foo= ', empty_value='X')) self.assertEqual([KV('foo', 'X'), KV('bar', 'X')], self._parse('foo= bar=', empty_value='X')) self.assertEqual([KV('foo', 'X'), KV('bar', 'X')], self._parse('foo= bar=', empty_value='X'))
def test_add(self): a = KVL() a.append(KV('foo', 'hi')) a.append(KV('bar', '666')) b = KVL() b.append(KV('apple', '6')) b.append(KV('kiwi', '7')) c = a + b self.assertEqual(4, len(c)) it = iter(c) self.assertEqual(KV('foo', 'hi'), next(it)) self.assertEqual(KV('bar', '666'), next(it)) self.assertEqual(KV('apple', '6'), next(it)) self.assertEqual(KV('kiwi', '7'), next(it))
def test_is_homogeneous(self): l1 = KVL() l1.append(KV('foo', 666)) l1.append(KV('bar', 667)) l1.append(KV('foo', 668)) self.assertEqual( True, l1.is_homogeneous(compat.STRING_TYPES, compat.INTEGER_TYPES)) l2 = KVL() l2.append(KV('foo', 666)) l2.append(KV('bar', '667')) l2.append(KV('foo', 668)) self.assertEqual( False, l2.is_homogeneous(compat.STRING_TYPES, compat.INTEGER_TYPES))
def test___cmp__(self): a = KVL() a.append(KV('foo', 'hi')) a.append(KV('bar', '666')) b = KVL() b.append(KV('apple', '6')) b.append(KV('kiwi', '7')) self.assertEqual(1, cmp(a, b)) c = KVL() c.append(KV('foo', 'hi')) c.append(KV('bar', '666')) self.assertEqual(0, cmp(a, c))
def test_is_instance(self): self.assertEqual( True, KV('foo', 666).is_instance(compat.STRING_TYPES, compat.INTEGER_TYPES))
def test_escaped_equal(self): self.assertEqual([KV('foo', 'bar=baz')], self._parse(r'foo=bar\=baz'))
def test_ignore_comments(self): self.assertEqual( [KV('foo', '#bar'), KV('fruit', '#orange')], self._parse('foo=#bar fruit=#orange', ignore_comments=True))
def test_parse_key_values(self): self.assertEqual( [KV('foo', '123'), KV('bar', 'hello')], self._parse('foo=123 bar=hello')) self.assertEqual( [KV('foo', '1 2 3'), KV('bar', 'hello')], self._parse('foo=\"1 2 3\" bar=hello')) self.assertEqual( [KV('foo', '1 2 3'), KV('bar', 'hello')], self._parse(' foo=\"1 2 3\" bar=hello ')) self.assertEqual( [KV('foo', '1 2 3'), KV('bar', 'hello')], self._parse(' foo=\"1 2 3\" bar=hello ')) self.assertEqual( [KV('f oo', '1 2 3'), KV('bar', 'hello')], self._parse(' f\\ oo=\"1 2 3\" bar=hello ')) self.assertEqual( [KV('foo', '1 2 3'), KV('bar', 'hello')], self._parse(r' foo=1\ 2\ 3 bar=hello '))
def test_from_dict(self): self.assertEqual(KVL([KV('a', 5), KV('b', 6)]), KVL.from_dict({ 'a': 5, 'b': 6 }))
def test_substitute_variables(self): l = KVL([('a', 'a is ${foo}'), ('b', 'b is ${bar}')]) l.substitute_variables({'foo': 'apple', 'bar': 'kiwi'}) self.assertEqual(KVL([KV('a', 'a is apple'), KV('b', 'b is kiwi')]), l)
def test_append(self): l = KVL() l.append(KV('foo', 'hi')) self.assertEqual(1, len(l)) self.assertEqual(KV('foo', 'hi'), l[0])
def test_to_string(self): l = KVL() l.append(KV('foo', 'hi')) l.append(KV('bar', '666')) self.assertEqual('foo=hi;bar=666', l.to_string())
def test_str(self): self.assertEqual('foo=666', str(KV('foo', 666)))