#!/bin/sh

# This is a lsdiff(1) testcase for range exclusion functionality (-F x prefix).

. ${top_srcdir-.}/tests/common.sh

# Create a patch with 5 files to test various range exclusion patterns
cat << EOF > multi-file.patch
--- file1.txt
+++ file1.txt
@@ -1 +1 @@
-old1
+new1
--- file2.txt
+++ file2.txt
@@ -1 +1 @@
-old2
+new2
--- file3.txt
+++ file3.txt
@@ -1 +1 @@
-old3
+new3
--- file4.txt
+++ file4.txt
@@ -1 +1 @@
-old4
+new4
--- file5.txt
+++ file5.txt
@@ -1 +1 @@
-old5
+new5
EOF

# Test 1: Exclude single file (x2) - should show files 1, 3, 4, 5
echo "Testing single file exclusion (x2)..."
${LSDIFF} -F x2 multi-file.patch 2>errors1 >result1 || exit 1
[ -s errors1 ] && { echo "Unexpected errors in test 1:"; cat errors1; exit 1; }

cat << EOF | cmp - result1 || { echo "Test 1 failed"; exit 1; }
file1.txt
file3.txt
file4.txt
file5.txt
EOF

# Test 2: Exclude comma-separated list (x1,3,5) - should show files 2, 4
echo "Testing comma-separated exclusion (x1,3,5)..."
${LSDIFF} -F x1,3,5 multi-file.patch 2>errors2 >result2 || exit 1
[ -s errors2 ] && { echo "Unexpected errors in test 2:"; cat errors2; exit 1; }

cat << EOF | cmp - result2 || { echo "Test 2 failed"; exit 1; }
file2.txt
file4.txt
EOF

# Test 3: Exclude range (x2-4) - should show files 1, 5
echo "Testing range exclusion (x2-4)..."
${LSDIFF} -F x2-4 multi-file.patch 2>errors3 >result3 || exit 1
[ -s errors3 ] && { echo "Unexpected errors in test 3:"; cat errors3; exit 1; }

cat << EOF | cmp - result3 || { echo "Test 3 failed"; exit 1; }
file1.txt
file5.txt
EOF

# Test 4: Exclude open-ended range (x3-) - should show files 1, 2
echo "Testing open-ended range exclusion (x3-)..."
${LSDIFF} -F x3- multi-file.patch 2>errors4 >result4 || exit 1
[ -s errors4 ] && { echo "Unexpected errors in test 4:"; cat errors4; exit 1; }

cat << EOF | cmp - result4 || { echo "Test 4 failed"; exit 1; }
file1.txt
file2.txt
EOF

# Test 5: Mixed exclusion (x1,4-5) - should show files 2, 3
echo "Testing mixed exclusion (x1,4-5)..."
${LSDIFF} -F x1,4-5 multi-file.patch 2>errors5 >result5 || exit 1
[ -s errors5 ] && { echo "Unexpected errors in test 5:"; cat errors5; exit 1; }

cat << EOF | cmp - result5 || { echo "Test 5 failed"; exit 1; }
file2.txt
file3.txt
EOF

# Test 6: Compare with positive range selection to ensure exclusion works correctly
# First test positive selection (2,4) - should show files 2, 4
echo "Testing positive range selection for comparison (2,4)..."
${LSDIFF} -F 2,4 multi-file.patch 2>errors6 >result6 || exit 1
[ -s errors6 ] && { echo "Unexpected errors in test 6:"; cat errors6; exit 1; }

cat << EOF | cmp - result6 || { echo "Test 6 failed"; exit 1; }
file2.txt
file4.txt
EOF

# Test 7: Verify that exclusion of 2,4 gives the complement of positive selection
echo "Testing exclusion complement (x2,4)..."
${LSDIFF} -F x2,4 multi-file.patch 2>errors7 >result7 || exit 1
[ -s errors7 ] && { echo "Unexpected errors in test 7:"; cat errors7; exit 1; }

cat << EOF | cmp - result7 || { echo "Test 7 failed"; exit 1; }
file1.txt
file3.txt
file5.txt
EOF

echo "All range exclusion tests passed!"
