def testProject(self): for termStr, pathStr, expectedResultStr in self.projectTestCases: term = factory.parse(termStr) path_ = path.Path.fromStr(pathStr) expectedResult = factory.parse(expectedResultStr) result = path_.project(term) self.failUnlessEqual(result, expectedResult, '%s . %s == %s (!= %s)' % (termStr, pathStr, result, expectedResult))
def testIdentity(self): annos = factory.parse('[1,2]') for terms1Str in self.identityTestCases: for terms2Str in self.identityTestCases: for term1Str in terms1Str: for term2Str in terms2Str: term1 = factory.parse(term1Str) term2 = factory.parse(term2Str) expectedResult = term1Str == term2Str result = term1.isEquivalent(term2) self.failUnlessEqual(result, expectedResult, msg = '%s <=> %s = %r (!= %r)' % (term1Str, term2Str, result, expectedResult)) result = term1.isEqual(term2) self.failUnlessEqual(result, expectedResult, msg = '%s == %s = %r (!= %r)' % (term1Str, term2Str, result, expectedResult)) if expectedResult and types.isAppl(term2): term2 = annotation.set(term2, factory.parse("A(1)")) result = term1.isEquivalent(term2) self.failUnlessEqual(result, True, msg = '%s <=> %s = %r (!= %r)' % (term1Str, term2Str, result, True)) result = term1.isEqual(term2) self.failUnlessEqual(result, False, msg = '%s == %s = %r (!= %r)' % (term1Str, term2Str, result, False))
def _testHash(self, cmpf, hashf, msg = None): for terms1Str in self.identityTestCases: for term1Str in terms1Str: term1 = factory.parse(term1Str) hash1 = hashf(term1) self.failUnless(isinstance(hash1, int)) self.failIfEqual(hash1, -1) for terms2Str in self.identityTestCases: for term2Str in terms2Str: term2 = factory.parse(term2Str) hash2 = hashf(term2) term_eq = cmpf(term1, term2) hash_eq = hash1 == hash2 detail = '%s (%d) and %s (%d)' % ( term1Str, hash1, term2Str, hash2 ) if term_eq: self.failUnless(hash_eq, '%s hash/equality incoerence for ' '%s' % (msg, detail) ) elif 0: # XXX: this fails on python 2.3 but no on 2.4... self.failIf(hash_eq, '%s hash colision for ' '%s' % (msg, detail) )
def testTransform(self): func = lambda x: x.factory.make('X(_)', x) for termStr, pathStr, expectedResultStr in self.transformTestCases: term = factory.parse(termStr) path_ = path.Path.fromStr(pathStr) expectedResult = factory.parse(expectedResultStr) result = path_.transform(term, func) self.failUnlessEqual(result, expectedResult, '%s . %s == %s (!= %s)' % (termStr, pathStr, result, expectedResult))
def testSplit(self): for inputStr, index, expectedHeadStr, expectedTailStr in self.splitTestCases: input = factory.parse(inputStr) expectedHead = factory.parse(expectedHeadStr) expectedTail = factory.parse(expectedTailStr) head, tail = lists.split(input, index) self.failUnlessEqual(head, expectedHead) self.failUnlessEqual(tail, expectedTail)
def testMake(self): for patternStr, argsStr, kargsStr, expectedResultStr in self.makeTestCases: args = self.parseArgs(argsStr) kargs = self.parseKargs(kargsStr) expectedResult = factory.parse(expectedResultStr) result = factory.make(patternStr, *args, **kargs) self.failUnlessEqual(result, expectedResult)
def testWrite(self): for terms1Str in self.identityTestCases: for term1Str in terms1Str: term1 = factory.parse(term1Str) term2Str = str(term1) self.failUnlessEqual(term1Str, term2Str)
def testReal(self): for termStr in self.realTestCases: value = float(termStr) _term = factory.parse(termStr) self.failUnless(_term.factory is factory) self.failUnlessEqual(_term.getType(), types.REAL) self.failUnlessAlmostEqual(_term.getValue(), value) self.failIfMutable(_term)
def testStr(self): for termStr, value in self.strTestCases: _term = factory.parse(termStr) self.failUnless(_term.factory is factory) self.failUnlessEqual(_term.getType(), types.STR) self.failUnlessEqual(_term.getValue(), value) self.failUnlessEqual(str(_term), termStr) self.failIfMutable(_term)
def testAppl(self): for termStr, name, arity in self.applTestCases: _term = factory.parse(termStr) self.failUnless(_term.factory is factory) self.failUnlessEqual(_term.type, types.APPL) self.failUnlessEqual(_term.name, name) self.failUnlessEqual(_term.getArity(), arity) self.failUnlessEqual(str(_term), termStr) self.failIfMutable(_term)
def testInt(self): for termStr in self.intTestCases: value = int(termStr) _term = factory.parse(termStr) self.failUnless(_term.factory is factory) self.failUnlessEqual(_term.getType(), types.INT) self.failUnlessEqual(_term.getValue(), value) self.failUnlessEqual(str(_term), termStr) self.failIfMutable(_term)
def testList(self): for termStr, length in self.listTestCases: _term = factory.parse(termStr) self.failUnless(_term.factory is factory) self.failUnless(_term.getType() & types.LIST) self.failUnlessEqual(not _term, length == 0) self.failUnlessEqual(len(_term), length) self.failUnlessEqual(str(_term), termStr) self.failIfMutable(_term)
def testMatch(self): for termStr, patternStr, expectedResult, expectedArgsStr, expectedKargsStr in self.matchTestCases: term = factory.parse(termStr) expectedArgs = self.parseArgs(expectedArgsStr) expectedKargs = self.parseKargs(expectedKargsStr) match = factory.match(patternStr, term) result = bool(match) self.failUnlessEqual(result, expectedResult, msg = '%s ~ %s = %r (!= %r)' % (patternStr, termStr, result, expectedResult)) if match: self.failUnlessEqual(match.args, expectedArgs, msg = '%s ~ %s = %r (!= %r)' % (patternStr, termStr, match.args, expectedArgs)) self.failUnlessEqual(match.kargs, expectedKargs, msg = '%s ~ %s = %r (!= %r)' % (patternStr, termStr, match.kargs, expectedKargs))
def testValidate(self): asd = parse(self.sampleDescription) from aterm.factory import factory for productionName, termStr, expectedResult in self.validateTestCases: term = factory.parse(termStr) try: asd.validate(productionName, term) except MismatchException: result = False if expectedResult != False: raise else: result = True self.failUnlessEqual(result, expectedResult, "%s ~ %s" % (productionName, termStr))
def testAnnotations(self): for terms1Str in self.identityTestCases: for term1Str in terms1Str: term1 = factory.parse(term1Str) if not types.isAppl(term1): continue term = term1 self.failUnlessEqual(term.annotations, factory.parse("[]")) term = annotation.set(term, factory.parse("A(1)")) self.failUnlessEqual(term.annotations, factory.parse("[A(1)]")) term = annotation.set(term, factory.parse("B(2)")) self.failUnlessEqual(annotation.get(term, "A"), factory.parse("A(1)")) self.failUnlessEqual(annotation.get(term, "B"), factory.parse("B(2)")) term = annotation.set(term, factory.parse("A(3)")) self.failUnlessEqual(annotation.get(term, "A"), factory.parse("A(3)")) self.failUnlessEqual(annotation.get(term, "B"), factory.parse("B(2)")) term = annotation.remove(term, "A") self.failUnlessEqual(term.annotations, factory.parse("[B(2)]")) try: annotation.get(term, "C(_)") self.fail() except ValueError: pass self.failUnless(term.isEquivalent(term1))
def parseKargs(self, kargs): res = {} for name, value in kargs.iteritems(): res[name] = factory.parse(value) return res
def parseArgs(self, args): return [factory.parse(value) for value in args]
def testDeAnnotate(self): for expectedResultStr, termStr in self.annotateTestCases: term = factory.parse(termStr) expectedResult = factory.parse(expectedResultStr) result = path.deannotate(term) self.failUnlessEqual(result, expectedResult)