def test_eq(self): a = Conjunction([TypeIdentifier('a')]) b = Conjunction([String('a')]) assert a == a assert a != b assert a == TypeIdentifier('a') assert a != String('a')
def test__delitem__(self): a = Conjunction([AVM({'A.B': String('x')}), AVM({'A.D': String('y')})]) del a['A.B'] assert 'A.B' not in a assert 'A.D' in a del a['A'] assert 'A' not in a
def test_format_lists(): assert tdl.format(ConsList()) == '< ... >' assert tdl.format(ConsList(end=tdl.EMPTY_LIST_TYPE)) == '< >' assert tdl.format(ConsList([String('a')])) == '< "a", ... >' assert tdl.format(ConsList([String('a')], end=Coreference('b'))) == '< "a" . #b >' assert tdl.format(DiffList()) == '<! !>' assert tdl.format(DiffList([String('a')])) == '<! "a" !>' assert tdl.format(DiffList([String('a'), String('b')])) == '<! "a", "b" !>'
def test_AVM(): a = AVM() assert a.features() == [] assert 'ATTR' not in a with pytest.raises(TypeError): AVM({'ATTR': 'val'}) a = AVM([('ATTR', Conjunction([TypeIdentifier('a')]))]) assert len(a.features()) == 1 assert 'ATTR' in a assert a.features()[0][0] == 'ATTR' assert a.features()[0][1].types() == ['a'] assert a.features()[0][1] == a['ATTR'] a = AVM([('ATTR1.ATTR2', Conjunction([String('b')]))]) assert len(a.features()) == 1 assert a.features()[0][0] == 'ATTR1.ATTR2' assert a.features()[0][1] == a['ATTR1.ATTR2'] a = AVM([('ATTR1', Conjunction([AVM([('ATTR2.ATTR3', Conjunction([String('b')]))]) ]))]) assert len(a.features()) == 1 assert a.features()[0][0] == 'ATTR1' assert a.features()[0][1] == a['ATTR1'] assert a['ATTR1'].features()[0][0] == 'ATTR2.ATTR3' assert a['ATTR1'].features()[0][1] == a['ATTR1.ATTR2.ATTR3'] assert a['ATTR1.ATTR2.ATTR3'] == a['ATTR1']['ATTR2']['ATTR3'] assert a.features(expand=True)[0][0] == 'ATTR1.ATTR2.ATTR3' a.normalize() assert a.features()[0][0] == 'ATTR1.ATTR2.ATTR3' assert a.features(expand=True)[0][0] == 'ATTR1.ATTR2.ATTR3' a = AVM() a['ATTR1.ATTR2'] = Conjunction([TypeIdentifier('a')]) assert len(a.features()) == 1 assert a.features()[0][0] == 'ATTR1.ATTR2' b = AVM() b['ATTR1.ATTR2'] = TypeIdentifier('a') assert a == b a = AVM([('ATTR1', Conjunction( [TypeIdentifier('b'), AVM([('ATTR2', TypeIdentifier('c'))])]))]) assert a.features(expand=True) == [('ATTR1', TypeIdentifier('b')), ('ATTR1.ATTR2', TypeIdentifier('c'))]
def test_Term(): q = Term(docstring='hi') assert q.docstring == 'hi' r = Regex('r*') s = String('s') t = TypeIdentifier('t') a = AVM() cl = ConsList() dl = DiffList() c = Coreference(None) assert isinstance(r, Term) assert isinstance(s, Term) assert isinstance(t, Term) assert isinstance(a, Term) assert isinstance(cl, Term) assert isinstance(dl, Term) assert isinstance(c, Term) assert isinstance(r & s, Conjunction) assert (r & s).terms == [r, s] assert isinstance(s & t, Conjunction) assert isinstance(t & a, Conjunction) assert isinstance(a & cl, Conjunction) assert isinstance(cl & dl, Conjunction) assert isinstance(dl & c, Conjunction) assert isinstance(c & r, Conjunction) assert isinstance((r & s) & t, Conjunction) assert ((r & s) & t).terms == [r, s, t] assert isinstance(r & (s & t), Conjunction) assert (r & (s & t)).terms == [r, s, t]
def test_TypeDefinition(): with pytest.raises(TypeError): TypeDefinition() with pytest.raises(TypeError): TypeDefinition('typename') t = TypeDefinition('typename', Conjunction([TypeIdentifier('a')])) assert t.identifier == 'typename' assert t.supertypes == t.conjunction.types() == [TypeIdentifier('a')] assert t.features() == [] assert t.documentation() is None assert t.documentation(level='top') == [] # promote simple definition to Conjunction t = TypeDefinition('typename', TypeIdentifier('a')) assert t.identifier == 'typename' assert t.supertypes == t.conjunction.types() == [TypeIdentifier('a')] assert t.features() == [] assert t.documentation() is None assert t.documentation(level='top') == [] # must have a supertype (test currently only at parse-time) # with pytest.raises(TdlError): # TypeDefinition('t', Conjunction([AVM([('ATTR', String('s'))])])) t = TypeDefinition( 't', Conjunction([AVM([('ATTR', String('s'))]), TypeIdentifier('a')])) assert t.supertypes == [TypeIdentifier('a')] assert t.features() == [('ATTR', Conjunction([String('s')]))] t = TypeDefinition('t', TypeIdentifier('a'), docstring='doc') assert t.docstring == 'doc' assert t.documentation() == 'doc' assert t.documentation(level='top') == ['doc'] t = TypeDefinition('t', TypeIdentifier('a', docstring='a doc'), docstring='doc') assert t.docstring == 'doc' assert t.documentation() == 'a doc' assert t.documentation(level='top') == ['a doc', 'doc']
def test_format_docstring_terms(): assert tdl.format(TypeIdentifier('a', docstring='doc')) == '"""\ndoc\n"""\na' assert tdl.format(String('a', docstring='doc')) == '"""\ndoc\n"""\n"a"' assert tdl.format(Regex('a', docstring='doc')) == '"""\ndoc\n"""\n^a$' assert tdl.format(Coreference('a', docstring='doc')) == '"""\ndoc\n"""\n#a' assert tdl.format(AVM(docstring='doc')) == '"""\ndoc\n"""\n[ ]' assert tdl.format(ConsList(docstring='doc')) == '"""\ndoc\n"""\n< ... >' assert tdl.format(DiffList(docstring='doc')) == '"""\ndoc\n"""\n<! !>' # escape docstrings if necessary assert tdl.format( TypeIdentifier('a', docstring='"one" ""two"" """three""" """"""""') ) == '"""\n"one" ""two"" ""\\"three""\\" ""\\"""\\"""\n"""\na'
def test_TypeIdentifier(): with pytest.raises(TypeError): t = TypeIdentifier() t = TypeIdentifier('t') # case-insensitive comparision assert t == TypeIdentifier('t') assert t == TypeIdentifier('T') assert t != String('t') assert t != Regex('t') assert t == 't' assert t == 'T' t = TypeIdentifier('t2', docstring='doc') assert t == 't2' assert t == TypeIdentifier('t2', docstring='foo')
def test_Regex(): with pytest.raises(TypeError): t = Regex() t = Regex('r') # case-sensitive comparison assert t == Regex('r') assert t != Regex('R') assert t != TypeIdentifier('r') assert t != String('r') assert t == 'r' assert t != 'R' t = Regex('r2', docstring='doc') assert t == 'r2' assert t == Regex('r2', docstring='foo')
def test_DiffList(): d = DiffList() assert len(d) == 0 assert d.last == 'LIST' assert d['LAST'] == d[d.last] d = DiffList([TypeIdentifier('a')]) assert len(d) == 1 assert d.last == 'LIST.REST' assert d['LAST'] == d[d.last] d = DiffList([TypeIdentifier('a'), String('b')]) assert len(d) == 2 assert d.last == 'LIST.REST.REST' assert d['LAST'] == d[d.last] # append() is not defined for DiffList with pytest.raises(AttributeError): d.append(TypeIdentifier('d'))
def test_String(): with pytest.raises(TypeError): t = String() t = String('s') # case-sensitive comparison assert t == String('s') assert t != String('S') assert t != TypeIdentifier('s') assert t != Regex('s') assert t == 's' assert t != 'S' t = String('s2', docstring='doc') assert t == 's2' assert t == String('s2', docstring='foo')
def test_format_TypeTerms(): assert tdl.format(TypeIdentifier('a-type')) == 'a-type' assert tdl.format(String('a string')) == '"a string"' assert tdl.format(Regex('a*re[g]ex')) == '^a*re[g]ex$' assert tdl.format(Coreference('coref')) == '#coref'