Improve how to use custom assertions#530
Merged
Chemaclass merged 5 commits intomainfrom Dec 4, 2025
Merged
Conversation
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.
📚 Description
Closes: #528
Improves how custom assertions display test names in failure messages by walking up the call stack to find the actual test function.
The Problem
Previously, when a custom assertion failed, the failure message would show the custom assertion's name instead of the test function name. This made it difficult to identify which test actually failed.
For example, with a custom assertion like:
Before (confusing):
After (clear):
The Solution
A new helper function
helper::find_test_function_name()walks up the Bash call stack (FUNCNAMEarray) to find the first function that matches the test naming convention (test_*ortestCamelCase). This ensures that regardless of how deeply nested the assertion is, the correct test name is always displayed.This works for:
fail()bashunit::assertion_failed()assert_sameinternally)🔖 Changes
helper::find_test_function_name()function insrc/helpers.shthat walks the call stack to find test functionssrc/assert.sh,src/assert_arrays.sh,src/assert_files.sh,src/assert_folders.sh, andsrc/assert_snapshot.shto use the new helpertests/unit/custom_assertions_test.shcovering various custom assertion patternsdocs/custom-asserts.mdwith:bashunit::assertion_failedandbashunit::assertion_passed🖼️ Demo
🧪 Demo
fail "Invalid JSON: $json"
bashunit::assertion_failed '{"status": "ok"}' "$json"
✅ To-do list
CHANGELOG.mdto reflect the new feature or fix