def test_annotation_key_only(self): text = '''\ fruit name: lemon tart: true is_good[annotation1,annotation2]: true fruit name: apple tart: true is_good[annotation2]: true fruit name: watermelon tart: false is_good[annotation1]: true fruit name: strawberry tart: false is_good: true ''' s = simple_config.from_text(text) with self.assertRaises(simple_config.error): s.find_all_sections('foo') sections = s.find_all_sections('fruit') self.assertEqual( 4, len(sections) ) self.assertEqual( 'fruit', sections[0].header_.name ) self.assertEqual( 'lemon', sections[0].find_by_key('name') ) self.assertEqual( 'true', sections[0].find_by_key('tart') ) self.assertEqual( 'true', sections[0].find_by_key('is_good') ) self.assertEqual( KVL([ ( 'annotation1', None), ( 'annotation2', None ) ]), sections[0].find_entry('is_good').annotations ) self.assertEqual( 'fruit', sections[1].header_.name ) self.assertEqual( 'apple', sections[1].find_by_key('name') ) self.assertEqual( 'true', sections[1].find_by_key('tart') ) self.assertEqual( 'true', sections[1].find_by_key('is_good') ) self.assertEqual( KVL([ ( 'annotation2', None ) ]), sections[1].find_entry('is_good').annotations ) self.assertEqual( 'fruit', sections[2].header_.name ) self.assertEqual( 'watermelon', sections[2].find_by_key('name') ) self.assertEqual( 'false', sections[2].find_by_key('tart') ) self.assertEqual( 'true', sections[2].find_by_key('is_good') ) self.assertEqual( KVL([ ( 'annotation1', None ) ]), sections[2].find_entry('is_good').annotations ) self.assertEqual( 'fruit', sections[3].header_.name ) self.assertEqual( 'strawberry', sections[3].find_by_key('name') ) self.assertEqual( 'false', sections[3].find_by_key('tart') ) self.assertEqual( 'true', sections[3].find_by_key('is_good') ) self.assertEqual( None, sections[3].find_entry('is_good').annotations )
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_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_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_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___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___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___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_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___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_remove(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(KV('bar', '666')) self.assertEqual(1, len(l)) self.assertEqual(KV('foo', 'hi'), l[0])
def test_extends(self): text = '''\ fruit name: apple color: red base: fructose cheese name: brie type: creamy kiwi extends fruit color: green where: new zealand ''' s = simple_config.from_text(text) sections = s.find_all_sections('kiwi') self.assertEqual( 1, len(sections) ) self.assertEqual( 'kiwi', sections[0].header_.name ) self.assertEqual( 'fruit', sections[0].header_.extends ) self.assertEqual( 'green', sections[0].find_by_key('color') ) self.assertEqual( 'apple', sections[0].find_by_key('name') ) self.assertEqual( 'fructose', sections[0].find_by_key('base') ) self.assertEqual( { 'base': 'fructose', 'color': 'green', 'name': 'apple', 'where': 'new zealand', }, sections[0].to_dict() ) self.assertEqual( KVL([ ( 'name', 'apple'), ( 'color', 'red'), ( 'base', 'fructose'), ( 'color', 'green'), ( 'where', 'new zealand'), ]), sections[0].to_key_value_list() ) self.assertEqual( True, sections[0].has_key('base') ) self.assertEqual( True, sections[0].has_key('color') ) self.assertEqual( True, sections[0].has_key('name') )
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_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_from_dict(self): self.assertEqual(KVL([KV('a', 5), KV('b', 6)]), KVL.from_dict({ 'a': 5, 'b': 6 }))
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())