@matheusbsbs / matheus-bagliano / commits / 5094d15874
Fork 0

t4034 (diff --word-diff): style suggestions

Rearrange code to be easier to browse: - first data - then functions - then test assertions Mark up inline test vectors as cat >vector <<-\EOF data data EOF for visual scannability. Use words like "set up" for tests that set up for other tests, to make it obvious which tests are safe to skip. Use repeated function calls instead of a loop for the language-specific tests, so the invocations can be easily tweaked individually (for example if one starts to fail). This means if you add a new subdirectory to t4034/, it will not be automatically used. I think that's worth it for the added explicitness. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

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