def test_mapping_node_2(self): node = node_from_lines(lines=[ Line(indent=0, tokens=[Value(data='key'), Separator(), Value(data='value')]), Line(indent=0, tokens=[ Value(data='key1'), Separator(), ]), Line(indent=1, tokens=[Value(data='key2'), Separator(), Value(data='value')]), ]) self.assertEqual( node, MappingNode( mapping={ ScalarNode(data='key'): ScalarNode(data='value'), ScalarNode(data='key1'): MappingNode( mapping={ ScalarNode(data='key2'): ScalarNode(data='value') }), }))
def test_from_object(self): obj = {'key': 'value'} expectedNode = MappingNode( mapping={ ScalarNode(data='key'): ScalarNode(data='value') } ) self.assertEqual( node_from_object(obj), expectedNode )
def test_sequence_node_2(self): node = node_from_lines(lines=[ Line(indent=1, tokens=[SequenceIndent(), Value(data='value')]), Line(indent=1, tokens=[SequenceIndent(), Value(data='value')]) ]) self.assertEqual( node, SequenceNode( items=[ScalarNode(data='value'), ScalarNode(data='value')]))
def test_from_object(self): obj = ['value', {'key': 'value'}] expectedNode = SequenceNode( items=[ ScalarNode(data='value'), MappingNode( mapping={ScalarNode(data='key'): ScalarNode(data='value')} ) ] ) self.assertEqual( node_from_object(obj), expectedNode )
def test_to_object_complex_specify_indent_size(self): node = MappingNode( mapping={ ScalarNode(data='key1'): ScalarNode(data='value'), ScalarNode(data='key'): MappingNode( mapping={ ScalarNode(data='key1'): ScalarNode(data='value'), ScalarNode(data='key'): MappingNode( mapping={ ScalarNode(data='key'): ScalarNode( data='value') }) }) }) expectedString = '''key1: value key: key1: value key: key: value ''' self.assertEqual(string_from_node(node, indentLevel=0, indentSize=4), expectedString)
def get_key_value_node(lines): if not lines: raise Exception('Lines needs to be non-empty') if not lines[0].is_mapping(): raise Exception(f'these lines are not a mapping {lines}') if len(lines) == 1: line = lines[0] keyNode = ScalarNode(data=line.tokens[0].data) data = line.tokens[2].data if len(line.tokens) > 2 else None valueNode = ScalarNode(data=data) return keyNode, valueNode else: keyNode = ScalarNode(data=lines[0].tokens[0].data) valueNode = node_from_lines(lines=lines[1:]) return keyNode, valueNode
def test_from_object(self): obj = 'value' expectedNode = ScalarNode(data=obj) self.assertEqual( node_from_object(obj), expectedNode )
def test_to_object(self): value = 'value string' node = ScalarNode(data=value) expectedObject = value self.assertEqual( object_from_node( node ), # sigh. I can't use keyword arguments with singledispatch functions expectedObject)
def scalar_node_from_lines(lines): return ScalarNode(data=lines[0].tokens[0].data)
def _1(obj) -> 'ScalarNode': return ScalarNode(data=obj)
def test_scalar_node(self): node = node_from_lines( lines=[Line(indent=0, tokens=[Value(data='value')])]) self.assertEqual(node, ScalarNode(data='value'))
def test_to_object(self): node = MappingNode( mapping={ScalarNode(data='key'): ScalarNode(data='value')}) expectedString = 'key: value\n' self.assertEqual(string_from_node(node, indentLevel=0), expectedString)
def test_to_object(self): mapping = {ScalarNode(data='key'): ScalarNode(data='value')} node = MappingNode(mapping=mapping) expectedObject = {'key': 'value'} self.assertEqual(object_from_node(node), expectedObject)
def test_to_object(self): items = [ScalarNode(data='value')] node = SequenceNode(items=items) expectedObject = ['value'] self.assertEqual(object_from_node(node), expectedObject)