CPP01 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
test.sh
[詳解]
1#!/bin/bash
2## @file test.sh
3# @brief Script to test the operation of the sedForLosers program.
4#
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.
8
9# Define colors for OK and KO messages
10GREEN="\033[0;32m"
11RED="\033[0;31m"
12YELLOW="\033[0;33m"
13NC="\033[0m" # No color
14# \e[1;31m: 赤色で強調表示 (1: 太字, 31: 赤)
15# \e[m: 色をリセット
16
17## @brief Checks if the number of arguments is correct.
18# @param $# The number of arguments passed to the script.
19function check_args() {
20 if [ $# -ne 3 ]; then
21 echo "Usage: $0 <input_file> <search_string> <replace_string>"
22 exit 1
23 fi
24}
25
26# Assign arguments to variables
27input_file="$1"
28search_string="$2"
29replace_string="$3"
30expected_file="${input_file}.expected"
31actual_file="${input_file}.replace"
32
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."
37 echo ""
38 return 1
39 elif [[ "$search_string" == "" ]]; then
40 echo "No previous regular expression. ( search string empty. )"
41 echo ""
42 return 1
43 fi
44 echo "Input file '$input_file' is exist."
45 echo ""
46 return 0
47}
48
49## @brief Removes the expected and actual output files.
50function clear_files() {
51 rm $expected_file $actual_file
52}
53
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"
58 echo ""
59}
60
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 オプション: エスケープシーケンスを解釈
69 else
70 # Output If not found, output as is
71 echo "$line"
72 fi
73 done < "$input_file"
74 printf "\`\`\`\n\n"
75}
76
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"
81 status=$?
82 echo ""
83 return $status
84}
85
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() {
89 local result
90
91 # Comparison
92 echo "$ diff $expected_file $actual_file"
93 echo ""
94 diff "$expected_file" "$actual_file"
95
96 # 結果判定
97 if [ $? -eq 0 ]; then
98 echo -e "TEST ${GREEN}PASSED${NC}: Output matches expected result."
99 result=$ture
100 else
101 echo -e "TEST ${RED}FAILED${NC}: Output differs from expected result."
102 result=$false
103 fi
104 echo ""
105 return $result
106}
107
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.
112function set_args() {
113 input_file="$1"
114 search_string="$2"
115 replace_string="$3"
116 expected_file="${input_file}.expected"
117 actual_file="${input_file}.replace"
118}
119
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"
128 check_input_file
129
130 if [ $? -eq 0 ]; then
131 display_find_string
132 exec_sed_string
133 fi
134
135 exec_sedForLosers
136
137 if [ $? -eq 0 ]; then
138 diff_expected_actual
139 clear_files
140 else
141 echo "returned 1"
142 if [[ "$2" == "" ]] || [[ "$1" ]]; then
143 echo -e "TEST ${GREEN}PASSED${NC}: Error handle OK."
144 else
145 echo -e "TEST ${RED}FAILED${NC}: Error handle NG."
146 fi
147 echo ""
148 fi
149}
150
151## @brief Main function to execute the test scripts and tee the output to a log file.
152function main() {
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"
159}
160
161main | tee trace.log