# Currently, it is tested whether the correct commands are built
# for various input paths.
# The parameter -l is used to generate the list of commands that
# would be executed.

# Global parameters:
NUM_TESTS=27
NUM_ERRORS=0

# Store the current directory and switch to test directory:
OLD_DIR=$(pwd)
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $DIR
echo "Current dir: $DIR"

# The basic operation with one or more directories is tested: 
echo "Running basic operation tests..."
../map/mapper.py "ls _" data/ > output/test1
../map/mapper.py "ls _" data/* > output/test2
../map/mapper.py "ls _" data/*txt > output/test3
../map/mapper.py "ls _" data/subfolder/*abc data/anothersubfolder/*txt > output/test4
../map/mapper.py "ls _" data/ data/ > output/test5
../map/mapper.py "ls _" data/* data/* > output/test6

# The same type of operation with recursion is tested:
echo "Running basic operation tests with recursion..."
../map/mapper.py -r "ls _" data/*txt > output/test7
../map/mapper.py -r "ls _" data/*mat > output/test8
../map/mapper.py -r "ls _" data/*txt data/anothersubfolder data/subfolder/*abc > output/test9
../map/mapper.py -r "ls _" data/*jpg data/*txt data/*abc > output/test10

# Operations with folders instead of files are tested:
echo "Running tests with folders..."
../map/mapper.py -ld "mv _ _/../.." data/ > output/test11
../map/mapper.py -ldr "mv _ _/../.." data/ > output/test12
../map/mapper.py -ld "mv _ _\_%" data/ > output/test13
../map/mapper.py -ldr "mv _ _\_%" data/ > output/test14

# The different placeholders are tested:
echo "Running tests with placeholders..."
../map/mapper.py -l "command -option\:a _ -output\:b &:-%#" data/ > output/test15
../map/mapper.py -l "command -p\:\_ -p2\:\% -p3\:\# _ " data/ > output/test16

# The different options are tested:
echo "Running tests with different options..."
../map/mapper.py -lr -c 10 -n 2  "mv _ &:-%#" data/ > output/test17
../map/mapper.py -lr -c 10 -n 2 -x abc "mv _ &:-%#" data/ > output/test18
../map/mapper.py -lr -c 10 -n 2 -x abc "mv _ &:-%#" data/*txt > output/test19
../map/mapper.py -lr -c 10 -n 2 -x abc,txt "mv _ &:-%#" data/*txt > output/test20
../map/mapper.py -lr -c 10 -n 2 -x abc,txt "mv _ &:-%#" data/ > output/test21

# Wildcards together with different input folders are tested:
../map/mapper.py "ls _" data/*abc data/anothersubfolder/* > output/test22
../map/mapper.py -r "ls _" data/*abc data/anothersubfolder/* > output/test23

# Files with special extensions are tested:
../map/mapper.py -x . "ls _" data/ > output/test24
../map/mapper.py -r -x . "ls _" data/ > output/test25
../map/mapper.py -x ^ "ls _" data/ > output/test26
../map/mapper.py -r -x ^ "ls _" data/ > output/test27

echo "All tests have been executed."

echo "Comparing results to baseline..."

# Compare results against expected results:
for i in `seq 1 $NUM_TESTS`; do
	result=`cmp --silent output/test$i output/correct_result_test$i && echo "1" || echo "0"`
	if [ "$result" -eq 0 ]
	then
		echo "**********************"
		echo "*** Test $i failed ***"
		echo "**********************"
		echo "Expected output:"
		echo "----------------"
		cat output/correct_result_test$i
		printf "\n"
		echo "Test output:"
		echo "------------"
		cat output/test$i
		NUM_ERRORS=$((NUM_ERRORS+1))
	fi
done

printf "\n"
echo "Testing complete."
if [ "$NUM_ERRORS" -eq 1 ]
then
	echo "There was $NUM_ERRORS error."
else
	echo "There were $NUM_ERRORS errors."
fi

# Switch back to the initial directory:
cd $OLD_DIR
