TestDiff.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import cStringIO as StringIO
  2. from util import Diff
  3. class TestDiff:
  4. def testDiff(self):
  5. assert Diff.diff(
  6. [],
  7. ["one", "two", "three"]
  8. ) == [("+", ["one", "two","three"])]
  9. assert Diff.diff(
  10. ["one", "two", "three"],
  11. ["one", "two", "three", "four", "five"]
  12. ) == [("=", 11), ("+", ["four", "five"])]
  13. assert Diff.diff(
  14. ["one", "two", "three", "six"],
  15. ["one", "two", "three", "four", "five", "six"]
  16. ) == [("=", 11), ("+", ["four", "five"]), ("=", 3)]
  17. assert Diff.diff(
  18. ["one", "two", "three", "hmm", "six"],
  19. ["one", "two", "three", "four", "five", "six"]
  20. ) == [("=", 11), ("-", 3), ("+", ["four", "five"]), ("=", 3)]
  21. assert Diff.diff(
  22. ["one", "two", "three"],
  23. []
  24. ) == [("-", 11)]
  25. def testDiffLimit(self):
  26. old_f = StringIO.StringIO("one\ntwo\nthree\nhmm\nsix")
  27. new_f = StringIO.StringIO("one\ntwo\nthree\nfour\nfive\nsix")
  28. actions = Diff.diff(list(old_f), list(new_f), limit=1024)
  29. assert actions
  30. old_f = StringIO.StringIO("one\ntwo\nthree\nhmm\nsix")
  31. new_f = StringIO.StringIO("one\ntwo\nthree\nfour\nfive\nsix"*1024)
  32. actions = Diff.diff(list(old_f), list(new_f), limit=1024)
  33. assert actions is False
  34. def testPatch(self):
  35. old_f = StringIO.StringIO("one\ntwo\nthree\nhmm\nsix")
  36. new_f = StringIO.StringIO("one\ntwo\nthree\nfour\nfive\nsix")
  37. actions = Diff.diff(
  38. list(old_f),
  39. list(new_f)
  40. )
  41. old_f.seek(0)
  42. assert Diff.patch(old_f, actions).getvalue() == new_f.getvalue()