-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathconsole_results.sh
More file actions
215 lines (179 loc) · 7.73 KB
/
console_results.sh
File metadata and controls
215 lines (179 loc) · 7.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/bin/bash
_SUCCESSFUL_TEST_COUNT=0
function console_results::render_result() {
if [[ "$(state::is_duplicated_test_functions_found)" == true ]]; then
console_results::print_execution_time
printf "%s%s%s\n" "${_COLOR_RETURN_ERROR}" "Duplicate test functions found" "${_COLOR_DEFAULT}"
printf "File with duplicate functions: %s\n" "$(state::get_file_with_duplicated_function_names)"
printf "Duplicate functions: %s\n" "$(state::get_duplicated_function_names)"
return 1
fi
echo ""
local total_tests=0
((total_tests += $(state::get_tests_passed))) || true
((total_tests += $(state::get_tests_skipped))) || true
((total_tests += $(state::get_tests_incomplete))) || true
((total_tests += $(state::get_tests_snapshot))) || true
((total_tests += $(state::get_tests_failed))) || true
local total_assertions=0
((total_assertions += $(state::get_assertions_passed))) || true
((total_assertions += $(state::get_assertions_skipped))) || true
((total_assertions += $(state::get_assertions_incomplete))) || true
((total_assertions += $(state::get_assertions_snapshot))) || true
((total_assertions += $(state::get_assertions_failed))) || true
printf "%sTests: %s" "$_COLOR_FAINT" "$_COLOR_DEFAULT"
if [[ "$(state::get_tests_passed)" -gt 0 ]] || [[ "$(state::get_assertions_passed)" -gt 0 ]]; then
printf " %s%s passed%s," "$_COLOR_PASSED" "$(state::get_tests_passed)" "$_COLOR_DEFAULT"
fi
if [[ "$(state::get_tests_skipped)" -gt 0 ]] || [[ "$(state::get_assertions_skipped)" -gt 0 ]]; then
printf " %s%s skipped%s," "$_COLOR_SKIPPED" "$(state::get_tests_skipped)" "$_COLOR_DEFAULT"
fi
if [[ "$(state::get_tests_incomplete)" -gt 0 ]] || [[ "$(state::get_assertions_incomplete)" -gt 0 ]]; then
printf " %s%s incomplete%s," "$_COLOR_INCOMPLETE" "$(state::get_tests_incomplete)" "$_COLOR_DEFAULT"
fi
if [[ "$(state::get_tests_snapshot)" -gt 0 ]] || [[ "$(state::get_assertions_snapshot)" -gt 0 ]]; then
printf " %s%s snapshot%s," "$_COLOR_SNAPSHOT" "$(state::get_tests_snapshot)" "$_COLOR_DEFAULT"
fi
if [[ "$(state::get_tests_failed)" -gt 0 ]] || [[ "$(state::get_assertions_failed)" -gt 0 ]]; then
printf " %s%s failed%s," "$_COLOR_FAILED" "$(state::get_tests_failed)" "$_COLOR_DEFAULT"
fi
printf " %s total\n" "$total_tests"
printf "%sAssertions:%s" "$_COLOR_FAINT" "$_COLOR_DEFAULT"
if [[ "$(state::get_tests_passed)" -gt 0 ]] || [[ "$(state::get_assertions_passed)" -gt 0 ]]; then
printf " %s%s passed%s," "$_COLOR_PASSED" "$(state::get_assertions_passed)" "$_COLOR_DEFAULT"
fi
if [[ "$(state::get_tests_skipped)" -gt 0 ]] || [[ "$(state::get_assertions_skipped)" -gt 0 ]]; then
printf " %s%s skipped%s," "$_COLOR_SKIPPED" "$(state::get_assertions_skipped)" "$_COLOR_DEFAULT"
fi
if [[ "$(state::get_tests_incomplete)" -gt 0 ]] || [[ "$(state::get_assertions_incomplete)" -gt 0 ]]; then
printf " %s%s incomplete%s," "$_COLOR_INCOMPLETE" "$(state::get_assertions_incomplete)" "$_COLOR_DEFAULT"
fi
if [[ "$(state::get_tests_snapshot)" -gt 0 ]] || [[ "$(state::get_assertions_snapshot)" -gt 0 ]]; then
printf " %s%s snapshot%s," "$_COLOR_SNAPSHOT" "$(state::get_assertions_snapshot)" "$_COLOR_DEFAULT"
fi
if [[ "$(state::get_tests_failed)" -gt 0 ]] || [[ "$(state::get_assertions_failed)" -gt 0 ]]; then
printf " %s%s failed%s," "$_COLOR_FAILED" "$(state::get_assertions_failed)" "$_COLOR_DEFAULT"
fi
printf " %s total\n" "$total_assertions"
if [[ "$(state::get_tests_failed)" -gt 0 ]]; then
printf "\n%s%s%s\n" "$_COLOR_RETURN_ERROR" " Some tests failed " "$_COLOR_DEFAULT"
console_results::print_execution_time
return 1
fi
if [[ "$(state::get_tests_incomplete)" -gt 0 ]]; then
printf "\n%s%s%s\n" "$_COLOR_RETURN_INCOMPLETE" " Some tests incomplete " "$_COLOR_DEFAULT"
console_results::print_execution_time
return 0
fi
if [[ "$(state::get_tests_skipped)" -gt 0 ]]; then
printf "\n%s%s%s\n" "$_COLOR_RETURN_SKIPPED" " Some tests skipped " "$_COLOR_DEFAULT"
console_results::print_execution_time
return 0
fi
if [[ "$(state::get_tests_snapshot)" -gt 0 ]]; then
printf "\n%s%s%s\n" "$_COLOR_RETURN_SNAPSHOT" " Some snapshots created " "$_COLOR_DEFAULT"
console_results::print_execution_time
return 0
fi
if [[ $total_tests -eq 0 ]]; then
printf "\n%s%s%s\n" "$_COLOR_RETURN_ERROR" " No tests found " "$_COLOR_DEFAULT"
console_results::print_execution_time
return 1
fi
printf "\n%s%s%s\n" "$_COLOR_RETURN_SUCCESS" " All tests passed " "$_COLOR_DEFAULT"
console_results::print_execution_time
return 0
}
function console_results::print_execution_time() {
if [[ $SHOW_EXECUTION_TIME == false ]]; then
return
fi
_EXECUTION_TIME=$(clock::runtime_in_milliseconds)
printf "${_COLOR_BOLD}%s${_COLOR_DEFAULT}\n" "Time taken: ${_EXECUTION_TIME} ms"
}
function console_results::print_successful_test() {
((_SUCCESSFUL_TEST_COUNT++)) || true
if [[ "$SIMPLE_OUTPUT" == true ]]; then
if (( _SUCCESSFUL_TEST_COUNT % 50 != 0 )); then
printf "."
else
echo "."
fi
else
local test_name=$1
shift
if [[ -z "$*" ]]; then
printf "%s✓ Passed%s: %s\n" "$_COLOR_PASSED" "$_COLOR_DEFAULT" "${test_name}"
else
printf "%s✓ Passed%s: %s (%s)\n" "$_COLOR_PASSED" "$_COLOR_DEFAULT" "${test_name}" "$*"
fi
fi
}
function console_results::print_failure_message() {
local test_name=$1
local failure_message=$2
printf "\
${_COLOR_FAILED}✗ Failed${_COLOR_DEFAULT}: %s
${_COLOR_FAINT}Message:${_COLOR_DEFAULT} ${_COLOR_BOLD}'%s'${_COLOR_DEFAULT}\n"\
"${test_name}" "${failure_message}"
}
function console_results::print_failed_test() {
local test_name=$1
local expected=$2
local failure_condition_message=$3
local actual=$4
local extra_key=$5
local extra_value=$6
printf "\
${_COLOR_FAILED}✗ Failed${_COLOR_DEFAULT}: %s
${_COLOR_FAINT}Expected${_COLOR_DEFAULT} ${_COLOR_BOLD}'%s'${_COLOR_DEFAULT}
${_COLOR_FAINT}%s${_COLOR_DEFAULT} ${_COLOR_BOLD}'%s'${_COLOR_DEFAULT}\n"\
"${test_name}" "${expected}" "${failure_condition_message}" "${actual}"
if [ -n "$extra_key" ]; then
printf "\
${_COLOR_FAINT}%s${_COLOR_DEFAULT} ${_COLOR_BOLD}'%s'${_COLOR_DEFAULT}\n"\
"${extra_key}" "${extra_value}"
fi
}
function console_results::print_failed_snapshot_test() {
local test_name=$1
local snapshot_file=$2
printf "${_COLOR_FAILED}✗ Failed${_COLOR_DEFAULT}: %s
${_COLOR_FAINT}Expected to match the snapshot${_COLOR_DEFAULT}\n" "$test_name"
if command -v git > /dev//null; then
local actual_file
actual_file="${snapshot_file}.tmp"
echo "$actual" > "$actual_file"
git diff --no-index --word-diff --color=always "$snapshot_file" "$actual_file" 2>/dev/null\
| tail -n +6 | sed "s/^/ /"
rm "$actual_file"
fi
}
function console_results::print_skipped_test() {
local test_name=$1
local reason=${2-}
printf "${_COLOR_SKIPPED}↷ Skipped${_COLOR_DEFAULT}: %s\n" "${test_name}"
if [[ -n "$reason" ]]; then
printf "${_COLOR_FAINT} %s${_COLOR_DEFAULT}\n" "${reason}"
fi
}
function console_results::print_incomplete_test() {
local test_name=$1
local pending=${2-}
printf "${_COLOR_INCOMPLETE}✒ Incomplete${_COLOR_DEFAULT}: %s\n" "${test_name}"
if [[ -n "$pending" ]]; then
printf "${_COLOR_FAINT} %s${_COLOR_DEFAULT}\n" "${pending}"
fi
}
function console_results::print_snapshot_test() {
local test_name
test_name=$(helper::normalize_test_function_name "$1")
printf "${_COLOR_SNAPSHOT}✎ Snapshot${_COLOR_DEFAULT}: %s\n" "${test_name}"
}
function console_results::print_error_test() {
local test_name
test_name=$(helper::normalize_test_function_name "$1")
local error="$2"
printf "${_COLOR_FAILED}✗ Failed${_COLOR_DEFAULT}: %s
${_COLOR_FAINT}%s${_COLOR_DEFAULT}\n" "${test_name}" "${error}"
}