Skip to content

Commit 5e15eda

Browse files
committed
feat(doubles): add assert_have_been_called_nth_with assertion
Adds dedicated assertion to verify arguments passed on the Nth invocation of a spy, with explicit API and index bounds checking.
1 parent 857c64b commit 5e15eda

3 files changed

Lines changed: 84 additions & 0 deletions

File tree

.claude/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"$schema": "https://json.schemastore.org/claude-code-settings.json",
3+
"includeCoAuthoredBy": false
4+
}

src/test_doubles.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,49 @@ function assert_have_been_called_times() {
156156
bashunit::state::add_assertions_passed
157157
}
158158

159+
function assert_have_been_called_nth_with() {
160+
local nth=$1
161+
local command=$2
162+
shift 2
163+
local expected="$*"
164+
165+
local variable
166+
variable="$(bashunit::helper::normalize_variable_name "$command")"
167+
local times_file_var="${variable}_times_file"
168+
local file_var="${variable}_params_file"
169+
local label
170+
label="$(bashunit::helper::normalize_test_function_name "${FUNCNAME[1]}")"
171+
172+
local times=0
173+
if [ -f "${!times_file_var-}" ]; then
174+
times=$(cat "${!times_file_var}" 2>/dev/null || echo 0)
175+
fi
176+
177+
if [ "$nth" -gt "$times" ]; then
178+
bashunit::state::add_assertions_failed
179+
bashunit::console_results::print_failed_test "${label}" \
180+
"expected call" "at index ${nth} but" "only called ${times} times"
181+
return
182+
fi
183+
184+
local line=""
185+
if [ -f "${!file_var-}" ]; then
186+
line=$(sed -n "${nth}p" "${!file_var}" 2>/dev/null || true)
187+
fi
188+
189+
local raw
190+
IFS=$'\x1e' read -r raw _ <<<"$line" || true
191+
192+
if [ "$expected" != "$raw" ]; then
193+
bashunit::state::add_assertions_failed
194+
bashunit::console_results::print_failed_test "${label}" \
195+
"$expected" "but got " "$raw"
196+
return
197+
fi
198+
199+
bashunit::state::add_assertions_passed
200+
}
201+
159202
function assert_not_called() {
160203
local command=$1
161204
local label="${2:-$(bashunit::helper::normalize_test_function_name "${FUNCNAME[1]}")}"

tests/unit/test_doubles_test.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,40 @@ function test_spy_with_pipe_in_arguments() {
177177

178178
assert_have_been_called_with grep '-E foo|bar'
179179
}
180+
181+
function test_successful_spy_nth_called_with() {
182+
bashunit::spy ps
183+
184+
ps first_a first_b
185+
ps second
186+
ps third
187+
188+
assert_have_been_called_nth_with 1 ps "first_a first_b"
189+
assert_have_been_called_nth_with 2 ps "second"
190+
assert_have_been_called_nth_with 3 ps "third"
191+
}
192+
193+
function test_unsuccessful_spy_nth_called_with() {
194+
bashunit::spy ps
195+
196+
ps first
197+
ps second
198+
199+
assert_same \
200+
"$(bashunit::console_results::print_failed_test \
201+
"Unsuccessful spy nth called with" \
202+
"wrong" "but got " "first")" \
203+
"$(assert_have_been_called_nth_with 1 ps "wrong")"
204+
}
205+
206+
function test_unsuccessful_spy_nth_called_with_invalid_index() {
207+
bashunit::spy ps
208+
209+
ps first
210+
211+
assert_same \
212+
"$(bashunit::console_results::print_failed_test \
213+
"Unsuccessful spy nth called with invalid index" \
214+
"expected call" "at index 5 but" "only called 1 times")" \
215+
"$(assert_have_been_called_nth_with 5 ps "first")"
216+
}

0 commit comments

Comments
 (0)