class EnvTest(unittest.TestCase): def setUp(self): self.env = Env() self.parser = Parser() def tearDown(self): None def test_prefix_binding(self): prefix_uri = Uri('http://test.prefix.eg/') prefix = 'prefix' self.env.bind_prefix(prefix, prefix_uri) self.assertTrue('prefix' in [p for (p, n) in self.env._rdf._g.namespaces()]) def test_get_and_set_default_prefix(self): prefix = 'x' self.env.bind_prefix(prefix, Uri('http://eg/')) before = Uri(self.env._rdf._g.identifier.toPython()) self.assertEqual(before, self.env.uri) self.assertEqual(self.env.prefix, None) self.env.prefix = prefix self.env.uri = Uri('http://eg/') self.assertEqual(Uri('http://eg/'), self.env.uri) self.assertEqual('x', self.env.prefix) def test_self_uri_set(self): uri = Uri('setselfuri') self.env.self_uri = uri self.assertEqual(self.env.self_uri, uri) def test_assignment(self): uri = Uri('http://test.variable/#x') value = Value(12345) self.env.assign(uri, value) self.assertEqual(self.env._symbol_table.get(uri), value) def test_lookup(self): uri = Uri('http://test.variable/#x') value = Value(12345) self.env.assign(uri, value) self.assertEqual(self.env.lookup(uri), value) def test_template_binding(self): template = self.parser.parse('t()(x = 1 y = 2)')[0] uri = template.identifier.evaluate(self.env) with self.assertRaises(KeyError): self.env.lookup_template(uri) self.env.assign_template(uri, template.as_triples(self.env)) self.assertEqual(self.env._template_table.get(uri, False), template.as_triples(self.env)) def test_template_lookup(self): template = self.parser.parse('t()(x = 1 y = 2)')[0] uri = template.identifier.evaluate(self.env) with self.assertRaises(KeyError): self.env.lookup_template(uri) self.env._template_table[uri] = template.as_triples(self.env) self.assertEqual(self.env.lookup_template(uri), template.as_triples(self.env)) def test_extension_binding(self): t = self.parser.parse('t()(@extension E() @extension F())')[0] uri = t.identifier.evaluate(self.env) extensions = t.extensions self.assertFalse(self.env.lookup_extensions(uri)) self.env.assign_extensions(uri, extensions) self.assertEqual(extensions, self.env._extension_table.get(uri, None)) def test_extension_lookup(self): t = self.parser.parse('t()(@extension E() @extension F())')[0] uri = t.identifier.evaluate(self.env) extensions = t.extensions self.assertFalse(self.env.lookup_extensions(uri)) self.env._extension_table[uri] = extensions self.assertEqual(extensions, self.env.lookup_extensions(uri))
class TestCoreIdentifier(unittest.TestCase): def setUp(self): self.env = Env() self.env.current_self = Uri('') self.env.bind_prefix('p', Uri('prefix')) self.env.prefix = 'p' def test_identifier_equal(self): i1 = Identifier(Name('n1'), Name('n2'), Name('n3')) i2 = Identifier(Name('n1'), Name('n2'), Name('n3')) self.assertEqual(i1, i2) i3 = Identifier(Name('n1'), Name('n2')) self.assertNotEqual(i1, i3) def test_identifier_evaluate_unbound_single_name(self): i = Identifier(Name('first')) expected = Uri('prefixfirst') actually = i.evaluate(self.env) self.assertEqual(expected, actually) def test_identifier_evaluate_unbound_two_names(self): i = Identifier(Name('first'), Name('second')) expected = Uri('prefixfirstsecond') actually = i.evaluate(self.env) self.assertEqual(expected, actually) def test_identifier_evaluate_unbound_self_as_prefix(self): i = Identifier(Self(), Name('n')) expected = Identifier(Self(), Name('n')) actually = i.evaluate(self.env) self.assertEqual(expected, actually) def test_identifier_evaluate_multiple_unbound(self): i = Identifier(Name('n1'), Name('n2'), Name('n3'), Name('n4')) expected = Uri('prefixn1n2n3n4') actually = i.evaluate(self.env) self.assertEqual(expected, actually) def test_identifier_evaluate_bound_local(self): i = Identifier(Name('first')) expected = Value(1) self.env.assign(i.evaluate(self.env), expected) actually = i.evaluate(self.env) self.assertEqual(expected, actually) def test_identifier_evaluate_bound_prefixed(self): i = Identifier(Name('first'), Name('second')) expected = Value(1) self.env.assign(i.evaluate(self.env), expected) actually = i.evaluate(self.env) self.assertEqual(expected, actually) def test_identifier_evaluate_bound_multiple_parts(self): i = Identifier(Name('first'), Name('second'), Name('third'), Name('fourth')) expected = Value(1) self.env.assign(i.evaluate(self.env), expected) actually = i.evaluate(self.env) self.assertEqual(expected, actually) def test_identifier_evaluate_uri_and_symbol_parts(self): i = Identifier(Uri('first'), Name('second')) expected = Value(1) uri = Uri('firstsecond') self.assertEqual(i.evaluate(self.env), uri) self.env.assign(i.evaluate(self.env), expected) actually = i.evaluate(self.env) self.assertEqual(expected, actually) def test_identifier_evaluate_bound_prefix(self): i = Identifier(Name('first'), Name('second')) prefix = Uri('http://first.org/#') self.env.assign(Identifier(Name('first')).evaluate(self.env), prefix) expected = Uri('http://first.org/#second') actually = i.evaluate(self.env) self.assertEqual(expected, actually) def test_identifier_evaluate_bound_double_prefix(self): i = Identifier(Name('first'), Name('second'), Name('third')) prefix = Uri('http://first.org/#second#') uri = Identifier(Name('first'), Name('second')).evaluate(self.env) self.env.assign(uri, prefix) expected = Uri('http://first.org/#second#third') actually = i.evaluate(self.env) self.assertEqual(expected, actually) def test_identifier_evaluate_bound_prefix_not_uri(self): i = Identifier(Name('first'), Name('second')) value = Value(12345) self.env.assign(Identifier(Name('first')).evaluate(self.env), value) expected = Uri('prefixfirstsecond') actually = i.evaluate(self.env) self.assertEqual(expected, actually) def test_identifier_evaluate_double_bound_prefix_not_uri(self): i = Identifier(Name('first'), Name('second'), Name('third')) value = Value(12345) prefix_part = Identifier(Name('first'), Name('second')) self.env.assign(prefix_part.evaluate(self.env), value) expected = Uri('prefixfirstsecondthird') actually = i.evaluate(self.env) self.assertEqual(expected, actually) def test_identifier_uri_first_unbound(self): i = Identifier(Uri('http://literal.eg/'), Name('literal')) expected = Uri('http://literal.eg/literal') actually = i.evaluate(self.env) self.assertEqual(expected, actually)