|
1 | 1 | import pytest |
2 | | -from exhash import line_hash, lnhash, lnhashview, exhash |
| 2 | +from exhash import line_hash, lnhash, lnhashview, exhash, exhash_result |
3 | 3 |
|
4 | 4 | def test_line_hash_returns_4_hex(): |
5 | 5 | h = line_hash("hello") |
@@ -27,86 +27,98 @@ def test_lnhashview_empty(): assert lnhashview("") == [] |
27 | 27 |
|
28 | 28 | def test_exhash_noop(): |
29 | 29 | res = exhash("foo\nbar\n", []) |
30 | | - assert res.lines == ["foo", "bar"] |
31 | | - assert res.text() == "foo\nbar" |
32 | | - assert res.modified == [] |
33 | | - assert res.deleted == [] |
| 30 | + assert res["lines"] == ["foo", "bar"] |
| 31 | + assert '\n'.join(res["lines"]) == "foo\nbar" |
| 32 | + assert res["modified"] == [] |
| 33 | + assert res["deleted"] == [] |
34 | 34 |
|
35 | 35 | def test_exhash_substitute(): |
36 | 36 | text = "foo\nbar\n" |
37 | 37 | addr = lnhash(1, "foo") |
38 | 38 | res = exhash(text, [f"{addr}s/foo/baz/"]) |
39 | | - assert res.lines == ["baz", "bar"] |
40 | | - assert res.modified == [1] |
41 | | - assert len(res.hashes) == 2 |
| 39 | + assert res["lines"] == ["baz", "bar"] |
| 40 | + assert res["modified"] == [1] |
| 41 | + assert len(res["hashes"]) == 2 |
42 | 42 |
|
43 | 43 | def test_exhash_delete(): |
44 | 44 | text = "a\nb\nc\n" |
45 | 45 | addr = lnhash(2, "b") |
46 | 46 | res = exhash(text, [f"{addr}d"]) |
47 | | - assert res.lines == ["a", "c"] |
48 | | - assert 2 in res.deleted |
| 47 | + assert res["lines"] == ["a", "c"] |
| 48 | + assert 2 in res["deleted"] |
49 | 49 |
|
50 | 50 | def test_exhash_append(): |
51 | 51 | text = "a\nb\n" |
52 | 52 | addr = lnhash(1, "a") |
53 | 53 | res = exhash(text, [f"{addr}a\nx\ny"]) |
54 | | - assert res.lines == ["a", "x", "y", "b"] |
55 | | - assert res.modified == [2, 3] |
| 54 | + assert res["lines"] == ["a", "x", "y", "b"] |
| 55 | + assert res["modified"] == [2, 3] |
56 | 56 |
|
57 | 57 | def test_exhash_insert(): |
58 | 58 | text = "a\nb\n" |
59 | 59 | addr = lnhash(2, "b") |
60 | 60 | res = exhash(text, [f"{addr}i\nx"]) |
61 | | - assert res.lines == ["a", "x", "b"] |
62 | | - assert res.modified == [2] |
| 61 | + assert res["lines"] == ["a", "x", "b"] |
| 62 | + assert res["modified"] == [2] |
63 | 63 |
|
64 | 64 | def test_exhash_stale_hash_raises(): |
65 | 65 | text = "hello\nworld\n" |
66 | 66 | addr = lnhash(1, "wrong") |
67 | 67 | with pytest.raises(ValueError): exhash(text, [f"{addr}d"]) |
68 | 68 |
|
69 | | -def test_exhash_repr_shows_modified(): |
| 69 | +def test_exhash_result_is_dict(): |
70 | 70 | text = "foo\nbar\n" |
71 | 71 | addr = lnhash(1, "foo") |
72 | 72 | res = exhash(text, [f"{addr}s/foo/baz/"]) |
73 | | - r = repr(res) |
74 | | - assert "baz" in r |
75 | | - assert "bar" not in r |
76 | | - assert r == f"{lnhash(1, 'baz')} baz" |
| 73 | + assert isinstance(res, dict) |
| 74 | + assert set(res.keys()) == {"lines", "hashes", "modified", "deleted"} |
| 75 | + |
| 76 | + |
| 77 | +def test_exhash_result_formats_modified(): |
| 78 | + text = "foo\nbar\n" |
| 79 | + addr = lnhash(1, "foo") |
| 80 | + res = exhash(text, [f"{addr}s/foo/baz/"]) |
| 81 | + assert exhash_result([res]) == f"{lnhash(1, 'baz')} baz" |
| 82 | + |
| 83 | + |
| 84 | +def test_exhash_result_no_modifications_is_empty(): assert exhash_result([exhash("foo\n", [])]) == "" |
| 85 | + |
| 86 | + |
| 87 | +def test_exhash_result_requires_list_of_dict(): |
| 88 | + with pytest.raises(TypeError): exhash_result(dict(lines=[], hashes=[], modified=[])) |
| 89 | + with pytest.raises(TypeError): exhash_result([1]) |
77 | 90 |
|
78 | | -def test_exhash_repr_noop_empty(): assert repr(exhash("foo\n", [])) == "" |
79 | 91 |
|
80 | 92 | def test_exhash_view(): |
81 | 93 | text = "foo\nbar\n" |
82 | 94 | res = exhash(text, []) |
83 | | - assert res.view() == f"{lnhash(1, 'foo')} foo\n{lnhash(2, 'bar')} bar" |
| 95 | + view = '\n'.join(f"{h} {l}" for h, l in zip(res["hashes"], res["lines"])) |
| 96 | + assert view == f"{lnhash(1, 'foo')} foo\n{lnhash(2, 'bar')} bar" |
84 | 97 |
|
85 | 98 | def test_exhash_result_hashes_match(): |
86 | 99 | text = "foo\nbar\n" |
87 | 100 | res = exhash(text, []) |
88 | | - for i, (h, line) in enumerate(zip(res.hashes, res.lines)): assert h == lnhash(i + 1, line) |
| 101 | + for i, (h, line) in enumerate(zip(res["hashes"], res["lines"])): assert h == lnhash(i + 1, line) |
89 | 102 |
|
90 | 103 | def test_exhash_multiple_cmds(): |
91 | 104 | text = "a\nb\nc\n" |
92 | 105 | a1, a3 = lnhash(1, "a"), lnhash(3, "c") |
93 | 106 | res = exhash(text, [f"{a1}s/a/A/", f"{a3}s/c/C/"]) |
94 | | - assert res.lines == ["A", "b", "C"] |
95 | | - assert res.modified == [1, 3] |
| 107 | + assert res["lines"] == ["A", "b", "C"] |
| 108 | + assert res["modified"] == [1, 3] |
96 | 109 |
|
97 | 110 | def test_exhash_append_trailing_newline(): |
98 | 111 | text = "a\nb\n" |
99 | 112 | addr = lnhash(1, "a") |
100 | 113 | res = exhash(text, [f"{addr}a\nx\n"]) |
101 | | - assert res.lines == ["a", "x", "", "b"] |
| 114 | + assert res["lines"] == ["a", "x", "", "b"] |
102 | 115 |
|
103 | 116 | def test_exhash_multiline_non_text_cmd_raises(): |
104 | 117 | text = "a\nb\n" |
105 | 118 | addr = lnhash(1, "a") |
106 | 119 | with pytest.raises(ValueError): exhash(text, [f"{addr}d\nextra"]) |
107 | 120 |
|
108 | | -def test_exhash_accepts_tuple_cmds(): |
| 121 | +def test_exhash_requires_list_cmds(): |
109 | 122 | text = "a\nb\n" |
110 | 123 | a1, a2 = lnhash(1, "a"), lnhash(2, "b") |
111 | | - res = exhash(text, (f"{a1}s/a/A/", f"{a2}s/b/B/")) |
112 | | - assert res.lines == ["A", "B"] |
| 124 | + with pytest.raises(TypeError): exhash(text, (f"{a1}s/a/A/", f"{a2}s/b/B/")) |
0 commit comments