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_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_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_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_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__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_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_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__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__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__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_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_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_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_add(self): c = Conjunction() with pytest.raises(TypeError): c.add('b') c.add(TypeIdentifier('a')) assert c.terms == [TypeIdentifier('a')]