def trees(self): return GridFixture([ [ ListTree.parse("a"), ListTree.parse("<ul><li>a</li></ul>") ], [ ListTree.parse("<ul><li>BB</li></ul>"), ListTree.parse("<ul><li>a</li><li>BB</li></ul>")]], {"name": TreeTypeAdapter})
def testParseTreeNoCloseLi(self): try: ListTree.parse("tree<ul><li>a<li>b<ul><li>c</ul></ul>") except Exception: pass else: self.fail("Doesn't handle lists with </li> missing.")
def trees(self): return GridFixture( [[ListTree.parse("a"), ListTree.parse("<ul><li>a</li></ul>")], [ ListTree.parse("<ul><li>BB</li></ul>"), ListTree.parse("<ul><li>a</li><li>BB</li></ul>") ]], {"name": TreeTypeAdapter})
def _error(self, file1, file2, msg=None): if msg is None: msg = file2 file2 = None if file2 is None: return ListTree(self._htmlLink(file1), [ListTree("<i>%s</i>" % msg)]) else: error = self._error(file1, msg) error.addChild(ListTree(self._htmlLink(file2))) return error
def tree(self, aString=None): if aString is None: aTree = ListTree("") aTree.addChild(ListTree("a")) aTree.addChild(ListTree("BB")) return aTree else: return ListTree.parse(aString)
class TeeTree(ListTree): # implements Tree, TreeInterface { _typeDict = {"tree": TreeTypeAdapter} tree = None # tree def __init__(self, tree): self.tree = ListTree("B", tree.getChildren()) _typeDict["title.RenameTo"] = "getTitle" _typeDict["getTitle.types"] = ["String"] def getTitle(self): return self.tree.getTitle() # _typeDict["text.RenameTo"] = "getText" _typeDict["getText.types"] = ["String"] def getText(self): return self.tree.getText() _typeDict["children.RenameTo"] = "getChildren" _typeDict["getChildren.types"] = [TreeTypeAdapter] # ??? def getChildren(self): return self.tree.getChildren() _typeDict["text.types"] = ["String"] def text(self): return self.tree.textToString() _typeDict["parseTree.types"] = [TreeTypeAdapter, "String"] def parseTree(self, tree): return TeeTree(tree) _typeDict["toString.types"] = ["String"] def toString(self): __pychecker__ = "no-override" return self.tree.toString() _typeDict["toTree.types"] = [TreeTypeAdapter] def toTree(self): return self.tree
def tree(self, aString = None): if aString is None: aTree = ListTree("") aTree.addChild(ListTree("a")) aTree.addChild(ListTree("BB")) return aTree else: return ListTree.parse(aString)
class Test_ListTree(unittest.TestCase): topTree = None tree = None toplessTree = None def setUp(self): print '%s %s' % (self.id(), self.shortDescription()) self.topTree = ListTree("top") self.tree = ListTree("tree", [ListTree("a"), ListTree("b", [ListTree("c")])]) self.toplessTree = ListTree("", [ListTree("a"), ListTree("b")]) def testEqualsSameOne(self): self.treesEqual(self.topTree, self.topTree) def testEqualsSimilarOne(self): self.treesEqual(self.topTree, ListTree("top")) def testNotEqualsSimilarOne(self): self.treesUnEqual(self.topTree, ListTree("bottom")) def testTopToString(self): print self.topTree.toString() assert "top" == self.topTree.toString() def testToplessTreeToString(self): assert ("<ul><li>a</li><li>b</li></ul>" == self.toplessTree.toString()) def testEqualsSameTree(self): self.treesEqual(self.tree, self.tree) def testEqualsSimilarTree(self): tree2 = ListTree("tree", [ListTree("a"), ListTree("b", [ListTree("c")])]) self.treesEqual(self.tree, tree2) def testNotEqualsTop(self): self.treesUnEqual(self.tree, self.topTree) self.treesUnEqual(self.topTree, self.toplessTree) def testNotEqualsSimilarShapedTree(self): tree2 = ListTree("tree", [ListTree("a"), ListTree("b", [ListTree("C")])]) self.treesUnEqual(self.tree, tree2) def testNotEqualsDifferentShapedTree(self): tree2 = ListTree("tree", [ListTree("a", [ListTree("c")]), ListTree("b")]) self.treesUnEqual(self.tree, tree2) self.treesUnEqual(self.tree, self.toplessTree) def testTreeToString(self): tree = "tree<ul><li>a</li><li>b<ul><li>c</li></ul></li></ul>" result = self.tree.toString() print tree print result assert tree == self.tree.toString() def testParseTop(self): assert self.topTree, ListTree.parse("top") def testParseTree1(self): text = "tree<ul><li>a</li></ul>" parsed = ListTree.parse(text).toString() print text print parsed assert text == parsed def testParseTree(self): self.assertParsed( "tree<ul><li>a</li><li>b<ul><li>c</li></ul></li></ul>") def testParseToplessTree(self): self.assertParsed( "<ul><li>a</li><li>b<ul><li>c</li></ul></li></ul>") def testParseTags0(self): self.assertParsed("<i>a</i>") def testParseSpace(self): assert "a" == ListTree.parse("<i>a </i>").text def testParseTags1(self): self.assertParsed("tree<ul><li><i>a</i></li></ul>") def testParseTags2(self): self.assertParsed("tree<ul><li>a<i>b</i><b>c</b></li></ul>") def testEqualsSimilarWithTags(self): self.treesEqual(self.topTree, ListTree("<i>top</i>")) def testTopText(self): assert "top" == ListTree("top").textToString() def testToplessTreeText(self): text = "<ul><li>a</li><li>b</li></ul>" result = self.toplessTree.textToString() assert text == result def testTopTextWithTags(self): assert "top" == ListTree("<i><b>top</b></i>").textToString() def testTreeText(self): assert ("tree<ul><li>a</li><li>b<ul><li>c</li></ul></li></ul>" == self.tree.textToString()) def testTreeTextWithTags(self): s = "tree<ul><li>a<i>b</i><b>c</b></li></ul>" assert "tree<ul><li>abc</li></ul>" == ListTree.parse(s).textToString() def testParseTreeNoCloseLi(self): try: ListTree.parse("tree<ul><li>a<li>b<ul><li>c</ul></ul>") except Exception: pass else: self.fail("Doesn't handle lists with </li> missing.") def assertParsed(self, s): print s result = ListTree.parse(s).toString() print result assert s == result def treesEqual(self, t1, t2): assert t1 == t2 assert t2 == t1 assert ListTree.equals(t1, t2) assert ListTree.equals(t2, t1) def treesUnEqual(self, t1, t2): assert t1 != t2 assert t2 != t1 assert not ListTree.equals(t1, t2) assert not ListTree.equals(t2, t1)
def __init__(self, tree): self.tree = ListTree("B", tree.getChildren())
def testParseTree1(self): text = "tree<ul><li>a</li></ul>" parsed = ListTree.parse(text).toString() print text print parsed assert text == parsed
def testNotEqualsDifferentShapedTree(self): tree2 = ListTree("tree", [ListTree("a", [ListTree("c")]), ListTree("b")]) self.treesUnEqual(self.tree, tree2) self.treesUnEqual(self.tree, self.toplessTree)
def testEqualsSimilarTree(self): tree2 = ListTree( "tree", [ListTree("a"), ListTree("b", [ListTree("c")])]) self.treesEqual(self.tree, tree2)
def testEqualsSimilarOne(self): self.treesEqual(self.topTree, ListTree("top"))
class Test_ListTree(unittest.TestCase): topTree = None tree = None toplessTree = None def setUp(self): print '%s %s' % (self.id(), self.shortDescription()) self.topTree = ListTree("top") self.tree = ListTree( "tree", [ListTree("a"), ListTree("b", [ListTree("c")])]) self.toplessTree = ListTree("", [ListTree("a"), ListTree("b")]) def testEqualsSameOne(self): self.treesEqual(self.topTree, self.topTree) def testEqualsSimilarOne(self): self.treesEqual(self.topTree, ListTree("top")) def testNotEqualsSimilarOne(self): self.treesUnEqual(self.topTree, ListTree("bottom")) def testTopToString(self): print self.topTree.toString() assert "top" == self.topTree.toString() def testToplessTreeToString(self): assert ("<ul><li>a</li><li>b</li></ul>" == self.toplessTree.toString()) def testEqualsSameTree(self): self.treesEqual(self.tree, self.tree) def testEqualsSimilarTree(self): tree2 = ListTree( "tree", [ListTree("a"), ListTree("b", [ListTree("c")])]) self.treesEqual(self.tree, tree2) def testNotEqualsTop(self): self.treesUnEqual(self.tree, self.topTree) self.treesUnEqual(self.topTree, self.toplessTree) def testNotEqualsSimilarShapedTree(self): tree2 = ListTree( "tree", [ListTree("a"), ListTree("b", [ListTree("C")])]) self.treesUnEqual(self.tree, tree2) def testNotEqualsDifferentShapedTree(self): tree2 = ListTree("tree", [ListTree("a", [ListTree("c")]), ListTree("b")]) self.treesUnEqual(self.tree, tree2) self.treesUnEqual(self.tree, self.toplessTree) def testTreeToString(self): tree = "tree<ul><li>a</li><li>b<ul><li>c</li></ul></li></ul>" result = self.tree.toString() print tree print result assert tree == self.tree.toString() def testParseTop(self): assert self.topTree, ListTree.parse("top") def testParseTree1(self): text = "tree<ul><li>a</li></ul>" parsed = ListTree.parse(text).toString() print text print parsed assert text == parsed def testParseTree(self): self.assertParsed( "tree<ul><li>a</li><li>b<ul><li>c</li></ul></li></ul>") def testParseToplessTree(self): self.assertParsed("<ul><li>a</li><li>b<ul><li>c</li></ul></li></ul>") def testParseTags0(self): self.assertParsed("<i>a</i>") def testParseSpace(self): assert "a" == ListTree.parse("<i>a </i>").text def testParseTags1(self): self.assertParsed("tree<ul><li><i>a</i></li></ul>") def testParseTags2(self): self.assertParsed("tree<ul><li>a<i>b</i><b>c</b></li></ul>") def testEqualsSimilarWithTags(self): self.treesEqual(self.topTree, ListTree("<i>top</i>")) def testTopText(self): assert "top" == ListTree("top").textToString() def testToplessTreeText(self): text = "<ul><li>a</li><li>b</li></ul>" result = self.toplessTree.textToString() assert text == result def testTopTextWithTags(self): assert "top" == ListTree("<i><b>top</b></i>").textToString() def testTreeText(self): assert ("tree<ul><li>a</li><li>b<ul><li>c</li></ul></li></ul>" == self.tree.textToString()) def testTreeTextWithTags(self): s = "tree<ul><li>a<i>b</i><b>c</b></li></ul>" assert "tree<ul><li>abc</li></ul>" == ListTree.parse(s).textToString() def testParseTreeNoCloseLi(self): try: ListTree.parse("tree<ul><li>a<li>b<ul><li>c</ul></ul>") except Exception: pass else: self.fail("Doesn't handle lists with </li> missing.") def assertParsed(self, s): print s result = ListTree.parse(s).toString() print result assert s == result def treesEqual(self, t1, t2): assert t1 == t2 assert t2 == t1 assert ListTree.equals(t1, t2) assert ListTree.equals(t2, t1) def treesUnEqual(self, t1, t2): assert t1 != t2 assert t2 != t1 assert not ListTree.equals(t1, t2) assert not ListTree.equals(t2, t1)
def treesEqual(self, t1, t2): assert t1 == t2 assert t2 == t1 assert ListTree.equals(t1, t2) assert ListTree.equals(t2, t1)
def treesUnEqual(self, t1, t2): assert t1 != t2 assert t2 != t1 assert not ListTree.equals(t1, t2) assert not ListTree.equals(t2, t1)
def assertParsed(self, s): print s result = ListTree.parse(s).toString() print result assert s == result
def testParseSpace(self): assert "a" == ListTree.parse("<i>a </i>").text
def testTreeTextWithTags(self): s = "tree<ul><li>a<i>b</i><b>c</b></li></ul>" assert "tree<ul><li>abc</li></ul>" == ListTree.parse(s).textToString()
def setUp(self): print '%s %s' % (self.id(), self.shortDescription()) self.topTree = ListTree("top") self.tree = ListTree("tree", [ListTree("a"), ListTree("b", [ListTree("c")])]) self.toplessTree = ListTree("", [ListTree("a"), ListTree("b")])
def testParseTop(self): assert self.topTree, ListTree.parse("top")
def setUp(self): print '%s %s' % (self.id(), self.shortDescription()) self.topTree = ListTree("top") self.tree = ListTree( "tree", [ListTree("a"), ListTree("b", [ListTree("c")])]) self.toplessTree = ListTree("", [ListTree("a"), ListTree("b")])
def testNotEqualsSimilarOne(self): self.treesUnEqual(self.topTree, ListTree("bottom"))
def testNotEqualsSimilarShapedTree(self): tree2 = ListTree( "tree", [ListTree("a"), ListTree("b", [ListTree("C")])]) self.treesUnEqual(self.tree, tree2)
class CompareFilesFixture(DoFixture): _typeDict = {"baseDir": "String"} OK = ListTree("OK") baseDir = "" def __init__(self, sut=None): self.baseDir = os.getcwd() # print "in CompareFilesFixture.__init__ cwd: '%s'" % self.baseDir super(CompareFilesFixture, self).__init__(sut) # Returns OK if the two directories contain equal directories and files. # Otherwise it returns an error message for the mismatches. _typeDict["folderSameAs.types"] = [TreeTypeAdapter, "String", "String"] _typeDict["directorySameAs.RenameTo"] = "folderSameAs" def folderSameAs(self, diryName1, diryName2): result = self._dirySameAs(diryName1, diryName2) if result != self.OK: return result else: return ListTree(os.path.basename(diryName1)) # Returns OK if the two files match. # Otherwise it returns an error message for the mismatch. _typeDict["fileSameAs.types"] = [TreeTypeAdapter, "String", "String"] def fileSameAs(self, fileName1, fileName2): return self._filesSameAs(fileName1, fileName2) # ----------- end of externally callable routines --------------- # --------- edit checking routines: existance, type, type match ----------- def _filesSameAs(self, file1, file2): f1Status = self._checkStatus(file1) f2Status = self._checkStatus(file2) ## em("\nin _filesSameAs\nfile1: '%s' status: '%s'" ## "\nfile2: '%s' status: '%s'" % ## (file1, f1Status, file2, f2Status)) if f1Status == "dir" and f2Status == "dir": return self._dirySame(file1, file2) if f1Status == "file" and f2Status == "file": return self._filesSame(file1, file2) # should this be error? if f1Status == "missing": return self._error(file1, "Missing") if f2Status == "missing": return self._error(file2, "Missing") return self._error(file1, "Mismatch: file and folder") def _dirySameAs(self, diry1, diry2): d1Status = self._checkStatus(diry1) d2Status = self._checkStatus(diry2) ## em("\nin _dirySameAs\ndiry1: '%s' status: '%s'" ## "\ndiry2: '%s' status: '%s'" % ## (diry1, d1Status, diry2, d2Status)) if d1Status == "dir" and d2Status == "dir": return self._dirySame(diry1, diry2) if d1Status == "file" and d2Status == "file": return self._filesSame(diry1, diry2) if d1Status == "missing": return self._error(diry1, "Missing") if d2Status == "missing": return self._error(diry2, "Missing") return self._error(diry1, "Mismatch: file and folder") def _checkStatus(self, fileOrDir): path = os.path.join(self.baseDir, fileOrDir) if os.path.isdir(path): return "dir" elif os.path.isfile(path): return "file" return "missing" # ---------------- main compare routines ---------------------- def _dirySame(self, diry1, diry2): ## em("\nin _dirySame 1: '%s' 2: '%s'" % (diry1, diry2)) resultTree = ListTree(os.path.basename(diry1)) anyErrors = False surplus = [] # hack to match Java version specification tests. d1List = os.listdir(diry1) d1List.sort() d2List = os.listdir(diry2) d2List.sort() d1 = 0 d2 = 0 while d1 < len(d1List) and d2 < len(d2List): d1Base = d1List[d1] d2Base = d2List[d2] d1Path = os.path.join(diry1, d1Base) d2Path = os.path.join(diry2, d2Base) ## em("--- d1Base: '%s' d2Base: '%s'" % (d1Base, d2Base)) if d1Base < d2Base: d1 += 1 resultTree.children = self._mismatchError(d1Path, "Missing") anyErrors = True continue if d1Base > d2Base: d2 += 1 surplus.append(self._mismatchError(d2Path, "Surplus")) anyErrors = True continue d1Status = self._checkStatus(d1Path) d2Status = self._checkStatus(d2Path) if d1Status == "missing": anyErrors = True resultTree.children = self._error(d1Base, "Missing") elif d2Status == "missing": anyErrors = True resultTree.children = self._error(d2Base, "Missing") elif d1Status != d2Status: resultTree.children = self._error(d1Base, d2Base, "Can't compare a folder with a file") anyErrors = True elif d1Status == "dir": if d1Base != "CVS": resultTree.children = self._dirySame(d1Path, d2Path) # recursion else: filesSameAs = self._filesSame(d1Path, d2Path) if filesSameAs == self.OK: resultTree.children = ListTree(self._htmlLink(d1Path)) else: anyErrors = True resultTree.children = filesSameAs d1 += 1 d2 += 1 while d1 < len(d1List): anyErrors = True d1Path = os.path.join(diry1, d1List[d1]) d1 += 1 resultTree.children = self._mismatchError(d1Path, "Missing") while d2 < len(d2List): d2Path = os.path.join(diry2, d2List[d2]) d2 += 1 anyErrors = True surplus.append(self._mismatchError(d2Path, "Surplus")) resultTree._children += surplus if anyErrors: return resultTree return self.OK def _mismatchError(self, path, kind): result = self._checkStatus(path) ## em("--- mismatch. path: '%s' kind: '%s' status: '%s'" % ## (path, kind, result)) base = os.path.basename(path) if result == "dir": return self._error(base, "%s folder" % kind) else: return self._error(base, kind) def _filesSame(self, file1, file2): if not os.path.exists(file1): return self._error(file1, "File doesn't exist") if not os.path.exists(file2): return self._error(file2, "File doesn't exist") f1Len = self._getLen(file1) f2Len = self._getLen(file2) if f1Len < 0: return self._error(file1, "File is inaccessible") if f2Len < 0: return self._error(file2, "File is inaccessible") lengthDifference = f2Len - f1Len if lengthDifference > 0: return self._error(file1, file2, "File shorter by %s" " bytes than: " % lengthDifference) elif lengthDifference < 0: return self._error(file1, file2, "File longer by %s" " bytes than: " % abs(lengthDifference)) return self._compareFiles(file1, file2) def _getLen(self, path): try: length = os.stat(path).st_size except: length = -1 return length def _compareFiles(self, file1, file2): f1Text = self._getFileText(file1) f2Text = self._getFileText(file2) if f1Text == f2Text: return self.OK i = 0 while i < len(f1Text): if f1Text[i] != f2Text[i]: return self._error(file1, file2, "Files differ at byte position %s" % i) i += 1 return self.OK # should not be possible def _getFileText(self, path): f1 = open(path, "rb") f1Text = f1.read() f1.close() return f1Text def _error(self, file1, file2, msg=None): if msg is None: msg = file2 file2 = None if file2 is None: return ListTree(self._htmlLink(file1), [ListTree("<i>%s</i>" % msg)]) else: error = self._error(file1, msg) error.addChild(ListTree(self._htmlLink(file2))) return error def _htmlLink(self, path): # don't know what to do with this... lastPart = os.path.basename(path) return lastPart
def testEqualsSimilarWithTags(self): self.treesEqual(self.topTree, ListTree("<i>top</i>"))
def folderSameAs(self, diryName1, diryName2): result = self._dirySameAs(diryName1, diryName2) if result != self.OK: return result else: return ListTree(os.path.basename(diryName1))
def plus12(self, t1, t2): # em("in plus12. t1: '%s' t2: '%s'" % (t1, t2)) result = ListTree("", [t1, t2]) return result
def testTopText(self): assert "top" == ListTree("top").textToString()
def testTopTextWithTags(self): assert "top" == ListTree("<i><b>top</b></i>").textToString()
def _dirySame(self, diry1, diry2): ## em("\nin _dirySame 1: '%s' 2: '%s'" % (diry1, diry2)) resultTree = ListTree(os.path.basename(diry1)) anyErrors = False surplus = [] # hack to match Java version specification tests. d1List = os.listdir(diry1) d1List.sort() d2List = os.listdir(diry2) d2List.sort() d1 = 0 d2 = 0 while d1 < len(d1List) and d2 < len(d2List): d1Base = d1List[d1] d2Base = d2List[d2] d1Path = os.path.join(diry1, d1Base) d2Path = os.path.join(diry2, d2Base) ## em("--- d1Base: '%s' d2Base: '%s'" % (d1Base, d2Base)) if d1Base < d2Base: d1 += 1 resultTree.children = self._mismatchError(d1Path, "Missing") anyErrors = True continue if d1Base > d2Base: d2 += 1 surplus.append(self._mismatchError(d2Path, "Surplus")) anyErrors = True continue d1Status = self._checkStatus(d1Path) d2Status = self._checkStatus(d2Path) if d1Status == "missing": anyErrors = True resultTree.children = self._error(d1Base, "Missing") elif d2Status == "missing": anyErrors = True resultTree.children = self._error(d2Base, "Missing") elif d1Status != d2Status: resultTree.children = self._error(d1Base, d2Base, "Can't compare a folder with a file") anyErrors = True elif d1Status == "dir": if d1Base != "CVS": resultTree.children = self._dirySame(d1Path, d2Path) # recursion else: filesSameAs = self._filesSame(d1Path, d2Path) if filesSameAs == self.OK: resultTree.children = ListTree(self._htmlLink(d1Path)) else: anyErrors = True resultTree.children = filesSameAs d1 += 1 d2 += 1 while d1 < len(d1List): anyErrors = True d1Path = os.path.join(diry1, d1List[d1]) d1 += 1 resultTree.children = self._mismatchError(d1Path, "Missing") while d2 < len(d2List): d2Path = os.path.join(diry2, d2List[d2]) d2 += 1 anyErrors = True surplus.append(self._mismatchError(d2Path, "Surplus")) resultTree._children += surplus if anyErrors: return resultTree return self.OK
def __init__(self, i, aTree): self.i = i self.tree = ListTree.parse(aTree)