def test_from_str(self): a = QN('a') b = QN('b') a_dot_b = QN(a, attr='b') a_sub_b = QN(a, subscript=b) self.assertEqual(qual_names.from_str('a.b'), a_dot_b) self.assertEqual(qual_names.from_str('a'), a) self.assertEqual(qual_names.from_str('a[b]'), a_sub_b)
def test_basic(self): scope = activity.Scope(None) self.assertMissing(QN('foo'), scope) scope.mark_read(QN('foo')) self.assertReadOnly(QN('foo'), scope) scope.mark_modified(QN('foo')) self.assertReadWrite(QN('foo'), scope)
def test_copy_of(self): scope = activity.Scope(None) scope.mark_read(QN('foo')) self.assertTrue(QN('foo') in activity.Scope.copy_of(scope).used) child_scope = activity.Scope(scope) child_scope.mark_read(QN('bar')) self.assertTrue(QN('bar') in activity.Scope.copy_of(child_scope).used)
def test_copy_of(self): scope = activity.Scope(None) scope.mark_read(QN('foo')) other = activity.Scope.copy_of(scope) self.assertReadOnly(QN('foo'), other) child_scope = activity.Scope(scope) child_scope.mark_read(QN('bar')) other = activity.Scope.copy_of(child_scope) self.assertReadOnly(QN('bar'), other)
def test_comparison(self): less_than_apos = chr(ord('\'') - 1) self.assertGreater(QN('z'), QN(qual_names.Literal('a'))) self.assertLess(QN(less_than_apos), QN(qual_names.Literal('a'))) self.assertGreater(QN(qual_names.Literal('z')), QN(less_than_apos)) self.assertLess(QN(qual_names.Literal('a')), QN('z'))
def test_subscripts(self): a = QN('a') b = QN('b') a_sub_b = QN(a, subscript=b) self.assertEqual(a_sub_b.qn, (a, b)) self.assertEqual(str(a_sub_b), 'a[b]') self.assertEqual(a_sub_b.ssf(), 'a_sub_b') self.assertEqual(a_sub_b.ast().value.id, 'a') self.assertEqual(a_sub_b.ast().slice.value.id, 'b') self.assertTrue(a_sub_b.is_composite()) self.assertTrue(a_sub_b.has_subscript()) self.assertEqual(a_sub_b.parent.qn, ('a', )) c = QN('c') b_sub_c = QN(b, subscript=c) a_sub_b_sub_c = QN(a, subscript=b_sub_c) self.assertEqual(a_sub_b_sub_c.qn, (a, b_sub_c)) self.assertTrue(a_sub_b.is_composite()) self.assertTrue(a_sub_b_sub_c.is_composite()) self.assertTrue(a_sub_b.has_subscript()) self.assertTrue(a_sub_b_sub_c.has_subscript()) self.assertEqual(b_sub_c.qn, (b, c)) self.assertEqual(str(a_sub_b_sub_c), 'a[b[c]]') self.assertEqual(a_sub_b_sub_c.ssf(), 'a_sub_b_sub_c') self.assertEqual(a_sub_b_sub_c.ast().value.id, 'a') self.assertEqual(a_sub_b_sub_c.ast().slice.value.value.id, 'b') self.assertEqual(a_sub_b_sub_c.ast().slice.value.slice.value.id, 'c') self.assertEqual(b_sub_c.ast().slice.value.id, 'c') self.assertEqual(a_sub_b_sub_c.parent.qn, ('a', )) with self.assertRaises(ValueError): QN('a', 'b')
def test_basic(self): a = QN('a') self.assertEqual(a.qn, ('a', )) self.assertEqual(str(a), 'a') self.assertEqual(a.ssf(), 'a') self.assertEqual(a.ast().id, 'a') self.assertFalse(a.is_composite()) with self.assertRaises(ValueError): _ = a.parent a_b = QN(a, attr='b') self.assertEqual(a_b.qn, (a, 'b')) self.assertEqual(str(a_b), 'a.b') self.assertEqual(a_b.ssf(), 'a_b') self.assertEqual(a_b.ast().value.id, 'a') self.assertEqual(a_b.ast().attr, 'b') self.assertTrue(a_b.is_composite()) self.assertEqual(a_b.parent.qn, ('a', ))
def test_referenced(self): scope = activity.Scope(None) scope.mark_read(QN('a')) child = activity.Scope(scope) child.mark_read(QN('b')) child2 = activity.Scope(child, isolated=False) child2.mark_read(QN('c')) self.assertTrue(QN('c') in child2.referenced) self.assertTrue(QN('b') in child2.referenced) self.assertFalse(QN('a') in child2.referenced) self.assertTrue(QN('c') in child.referenced) self.assertTrue(QN('b') in child.referenced) self.assertFalse(QN('a') in child.referenced)
def test_literals(self): a = QN('a') a_sub_str_b = QN(a, subscript=QN(qual_names.StringLiteral('b'))) a_sub_b = QN(a, subscript=QN('b')) self.assertNotEqual(a_sub_str_b, a_sub_b) self.assertNotEqual(hash(a_sub_str_b), hash(a_sub_b)) a_sub_three = QN(a, subscript=QN(qual_names.NumberLiteral(3))) self.assertEqual(a_sub_three.ast().slice.value.n, 3)
def test_equality(self): a = QN('a') a2 = QN('a') a_b = QN(a, attr='b') self.assertEqual(a2.qn, ('a', )) with self.assertRaises(ValueError): _ = a.parent a_b2 = QN(a, attr='b') self.assertEqual(a_b2.qn, (a, 'b')) self.assertEqual(a_b2.parent.qn, ('a', )) self.assertTrue(a2 == a) self.assertFalse(a2 is a) self.assertTrue(a_b.parent == a) self.assertTrue(a_b2.parent == a) self.assertTrue(a_b2 == a_b) self.assertFalse(a_b2 is a_b) self.assertFalse(a_b2 == a) a_sub_b = QN(a, subscript='b') a_sub_b2 = QN(a, subscript='b') self.assertTrue(a_sub_b == a_sub_b2) self.assertFalse(a_sub_b == a_b)
def test_nesting(self): scope = activity.Scope(None) scope.mark_write(QN('foo')) scope.mark_read(QN('bar')) child = activity.Scope(scope) self.assertTrue(child.has(QN('foo'))) self.assertTrue(scope.has(QN('foo'))) child.mark_write(QN('bar')) self.assertTrue(child.has(QN('bar'))) self.assertFalse(scope.has(QN('bar')))
def test_basic(self): scope = activity.Scope(None) self.assertFalse(scope.has(QN('foo'))) scope.mark_read(QN('foo')) self.assertFalse(scope.has(QN('foo'))) scope.mark_write(QN('foo')) self.assertTrue(scope.has(QN('foo'))) scope.mark_read(QN('bar')) self.assertFalse(scope.has(QN('bar')))
def test_literals(self): a = QN('a') a_sub_str_b = QN(a, subscript=QN(qual_names.Literal('b'))) a_sub_b = QN(a, subscript=QN('b')) self.assertNotEqual(a_sub_str_b, a_sub_b) self.assertNotEqual(hash(a_sub_str_b), hash(a_sub_b)) self.assertEqual(a_sub_str_b.ast().slice.value, 'b') self.assertEqual(str(a_sub_str_b), "a['b']") a_sub_three = QN(a, subscript=QN(qual_names.Literal(3))) self.assertEqual(a_sub_three.ast().slice.value, 3) self.assertEqual(str(a_sub_three), 'a[3]')
def test_support_set(self): a = QN('a') b = QN('b') c = QN('c') a_sub_b = QN(a, subscript=b) a_dot_b = QN(a, attr='b') a_dot_b_dot_c = QN(a_dot_b, attr='c') a_dot_b_sub_c = QN(a_dot_b, subscript=c) self.assertSetEqual(a.support_set, set((a, ))) self.assertSetEqual(a_sub_b.support_set, set((a, b))) self.assertSetEqual(a_dot_b.support_set, set((a, ))) self.assertSetEqual(a_dot_b_dot_c.support_set, set((a, ))) self.assertSetEqual(a_dot_b_sub_c.support_set, set((a, c)))
def test_copy_from(self): scope = activity.Scope(None) scope.mark_modified(QN('foo')) other = activity.Scope(None) other.copy_from(scope) self.assertWriteOnly(QN('foo'), other) scope.mark_modified(QN('bar')) scope.copy_from(other) self.assertMissing(QN('bar'), scope) scope.mark_modified(QN('bar')) scope.merge_from(other) self.assertWriteOnly(QN('bar'), scope) self.assertMissing(QN('bar'), other)
def test_copy_from(self): scope = activity.Scope(None) scope.mark_write(QN('foo')) other = activity.Scope(None) other.copy_from(scope) self.assertTrue(QN('foo') in other.modified) scope.mark_write(QN('bar')) scope.copy_from(other) self.assertFalse(QN('bar') in scope.modified) scope.mark_write(QN('bar')) scope.merge_from(other) self.assertTrue(QN('bar') in scope.modified) self.assertFalse(QN('bar') in other.modified)
def test_hashable(self): d = {QN('a'): 'a', QN('b'): 'b'} self.assertEqual(d[QN('a')], 'a') self.assertEqual(d[QN('b')], 'b') self.assertNotIn(QN('c'), d)
def test_hashable(self): d = {QN('a'): 'a', QN('b'): 'b'} self.assertEqual(d[QN('a')], 'a') self.assertEqual(d[QN('b')], 'b') self.assertTrue(QN('c') not in d)
def test_nested_attrs_subscripts(self): a = QN('a') b = QN('b') c = QN('c') b_sub_c = QN(b, subscript=c) a_sub_b_sub_c = QN(a, subscript=b_sub_c) b_dot_c = QN(b, attr='c') a_sub__b_dot_c = QN(a, subscript=b_dot_c) a_sub_b = QN(a, subscript=b) a_sub_b__dot_c = QN(a_sub_b, attr='c') a_dot_b = QN(a, attr='b') a_dot_b_sub_c = QN(a_dot_b, subscript=c) self.assertEqual(str(a_sub_b_sub_c), 'a[b[c]]') self.assertEqual(str(a_sub__b_dot_c), 'a[b.c]') self.assertEqual(str(a_sub_b__dot_c), 'a[b].c') self.assertEqual(str(a_dot_b_sub_c), 'a.b[c]') self.assertNotEqual(a_sub_b_sub_c, a_sub__b_dot_c) self.assertNotEqual(a_sub_b_sub_c, a_sub_b__dot_c) self.assertNotEqual(a_sub_b_sub_c, a_dot_b_sub_c) self.assertNotEqual(a_sub__b_dot_c, a_sub_b__dot_c) self.assertNotEqual(a_sub__b_dot_c, a_dot_b_sub_c) self.assertNotEqual(a_sub_b__dot_c, a_dot_b_sub_c)