def test_format_environments(): assert tdl.format(TypeEnvironment()) == (':begin :type.\n' ':end :type.') e = TypeEnvironment(entries=[ TypeDefinition( 'id', Conjunction( [TypeIdentifier('a'), AVM([('ATTR', TypeIdentifier('b'))])])) ]) assert tdl.format(e) == (':begin :type.\n' ' id := a &\n' ' [ ATTR b ].\n' ':end :type.') e = TypeEnvironment(entries=[ FileInclude('other.tdl'), InstanceEnvironment(status='lex-rule', entries=[FileInclude('lrules.tdl')]), FileInclude('another.tdl') ]) assert tdl.format(e) == (':begin :type.\n' ' :include "other.tdl".\n' ' :begin :instance :status lex-rule.\n' ' :include "lrules.tdl".\n' ' :end :instance.\n' ' :include "another.tdl".\n' ':end :type.')
def test__contains__(self): a = Conjunction() assert 'ATTR' not in a a.add(AVM({'ATTR': TypeIdentifier('val')})) assert 'ATTR' in a a.add(AVM({'ATTR': TypeIdentifier('val2')})) # two AVMs assert 'ATTR' in a
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__getitem__(self): a = Conjunction() with pytest.raises(KeyError): a['ATTR'] a.add(AVM({'ATTR': TypeIdentifier('val')})) assert a['ATTR'] == TypeIdentifier('val') a.add(AVM({'ATTR': TypeIdentifier('val2')})) # two AVMs assert a['ATTR'] == Conjunction([TypeIdentifier('val'), TypeIdentifier('val2')])
def test_and(self): c = Conjunction() assert len(c.terms) == 0 c &= TypeIdentifier('a') assert len(c.terms) == 1 c &= TypeIdentifier('b') assert len(c.terms) == 2 with pytest.raises(TypeError): c &= 'b'
def test_init(self): c = Conjunction() assert c.terms == [] c = Conjunction([TypeIdentifier('a')]) assert len(c.terms) == 1 with pytest.raises(TypeError): Conjunction('a') with pytest.raises(TypeError): Conjunction(['a']) with pytest.raises(TypeError): Conjunction(TypeIdentifier('a'))
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_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__setitem__(self): a = Conjunction() with pytest.raises(TDLError): a['ATTR'] = TypeIdentifier('val') a.add(AVM()) a['ATTR'] = TypeIdentifier('val') assert a['ATTR'] == TypeIdentifier('val') # reassignment overwrites a['ATTR'] = TypeIdentifier('val2') assert a['ATTR'] == TypeIdentifier('val2') a['ATTR'] &= TypeIdentifier('val3') assert a['ATTR'] == Conjunction([TypeIdentifier('val2'), TypeIdentifier('val3')]) # setitem adds to the last AVM a.add(AVM()) a['ATTR'] = TypeIdentifier('val4') assert a.terms[1]['ATTR'] == TypeIdentifier('val4')
def test_format_AVM(): assert tdl.format(AVM()) == '[ ]' assert tdl.format(AVM([('ATTR', Conjunction([TypeIdentifier('a')])) ])) == '[ ATTR a ]' assert tdl.format( AVM([('ATTR1', Conjunction([TypeIdentifier('a')])), ('ATTR2', Conjunction([TypeIdentifier('b')])) ])) == ('[ ATTR1 a,\n' ' ATTR2 b ]') assert tdl.format( AVM([('ATTR1', AVM([('ATTR2', Conjunction([TypeIdentifier('a')]))])) ])) == ('[ ATTR1.ATTR2 a ]') assert tdl.format( AVM([('ATTR1', Conjunction([ AVM([('ATTR2', Conjunction([TypeIdentifier('a')]))]) ]))])) == ('[ ATTR1 [ ATTR2 a ] ]')
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_Conjunction(): c = Conjunction() orig_c = c assert len(c.terms) == 0 c &= TypeIdentifier('a') assert len(c.terms) == 1 assert len(orig_c.terms) == 0 with pytest.raises(TypeError): c &= 'b' with pytest.raises(TypeError): c.add('b')
def test_format_typedefs(): t = TypeDefinition('id', Conjunction([ TypeIdentifier('a', docstring='a doc'), AVM([('ATTR', TypeIdentifier('b'))]) ]), docstring='t doc') t2 = TypeDefinition('id', Conjunction([ TypeIdentifier('a'), AVM([('ATTR', TypeIdentifier('b'))], docstring='a doc') ]), docstring='t doc') a = TypeAddendum( 'id', Conjunction([AVM([('ATTR', TypeIdentifier('b', docstring='b doc'))])]), docstring='t doc') lr = LexicalRuleDefinition('id', affix_type='suffix', patterns=[('a', 'b'), ('c', 'd')], conjunction=Conjunction( [TypeIdentifier('a', docstring='a doc')]), docstring='lr doc') assert tdl.format(t) == ('id := """\n' ' a doc\n' ' """\n' ' a &\n' ' [ ATTR b ]\n' ' """\n' ' t doc\n' ' """.') assert tdl.format(t2) == ('id := a &\n' ' """\n' ' a doc\n' ' """\n' ' [ ATTR b ]\n' ' """\n' ' t doc\n' ' """.') assert tdl.format(a) == ('id :+ [ ATTR """\n' ' b doc\n' ' """\n' ' b ]\n' ' """\n' ' t doc\n' ' """.') assert tdl.format(lr) == ('id :=\n' '%suffix (a b) (c d)\n' ' """\n' ' a doc\n' ' """\n' ' a\n' ' """\n' ' lr doc\n' ' """.')
def test_ConsList(): c = ConsList() assert len(c) == 0 assert c.terminated is False assert c.features() == [] c.terminate(tdl.EMPTY_LIST_TYPE) assert c.terminated is True assert c.features() == [] c = ConsList([TypeIdentifier('a')]) assert len(c) == 1 assert c['FIRST'] is not None assert c['REST'] == AVM() c.append(TypeIdentifier('b')) assert len(c) == 2 assert c['REST.FIRST'] is not None assert c['REST.REST'] == AVM() c.terminate(tdl.EMPTY_LIST_TYPE) with pytest.raises(TdlError): c.append(TypeIdentifier('c')) c = ConsList([TypeIdentifier('a')], end=Coreference('x')) assert len(c) == 2 assert c.terminated is True
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_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_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_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_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_add(self): c = Conjunction() with pytest.raises(TypeError): c.add('b') c.add(TypeIdentifier('a')) assert c.terms == [TypeIdentifier('a')]
def test_format_Conjunction(): assert tdl.format(Conjunction()) == '' assert tdl.format(Conjunction([TypeIdentifier('a')])) == 'a' assert tdl.format(Conjunction([TypeIdentifier('a'), TypeIdentifier('b')])) == 'a & b'
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'