Esempio n. 1
0
    def test_repr(self):
        # Subclass elements to demonstrate dispatch occurs correctly.
        class Face1(grammar.TypeExpression):
            def __repr__(self):
                return "-_-"

        class Exclaim(grammar.TypeExpression):
            def __repr__(self):
                return '!'

        class Face2(grammar.Predicate):
            def __repr__(self):
                return '(o_o)'

        self.assertEqual(repr(grammar.TypeExpression('!')), '!')
        self.assertEqual(
            repr(grammar.TypeExpression('!', fields=(Face1(''), ))), '![-_-]')
        self.assertEqual(
            repr(grammar.TypeExpression('!',
                                        fields=(Face1(''), Exclaim('!')))),
            '![-_-, !]')
        self.assertEqual(
            repr(
                grammar.TypeExpression('!',
                                       fields=(Face1(''), Exclaim('!')),
                                       predicate=Face2(True))),
            '![-_-, !] % (o_o)')

        self.assertEqual(
            repr(grammar.TypeExpression('(o_-)', predicate=Face2(True))),
            '(o_-) % (o_o)')
Esempio n. 2
0
    def test_several(self):
        X = grammar.TypeExpression('X')
        Y = grammar.TypeExpression('Y')
        Z = grammar.TypeExpression('Z')

        self.assertIsInstance(X & Y & Z, grammar.IntersectionTypeExpression)
        self.assertEqual(X & Y & Z & X & Z, Y & Z & X)
Esempio n. 3
0
    def test_several(self):
        X = grammar.TypeExpression('X')
        Y = grammar.TypeExpression('Y')
        Z = grammar.TypeExpression('Z')

        self.assertIsInstance(X | Y | Z, grammar.UnionTypeExpression)
        self.assertEqual(X | Y | Z | X | Z, Y | Z | X)
Esempio n. 4
0
    def test_mod(self):
        X = grammar.TypeExpression('X')
        with self.assertRaisesRegex(TypeError, 'fields'):
            X['scikit-bio/assets/.no.gif']

        Y = grammar.TypeExpression('Y', fields=(X, ))
        with self.assertRaisesRegex(TypeError, 'fields'):
            Y[';-)']
Esempio n. 5
0
    def test_apply_predicate(self):
        predicate = grammar.Predicate(True)
        Y = grammar.TypeExpression('Y')
        X = grammar.TypeExpression('X', fields=(Y, ))

        result = X._apply_predicate_(predicate)
        self.assertIsInstance(result, grammar.TypeExpression)
        self.assertEqual(result.fields, (Y, ))
Esempio n. 6
0
    def test_is_subtype_matches(self):
        X = grammar.TypeExpression('X')
        X_ = grammar.TypeExpression('X')

        self.assertTrue(X._is_subtype_(X))
        self.assertTrue(X_._is_subtype_(X))
        self.assertTrue(X._is_subtype_(X_))
        self.assertTrue(X_._is_subtype_(X_))
Esempio n. 7
0
    def test_is_subtype_matches_w_fields(self):
        F1 = grammar.TypeExpression('F1')
        F2 = grammar.TypeExpression('F2')
        X = grammar.TypeExpression('X', fields=(F1, ))
        X_ = grammar.TypeExpression('X', fields=(F2, ))

        self.assertFalse(X_._is_subtype_(X))
        self.assertFalse(X._is_subtype_(X_))
Esempio n. 8
0
    def test_hashable(self):
        a = grammar.TypeExpression('X')
        b = grammar.TypeExpression('Y', fields=(a, ))
        c = grammar.TypeExpression('Y', fields=(a, ))
        d = grammar.TypeExpression('Z', predicate=grammar.Predicate("stuff"))

        self.assertIsInstance(a, collections.Hashable)
        # There really shouldn't be a collision between these:
        self.assertNotEqual(hash(a), hash(d))

        self.assertEqual(b, c)
        self.assertEqual(hash(b), hash(c))
Esempio n. 9
0
    def test_is_subtype_diff_predicates(self):
        class Pred(grammar.Predicate):
            def __init__(self, value):
                self.value = value
                super().__init__(value)

            def _is_subtype_(self, other):
                return self.value <= other.value

        P1 = Pred(1)
        P2 = Pred(2)
        X = grammar.TypeExpression('X', predicate=P1)
        X_ = grammar.TypeExpression('X', predicate=P2)

        self.assertFalse(X_._is_subtype_(X))
        self.assertTrue(X._is_subtype_(X_))
Esempio n. 10
0
    def test_is_subtype_matches_w_predicate(self):
        class Pred(grammar.Predicate):
            def __init__(self, value=0):
                self.value = value
                super().__init__(value)

            def _is_subtype_(self, other):
                return self.value <= other.value

        P1 = Pred(1)
        P1_ = Pred(1)
        X = grammar.TypeExpression('X', predicate=P1)
        X_ = grammar.TypeExpression('X', predicate=P1_)

        self.assertTrue(X._is_subtype_(X))
        self.assertTrue(X_._is_subtype_(X))
        self.assertTrue(X._is_subtype_(X_))
        self.assertTrue(X_._is_subtype_(X_))
Esempio n. 11
0
    def test_apply_fields(self):
        X = grammar.TypeExpression('X')
        Example = grammar.CompositeType('Example', ('foo', ))

        result = Example._apply_fields_((X, ))

        self.assertEqual(result.fields, (X, ))
        self.assertEqual(result.name, 'Example')
        self.assertIsInstance(result, grammar.TypeExpression)
Esempio n. 12
0
    def test_wrong_length(self):
        X = grammar.TypeExpression('X')
        composite_type = grammar.CompositeType('C', ['foo', 'bar'])
        with self.assertRaisesRegex(TypeError, '1'):
            composite_type[X]

        composite_type = grammar.CompositeType('C', ['foo'])
        with self.assertRaisesRegex(TypeError, '2'):
            composite_type[X, X]
Esempio n. 13
0
    def test_validate_intersection_implements_handshake(self):
        local = {}
        X = grammar.TypeExpression('X')

        class Example(grammar.TypeExpression):
            def _validate_intersection_(self, other, handshake=False):
                local['other'] = other
                local['handshake'] = handshake

        X._validate_intersection_(Example('Example'))
        self.assertIs(local['other'], X)
        self.assertTrue(local['handshake'])
Esempio n. 14
0
 def test_nested_expression(self):
     X = grammar.TypeExpression('X')
     C = grammar.CompositeType('C', ['foo', 'bar'])
     self.assertEqual(repr(C[X, C[C[X, X], X]]), 'C[X, C[C[X, X], X]]')
Esempio n. 15
0
 def test_validate_intersection_w_composite_type(self):
     X = grammar.TypeExpression('X')
     with self.assertRaisesRegex(TypeError, 'incomplete'):
         X._validate_intersection_(
             grammar.CompositeType('A', field_names=('X', )))
Esempio n. 16
0
    def test_is_subtype_wrong_name(self):
        Y = grammar.TypeExpression('Y')
        X = grammar.TypeExpression('X')

        self.assertFalse(Y._is_subtype_(X))
        self.assertFalse(X._is_subtype_(Y))
Esempio n. 17
0
 def test_immutable(self):
     # If this test fails, then the hiearchy has been rearranged and the
     # properties tested for `_TypeBase` should be tested for
     # this class.
     #     - Your Friendly Dead Man's Switch
     self.assertIsInstance(grammar.TypeExpression('X'), grammar._TypeBase)
Esempio n. 18
0
 def test_eq_nonsense(self):
     X = grammar.TypeExpression('X')
     self.assertIs(X.__eq__(42), NotImplemented)
     self.assertFalse(X == 42)
Esempio n. 19
0
 def test_validate_predicate_w_valid(self):
     predicate = grammar.Predicate(True)
     X = grammar.TypeExpression('X')
     X._validate_predicate_(predicate)
Esempio n. 20
0
 def test_eq_different_instances(self):
     X = grammar.TypeExpression('X')
     X_ = grammar.TypeExpression('X')
     self.assertIsNot(X, X_)
     self.assertEqual(X, X_)
Esempio n. 21
0
 def test_validate_intersection_w_nonsense(self):
     X = grammar.TypeExpression('X')
     with self.assertRaisesRegex(TypeError, 'expression'):
         X._validate_intersection_(42)
Esempio n. 22
0
 def test_mod_w_none_predicate(self):
     X = grammar.TypeExpression('X', predicate=None)
     predicate = grammar.Predicate("Truthy")
     self.assertIs((X % predicate).predicate, predicate)
Esempio n. 23
0
 def test_mod_w_existing_predicate(self):
     X = grammar.TypeExpression('X', predicate=grammar.Predicate('Truthy'))
     with self.assertRaisesRegex(TypeError, 'predicate'):
         X % grammar.Predicate('Other')
Esempio n. 24
0
 def test_validate_intersection_w_valid(self):
     X = grammar.TypeExpression('X')
     Y = grammar.TypeExpression('Y')
     X._validate_intersection_(Y)
Esempio n. 25
0
 def test_mod_w_none(self):
     X = grammar.TypeExpression('X')
     self.assertEqual(X % None, X)
Esempio n. 26
0
 def test_build_intersection(self):
     X = grammar.TypeExpression('X')
     Y = grammar.TypeExpression('Y')
     intersection = X._build_intersection_((X, Y))
     self.assertIsInstance(intersection, grammar.IntersectionTypeExpression)
     self.assertEqual(intersection.members, frozenset({X, Y}))
Esempio n. 27
0
 def test_identity(self):
     X = grammar.TypeExpression('X')
     X_ = grammar.TypeExpression('X')
     self.assertIs(X & X_, X_)
Esempio n. 28
0
 def test_validate_predicate_w_nonsense(self):
     X = grammar.TypeExpression('X')
     with self.assertRaisesRegex(TypeError, 'predicate'):
         X._validate_predicate_(42)
Esempio n. 29
0
 def test_type_expr_not_semantic(self):
     TypeExpr = grammar.TypeExpression('TypeExpr')
     self.assertFalse(semantic.is_semantic_type(TypeExpr))
Esempio n. 30
0
 def test_validate_field_w_typeexp(self):
     Example = grammar.CompositeType('Example', ('foo', ))
     # Check that no error is raised:
     Example._validate_field_('foo', grammar.TypeExpression('X'))