t / t4018-diff-funcname.shon commit t4018: convert java pattern test to the new infrastructure (dd4dc5c)
   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
  32
  33test_expect_funcname () {
  34        lang=${2-java}
  35        test_expect_code 1 git diff --no-index -U1 \
  36                "Beer.$lang" "Beer-correct.$lang" >diff &&
  37        grep "^@@.*@@ $1" diff
  38}
  39
  40diffpatterns="
  41        ada
  42        bibtex
  43        cpp
  44        csharp
  45        fortran
  46        html
  47        java
  48        matlab
  49        objc
  50        pascal
  51        perl
  52        php
  53        python
  54        ruby
  55        tex
  56"
  57
  58for p in $diffpatterns
  59do
  60        test_expect_success "builtin $p pattern compiles" '
  61                echo "*.java diff=$p" >.gitattributes &&
  62                test_expect_code 1 git diff --no-index \
  63                        Beer.java Beer-correct.java 2>msg &&
  64                ! grep fatal msg &&
  65                ! grep error msg
  66        '
  67        test_expect_success "builtin $p wordRegex pattern compiles" '
  68                echo "*.java diff=$p" >.gitattributes &&
  69                test_expect_code 1 git diff --no-index --word-diff \
  70                        Beer.java Beer-correct.java 2>msg &&
  71                ! grep fatal msg &&
  72                ! grep error msg
  73        '
  74done
  75
  76test_expect_success 'set up .gitattributes declaring drivers to test' '
  77        cat >.gitattributes <<-\EOF
  78        *.java diff=java
  79        EOF
  80'
  81
  82test_expect_success 'custom pattern' '
  83        test_config diff.java.funcname "!static
  84!String
  85[^      ].*s.*" &&
  86        test_expect_funcname "int special;\$"
  87'
  88
  89test_expect_success 'last regexp must not be negated' '
  90        test_config diff.java.funcname "!static" &&
  91        test_expect_code 128 git diff --no-index Beer.java Beer-correct.java 2>msg &&
  92        grep ": Last expression must not be negated:" msg
  93'
  94
  95test_expect_success 'pattern which matches to end of line' '
  96        test_config diff.java.funcname "Beer\$" &&
  97        test_expect_funcname "Beer\$"
  98'
  99
 100test_expect_success 'alternation in pattern' '
 101        test_config diff.java.funcname "Beer$" &&
 102        test_config diff.java.xfuncname "^[     ]*((public|static).*)$" &&
 103        test_expect_funcname "public static void main("
 104'
 105
 106test_expect_success 'setup hunk header tests' '
 107        for i in $diffpatterns
 108        do
 109                echo "$i-* diff=$i"
 110        done > .gitattributes &&
 111
 112        # add all test files to the index
 113        (
 114                cd "$TEST_DIRECTORY"/t4018 &&
 115                git --git-dir="$TRASH_DIRECTORY/.git" add .
 116        ) &&
 117
 118        # place modified files in the worktree
 119        for i in $(git ls-files)
 120        do
 121                sed -e "s/ChangeMe/IWasChanged/" <"$TEST_DIRECTORY/t4018/$i" >"$i" || return 1
 122        done
 123'
 124
 125# check each individual file
 126for i in $(git ls-files)
 127do
 128        if grep broken "$i" >/dev/null 2>&1
 129        then
 130                result=failure
 131        else
 132                result=success
 133        fi
 134        test_expect_$result "hunk header: $i" "
 135                test_when_finished 'cat actual' &&      # for debugging only
 136                git diff -U1 $i >actual &&
 137                grep '@@ .* @@.*RIGHT' actual
 138        "
 139done
 140
 141test_done