Skip to content

Commit 03ca0ba

Browse files
committed
feat(tests): add error handling for unexpected function calls
Add tests for `__refute_stream_line`, `__assert_stream`, and `__assert_line` to ensure they produce appropriate error messages when called directly. Update the implementations to handle unexpected calls by providing clear guidance on correct usage. This improves user experience and debugging by preventing silent failures and clarifying intended function usage.
1 parent d7dd6fb commit 03ca0ba

File tree

6 files changed

+51
-6
lines changed

6 files changed

+51
-6
lines changed

src/assert_line.bash

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,12 @@ __assert_line() {
178178
local -ar stream_lines=("${stderr_lines[@]}")
179179
local -r stream_type=stderr
180180
else
181-
# Coding error: unknown caller
182-
:
181+
# Unknown caller
182+
echo "Unexpected call to \`${FUNCNAME[0]}\`
183+
Did you mean to call \`assert_line\` or \`assert_stderr_line\`?" \
184+
| batslib_decorate "ERROR: ${FUNCNAME[0]}" \
185+
| fail
186+
return $?
183187
fi
184188

185189
# Handle options.

src/assert_output.bash

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,12 @@ __assert_stream() {
170170
elif [[ ${stream_type} == "stderr" ]]; then
171171
: "${stderr?}"
172172
else
173-
# Not reachable: should be either output or stderr
174-
:
173+
# Unknown caller
174+
echo "Unexpected call to \`${FUNCNAME[0]}\`
175+
Did you mean to call \`assert_output\` or \`assert_stderr\`?" |
176+
batslib_decorate "ERROR: ${FUNCNAME[0]}" |
177+
fail
178+
return $?
175179
fi
176180
local -r stream="${!stream_type}"
177181

src/refute_line.bash

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,12 @@ __refute_stream_line() {
175175
local -ar stream_lines=("${stderr_lines[@]}")
176176
local -r stream_type=stderr
177177
else
178-
# Coding error: unknown caller
179-
:
178+
# Unknown caller
179+
echo "Unexpected call to \`${FUNCNAME[0]}\`
180+
Did you mean to call \`refute_line\` or \`refute_stderr_line\`?" |
181+
batslib_decorate "ERROR: ${FUNCNAME[0]}" |
182+
fail
183+
return $?
180184
fi
181185

182186
# Handle options.

test/assert_line.bats

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,3 +349,14 @@ ERR_MSG
349349
run assert_line -- '-p'
350350
assert_test_pass
351351
}
352+
353+
@test "__assert_line(): call to __assert_line shows error" {
354+
run __assert_line
355+
assert_test_fail <<'ERR_MSG'
356+
357+
-- ERROR: __assert_line --
358+
Unexpected call to `__assert_line`
359+
Did you mean to call `assert_line` or `assert_stderr_line`?
360+
--
361+
ERR_MSG
362+
}

test/assert_output.bats

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,14 @@ ERR_MSG
283283
run assert_output -- '-p'
284284
assert_test_pass
285285
}
286+
287+
@test "__assert_stream(): call to __assert_stream shows error" {
288+
run __assert_stream
289+
assert_test_fail <<'ERR_MSG'
290+
291+
-- ERROR: __assert_stream --
292+
Unexpected call to `__assert_stream`
293+
Did you mean to call `assert_output` or `assert_stderr`?
294+
--
295+
ERR_MSG
296+
}

test/refute_line.bats

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,14 @@ ERR_MSG
342342
run refute_line -- '-p'
343343
assert_test_pass
344344
}
345+
346+
@test "__refute_stream_line(): call to __refute_stream_line shows error" {
347+
run __refute_stream_line
348+
assert_test_fail <<'ERR_MSG'
349+
350+
-- ERROR: __refute_stream_line --
351+
Unexpected call to `__refute_stream_line`
352+
Did you mean to call `refute_line` or `refute_stderr_line`?
353+
--
354+
ERR_MSG
355+
}

0 commit comments

Comments
 (0)