feat(assert): add optional custom label to assert functions (#77)#616
Merged
Chemaclass merged 2 commits intoTypedDevs:mainfrom Mar 30, 2026
Merged
Conversation
…TypedDevs#77) Most assert functions now accept an optional trailing label parameter that overrides the auto-derived test function name in failure output. Examples: assert_same "expected" "$actual" "checking user name" assert_empty "$result" "result should be empty" assert_not_same "foo" "$val" "values must differ" Affected functions in src/assert.sh: assert_same, assert_equals, assert_not_equals, assert_not_same, assert_empty, assert_not_empty, assert_less_than, assert_less_or_equal_than, assert_greater_than, assert_greater_or_equal_than, assert_contains_ignore_case, assert_string_not_starts_with, assert_string_matches_format, assert_string_not_matches_format Variadic functions (assert_contains, assert_not_contains, assert_matches, assert_not_matches, assert_string_starts_with, assert_string_ends_with, assert_string_not_ends_with, assert_line_count) and exit-code functions ($3 already taken) do not support the label override. Note: assert_files.sh and assert_folders.sh already supported custom labels via their existing optional parameters.
8a12abb to
956d493
Compare
Chemaclass
approved these changes
Mar 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue
Closes #77
Background
When an assertion fails, bashunit derives the label from the test function name. In complex tests with multiple assertions on the same subject, the auto-derived label doesn't give enough context about which assertion failed or why.
PHPUnit solves this with an optional message parameter. This PR brings the same to bashunit.
Goal
Allow test authors to pass an optional trailing label to most assert functions. When provided, it replaces the auto-derived test function name in failure output.
Changes
src/assert.shbashunit::assert::label()helper: uses custom label if provided, falls back tofind_test_function_nameassert_same,assert_equals,assert_not_equals,assert_not_same,assert_empty,assert_not_empty,assert_less_than,assert_less_or_equal_than,assert_greater_than,assert_greater_or_equal_than,assert_contains_ignore_case,assert_string_not_starts_with,assert_string_matches_format,assert_string_not_matches_formattests/unit/assert_basic_test.shNot supported (variadic
${@:2}or$3already taken):assert_contains,assert_not_contains,assert_matches,assert_not_matches,assert_string_starts_with,assert_string_ends_with,assert_string_not_ends_with,assert_line_count, exit-code assertionsNote:
assert_files.shandassert_folders.shalready support custom labels via their existing optional params — unchanged.Example
Failure output: