t / t4018-diff-funcname.shon commit Merge branch 'maint' (1c71541)
   1#!/bin/sh
   2#
   3# Copyright (c) 2007 Johannes E. Schindelin
   4#
   5
   6test_description='Test custom diff function name patterns'
   7
   8. ./test-lib.sh
   9
  10LF='
  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;
  34
  35use strict;
  36use warnings;
  37use parent qw(Exporter);
  38our @EXPORT_OK = qw(round finalround);
  39
  40sub other; # forward declaration
  41
  42# hello
  43
  44sub 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
  53sub finalround
  54{
  55        print "Go to the store, buy some more\n";
  56        print "99 bottles of beer on the wall.\n");
  57}
  58
  59sub withheredocument {
  60        print <<"EOF"
  61decoy here-doc
  62EOF
  63        # some lines of context
  64        # to pad it out
  65        print "hello\n";
  66}
  67
  68__END__
  69
  70=head1 NAME
  71
  72Beer - 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        }
  84
  85        song;
  86
  87=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
  95
  96test_config () {
  97        git config "$1" "$2" &&
  98        test_when_finished "git config --unset $1"
  99}
 100
 101test_expect_funcname () {
 102        lang=${2-java}
 103        test_expect_code 1 git diff --no-index -U1 \
 104                "Beer.$lang" "Beer-correct.$lang" >diff &&
 105        grep "^@@.*@@ $1" diff
 106}
 107
 108for p in ada bibtex cpp csharp fortran html java matlab objc pascal perl php python ruby tex
 109do
 110        test_expect_success "builtin $p pattern compiles" '
 111                echo "*.java diff=$p" >.gitattributes &&
 112                test_expect_code 1 git diff --no-index \
 113                        Beer.java Beer-correct.java 2>msg &&
 114                ! grep fatal msg &&
 115                ! grep error msg
 116        '
 117        test_expect_success "builtin $p wordRegex pattern compiles" '
 118                echo "*.java diff=$p" >.gitattributes &&
 119                test_expect_code 1 git diff --no-index --word-diff \
 120                        Beer.java Beer-correct.java 2>msg &&
 121                ! grep fatal msg &&
 122                ! grep error msg
 123        '
 124done
 125
 126test_expect_success 'default behaviour' '
 127        rm -f .gitattributes &&
 128        test_expect_funcname "public class Beer\$"
 129'
 130
 131test_expect_success 'set up .gitattributes declaring drivers to test' '
 132        cat >.gitattributes <<-\EOF
 133        *.java diff=java
 134        *.perl diff=perl
 135        EOF
 136'
 137
 138test_expect_success 'preset java pattern' '
 139        test_expect_funcname "public static void main("
 140'
 141
 142test_expect_success 'preset perl pattern' '
 143        test_expect_funcname "sub round {\$" perl
 144'
 145
 146test_expect_success 'perl pattern accepts K&R style brace placement, too' '
 147        test_expect_funcname "sub finalround\$" perl
 148'
 149
 150test_expect_success 'but is not distracted by end of <<here document' '
 151        test_expect_funcname "sub withheredocument {\$" perl
 152'
 153
 154test_expect_success 'perl pattern is not distracted by sub within POD' '
 155        test_expect_funcname "=head" perl
 156'
 157
 158test_expect_success 'perl pattern gets full line of POD header' '
 159        test_expect_funcname "=head1 SYNOPSIS\$" perl
 160'
 161
 162test_expect_success 'perl pattern is not distracted by forward declaration' '
 163        test_expect_funcname "package Beer;\$" perl
 164'
 165
 166test_expect_success 'custom pattern' '
 167        test_config diff.java.funcname "!static
 168!String
 169[^      ].*s.*" &&
 170        test_expect_funcname "int special;\$"
 171'
 172
 173test_expect_success 'last regexp must not be negated' '
 174        test_config diff.java.funcname "!static" &&
 175        test_expect_code 128 git diff --no-index Beer.java Beer-correct.java 2>msg &&
 176        grep ": Last expression must not be negated:" msg
 177'
 178
 179test_expect_success 'pattern which matches to end of line' '
 180        test_config diff.java.funcname "Beer\$" &&
 181        test_expect_funcname "Beer\$"
 182'
 183
 184test_expect_success 'alternation in pattern' '
 185        test_config diff.java.funcname "Beer$" &&
 186        test_config diff.java.xfuncname "^[     ]*((public|static).*)$" &&
 187        test_expect_funcname "public static void main("
 188'
 189
 190test_done