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__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_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__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_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_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_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_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_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_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_AVM(): a = AVM() assert a.features() == [] assert 'ATTR' not in a 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'))]