t / t4018-diff-funcname.shon commit userdiff/perl: match full line of POD headers (12f0967)
   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 <<\EOF
  33package Beer;
  34
  35use strict;
  36use warnings;
  37use parent qw(Exporter);
  38our @EXPORT_OK = qw(round);
  39
  40sub round {
  41        my ($n) = @_;
  42        print "$n bottles of beer on the wall ";
  43        print "$n bottles of beer\n";
  44        print "Take one down, pass it around, ";
  45        $n = $n - 1;
  46        print "$n bottles of beer on the wall.\n";
  47}
  48
  49__END__
  50
  51=head1 NAME
  52
  53Beer - subroutine to output fragment of a drinking song
  54
  55=head1 SYNOPSIS
  56
  57        use Beer qw(round);
  58
  59        sub song {
  60                for (my $i = 99; $i > 0; $i--) {
  61                        round $i;
  62                }
  63        }
  64
  65        song;
  66
  67=cut
  68EOF
  69sed -e '
  70        s/beer\\/beer,\\/
  71        s/song;/song();/
  72' <Beer.perl >Beer-correct.perl
  73
  74test_config () {
  75        git config "$1" "$2" &&
  76        test_when_finished "git config --unset $1"
  77}
  78
  79test_expect_funcname () {
  80        lang=${2-java}
  81        test_expect_code 1 git diff --no-index -U1 \
  82                "Beer.$lang" "Beer-correct.$lang" >diff &&
  83        grep "^@@.*@@ $1" diff
  84}
  85
  86for p in bibtex cpp csharp fortran html java objc pascal perl php python ruby tex
  87do
  88        test_expect_success "builtin $p pattern compiles" '
  89                echo "*.java diff=$p" >.gitattributes &&
  90                test_expect_code 1 git diff --no-index \
  91                        Beer.java Beer-correct.java 2>msg &&
  92                ! grep fatal msg &&
  93                ! grep error msg
  94        '
  95        test_expect_success "builtin $p wordRegex pattern compiles" '
  96                echo "*.java diff=$p" >.gitattributes &&
  97                test_expect_code 1 git diff --no-index --word-diff \
  98                        Beer.java Beer-correct.java 2>msg &&
  99                ! grep fatal msg &&
 100                ! grep error msg
 101        '
 102done
 103
 104test_expect_success 'default behaviour' '
 105        rm -f .gitattributes &&
 106        test_expect_funcname "public class Beer\$"
 107'
 108
 109test_expect_success 'set up .gitattributes declaring drivers to test' '
 110        cat >.gitattributes <<-\EOF
 111        *.java diff=java
 112        *.perl diff=perl
 113        EOF
 114'
 115
 116test_expect_success 'preset java pattern' '
 117        test_expect_funcname "public static void main("
 118'
 119
 120test_expect_success 'preset perl pattern' '
 121        test_expect_funcname "sub round {\$" perl
 122'
 123
 124test_expect_success 'perl pattern is not distracted by sub within POD' '
 125        test_expect_funcname "=head" perl
 126'
 127
 128test_expect_success 'perl pattern gets full line of POD header' '
 129        test_expect_funcname "=head1 SYNOPSIS\$" perl
 130'
 131
 132test_expect_success 'custom pattern' '
 133        test_config diff.java.funcname "!static
 134!String
 135[^      ].*s.*" &&
 136        test_expect_funcname "int special;\$"
 137'
 138
 139test_expect_success 'last regexp must not be negated' '
 140        test_config diff.java.funcname "!static" &&
 141        test_expect_code 128 git diff --no-index Beer.java Beer-correct.java 2>msg &&
 142        grep ": Last expression must not be negated:" msg
 143'
 144
 145test_expect_success 'pattern which matches to end of line' '
 146        test_config diff.java.funcname "Beer\$" &&
 147        test_expect_funcname "Beer\$"
 148'
 149
 150test_expect_success 'alternation in pattern' '
 151        test_config diff.java.funcname "Beer$" &&
 152        test_config diff.java.xfuncname "^[     ]*((public|static).*)$" &&
 153        test_expect_funcname "public static void main("
 154'
 155
 156test_done