t / lib-diff-alternative.shon commit Merge branch 'sg/clone-initial-fetch-configuration' (84d1783)
   1# Helpers shared by the test scripts for diff algorithms (patience,
   2# histogram, etc).
   3
   4test_diff_frobnitz() {
   5        cat >file1 <<\EOF
   6#include <stdio.h>
   7
   8// Frobs foo heartily
   9int frobnitz(int foo)
  10{
  11    int i;
  12    for(i = 0; i < 10; i++)
  13    {
  14        printf("Your answer is: ");
  15        printf("%d\n", foo);
  16    }
  17}
  18
  19int fact(int n)
  20{
  21    if(n > 1)
  22    {
  23        return fact(n-1) * n;
  24    }
  25    return 1;
  26}
  27
  28int main(int argc, char **argv)
  29{
  30    frobnitz(fact(10));
  31}
  32EOF
  33
  34        cat >file2 <<\EOF
  35#include <stdio.h>
  36
  37int fib(int n)
  38{
  39    if(n > 2)
  40    {
  41        return fib(n-1) + fib(n-2);
  42    }
  43    return 1;
  44}
  45
  46// Frobs foo heartily
  47int frobnitz(int foo)
  48{
  49    int i;
  50    for(i = 0; i < 10; i++)
  51    {
  52        printf("%d\n", foo);
  53    }
  54}
  55
  56int main(int argc, char **argv)
  57{
  58    frobnitz(fib(10));
  59}
  60EOF
  61
  62        file1=$(git rev-parse --short $(git hash-object file1))
  63        file2=$(git rev-parse --short $(git hash-object file2))
  64        cat >expect <<EOF
  65diff --git a/file1 b/file2
  66index $file1..$file2 100644
  67--- a/file1
  68+++ b/file2
  69@@ -1,26 +1,25 @@
  70 #include <stdio.h>
  71 
  72+int fib(int n)
  73+{
  74+    if(n > 2)
  75+    {
  76+        return fib(n-1) + fib(n-2);
  77+    }
  78+    return 1;
  79+}
  80+
  81 // Frobs foo heartily
  82 int frobnitz(int foo)
  83 {
  84     int i;
  85     for(i = 0; i < 10; i++)
  86     {
  87-        printf("Your answer is: ");
  88         printf("%d\n", foo);
  89     }
  90 }
  91 
  92-int fact(int n)
  93-{
  94-    if(n > 1)
  95-    {
  96-        return fact(n-1) * n;
  97-    }
  98-    return 1;
  99-}
 100-
 101 int main(int argc, char **argv)
 102 {
 103-    frobnitz(fact(10));
 104+    frobnitz(fib(10));
 105 }
 106EOF
 107
 108        STRATEGY=$1
 109
 110        test_expect_success "$STRATEGY diff" '
 111                test_must_fail git diff --no-index "--$STRATEGY" file1 file2 > output &&
 112                test_cmp expect output
 113        '
 114
 115        test_expect_success "$STRATEGY diff output is valid" '
 116                mv file2 expect &&
 117                git apply < output &&
 118                test_cmp expect file2
 119        '
 120}
 121
 122test_diff_unique() {
 123        cat >uniq1 <<\EOF
 1241
 1252
 1263
 1274
 1285
 1296
 130EOF
 131
 132        cat >uniq2 <<\EOF
 133a
 134b
 135c
 136d
 137e
 138f
 139EOF
 140
 141        uniq1=$(git rev-parse --short $(git hash-object uniq1))
 142        uniq2=$(git rev-parse --short $(git hash-object uniq2))
 143        cat >expect <<EOF
 144diff --git a/uniq1 b/uniq2
 145index $uniq1..$uniq2 100644
 146--- a/uniq1
 147+++ b/uniq2
 148@@ -1,6 +1,6 @@
 149-1
 150-2
 151-3
 152-4
 153-5
 154-6
 155+a
 156+b
 157+c
 158+d
 159+e
 160+f
 161EOF
 162
 163        STRATEGY=$1
 164
 165        test_expect_success 'completely different files' '
 166                test_must_fail git diff --no-index "--$STRATEGY" uniq1 uniq2 > output &&
 167                test_cmp expect output
 168        '
 169}
 170