@matheusbsbs / matheus-bagliano / commits / a471833d51
Fork 0

test-lib: extend test_decode_color to handle more color codes

Enhance the test_decode_color function to handle all common color codes, including background colors and escapes that contain multiple codes. This change necessitates changing <WHITE> to <BOLD>, so update t4034 as well. This change is necessary for the next commit in order to test background colors properly. Signed-off-by: Kevin Ballard <kevin@sb.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Kevin Ballard committed Oct 20, 2010 at 15:17 UTC a471833d51b0902d68f9190157bf1421d19a951a
2 files changed +77 -44
t/t4034-diff-words.sh
+36 -36
@@ -35,10 +35,10 @@ aeff = aeff * ( aaa )
35 EOF
36
37 cat > expect <<\EOF
38 -<WHITE>diff --git a/pre b/post<RESET>
39 -<WHITE>index 330b04f..5ed8eff 100644<RESET>
40 -<WHITE>--- a/pre<RESET>
41 -<WHITE>+++ b/post<RESET>
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
@@ -122,10 +122,10 @@ test_expect_success '--word-diff=plain --no-color' '
122 '
123
124 cat > expect <<EOF
125 -<WHITE>diff --git a/pre b/post<RESET>
126 -<WHITE>index 330b04f..5ed8eff 100644<RESET>
127 -<WHITE>--- a/pre<RESET>
128 -<WHITE>+++ b/post<RESET>
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
@@ -143,10 +143,10 @@ test_expect_success '--word-diff=plain --color' '
143 '
144
145 cat > expect <<\EOF
146 -<WHITE>diff --git a/pre b/post<RESET>
147 -<WHITE>index 330b04f..5ed8eff 100644<RESET>
148 -<WHITE>--- a/pre<RESET>
149 -<WHITE>+++ b/post<RESET>
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>
@@ -163,10 +163,10 @@ test_expect_success 'word diff without context' '
163 '
164
165 cat > expect <<\EOF
166 -<WHITE>diff --git a/pre b/post<RESET>
167 -<WHITE>index 330b04f..5ed8eff 100644<RESET>
168 -<WHITE>--- a/pre<RESET>
169 -<WHITE>+++ b/post<RESET>
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
@@ -199,10 +199,10 @@ test_expect_success 'option overrides .gitattributes' '
199 '
200
201 cat > expect <<\EOF
202 -<WHITE>diff --git a/pre b/post<RESET>
203 -<WHITE>index 330b04f..5ed8eff 100644<RESET>
204 -<WHITE>--- a/pre<RESET>
205 -<WHITE>+++ b/post<RESET>
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
@@ -231,10 +231,10 @@ test_expect_success 'command-line overrides config' '
231 '
232
233 cat > expect <<\EOF
234 -<WHITE>diff --git a/pre b/post<RESET>
235 -<WHITE>index 330b04f..5ed8eff 100644<RESET>
236 -<WHITE>--- a/pre<RESET>
237 -<WHITE>+++ b/post<RESET>
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
@@ -260,10 +260,10 @@ test_expect_success 'remove diff driver regex' '
260 '
261
262 cat > expect <<\EOF
263 -<WHITE>diff --git a/pre b/post<RESET>
264 -<WHITE>index 330b04f..5ed8eff 100644<RESET>
265 -<WHITE>--- a/pre<RESET>
266 -<WHITE>+++ b/post<RESET>
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
@@ -282,10 +282,10 @@ echo 'aaa (aaa)' > pre
282 echo 'aaa (aaa) aaa' > post
283
284 cat > expect <<\EOF
285 -<WHITE>diff --git a/pre b/post<RESET>
286 -<WHITE>index c29453b..be22f37 100644<RESET>
287 -<WHITE>--- a/pre<RESET>
288 -<WHITE>+++ b/post<RESET>
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
@@ -301,10 +301,10 @@ echo '(:' > pre
301 echo '(' > post
302
303 cat > expect <<\EOF
304 -<WHITE>diff --git a/pre b/post<RESET>
305 -<WHITE>index 289cb9d..2d06f37 100644<RESET>
306 -<WHITE>--- a/pre<RESET>
307 -<WHITE>+++ b/post<RESET>
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
t/test-lib.sh
+41 -8
@@ -238,14 +238,47 @@ test_set_editor () {
238 }
239
240 test_decode_color () {
241 - sed -e 's/.\[1m/<WHITE>/g' \
242 - -e 's/.\[31m/<RED>/g' \
243 - -e 's/.\[32m/<GREEN>/g' \
244 - -e 's/.\[33m/<YELLOW>/g' \
245 - -e 's/.\[34m/<BLUE>/g' \
246 - -e 's/.\[35m/<MAGENTA>/g' \
247 - -e 's/.\[36m/<CYAN>/g' \
248 - -e 's/.\[m/<RESET>/g'
241 + awk '
242 + function name(n) {
243 + if (n == 0) return "RESET";
244 + if (n == 1) return "BOLD";
245 + if (n == 30) return "BLACK";
246 + if (n == 31) return "RED";
247 + if (n == 32) return "GREEN";
248 + if (n == 33) return "YELLOW";
249 + if (n == 34) return "BLUE";
250 + if (n == 35) return "MAGENTA";
251 + if (n == 36) return "CYAN";
252 + if (n == 37) return "WHITE";
253 + if (n == 40) return "BLACK";
254 + if (n == 41) return "BRED";
255 + if (n == 42) return "BGREEN";
256 + if (n == 43) return "BYELLOW";
257 + if (n == 44) return "BBLUE";
258 + if (n == 45) return "BMAGENTA";
259 + if (n == 46) return "BCYAN";
260 + if (n == 47) return "BWHITE";
261 + }
262 + {
263 + while (match($0, /\x1b\[[0-9;]*m/) != 0) {
264 + printf "%s<", substr($0, 1, RSTART-1);
265 + codes = substr($0, RSTART+2, RLENGTH-3);
266 + if (length(codes) == 0)
267 + printf "%s", name(0)
268 + else {
269 + n = split(codes, ary, ";");
270 + sep = "";
271 + for (i = 1; i <= n; i++) {
272 + printf "%s%s", sep, name(ary[i]);
273 + sep = ";"
274 + }
275 + }
276 + printf ">";
277 + $0 = substr($0, RSTART + RLENGTH, length($0) - RSTART - RLENGTH + 1);
278 + }
279 + print
280 + }
281 + '
282 }
283
284 q_to_nul () {