userdiff/perl: catch sub with brace on second line
[git] / t / t4018-diff-funcname.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Johannes E. Schindelin
4 #
5
6 test_description='Test custom diff function name patterns'
7
8 . ./test-lib.sh
9
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 <<\EOF
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 __END__
60
61 =head1 NAME
62
63 Beer - subroutine to output fragment of a drinking song
64
65 =head1 SYNOPSIS
66
67         use Beer qw(round finalround);
68
69         sub song {
70                 for (my $i = 99; $i > 0; $i--) {
71                         round $i;
72                 }
73                 finalround;
74         }
75
76         song;
77
78 =cut
79 EOF
80 sed -e '
81         s/hello/goodbye/
82         s/beer\\/beer,\\/
83         s/more\\/more,\\/
84         s/song;/song();/
85 ' <Beer.perl >Beer-correct.perl
86
87 test_config () {
88         git config "$1" "$2" &&
89         test_when_finished "git config --unset $1"
90 }
91
92 test_expect_funcname () {
93         lang=${2-java}
94         test_expect_code 1 git diff --no-index -U1 \
95                 "Beer.$lang" "Beer-correct.$lang" >diff &&
96         grep "^@@.*@@ $1" diff
97 }
98
99 for p in bibtex cpp csharp fortran html java objc pascal perl php python ruby tex
100 do
101         test_expect_success "builtin $p pattern compiles" '
102                 echo "*.java diff=$p" >.gitattributes &&
103                 test_expect_code 1 git diff --no-index \
104                         Beer.java Beer-correct.java 2>msg &&
105                 ! grep fatal msg &&
106                 ! grep error msg
107         '
108         test_expect_success "builtin $p wordRegex pattern compiles" '
109                 echo "*.java diff=$p" >.gitattributes &&
110                 test_expect_code 1 git diff --no-index --word-diff \
111                         Beer.java Beer-correct.java 2>msg &&
112                 ! grep fatal msg &&
113                 ! grep error msg
114         '
115 done
116
117 test_expect_success 'default behaviour' '
118         rm -f .gitattributes &&
119         test_expect_funcname "public class Beer\$"
120 '
121
122 test_expect_success 'set up .gitattributes declaring drivers to test' '
123         cat >.gitattributes <<-\EOF
124         *.java diff=java
125         *.perl diff=perl
126         EOF
127 '
128
129 test_expect_success 'preset java pattern' '
130         test_expect_funcname "public static void main("
131 '
132
133 test_expect_success 'preset perl pattern' '
134         test_expect_funcname "sub round {\$" perl
135 '
136
137 test_expect_success 'perl pattern accepts K&R style brace placement, too' '
138         test_expect_funcname "sub finalround\$" perl
139 '
140
141 test_expect_success 'perl pattern is not distracted by sub within POD' '
142         test_expect_funcname "=head" perl
143 '
144
145 test_expect_success 'perl pattern gets full line of POD header' '
146         test_expect_funcname "=head1 SYNOPSIS\$" perl
147 '
148
149 test_expect_success 'perl pattern is not distracted by forward declaration' '
150         test_expect_funcname "package Beer;\$" perl
151 '
152
153 test_expect_success 'custom pattern' '
154         test_config diff.java.funcname "!static
155 !String
156 [^      ].*s.*" &&
157         test_expect_funcname "int special;\$"
158 '
159
160 test_expect_success 'last regexp must not be negated' '
161         test_config diff.java.funcname "!static" &&
162         test_expect_code 128 git diff --no-index Beer.java Beer-correct.java 2>msg &&
163         grep ": Last expression must not be negated:" msg
164 '
165
166 test_expect_success 'pattern which matches to end of line' '
167         test_config diff.java.funcname "Beer\$" &&
168         test_expect_funcname "Beer\$"
169 '
170
171 test_expect_success 'alternation in pattern' '
172         test_config diff.java.funcname "Beer$" &&
173         test_config diff.java.xfuncname "^[     ]*((public|static).*)$" &&
174         test_expect_funcname "public static void main("
175 '
176
177 test_done