]> asedeno.scripts.mit.edu Git - git.git/blob - t/t4034-diff-words.sh
color-words: make regex configurable via attributes
[git.git] / t / t4034-diff-words.sh
1 #!/bin/sh
2
3 test_description='word diff colors'
4
5 . ./test-lib.sh
6
7 test_expect_success setup '
8
9         git config diff.color.old red
10         git config diff.color.new green
11
12 '
13
14 decrypt_color () {
15         sed \
16                 -e 's/.\[1m/<WHITE>/g' \
17                 -e 's/.\[31m/<RED>/g' \
18                 -e 's/.\[32m/<GREEN>/g' \
19                 -e 's/.\[36m/<BROWN>/g' \
20                 -e 's/.\[m/<RESET>/g'
21 }
22
23 word_diff () {
24         test_must_fail git diff --no-index "$@" pre post > output &&
25         decrypt_color < output > output.decrypted &&
26         test_cmp expect output.decrypted
27 }
28
29 cat > pre <<\EOF
30 h(4)
31
32 a = b + c
33 EOF
34
35 cat > post <<\EOF
36 h(4),hh[44]
37
38 a = b + c
39
40 aa = a
41
42 aeff = aeff * ( aaa )
43 EOF
44
45 cat > expect <<\EOF
46 <WHITE>diff --git a/pre b/post<RESET>
47 <WHITE>index 330b04f..5ed8eff 100644<RESET>
48 <WHITE>--- a/pre<RESET>
49 <WHITE>+++ b/post<RESET>
50 <BROWN>@@ -1,3 +1,7 @@<RESET>
51 <RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
52 <RESET>
53 a = b + c<RESET>
54
55 <GREEN>aa = a<RESET>
56
57 <GREEN>aeff = aeff * ( aaa )<RESET>
58 EOF
59
60 test_expect_success 'word diff with runs of whitespace' '
61
62         word_diff --color-words
63
64 '
65
66 cat > expect <<\EOF
67 <WHITE>diff --git a/pre b/post<RESET>
68 <WHITE>index 330b04f..5ed8eff 100644<RESET>
69 <WHITE>--- a/pre<RESET>
70 <WHITE>+++ b/post<RESET>
71 <BROWN>@@ -1,3 +1,7 @@<RESET>
72 h(4),<GREEN>hh<RESET>[44]
73 <RESET>
74 a = b + c<RESET>
75
76 <GREEN>aa = a<RESET>
77
78 <GREEN>aeff = aeff * ( aaa<RESET> )
79 EOF
80
81 test_expect_success 'word diff with a regular expression' '
82
83         word_diff --color-words="[a-z]+"
84
85 '
86
87 test_expect_success 'set a diff driver' '
88         git config diff.testdriver.wordregex "[^[:space:]]" &&
89         cat <<EOF > .gitattributes
90 pre diff=testdriver
91 post diff=testdriver
92 EOF
93 '
94
95 test_expect_success 'option overrides default' '
96
97         word_diff --color-words="[a-z]+"
98
99 '
100
101 cat > expect <<\EOF
102 <WHITE>diff --git a/pre b/post<RESET>
103 <WHITE>index 330b04f..5ed8eff 100644<RESET>
104 <WHITE>--- a/pre<RESET>
105 <WHITE>+++ b/post<RESET>
106 <BROWN>@@ -1,3 +1,7 @@<RESET>
107 h(4)<GREEN>,hh[44]<RESET>
108 <RESET>
109 a = b + c<RESET>
110
111 <GREEN>aa = a<RESET>
112
113 <GREEN>aeff = aeff * ( aaa )<RESET>
114 EOF
115
116 test_expect_success 'use default supplied by driver' '
117
118         word_diff --color-words
119
120 '
121
122 echo 'aaa (aaa)' > pre
123 echo 'aaa (aaa) aaa' > post
124
125 cat > expect <<\EOF
126 <WHITE>diff --git a/pre b/post<RESET>
127 <WHITE>index c29453b..be22f37 100644<RESET>
128 <WHITE>--- a/pre<RESET>
129 <WHITE>+++ b/post<RESET>
130 <BROWN>@@ -1 +1 @@<RESET>
131 aaa (aaa) <GREEN>aaa<RESET>
132 EOF
133
134 test_expect_success 'test parsing words for newline' '
135
136         word_diff --color-words="a+"
137
138
139 '
140
141 echo '(:' > pre
142 echo '(' > post
143
144 cat > expect <<\EOF
145 <WHITE>diff --git a/pre b/post<RESET>
146 <WHITE>index 289cb9d..2d06f37 100644<RESET>
147 <WHITE>--- a/pre<RESET>
148 <WHITE>+++ b/post<RESET>
149 <BROWN>@@ -1 +1 @@<RESET>
150 (<RED>:<RESET>
151 EOF
152
153 test_expect_success 'test when words are only removed at the end' '
154
155         word_diff --color-words=.
156
157 '
158
159 test_done