Skip to content

Commit c91d865

Browse files
committed
refactor: interpolated_fn for data providers
1 parent 33946c4 commit c91d865

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

src/helpers.sh

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,20 @@ declare -r BASHUNIT_GIT_REPO="https://github.com/TypedDevs/bashunit"
88
# @return string Eg: "Some logic camelCase"
99
#
1010
function helper::normalize_test_function_name() {
11-
local original_function_name="${1-}"
11+
local original_fn_name="${1-}"
12+
local interpolated_fn_name="${2-}"
1213

13-
if [[ -n "${BASHUNIT_CURRENT_FUNCTION_NAME-}" &&
14-
"$original_function_name" == "$BASHUNIT_CURRENT_FUNCTION_NAME" &&
15-
-n "${BASHUNIT_INTERPOLATED_FUNCTION_NAME-}" ]]; then
16-
original_function_name="$BASHUNIT_INTERPOLATED_FUNCTION_NAME"
14+
if [[ -n "${interpolated_fn_name-}" ]]; then
15+
original_fn_name="$interpolated_fn_name"
1716
fi
1817

1918
local result
2019

2120
# Remove the first "test_" prefix, if present
22-
result="${original_function_name#test_}"
21+
result="${original_fn_name#test_}"
2322
# If no "test_" was removed (e.g., "testFoo"), remove the "test" prefix
24-
if [[ "$result" == "$original_function_name" ]]; then
25-
result="${original_function_name#test}"
23+
if [[ "$result" == "$original_fn_name" ]]; then
24+
result="${original_fn_name#test}"
2625
fi
2726
# Replace underscores with spaces
2827
result="${result//_/ }"

src/runner.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ function runner::run_test() {
136136
local function_name="$1"
137137
shift
138138
export BASHUNIT_CURRENT_FUNCTION_NAME="$function_name"
139-
export BASHUNIT_INTERPOLATED_FUNCTION_NAME="$(helper::interpolate_function_name "$function_name" "$@")"
139+
local interpolated_function_name="$(helper::interpolate_function_name "$function_name" "$@")"
140140
local current_assertions_failed="$(state::get_assertions_failed)"
141141
local current_assertions_snapshot="$(state::get_assertions_snapshot)"
142142
local current_assertions_incomplete="$(state::get_assertions_incomplete)"
@@ -265,7 +265,7 @@ function runner::run_test() {
265265
return
266266
fi
267267

268-
local label="$(helper::normalize_test_function_name "$function_name")"
268+
local label="$(helper::normalize_test_function_name "$function_name" "$interpolated_function_name")"
269269

270270
console_results::print_successful_test "${label}" "$duration" "$@"
271271
state::add_tests_passed

tests/unit/helpers_test.sh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,14 @@ function test_to_run_with_filter_matching_string_in_function_name() {
228228
function test_interpolate_function_name() {
229229
local result
230230
result="$(helper::interpolate_function_name "test_name_::1::_foo" "bar")"
231+
231232
assert_same "test_name_'bar'_foo" "$result"
232233
}
233234

234235
function test_normalize_test_function_name_with_interpolation() {
235-
local fn="test_returns_value_::1::_given"
236-
export BASHUNIT_CURRENT_FUNCTION_NAME="$fn"
236+
local fn="test_returns_value_::1::_and_::2::_given"
237237
# shellcheck disable=SC2155
238-
export BASHUNIT_INTERPOLATED_FUNCTION_NAME="$(helper::interpolate_function_name "$fn" "3")"
239-
assert_same "Returns value '3' given" "$(helper::normalize_test_function_name "$fn")"
238+
local interpolated_fn="$(helper::interpolate_function_name "$fn" "3" "4")"
239+
240+
assert_same "Returns value '3' and '4' given" "$(helper::normalize_test_function_name "$fn" "$interpolated_fn")"
240241
}

0 commit comments

Comments
 (0)