From db82b0f5828351f5c4430a0f3056ae8c13b2cc93 Mon Sep 17 00:00:00 2001 From: xi Date: Mon, 20 Feb 2006 20:46:35 +0000 Subject: [PATCH] Fix bugs in Resolver and add more tests. git-svn-id: http://svn.pyyaml.org/branches/pyyaml3000@54 18f92427-320e-0410-9341-c67f048884a3 --- lib/yaml/resolver.py | 8 ++++---- tests/test_appliance.py | 12 ++++++++++++ tests/test_structure.py | 46 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/lib/yaml/resolver.py b/lib/yaml/resolver.py index 7cff321..25218d2 100644 --- a/lib/yaml/resolver.py +++ b/lib/yaml/resolver.py @@ -38,7 +38,7 @@ class BaseResolver: self.resolve_scalar(path, node) elif isinstance(node, SequenceNode): self.resolve_sequence(path, node) - for index in len(node.value): + for index in range(len(node.value)): self.resolve_node(path+[node, index], node.value[index]) elif isinstance(node, MappingNode): self.resolve_mapping(path, node) @@ -46,17 +46,17 @@ class BaseResolver: self.resolve_node(path+[node, None], key) self.resolve_node(path+[node, key], value) - def resolve_scalar(self, node): + def resolve_scalar(self, path, node): if node.tag is None: node.tag = self.detect_scalar(node.value) if node.tag is None or node.tag == u'!': node.tag = self.DEFAULT_SCALAR_TAG - def resolve_sequence(self, node): + def resolve_sequence(self, path, node): if node.tag is None or node.tag == u'!': node.tag = self.DEFAULT_SEQUENCE_TAG - def resolve_mapping(self, node): + def resolve_mapping(self, path, node): if node.tag is None or node.tag == u'!': node.tag = self.DEFAULT_MAPPING_TAG diff --git a/tests/test_appliance.py b/tests/test_appliance.py index 6879036..12239eb 100644 --- a/tests/test_appliance.py +++ b/tests/test_appliance.py @@ -298,3 +298,15 @@ class CanonicalParser: self.parse_stream() return self.events + def get(self): + return self.events.pop(0) + + def check(self, *choices): + for choice in choices: + if isinstance(self.events[0], choice): + return True + return False + + def peek(self): + return self.events[0] + diff --git a/tests/test_structure.py b/tests/test_structure.py index 06ff6f3..15f485a 100644 --- a/tests/test_structure.py +++ b/tests/test_structure.py @@ -4,6 +4,9 @@ import test_appliance from yaml.reader import Reader from yaml.scanner import Scanner from yaml.parser import * +from yaml.composer import * +from yaml.resolver import * +from yaml.nodes import * class TestStructure(test_appliance.TestAppliance): @@ -96,9 +99,50 @@ class TestParser(test_appliance.TestAppliance): #self.failUnlessEqual(event1.tag, event2.tag) pass - TestParser.add_tests('testParser', '.data', '.canonical') +class TestResolver(test_appliance.TestAppliance): + + def _testResolver(self, test_name, data_filename, canonical_filename): + nodes1 = None + nodes2 = None + try: + resolver1 = Resolver(Composer(Parser(Scanner(Reader(file(data_filename, 'rb')))))) + nodes1 = list(iter(resolver1)) + canonical = test_appliance.CanonicalParser(file(canonical_filename, 'rb').read()) + canonical.parse() + resolver2 = Resolver(Composer(canonical)) + nodes2 = list(iter(resolver2)) + self.failUnlessEqual(len(nodes1), len(nodes2)) + for node1, node2 in zip(nodes1, nodes2): + self._compare(node1, node2) + except: + print + print "DATA1:" + print file(data_filename, 'rb').read() + print "DATA2:" + print file(canonical_filename, 'rb').read() + print "NODES1:", nodes1 + print "NODES2:", nodes2 + raise + + def _compare(self, node1, node2): + self.failUnlessEqual(node1.__class__, node2.__class__) + if isinstance(node1, ScalarNode): + #self.failUnlessEqual(node1.tag, node2.tag) + self.failUnlessEqual(node1.value, node2.value) + elif isinstance(node1, SequenceNode): + self.failUnlessEqual(len(node1.value), len(node2.value)) + for item1, item2 in zip(node1.value, node2.value): + self._compare(item1, item2) + elif isinstance(node1, MappingNode): + self.failUnlessEqual(len(node1.value), len(node2.value)) + for (key1, value1), (key2, value2) in zip(node1.value, node2.value): + self._compare(key1, key2) + self._compare(value1, value2) + +TestResolver.add_tests('testResolver', '.data', '.canonical') + class TestParserOnCanonical(test_appliance.TestAppliance): def _testParserOnCanonical(self, test_name, canonical_filename): -- 2.11.4.GIT