-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhyp_diff_spec.rb
More file actions
195 lines (167 loc) · 6.03 KB
/
Copy pathhyp_diff_spec.rb
File metadata and controls
195 lines (167 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# encoding: utf-8
require "hyp_diff"
describe HypDiff do
def expect_diff(old, new, expected)
expect(HypDiff.compare(old, new)).to eq(expected)
end
it "diffs two texts, applying tags to indicate changes" do
expect_diff("byebye", "hello", '<del>byebye</del><ins>hello</ins>')
end
it "extracts text to diff from input markup, reapplying the (after-)markup to the diff" do
expect_diff(
"<b>byebye</b> world",
"<i>hello</i> world",
'<i><del>byebye</del><ins>hello</ins></i> world'
)
end
it "diffs word-by-word" do
expect_diff("byebye world", "hello world", '<del>byebye</del><ins>hello</ins> world')
end
it "handles pure additions" do
expect_diff("hello ", "hello world", 'hello <ins>world</ins>')
end
it "handles pure deletions" do
expect_diff("hello world", "hello ", 'hello <del>world</del>')
end
it "handles pure deletions at the beginning" do
expect_diff("hello world", " world", '<del>hello</del> world')
end
it "handles several pure deletions at the beginning" do
expect_diff("hello beautiful world", "world", '<del>hello beautiful </del>world')
end
it "merges consecutive additions into a single tag" do
expect_diff(
"hello world",
"why hello beautiful world",
"<ins>why </ins>hello<ins> beautiful</ins> world"
)
end
it "merges consecutive deletions into a single tag" do
expect_diff("hello beautiful world", "hello world", "hello <del>beautiful </del>world")
end
it "merge consecutive additions and edits into single tags" do
expect_diff(
"hello world",
"hello my beautiful",
"hello <del>world</del><ins>my beautiful</ins>"
)
end
it "merge consecutive deletions and edits into single tags" do
expect_diff(
"hello my beautiful",
"hello world",
"hello <del>my beautiful</del><ins>world</ins>"
)
end
describe "with callbacks for custom markup" do
it "uses them to generate the insertions and deletions" do
expect(HypDiff.compare(
"byebye world",
"hello world",
render_insertion: proc { |html| "<new>#{html}</new>" },
render_deletion: proc { |html| "<old>#{html}</old>" }
)).to eq(
"<old>byebye</old><new>hello</new> world"
)
end
end
describe "choosing which markup to use" do
it "allows to choose 'after'" do
expect(HypDiff.compare(
"<b>byebye world</b>",
"<i>hello world</i>",
markup_from: "after"
)).to eq(
"<i><del>byebye</del><ins>hello</ins> world</i>"
)
end
it "allows to choose 'before'" do
expect(HypDiff.compare(
"<b>byebye world</b>",
"<i>hello world</i>",
markup_from: "before"
)).to eq(
"<b><del>byebye</del><ins>hello</ins> world</b>"
)
end
end
describe "handling html entities" do
it "handles them transparently when whole words are entities" do
expect_diff(
"foo < bar",
"foo > bar",
"foo <del><</del><ins>></ins> bar"
)
end
it "handles them transparently when words contain entities" do
expect_diff(
"fü bär",
"fö bär",
"<del>fü</del><ins>fö</ins> bär"
)
end
end
describe "handling whitespace" do
it "treats consecutive whitespace as a single whitespace" do
expect_diff("hello world", "hello world", "hello world")
end
it "treats consecutive whitespace as a single whitespace across tags" do
expect_diff(
"<span>hello </span> <span> world</span>",
"hello world",
"hello world"
)
expect_diff(
"<span>hello </span>world",
"hello<span> world</span>",
"hello<span> world</span>"
)
end
it "considers trailing and leading whitespace for insertions and deletions" do
expect_diff("hello", "hello world", "hello<ins> world</ins>")
expect_diff("hello world", "hello", "hello<del> world</del>")
expect_diff("world", "hello world", "<ins>hello </ins>world")
expect_diff("hello world", "world", "<del>hello </del>world")
expect_diff(" world", "hello world", "<ins>hello</ins> world")
expect_diff("hello world", " world", "<del>hello</del> world")
expect_diff("hello ", "hello world", "hello <ins>world</ins>")
expect_diff("hello world", "hello ", "hello <del>world</del>")
end
it "considers trailing and leading whitespace changes" do
expect_diff("hello ", "hello", "hello<del> </del>")
expect_diff("hello", "hello ", "hello<ins> </ins>")
expect_diff(" hello", "hello", "<del> </del>hello")
expect_diff("hello", " hello", "<ins> </ins>hello")
end
it "considers changes of text and whitespace" do
expect_diff("hello world ", "hello friend", "hello <del>world </del><ins>friend</ins>")
expect_diff(" bye world", "hello world", "<del> bye</del><ins>hello</ins> world")
expect_diff("hello friend", "hello world ", "hello <del>friend</del><ins>world </ins>")
expect_diff("hello world", " bye world", "<del>hello</del><ins> bye</ins> world")
end
end
it "diffs punctuation signs as single tokens when followed by whitespace" do
expect_diff("hello world", "hello, world", "hello<ins>,</ins> world")
end
it "diffs changes of punctuation to words" do
expect_diff(
"hello, world",
"hello beautiful world",
"hello<del>,</del><ins> beautiful</ins> world"
)
expect_diff(
"hello beautiful world",
"hello, world",
"hello<del> beautiful</del><ins>,</ins> world"
)
end
it "diffs changes of punctuation to leading and trailing spaces" do
expect_diff("hello.", "hello ", "hello<del>.</del><ins> </ins>")
expect_diff("hello ", "hello.", "hello<del> </del><ins>.</ins>")
expect_diff(" hello", ".hello", "<del> </del><ins>.</ins>hello")
expect_diff(".hello", " hello", "<del>.</del><ins> </ins>hello")
end
it "diffs punctuation signs as single tokens when at end of string" do
expect_diff("hello world", "hello world.", "hello world<ins>.</ins>")
end
end