3# @brief Script to test the operation of the sedForLosers program.
5# This script takes an input file, a search string, and a replace string
6# as arguments and compares the output of the sedForLosers program
7# with the output of the standard `sed` command.
9# Define colors for OK and KO messages
13NC="\033[0m" # No color
14# \e[1;31m: 赤色で強調表示 (1: 太字, 31: 赤)
17## @brief Checks if the number of arguments is correct.
18# @param $# The number of arguments passed to the script.
19function check_args() {
21 echo "Usage: $0 <input_file> <search_string> <replace_string>"
26# Assign arguments to variables
30expected_file="${input_file}.expected"
31actual_file="${input_file}.replace"
33## @brief Checks if the input file exists.
34function check_input_file() {
35 if [ ! -f "$input_file" ]; then
36 echo "Input file '$input_file' not found."
39 elif [[ "$search_string" == "" ]]; then
40 echo "No previous regular expression. ( search string empty. )"
44 echo "Input file '$input_file' is exist."
49## @brief Removes the expected and actual output files.
50function clear_files() {
51 rm $expected_file $actual_file
54## @brief Executes the standard `sed` command to generate the expected output.
55function exec_sed_string() {
56 echo "$ sed" "s/$search_string/$replace_string/g" "$input_file" ">" "$expected_file"
57 sed "s/$search_string/$replace_string/g" "$input_file" > "$expected_file"
61## @brief Displays the content of the input file, highlighting the search string.
62function display_find_string() {
63 printf "\n\`\`\`{$input_file}\n"
64 while IFS= read -r line; do
65 if [[ "$line" == *"$search_string"* ]]; then
66 # Output in a different color when a string is found
67 echo -e "${line//$search_string/$YELLOW$search_string$NC}"
68 # -e オプション: エスケープシーケンスを解釈
70 # Output If not found, output as is
77## @brief Executes the `sedForLosers` program.
78function exec_sedForLosers() {
79 echo "$ ./sedForLosers" "\"$input_file\"" "\"$search_string\"" "\"$replace_string\""
80 ./sedForLosers "$input_file" "$search_string" "$replace_string"
86## @brief Compares the expected and actual output files using `diff`.
87# @return 0 if the files are identical, 1 otherwise.
88function diff_expected_actual() {
92 echo "$ diff $expected_file $actual_file"
94 diff "$expected_file" "$actual_file"
98 echo -e "TEST ${GREEN}PASSED${NC}: Output matches expected result."
101 echo -e "TEST ${RED}FAILED${NC}: Output differs from expected result."
108## @brief Assigns command-line arguments to variables.
109# @param $1 The input file.
110# @param $2 The string to search for.
111# @param $3 The string to replace with.
116 expected_file="${input_file}.expected"
117 actual_file="${input_file}.replace"
120## @brief Runs a single test case.
121# @param $1 The input file for the test.
122# @param $2 The search string for the test.
123# @param $3 The replace string for the test.
124function test_script() {
125 printf -- "--------------------------------------------\n"
126 printf ">>> test [$1] [$2] [$3] <<<\n"
127 set_args "$1" "$2" "$3"
130 if [ $? -eq 0 ]; then
137 if [ $? -eq 0 ]; then
142 if [[ "$2" == "" ]] || [[ "$1" ]]; then
143 echo -e "TEST ${GREEN}PASSED${NC}: Error handle OK."
145 echo -e "TEST ${RED}FAILED${NC}: Error handle NG."
151## @brief Main function to execute the test scripts and tee the output to a log file.
153 test_script "input.txt" "sed" "REPLACED"
154 test_script "input.txt" "Sed" "REPLACED"
155 test_script "input.txt" "is" "WAS"
156 test_script "input.txt" "the" ""
157 test_script "input.txt" "" "the"
158 test_script "non_exist.txt" "the" "foo"