def test_construct_map_duplicate(self): mark = Mark(None, None, 8, 8, None, None) children = [ ( ScalarNode("tag:yaml.org,2002:str", "node1", start_mark=mark), ScalarNode("tag:yaml.org,2002:str", "node1", start_mark=mark), ), ( ScalarNode("tag:yaml.org,2002:str", "node1", start_mark=mark), ScalarNode("tag:yaml.org,2002:str", "node1", start_mark=mark), ) ] node = MappingNode("tag:yaml.org,2002:map", children, start_mark=mark) with pytest.raises(ConstructorError): res, = Constructor("map").construct_yaml_map(node)
def test_construct_map(self): mark = Mark(None, None, 8, 8, None, None) children = [( ScalarNode("tag:yaml.org,2002:str", str(i), start_mark=mark), ScalarNode("tag:yaml.org,2002:int", str(i), start_mark=mark), ) for i in range(2)] node = MappingNode(None, children, start_mark=mark) res, = Constructor("map").construct_yaml_map(node) assert { "0": 0, "1": 1 } == {k.value: v.value for k, v in res.value.items()} assert res.loc.line == 9 assert res.loc.column == 9 assert res.loc.stream_name == "map"
def test_unknown_tag_wrong_type_comparison(self): n1 = ScalarNode(YAML_NS + "xyz", "foo") n2 = SequenceNode(YAML_NS + "xyz", [ystr("foo")]) self.assertFalse(nodes_equal(n1, n2)) n1 = Node(YAML_NS + "xyz", "foo", None, None) n2 = Node(YAML_NS + "xyz", "foo", None, None) self.assertFalse(nodes_equal(n1, n2))
def test_interpolate_str(self): from yaml.nodes import ScalarNode import StringIO context = DummyContext() loader = DummyLoader(context) node = ScalarNode('foo', 'scalar') f = StringIO.StringIO() loader = self._makeOne(context, f, lambda *arg: ()) self.assertEqual(loader.interpolate_str(loader, node), 'scalar')
def test_construct_int(self, value, parse): mark = Mark(None, None, 3, 7, None, None) node = ScalarNode(None, value, start_mark=mark) res = Constructor("int").construct_yaml_int(node) assert res.value == parse assert res.loc.line == 4 assert res.loc.column == 8 assert res.loc.stream_name == "int"
def test_construct_bool_true(self, value, parse): mark = Mark(None, None, 4, 5, None, None) node = ScalarNode(None, value, start_mark=mark) res = Constructor("bool").construct_yaml_bool(node) assert res.value == parse assert res.loc.line == 5 assert res.loc.column == 6 assert res.loc.stream_name == "bool"
def test_construct_str(self, value): mark = Mark(None, None, 0, 0, None, None) node = ScalarNode(None, value, start_mark=mark) res = Constructor("str").construct_yaml_str(node) assert res.value == value assert res.loc.line == 1 assert res.loc.column == 1 assert res.loc.stream_name == "str"
def test_construct_null(self, value): mark = Mark(None, None, 1, 2, None, None) node = ScalarNode(None, value, start_mark=mark) res = Constructor("null").construct_yaml_null(node) assert res.value is None assert res.loc.line == 2 assert res.loc.column == 3 assert res.loc.stream_name == "null"
def test_construct_float_nan_only(self, value): mark = Mark(None, None, 7, 1, None, None) node = ScalarNode(None, value, start_mark=mark) res = Constructor("float").construct_yaml_float(node) assert math.isnan(res.value) assert res.loc.line == 8 assert res.loc.column == 2 assert res.loc.stream_name == "float"
def test_construct_float_no_nan(self, value, parse): mark = Mark(None, None, 2, 8, None, None) node = ScalarNode(None, value, start_mark=mark) res = Constructor("float").construct_yaml_float(node) assert res.value == parse assert res.loc.line == 3 assert res.loc.column == 9 assert res.loc.stream_name == "float"
def test_unknown_tag_comparison(self): n1 = ScalarNode(YAML_NS + "xyz", "foo") n2 = ScalarNode(YAML_NS + "xyz", "foo") n3 = ScalarNode(YAML_NS + "xyz", "bar") self.assertTrue(nodes_equal(n1, n2)) self.assertFalse(nodes_equal(n1, n3)) n1 = SequenceNode(YAML_NS + "xyz", [ystr("foo")]) n2 = SequenceNode(YAML_NS + "xyz", [ystr("foo")]) n3 = SequenceNode(YAML_NS + "xyz", [ystr("bar")]) self.assertTrue(nodes_equal(n1, n2)) self.assertFalse(nodes_equal(n1, n3)) n1 = MappingNode(YAML_NS + "xyz", [(ystr("foo"), ystr("bar"))]) n2 = MappingNode(YAML_NS + "xyz", [(ystr("foo"), ystr("bar"))]) n3 = MappingNode(YAML_NS + "xyz", [(ystr("foo"), ystr("baz"))]) self.assertTrue(nodes_equal(n1, n2)) self.assertFalse(nodes_equal(n1, n3))
def test_interpolate_str_exc(self): from repoze.configuration.exceptions import ConfigurationError from yaml.nodes import ScalarNode import StringIO context = DummyContext(interpolation_exc=True) loader = DummyLoader(context) node = ScalarNode('foo', 'scalar', DummyMark(), DummyMark()) f = StringIO.StringIO() loader = self._makeOne(context, f, lambda *arg: ()) self.assertRaises(ConfigurationError, loader.interpolate_str, loader, node)
def test_set_comparison(self): null = ScalarNode(YAML_NULL_TAG, "") n1 = MappingNode(YAML_SET_TAG, [(ystr("a"), null), (ystr("b"), null)]) n2 = MappingNode(YAML_SET_TAG, [(ystr("b"), null), (ystr("a"), null)]) n3 = MappingNode(YAML_SET_TAG, [(ystr("b"), null), (ystr("a"), null), (ystr("c"), null)]) n4 = MappingNode(YAML_SET_TAG, [(ystr("b"), null), (ystr("a"), null), (ystr("d"), null)]) self.assertTrue(nodes_equal(n1, n2)) self.assertFalse(nodes_equal(n1, n3)) self.assertFalse(nodes_equal(n3, n1)) self.assertFalse(nodes_equal(n3, n4)) self.assertFalse(nodes_equal(n4, n3))
def test_construct_seq(self): mark = Mark(None, None, 4, 3, None, None) children = [ ScalarNode("tag:yaml.org,2002:int", str(i), start_mark=mark) for i in range(4) ] node = SequenceNode(None, children, start_mark=mark) # Sequences are single-element generators (lazy parsing) that we need # to exhaust. Simplest way of doing this is to assign to a single # element tuple and let the python do all the iterator funky-ness. res, = Constructor("seq").construct_yaml_seq(node) assert [0, 1, 2, 3] == [child.value for child in res.value] assert res.loc.line == 5 assert res.loc.column == 4 assert res.loc.stream_name == "seq"
def test_unequal_tags(self): n1 = ScalarNode(YAML_STR_TAG, "foo") n2 = ScalarNode(YAML_NS + "bar", "foo") self.assertFalse(nodes_equal(n1, n2))
def ystr(x): return ScalarNode(YAML_STR_TAG, x)