]> git.ipfire.org Git - thirdparty/git.git/blame - t/t4034-diff-words.sh
Git 1.7.8-rc2
[thirdparty/git.git] / t / t4034-diff-words.sh
CommitLineData
2e5d2003
JS
1#!/bin/sh
2
3test_description='word diff colors'
4
5. ./test-lib.sh
6
5094d158
JN
7cat >pre.simple <<-\EOF
8 h(4)
2e5d2003 9
5094d158
JN
10 a = b + c
11EOF
12cat >post.simple <<-\EOF
13 h(4),hh[44]
2e5d2003 14
5094d158 15 a = b + c
2e5d2003 16
5094d158 17 aa = a
2e5d2003 18
5094d158 19 aeff = aeff * ( aaa )
2e5d2003 20EOF
5094d158
JN
21cat >expect.letter-runs-are-words <<-\EOF
22 <BOLD>diff --git a/pre b/post<RESET>
23 <BOLD>index 330b04f..5ed8eff 100644<RESET>
24 <BOLD>--- a/pre<RESET>
25 <BOLD>+++ b/post<RESET>
26 <CYAN>@@ -1,3 +1,7 @@<RESET>
27 h(4),<GREEN>hh<RESET>[44]
2e5d2003 28
5094d158 29 a = b + c<RESET>
2e5d2003 30
5094d158 31 <GREEN>aa = a<RESET>
2e5d2003 32
5094d158 33 <GREEN>aeff = aeff * ( aaa<RESET> )
2e5d2003 34EOF
5094d158
JN
35cat >expect.non-whitespace-is-word <<-\EOF
36 <BOLD>diff --git a/pre b/post<RESET>
37 <BOLD>index 330b04f..5ed8eff 100644<RESET>
38 <BOLD>--- a/pre<RESET>
39 <BOLD>+++ b/post<RESET>
40 <CYAN>@@ -1,3 +1,7 @@<RESET>
41 h(4)<GREEN>,hh[44]<RESET>
2e5d2003 42
5094d158 43 a = b + c<RESET>
2e5d2003 44
5094d158 45 <GREEN>aa = a<RESET>
2e5d2003 46
5094d158 47 <GREEN>aeff = aeff * ( aaa )<RESET>
2e5d2003
JS
48EOF
49
5094d158
JN
50word_diff () {
51 test_must_fail git diff --no-index "$@" pre post >output &&
52 test_decode_color <output >output.decrypted &&
53 test_cmp expect output.decrypted
54}
2e5d2003 55
5094d158
JN
56test_language_driver () {
57 lang=$1
58 test_expect_success "diff driver '$lang'" '
59 cp "$TEST_DIRECTORY/t4034/'"$lang"'/pre" \
60 "$TEST_DIRECTORY/t4034/'"$lang"'/post" \
61 "$TEST_DIRECTORY/t4034/'"$lang"'/expect" . &&
62 echo "* diff='"$lang"'" >.gitattributes &&
63 word_diff --color-words
64 '
65}
2e5d2003 66
5094d158
JN
67test_expect_success setup '
68 git config diff.color.old red &&
69 git config diff.color.new green &&
70 git config diff.color.func magenta
2e5d2003
JS
71'
72
5094d158
JN
73test_expect_success 'set up pre and post with runs of whitespace' '
74 cp pre.simple pre &&
75 cp post.simple post
882749a0
TR
76'
77
5094d158
JN
78test_expect_success 'word diff with runs of whitespace' '
79 cat >expect <<-\EOF &&
80 <BOLD>diff --git a/pre b/post<RESET>
81 <BOLD>index 330b04f..5ed8eff 100644<RESET>
82 <BOLD>--- a/pre<RESET>
83 <BOLD>+++ b/post<RESET>
84 <CYAN>@@ -1,3 +1,7 @@<RESET>
85 <RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
86
87 a = b + c<RESET>
88
89 <GREEN>aa = a<RESET>
90
91 <GREEN>aeff = aeff * ( aaa )<RESET>
92 EOF
93 word_diff --color-words &&
94 word_diff --word-diff=color &&
882749a0 95 word_diff --color --word-diff=color
882749a0
TR
96'
97
882749a0 98test_expect_success '--word-diff=porcelain' '
5094d158
JN
99 sed 's/#.*$//' >expect <<-\EOF &&
100 diff --git a/pre b/post
101 index 330b04f..5ed8eff 100644
102 --- a/pre
103 +++ b/post
104 @@ -1,3 +1,7 @@
105 -h(4)
106 +h(4),hh[44]
107 ~
108 # significant space
109 ~
110 a = b + c
111 ~
112 ~
113 +aa = a
114 ~
115 ~
116 +aeff = aeff * ( aaa )
117 ~
118 EOF
882749a0 119 word_diff --word-diff=porcelain
882749a0
TR
120'
121
882749a0 122test_expect_success '--word-diff=plain' '
5094d158
JN
123 cat >expect <<-\EOF &&
124 diff --git a/pre b/post
125 index 330b04f..5ed8eff 100644
126 --- a/pre
127 +++ b/post
128 @@ -1,3 +1,7 @@
129 [-h(4)-]{+h(4),hh[44]+}
882749a0 130
5094d158 131 a = b + c
882749a0 132
5094d158 133 {+aa = a+}
882749a0 134
5094d158
JN
135 {+aeff = aeff * ( aaa )+}
136 EOF
137 word_diff --word-diff=plain &&
882749a0 138 word_diff --word-diff=plain --no-color
882749a0
TR
139'
140
5094d158
JN
141test_expect_success '--word-diff=plain --color' '
142 cat >expect <<-\EOF &&
143 <BOLD>diff --git a/pre b/post<RESET>
144 <BOLD>index 330b04f..5ed8eff 100644<RESET>
145 <BOLD>--- a/pre<RESET>
146 <BOLD>+++ b/post<RESET>
147 <CYAN>@@ -1,3 +1,7 @@<RESET>
148 <RED>[-h(4)-]<RESET><GREEN>{+h(4),hh[44]+}<RESET>
882749a0 149
5094d158 150 a = b + c<RESET>
882749a0 151
5094d158 152 <GREEN>{+aa = a+}<RESET>
882749a0 153
5094d158
JN
154 <GREEN>{+aeff = aeff * ( aaa )+}<RESET>
155 EOF
882749a0 156 word_diff --word-diff=plain --color
882749a0
TR
157'
158
a4ca1465 159test_expect_success 'word diff without context' '
5094d158
JN
160 cat >expect <<-\EOF &&
161 <BOLD>diff --git a/pre b/post<RESET>
162 <BOLD>index 330b04f..5ed8eff 100644<RESET>
163 <BOLD>--- a/pre<RESET>
164 <BOLD>+++ b/post<RESET>
165 <CYAN>@@ -1 +1 @@<RESET>
166 <RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
167 <CYAN>@@ -3,0 +4,4 @@<RESET> <RESET><MAGENTA>a = b + c<RESET>
168
169 <GREEN>aa = a<RESET>
170
171 <GREEN>aeff = aeff * ( aaa )<RESET>
172 EOF
168eff3c 173 word_diff --color-words --unified=0
168eff3c
MH
174'
175
2b6a5417 176test_expect_success 'word diff with a regular expression' '
5094d158 177 cp expect.letter-runs-are-words expect &&
2b6a5417 178 word_diff --color-words="[a-z]+"
2b6a5417
JS
179'
180
5094d158 181test_expect_success 'set up a diff driver' '
ae3b970a 182 git config diff.testdriver.wordRegex "[^[:space:]]" &&
5094d158
JN
183 cat <<-\EOF >.gitattributes
184 pre diff=testdriver
185 post diff=testdriver
186 EOF
80c49c3d
TR
187'
188
98a4d87b 189test_expect_success 'option overrides .gitattributes' '
5094d158 190 cp expect.letter-runs-are-words expect &&
80c49c3d 191 word_diff --color-words="[a-z]+"
80c49c3d
TR
192'
193
98a4d87b 194test_expect_success 'use regex supplied by driver' '
5094d158 195 cp expect.non-whitespace-is-word expect &&
80c49c3d 196 word_diff --color-words
80c49c3d
TR
197'
198
5094d158 199test_expect_success 'set up diff.wordRegex option' '
ae3b970a 200 git config diff.wordRegex "[[:alnum:]]+"
98a4d87b
BSSJ
201'
202
98a4d87b 203test_expect_success 'command-line overrides config' '
5094d158 204 cp expect.letter-runs-are-words expect &&
98a4d87b
BSSJ
205 word_diff --color-words="[a-z]+"
206'
207
5094d158
JN
208test_expect_success 'command-line overrides config: --word-diff-regex' '
209 cat >expect <<-\EOF &&
210 <BOLD>diff --git a/pre b/post<RESET>
211 <BOLD>index 330b04f..5ed8eff 100644<RESET>
212 <BOLD>--- a/pre<RESET>
213 <BOLD>+++ b/post<RESET>
214 <CYAN>@@ -1,3 +1,7 @@<RESET>
215 h(4),<GREEN>{+hh+}<RESET>[44]
882749a0 216
5094d158 217 a = b + c<RESET>
882749a0 218
5094d158 219 <GREEN>{+aa = a+}<RESET>
882749a0 220
5094d158
JN
221 <GREEN>{+aeff = aeff * ( aaa+}<RESET> )
222 EOF
882749a0
TR
223 word_diff --color --word-diff-regex="[a-z]+"
224'
225
98a4d87b 226test_expect_success '.gitattributes override config' '
5094d158 227 cp expect.non-whitespace-is-word expect &&
98a4d87b
BSSJ
228 word_diff --color-words
229'
230
5094d158
JN
231test_expect_success 'setup: remove diff driver regex' '
232 test_might_fail git config --unset diff.testdriver.wordRegex
98a4d87b
BSSJ
233'
234
5094d158
JN
235test_expect_success 'use configured regex' '
236 cat >expect <<-\EOF &&
237 <BOLD>diff --git a/pre b/post<RESET>
238 <BOLD>index 330b04f..5ed8eff 100644<RESET>
239 <BOLD>--- a/pre<RESET>
240 <BOLD>+++ b/post<RESET>
241 <CYAN>@@ -1,3 +1,7 @@<RESET>
242 h(4),<GREEN>hh[44<RESET>]
98a4d87b 243
5094d158 244 a = b + c<RESET>
98a4d87b 245
5094d158 246 <GREEN>aa = a<RESET>
98a4d87b 247
5094d158
JN
248 <GREEN>aeff = aeff * ( aaa<RESET> )
249 EOF
98a4d87b
BSSJ
250 word_diff --color-words
251'
252
2b6a5417 253test_expect_success 'test parsing words for newline' '
5094d158
JN
254 echo "aaa (aaa)" >pre &&
255 echo "aaa (aaa) aaa" >post &&
256 cat >expect <<-\EOF &&
257 <BOLD>diff --git a/pre b/post<RESET>
258 <BOLD>index c29453b..be22f37 100644<RESET>
259 <BOLD>--- a/pre<RESET>
260 <BOLD>+++ b/post<RESET>
261 <CYAN>@@ -1 +1 @@<RESET>
262 aaa (aaa) <GREEN>aaa<RESET>
263 EOF
2b6a5417 264 word_diff --color-words="a+"
2b6a5417
JS
265'
266
2b6a5417 267test_expect_success 'test when words are only removed at the end' '
5094d158
JN
268 echo "(:" >pre &&
269 echo "(" >post &&
270 cat >expect <<-\EOF &&
271 <BOLD>diff --git a/pre b/post<RESET>
272 <BOLD>index 289cb9d..2d06f37 100644<RESET>
273 <BOLD>--- a/pre<RESET>
274 <BOLD>+++ b/post<RESET>
275 <CYAN>@@ -1 +1 @@<RESET>
276 (<RED>:<RESET>
277 EOF
2b6a5417 278 word_diff --color-words=.
2b6a5417
JS
279'
280
882749a0 281test_expect_success '--word-diff=none' '
5094d158
JN
282 echo "(:" >pre &&
283 echo "(" >post &&
284 cat >expect <<-\EOF &&
285 diff --git a/pre b/post
286 index 289cb9d..2d06f37 100644
287 --- a/pre
288 +++ b/post
289 @@ -1 +1 @@
290 -(:
291 +(
292 EOF
882749a0 293 word_diff --word-diff=plain --word-diff=none
882749a0
TR
294'
295
5094d158
JN
296test_language_driver bibtex
297test_language_driver cpp
298test_language_driver csharp
299test_language_driver fortran
300test_language_driver html
301test_language_driver java
302test_language_driver objc
303test_language_driver pascal
5269edf1 304test_language_driver perl
5094d158
JN
305test_language_driver php
306test_language_driver python
307test_language_driver ruby
308test_language_driver tex
8d96e728 309
42536dd9
JM
310test_expect_success 'word-diff with diff.sbe' '
311 cat >expect <<-\EOF &&
312 diff --git a/pre b/post
313 index a1a53b5..bc8fe6d 100644
314 --- a/pre
315 +++ b/post
316 @@ -1,3 +1,3 @@
317 a
318
319 [-b-]{+c+}
320 EOF
321 cat >pre <<-\EOF &&
322 a
323
324 b
325 EOF
326 cat >post <<-\EOF &&
327 a
328
329 c
330 EOF
331 test_when_finished "git config --unset diff.suppress-blank-empty" &&
332 git config diff.suppress-blank-empty true &&
333 word_diff --word-diff=plain
334'
335
2e5d2003 336test_done