1#!/bin/sh
   2#
   3# Copyright (c) 2007 Johannes E. Schindelin
   4#
   5test_description='Test custom diff function name patterns'
   7. ./test-lib.sh
   9LF='
  11'
  12cat >Beer.java <<\EOF
  13public 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}
  30EOF
  31sed 's/beer\\/beer,\\/' <Beer.java >Beer-correct.java
  32cat >Beer.perl <<\EOT
  33package Beer;
  34use strict;
  36use warnings;
  37use parent qw(Exporter);
  38our @EXPORT_OK = qw(round finalround);
  39sub other; # forward declaration
  41# hello
  43sub 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}
  52sub finalround
  54{
  55        print "Go to the store, buy some more\n";
  56        print "99 bottles of beer on the wall.\n");
  57}
  58sub withheredocument {
  60        print <<"EOF"
  61decoy here-doc
  62EOF
  63        # some lines of context
  64        # to pad it out
  65        print "hello\n";
  66}
  67__END__
  69=head1 NAME
  71Beer - subroutine to output fragment of a drinking song
  73=head1 SYNOPSIS
  75        use Beer qw(round finalround);
  77        sub song {
  79                for (my $i = 99; $i > 0; $i--) {
  80                        round $i;
  81                }
  82                finalround;
  83        }
  84        song;
  86=cut
  88EOT
  89sed -e '
  90        s/hello/goodbye/
  91        s/beer\\/beer,\\/
  92        s/more\\/more,\\/
  93        s/song;/song();/
  94' <Beer.perl >Beer-correct.perl
  95test_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}
 102for p in ada bibtex cpp csharp fortran html java matlab objc pascal perl php python ruby tex
 104do
 105        test_expect_success "builtin $p pattern compiles" '
 106                echo "*.java diff=$p" >.gitattributes &&
 107                test_expect_code 1 git diff --no-index \
 108                        Beer.java Beer-correct.java 2>msg &&
 109                ! grep fatal msg &&
 110                ! grep error msg
 111        '
 112        test_expect_success "builtin $p wordRegex pattern compiles" '
 113                echo "*.java diff=$p" >.gitattributes &&
 114                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
 118        '
 119done
 120test_expect_success 'default behaviour' '
 122        rm -f .gitattributes &&
 123        test_expect_funcname "public class Beer\$"
 124'
 125test_expect_success 'set up .gitattributes declaring drivers to test' '
 127        cat >.gitattributes <<-\EOF
 128        *.java diff=java
 129        *.perl diff=perl
 130        EOF
 131'
 132test_expect_success 'preset java pattern' '
 134        test_expect_funcname "public static void main("
 135'
 136test_expect_success 'preset perl pattern' '
 138        test_expect_funcname "sub round {\$" perl
 139'
 140test_expect_success 'perl pattern accepts K&R style brace placement, too' '
 142        test_expect_funcname "sub finalround\$" perl
 143'
 144test_expect_success 'but is not distracted by end of <<here document' '
 146        test_expect_funcname "sub withheredocument {\$" perl
 147'
 148test_expect_success 'perl pattern is not distracted by sub within POD' '
 150        test_expect_funcname "=head" perl
 151'
 152test_expect_success 'perl pattern gets full line of POD header' '
 154        test_expect_funcname "=head1 SYNOPSIS\$" perl
 155'
 156test_expect_success 'perl pattern is not distracted by forward declaration' '
 158        test_expect_funcname "package Beer;\$" perl
 159'
 160test_expect_success 'custom pattern' '
 162        test_config diff.java.funcname "!static
 163!String
 164[^      ].*s.*" &&
 165        test_expect_funcname "int special;\$"
 166'
 167test_expect_success 'last regexp must not be negated' '
 169        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
 172'
 173test_expect_success 'pattern which matches to end of line' '
 175        test_config diff.java.funcname "Beer\$" &&
 176        test_expect_funcname "Beer\$"
 177'
 178test_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'
 184test_done