a gleam implementation of a CS assignment originally written in cpp

feat: improve tests

Changed files
+25 -24
test
+25 -24
test/test.sh
···
# Test configuration
temp_file="lab66.output"
program="../build/lab66"
+
output_file="../build/verses.txt"
test_timeout=${1:-1}
# Test counters
···
# Cleanup function
cleanup() {
-
rm -f "$temp_file" verses.txt
+
rm -f "$temp_file" "$output_file"
}
# Error trap
···
# Test with known valid verse
basic_input="Genesis\n1\n1"
-
resp=$(timeout "$test_timeout" bash -c "echo -e '$basic_input' | '$program'" 2>&1)
+
resp=$(timeout "$test_timeout" bash -c "echo -e '$basic_input' | '$program' OT.txt '$output_file'" 2>&1)
exit_code=$?
# Print the output
···
fi
# Check for verse output format
-
if [[ "$resp" == *"GENESIS 1:1"* ]]; then
+
if [[ "$resp" == *"1 In the beginning God created the heaven and the earth"* ]]; then
print_result "Verse Format" "PASS" "Program outputs verse in correct format"
else
print_result "Verse Format" "FAIL" "Program missing or incorrect verse format"
fi
-
# Check if verses.txt file was created
-
if [ -f "verses.txt" ]; then
-
verses_content=$(cat verses.txt)
+
# Check if test/verses.txt file was created
+
if [ -f "$output_file" ]; then
+
verses_content=$(cat "$output_file")
if [[ "$verses_content" == *"In the beginning God created the heaven and the earth"* ]]; then
-
print_result "File Output" "PASS" "verses.txt created with correct content"
+
print_result "File Output" "PASS" "verses.txt created with correct content in build directory"
else
print_result "File Output" "FAIL" "verses.txt has incorrect content"
fi
else
-
print_result "File Output" "FAIL" "verses.txt file not created"
+
print_result "File Output" "FAIL" "verses.txt file not created in build directory"
fi
# Function to run verse lookup test
···
local should_create_file="$6"
# Clean up previous test
-
rm -f verses.txt
+
rm -f "$output_file"
input="$book\n$chapter\n$verse"
-
output=$(timeout "$test_timeout" bash -c "echo -e '$input' | '$program'" 2>&1)
+
output=$(timeout "$test_timeout" bash -c "echo -e '$input' | '$program' OT.txt '$output_file'" 2>&1)
exit_code=$?
if [ $exit_code -eq 124 ]; then
···
# Check file creation expectations
if [ "$should_create_file" = "true" ]; then
-
if [ -f "verses.txt" ] && [ "$pattern_found" = "true" ]; then
+
if [ -f "$output_file" ] && [ "$pattern_found" = "true" ]; then
print_result "$test_name" "PASS" "Expected output found and file created"
-
elif [ ! -f "verses.txt" ]; then
-
print_result "$test_name" "FAIL" "Expected file verses.txt not created"
+
elif [ ! -f "$output_file" ]; then
+
print_result "$test_name" "FAIL" "Expected file verses.txt not created in build directory"
else
print_result "$test_name" "FAIL" "File created but expected output not found"
fi
else
-
if [ ! -f "verses.txt" ] && [ "$pattern_found" = "true" ]; then
+
if [ ! -f "$output_file" ] && [ "$pattern_found" = "true" ]; then
print_result "$test_name" "PASS" "Expected error output and no file created"
-
elif [ -f "verses.txt" ]; then
+
elif [ -f "$output_file" ]; then
print_result "$test_name" "FAIL" "verses.txt file should not be created for error cases"
else
print_result "$test_name" "FAIL" "Expected error message not found"
···
# Valid Verse Tests
print_section "Valid Verse Tests"
-
run_verse_test "Exodus 20:3" "Exodus" "20" "3" "EXODUS 20:3" true
-
run_verse_test "Psalm 23:1" "Psalm" "23" "1" "PSALM 23:1" true
+
run_verse_test "Exodus 20:3" "Exodus" "20" "3" "3 Thou shalt have no other gods before me" true
+
run_verse_test "Psalm 23:1" "Psalm" "23" "1" "1 The LORD is my shepherd; I shall not want" true
# Error Case Tests
print_section "Error Case Tests"
···
run_verse_test "Very Large Verse" "Genesis" "1" "999999" "Please enter the reference" false
# Test case sensitivity
-
run_verse_test "Lowercase Book" "genesis" "1" "1" "genesis does not exist in the Old Testament" false
-
run_verse_test "Mixed Case Book" "GEnesis" "1" "1" "GEnesis does not exist in the Old Testament" false
+
run_verse_test "Lowercase Book" "genesis" "1" "1" "1 In the beginning God created the heaven and the earth." true
+
run_verse_test "Mixed Case Book" "GEnesis" "1" "1" "1 In the beginning God created the heaven and the earth." true
# Test two-word books
-
run_verse_test "First Samuel" "First Samuel" "1" "1" "FIRST SAMUEL 1:1" true
-
run_verse_test "Second Kings" "Second Kings" "1" "1" "SECOND KINGS 1:1" true
+
run_verse_test "First Samuel" "First Samuel" "1" "1" "1 Now there was a certain man of Ramathaim-zophim, of mount Ephraim, and his name [was] Elkanah, the son of Jeroham, the son of Elihu, the son of Tohu, the son of Zuph, an Ephrathite:" true
+
run_verse_test "Second Kings" "Second Kings" "1" "1" "1 Then Moab rebelled against Israel after the death of Ahab" true
# Function to run file path test
run_file_path_test() {
···
local should_succeed="$4"
# Clean up previous test
-
rm -f verses.txt
+
rm -f "$output_file"
# Provide verse lookup input for file path tests
input="Genesis\n1\n1"
if [ -n "$file_path" ]; then
-
output=$(timeout "$test_timeout" bash -c "echo -e '$input' | '$program' '$file_path'" 2>&1)
+
output=$(timeout "$test_timeout" bash -c "echo -e '$input' | '$program' '$file_path' '$output_file'" 2>&1)
else
-
output=$(timeout "$test_timeout" bash -c "echo -e '$input' | '$program'" 2>&1)
+
output=$(timeout "$test_timeout" bash -c "echo -e '$input' | '$program' OT.txt '$output_file'" 2>&1)
fi
exit_code=$?