Skip to content

Commit 0e82405

Browse files
committed
refactor(testing): make error assertions independent with domain-specific messaging
Convert AssertError and AssertNoError from derived functions that delegate to nil assertions into independent base functions with error-specific messaging. Changes: - AssertError now directly checks err != nil and produces "expected error, got nil". - AssertNoError now directly checks err == nil and produces "unexpected error: %v". - Both functions log domain-appropriate success messages. - Update TESTING_core.md to reflect the new architecture hierarchy. - Maintain 100% test coverage and all existing tests pass. This improves clarity when debugging test failures by providing error-oriented messages rather than generic nil-checking messages. Signed-off-by: Alejandro Mery <amery@apptly.co>
1 parent 2e6c49d commit 0e82405

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

TESTING_core.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ Independent Base Functions:
2222
├── AssertContains (uses strings.Contains)
2323
├── AssertNil (uses IsNil utility)
2424
├── AssertNotNil (uses IsNil utility)
25+
├── AssertError (standalone implementation)
26+
├── AssertNoError (standalone implementation)
2527
├── AssertErrorIs (uses errors.Is)
2628
├── AssertTypeIs[T] (uses type assertion)
2729
├── AssertPanic (uses recover mechanism)
2830
└── AssertNoPanic (uses recover mechanism)
2931
3032
Derived Functions (depend on base functions):
3133
├── AssertTrue → calls AssertEqual(t, true, value, ...)
32-
├── AssertFalse → calls AssertEqual(t, false, value, ...)
33-
├── AssertError → calls AssertNotNil(t, err, ...)
34-
└── AssertNoError → calls AssertNil(t, err, ...)
34+
└── AssertFalse → calls AssertEqual(t, false, value, ...)
3535
```
3636

3737
### Testing Implications
@@ -85,11 +85,9 @@ The hierarchy exists for consistency and code reuse:
8585
1. **AssertTrue/AssertFalse → AssertEqual**: Ensures consistent formatting
8686
and logging behaviour for boolean assertions.
8787

88-
2. **AssertError/AssertNoError → AssertNil/AssertNotNil**: Treats errors as
89-
special cases of nil checking with appropriate naming.
90-
91-
3. **Independent Base Functions**: Provide fundamental comparison logic
92-
without dependencies on other assertion functions.
88+
2. **Independent Base Functions**: Provide fundamental comparison logic
89+
without dependencies on other assertion functions. Each base function
90+
is responsible for its own domain-specific validation and messaging.
9391

9492
This design allows for:
9593

testing.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,13 @@ func AssertContains(t T, s, substr, name string, args ...any) bool {
355355
// AssertError(t, err, "operation %s", "save")
356356
func AssertError(t T, err error, name string, args ...any) bool {
357357
t.Helper()
358-
return AssertNotNil(t, err, name, args...)
358+
ok := err != nil
359+
if !ok {
360+
doError(t, name, args, "expected error, got nil")
361+
} else {
362+
doLog(t, name, args, "error: %v", err)
363+
}
364+
return ok
359365
}
360366

361367
// AssertNoError fails the test if error is not nil.
@@ -368,7 +374,13 @@ func AssertError(t T, err error, name string, args ...any) bool {
368374
// AssertNoError(t, err, "loading %s", filename)
369375
func AssertNoError(t T, err error, name string, args ...any) bool {
370376
t.Helper()
371-
return AssertNil(t, err, name, args...)
377+
ok := err == nil
378+
if !ok {
379+
doError(t, name, args, "unexpected error: %v", err)
380+
} else {
381+
doLog(t, name, args, "no error")
382+
}
383+
return ok
372384
}
373385

374386
// AssertPanic runs a function expecting it to panic and optionally validates the panic value.

0 commit comments

Comments
 (0)