Skip to content

Commit c228a5c

Browse files
committed
Merge branch 'js/userdiff-cc'
Improves the pattern to match the hunk-header for C/C++. * js/userdiff-cc: userdiff: have 'cpp' hunk header pattern catch more C++ anchor points t4018: test cases showing that the cpp pattern misses many anchor points t4018: test cases for the built-in cpp pattern t4018: reduce test files for pattern compilation tests t4018: convert custom pattern test to the new infrastructure t4018: convert java pattern test to the new infrastructure t4018: convert perl pattern tests to the new infrastructure t4018: an infrastructure to test hunk headers userdiff: support unsigned and long long suffixes of integer constants userdiff: support C++ ->* and .* operators in the word regexp
2 parents e164a8f + 8a2e8da commit c228a5c

33 files changed

+303
-160
lines changed

t/t4018-diff-funcname.sh

Lines changed: 77 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -7,179 +7,103 @@ test_description='Test custom diff function name patterns'
77

88
. ./test-lib.sh
99

10-
LF='
11-
'
12-
cat >Beer.java <<\EOF
13-
public class Beer
14-
{
15-
int special;
16-
public static void main(String args[])
17-
{
18-
String s=" ";
19-
for(int x = 99; x > 0; x--)
20-
{
21-
System.out.print(x + " bottles of beer on the wall "
22-
+ x + " bottles of beer\n"
23-
+ "Take one down, pass it around, " + (x - 1)
24-
+ " bottles of beer on the wall.\n");
25-
}
26-
System.out.print("Go to the store, buy some more,\n"
27-
+ "99 bottles of beer on the wall.\n");
28-
}
29-
}
30-
EOF
31-
sed 's/beer\\/beer,\\/' <Beer.java >Beer-correct.java
32-
cat >Beer.perl <<\EOT
33-
package Beer;
34-
35-
use strict;
36-
use warnings;
37-
use parent qw(Exporter);
38-
our @EXPORT_OK = qw(round finalround);
39-
40-
sub other; # forward declaration
41-
42-
# hello
43-
44-
sub round {
45-
my ($n) = @_;
46-
print "$n bottles of beer on the wall ";
47-
print "$n bottles of beer\n";
48-
print "Take one down, pass it around, ";
49-
$n = $n - 1;
50-
print "$n bottles of beer on the wall.\n";
51-
}
52-
53-
sub finalround
54-
{
55-
print "Go to the store, buy some more\n";
56-
print "99 bottles of beer on the wall.\n");
57-
}
58-
59-
sub withheredocument {
60-
print <<"EOF"
61-
decoy here-doc
62-
EOF
63-
# some lines of context
64-
# to pad it out
65-
print "hello\n";
66-
}
67-
68-
__END__
69-
70-
=head1 NAME
71-
72-
Beer - subroutine to output fragment of a drinking song
73-
74-
=head1 SYNOPSIS
75-
76-
use Beer qw(round finalround);
77-
78-
sub song {
79-
for (my $i = 99; $i > 0; $i--) {
80-
round $i;
81-
}
82-
finalround;
83-
}
10+
test_expect_success 'setup' '
11+
# a non-trivial custom pattern
12+
git config diff.custom1.funcname "!static
13+
!String
14+
[^ ].*s.*" &&
8415
85-
song;
16+
# a custom pattern which matches to end of line
17+
git config diff.custom2.funcname "......Beer\$" &&
8618
87-
=cut
88-
EOT
89-
sed -e '
90-
s/hello/goodbye/
91-
s/beer\\/beer,\\/
92-
s/more\\/more,\\/
93-
s/song;/song();/
94-
' <Beer.perl >Beer-correct.perl
19+
# alternation in pattern
20+
git config diff.custom3.funcname "Beer$" &&
21+
git config diff.custom3.xfuncname "^[ ]*((public|static).*)$" &&
9522
96-
test_expect_funcname () {
97-
lang=${2-java}
98-
test_expect_code 1 git diff --no-index -U1 \
99-
"Beer.$lang" "Beer-correct.$lang" >diff &&
100-
grep "^@@.*@@ $1" diff
101-
}
23+
# for regexp compilation tests
24+
echo A >A.java &&
25+
echo B >B.java
26+
'
10227

103-
for p in ada bibtex cpp csharp fortran html java matlab objc pascal perl php python ruby tex
28+
diffpatterns="
29+
ada
30+
bibtex
31+
cpp
32+
csharp
33+
fortran
34+
html
35+
java
36+
matlab
37+
objc
38+
pascal
39+
perl
40+
php
41+
python
42+
ruby
43+
tex
44+
custom1
45+
custom2
46+
custom3
47+
"
48+
49+
for p in $diffpatterns
10450
do
10551
test_expect_success "builtin $p pattern compiles" '
10652
echo "*.java diff=$p" >.gitattributes &&
10753
test_expect_code 1 git diff --no-index \
108-
Beer.java Beer-correct.java 2>msg &&
109-
! grep fatal msg &&
110-
! grep error msg
54+
A.java B.java 2>msg &&
55+
! test_i18ngrep fatal msg &&
56+
! test_i18ngrep error msg
11157
'
11258
test_expect_success "builtin $p wordRegex pattern compiles" '
11359
echo "*.java diff=$p" >.gitattributes &&
11460
test_expect_code 1 git diff --no-index --word-diff \
115-
Beer.java Beer-correct.java 2>msg &&
116-
! grep fatal msg &&
117-
! grep error msg
61+
A.java B.java 2>msg &&
62+
! test_i18ngrep fatal msg &&
63+
! test_i18ngrep error msg
11864
'
11965
done
12066

121-
test_expect_success 'default behaviour' '
122-
rm -f .gitattributes &&
123-
test_expect_funcname "public class Beer\$"
124-
'
125-
126-
test_expect_success 'set up .gitattributes declaring drivers to test' '
127-
cat >.gitattributes <<-\EOF
128-
*.java diff=java
129-
*.perl diff=perl
130-
EOF
131-
'
132-
133-
test_expect_success 'preset java pattern' '
134-
test_expect_funcname "public static void main("
135-
'
136-
137-
test_expect_success 'preset perl pattern' '
138-
test_expect_funcname "sub round {\$" perl
139-
'
140-
141-
test_expect_success 'perl pattern accepts K&R style brace placement, too' '
142-
test_expect_funcname "sub finalround\$" perl
143-
'
144-
145-
test_expect_success 'but is not distracted by end of <<here document' '
146-
test_expect_funcname "sub withheredocument {\$" perl
147-
'
148-
149-
test_expect_success 'perl pattern is not distracted by sub within POD' '
150-
test_expect_funcname "=head" perl
151-
'
152-
153-
test_expect_success 'perl pattern gets full line of POD header' '
154-
test_expect_funcname "=head1 SYNOPSIS\$" perl
155-
'
156-
157-
test_expect_success 'perl pattern is not distracted by forward declaration' '
158-
test_expect_funcname "package Beer;\$" perl
159-
'
160-
161-
test_expect_success 'custom pattern' '
162-
test_config diff.java.funcname "!static
163-
!String
164-
[^ ].*s.*" &&
165-
test_expect_funcname "int special;\$"
166-
'
167-
16867
test_expect_success 'last regexp must not be negated' '
68+
echo "*.java diff=java" >.gitattributes &&
16969
test_config diff.java.funcname "!static" &&
170-
test_expect_code 128 git diff --no-index Beer.java Beer-correct.java 2>msg &&
171-
grep ": Last expression must not be negated:" msg
70+
test_expect_code 128 git diff --no-index A.java B.java 2>msg &&
71+
test_i18ngrep ": Last expression must not be negated:" msg
17272
'
17373

174-
test_expect_success 'pattern which matches to end of line' '
175-
test_config diff.java.funcname "Beer\$" &&
176-
test_expect_funcname "Beer\$"
74+
test_expect_success 'setup hunk header tests' '
75+
for i in $diffpatterns
76+
do
77+
echo "$i-* diff=$i"
78+
done > .gitattributes &&
79+
80+
# add all test files to the index
81+
(
82+
cd "$TEST_DIRECTORY"/t4018 &&
83+
git --git-dir="$TRASH_DIRECTORY/.git" add .
84+
) &&
85+
86+
# place modified files in the worktree
87+
for i in $(git ls-files)
88+
do
89+
sed -e "s/ChangeMe/IWasChanged/" <"$TEST_DIRECTORY/t4018/$i" >"$i" || return 1
90+
done
17791
'
17892

179-
test_expect_success 'alternation in pattern' '
180-
test_config diff.java.funcname "Beer$" &&
181-
test_config diff.java.xfuncname "^[ ]*((public|static).*)$" &&
182-
test_expect_funcname "public static void main("
183-
'
93+
# check each individual file
94+
for i in $(git ls-files)
95+
do
96+
if grep broken "$i" >/dev/null 2>&1
97+
then
98+
result=failure
99+
else
100+
result=success
101+
fi
102+
test_expect_$result "hunk header: $i" "
103+
test_when_finished 'cat actual' && # for debugging only
104+
git diff -U1 $i >actual &&
105+
grep '@@ .* @@.*RIGHT' actual
106+
"
107+
done
184108

185109
test_done

t/t4018/README

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
How to write RIGHT test cases
2+
=============================
3+
4+
Insert the word "ChangeMe" (exactly this form) at a distance of
5+
at least two lines from the line that must appear in the hunk header.
6+
7+
The text that must appear in the hunk header must contain the word
8+
"right", but in all upper-case, like in the title above.
9+
10+
To mark a test case that highlights a malfunction, insert the word
11+
BROKEN in all lower-case somewhere in the file.
12+
13+
This text is a bit twisted and out of order, but it is itself a
14+
test case for the default hunk header pattern. Know what you are doing
15+
if you change it.
16+
17+
BTW, this tests that the head line goes to the hunk header, not the line
18+
of equal signs.

t/t4018/cpp-c++-function

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Item RIGHT::DoSomething( Args with_spaces )
2+
{
3+
ChangeMe;
4+
}

t/t4018/cpp-class-constructor

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Item::Item(int RIGHT)
2+
{
3+
ChangeMe;
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Item::Item(int RIGHT) :
2+
member(0)
3+
{
4+
ChangeMe;
5+
}

t/t4018/cpp-class-definition

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class RIGHT
2+
{
3+
int ChangeMe;
4+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class RIGHT :
2+
public Baseclass
3+
{
4+
int ChangeMe;
5+
};

t/t4018/cpp-class-destructor

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
RIGHT::~RIGHT()
2+
{
3+
ChangeMe;
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
::Item get::it::RIGHT()
2+
{
3+
ChangeMe;
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
get::Item get::it::RIGHT()
2+
{
3+
ChangeMe;
4+
}
5+

0 commit comments

Comments
 (0)