Forking what is left of ZeroNet and hopefully adding an AT Proto Frontend/Proxy
1import io
2
3from util import Diff
4
5
6class TestDiff:
7 def testDiff(self):
8 assert Diff.diff(
9 [],
10 ["one", "two", "three"]
11 ) == [("+", ["one", "two","three"])]
12
13 assert Diff.diff(
14 ["one", "two", "three"],
15 ["one", "two", "three", "four", "five"]
16 ) == [("=", 11), ("+", ["four", "five"])]
17
18 assert Diff.diff(
19 ["one", "two", "three", "six"],
20 ["one", "two", "three", "four", "five", "six"]
21 ) == [("=", 11), ("+", ["four", "five"]), ("=", 3)]
22
23 assert Diff.diff(
24 ["one", "two", "three", "hmm", "six"],
25 ["one", "two", "three", "four", "five", "six"]
26 ) == [("=", 11), ("-", 3), ("+", ["four", "five"]), ("=", 3)]
27
28 assert Diff.diff(
29 ["one", "two", "three"],
30 []
31 ) == [("-", 11)]
32
33 def testUtf8(self):
34 assert Diff.diff(
35 ["one", "\xe5\xad\xa6\xe4\xb9\xa0\xe4\xb8\x8b", "two", "three"],
36 ["one", "\xe5\xad\xa6\xe4\xb9\xa0\xe4\xb8\x8b", "two", "three", "four", "five"]
37 ) == [("=", 20), ("+", ["four", "five"])]
38
39 def testDiffLimit(self):
40 old_f = io.BytesIO(b"one\ntwo\nthree\nhmm\nsix")
41 new_f = io.BytesIO(b"one\ntwo\nthree\nfour\nfive\nsix")
42 actions = Diff.diff(list(old_f), list(new_f), limit=1024)
43 assert actions
44
45 old_f = io.BytesIO(b"one\ntwo\nthree\nhmm\nsix")
46 new_f = io.BytesIO(b"one\ntwo\nthree\nfour\nfive\nsix"*1024)
47 actions = Diff.diff(list(old_f), list(new_f), limit=1024)
48 assert actions is False
49
50 def testPatch(self):
51 old_f = io.BytesIO(b"one\ntwo\nthree\nhmm\nsix")
52 new_f = io.BytesIO(b"one\ntwo\nthree\nfour\nfive\nsix")
53 actions = Diff.diff(
54 list(old_f),
55 list(new_f)
56 )
57 old_f.seek(0)
58 assert Diff.patch(old_f, actions).getvalue() == new_f.getvalue()