@@ -4,6 +4,7 @@ set -o pipefail
44RED=$( tput setaf 1)
55GREEN=$( tput setaf 2)
66BOLD=$( tput bold)
7+ UNDERLINE=$( tput smul)
78RESET=$( tput sgr 0)
89
910ACTION=" "
@@ -30,6 +31,10 @@ echo_v() {
3031 fi
3132}
3233
34+ echo_t () {
35+ printf " \t%s\n" " $1 "
36+ }
37+
3338bytes_to_human () {
3439 b=${1:- 0}
3540 d=' '
@@ -172,18 +177,21 @@ api_factory() {
172177test_factory () {
173178 for TEST_CASE in $@ ; do
174179 API_ERROR=0
175- echo " ${BOLD} Testing Case:$ {RESET} $TEST_CASE "
180+ echo " ${BOLD} Testing Case: \" $TEST_CASE \" $ {RESET}"
176181 echo_v " ${BOLD} Description: ${RESET} $( jq -r " .testCases.$TEST_CASE .description" $FILE ) "
177182 echo_v " ${BOLD} Action: ${RESET} $( jq -r " .testCases.$TEST_CASE .method //\" GET\" | ascii_upcase" $FILE ) $( jq -r " .testCases.$TEST_CASE .path" $FILE ) "
178183 call_api $TEST_CASE
179184 if [[ $API_ERROR == 1 ]]; then
180185 return
181186 fi
182- echo " Testing Header: "
183- test_runner $TEST_CASE " header"
184-
185- echo " Testing BODY: "
186- test_runner $TEST_CASE " body"
187+ tput cuf 2
188+ echo " ${BOLD}${UNDERLINE} a. Checking condition for header${RESET} "
189+ test_runner $TEST_CASE " header" " $RESPONSE_HEADER "
190+ echo " "
191+ echo " "
192+ tput cuf 2
193+ echo " ${BOLD}${UNDERLINE} b. Checking condition for body${RESET} "
194+ test_runner $TEST_CASE " body" " $RESPONSE_BODY "
187195
188196 echo " "
189197 echo " "
@@ -192,65 +200,80 @@ test_factory() {
192200
193201test_runner () {
194202 for test in " " contains eq hasKey[]" " ; do
195- local TEST_SCENARIO=$( jq -r " .testCases.$1 .test .$2 .$test ? | select(. !=null)" $FILE )
203+ local TEST_SCENARIO=$( jq -r " .testCases.$1 .expect .$2 .$test ? | select(. !=null)" $FILE )
196204 if [[ -z $TEST_SCENARIO ]]; then
197205 continue
198206 fi
207+ tput cuf 4
199208 if [[ $test == " contains" ]]; then
200- echo " CHECKING CONTAINS: "
201- contains " $TEST_SCENARIO " " $RESPONSE_BODY "
209+ echo " ${BOLD} Checking contains comparision ${RESET} "
210+ contains " $TEST_SCENARIO " " $3 "
202211 elif [[ $test == " eq" ]]; then
203- echo " CHECKING eq: "
204- check_eq " $TEST_SCENARIO " " $RESPONSE_BODY "
212+ echo " ${BOLD} Checking equality comparision ${RESET} "
213+ check_eq " $TEST_SCENARIO " " $3 "
205214 else
206- echo " CHECKING has_key: "
207- has_key " $TEST_SCENARIO " " $RESPONSE_BODY "
215+ echo " ${BOLD} Checking has key comparision ${RESET} "
216+ has_key " $TEST_SCENARIO " " $3 "
208217 fi
209- echo " "
210218 done
211219}
212220
213221contains () {
214- jq --argjson a " $1 " --argjson b " $2 " -n ' $a | select(. != null) | $b | contains($a)'
215- # local common=$(jq --argjson a "$1" --argjson b "$2" -n '$a | select( length != 0) | keys as $check_key | $b | keys - $check_key | map([.]) | . as $arr | $b | delpaths($arr) | if . != {} then . else null end')
216-
217- # if [ -z "$common" ]; then
218- # echo "\$common is NULL"
219- # return 1
220- # fi
221- # check_eq "$1" "$common"
222+ tput cuf 6
223+ local check=$( jq --argjson a " $1 " --argjson b " $2 " -n ' $a | select(. != null) | $b | contains($a)' )
224+ if [[ $check == " true" ]]; then
225+ echo " ${BOLD}${GREEN} Check Passed${RESET} "
226+ else
227+ echo " ${BOLD}${RED} Check Failed${RESET} "
228+ echo " EXPECTED:"
229+ echo " ${RED} $1 ${RESET} "
230+ echo " GOT:"
231+ echo " ${GREEN} $2 ${RESET} "
232+ fi
222233}
223234
224235has_key () {
225- # jq -argjson a "$1" --argjson b "$2" -n '$b | keys as $check_key | $header | keys | contains($check_key)'
226- local paths=$( jq -r ' def path2text($value):
227- def tos: if type == "number" then . else "\"\(tojson)\"" end;
228- reduce .[] as $segment (""; .
229- + ($segment
230- | if type == "string" then "." + . else "[\(.)]" end));
231-
232-
233- paths(scalars) as $p
234- | getpath($p) as $v
235- | $p | path2text($v)' <<< " $2" )
236+ # local paths=$(jq -r 'def path2text($value):
237+ # def tos: if type == "number" then . else "\"\(tojson)\"" end;
238+ # reduce .[] as $segment (""; .
239+ # + ($segment
240+ # | if type == "string" then "." + . else "[\(.)]" end));
241+ # paths(scalars) as $p
242+ # | getpath($p) as $v
243+ # | $p | path2text($v)' <<<"$2")
244+ local paths=$( jq -r ' path(..)|[.[]|tostring]|join(".")' <<< " $2" )
245+ tput cuf 6
236246 for path in $1 ; do
247+ local FOUND=0
237248 for data_path in $paths ; do
238- local FOUND=0
239249 if [[ " $path " == " $data_path " ]]; then
240- echo " FOUND: $path "
241250 FOUND=1
242251 break
243252 fi
244253 done
245-
246254 if [[ $FOUND == 0 ]]; then
247- echo " NOT FOUND: $path "
255+ echo " ${BOLD}${RED} Check Failed${RESET} "
256+ echo " CANNOT FIND KEY:"
257+ echo " ${RED} $path ${RESET} "
258+ return
248259 fi
249260 done
261+ echo " ${BOLD}${GREEN} Check Passed${RESET} "
250262}
251263
252264check_eq () {
253- jq --argjson a " $1 " --argjson b " $2 " -n ' def post_recurse(f): def r: (f | select(. != null) | r), .; r; def post_recurse: post_recurse(.[]?); ($a | (post_recurse | arrays) |= sort) as $a | ($b | (post_recurse | arrays) |= sort) as $b | $a == $b'
265+ tput cuf 6
266+ local check=$( jq --argjson a " $1 " --argjson b " $2 " -n ' def post_recurse(f): def r: (f | select(. != null) | r), .; r; def post_recurse: post_recurse(.[]?); ($a | (post_recurse | arrays) |= sort) as $a | ($b | (post_recurse | arrays) |= sort) as $b | $a == $b' )
267+ if [[ $check == " true" ]]; then
268+ echo " ${BOLD}${GREEN} Check Passed${RESET} "
269+ else
270+ tput cuf 2
271+ echo " ${BOLD}${RED} Check Failed${RESET} "
272+ echo " EXPECTED:"
273+ echo " ${RED} $1 ${RESET} "
274+ echo " GOT:"
275+ echo " ${GREEN} $2 ${RESET} "
276+ fi
254277}
255278
256279run () {
0 commit comments