diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-28 16:24:54 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-28 16:24:54 -0500 |
| commit | a74e84419015c14631913e854df6acd0c60a6f08 (patch) | |
| tree | 6b740aa633714bdd31a8eb38e614d4fa3f101bd2 | |
| parent | Update version badge (diff) | |
| download | checkmate-a74e84419015c14631913e854df6acd0c60a6f08.zip | |
Move assert functions to assert packagev0.2.0
| -rw-r--r-- | CHANGELOG.md | 7 | ||||
| -rw-r--r-- | assert/assert.go (renamed from assert.go) | 42 | ||||
| -rw-r--r-- | assert/assert_test.go (renamed from assert_test.go) | 50 |
3 files changed, 53 insertions, 46 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 91501e7..c45ee7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] - 2024-01-28 + +### Changed + +- Moved assertion functions to a `assert` module and removed `Assert` suffix + from many of the functions. + ## [0.1.0] - 2024-01-28 - Initial Release diff --git a/assert.go b/assert/assert.go index eb2544a..b120ec5 100644 --- a/assert.go +++ b/assert/assert.go @@ -6,7 +6,7 @@ // This is a simple and small library. If you need something more advanced and full-featured, // [Testify](https://github.com/stretchr/testify), [Gomega](https://github.com/onsi/gomega), or // [gotest.tools](https://github.com/gotestyourself/gotest.tools) are all great options. -package checkmate +package assert import ( "errors" @@ -16,10 +16,10 @@ import ( "github.com/google/go-cmp/cmp" ) -// AssertNil checks whether the value equals nil. An optional +// Nil checks whether the value equals nil. An optional // message and arguments for any format placeholders in that // message can be provided for if a failure occurs. -func AssertNil(t TestingT, value any, msgAndArgs ...any) { +func Nil(t TestingT, value any, msgAndArgs ...any) { if ht, ok := t.(helperT); ok { ht.Helper() } @@ -31,10 +31,10 @@ func AssertNil(t TestingT, value any, msgAndArgs ...any) { Assert(t, value == nil, msgAndArgs...) } -// AssertNil checks whether the value does not equal nil. +// NotNil checks whether the value does not equal nil. // An optional message and arguments for any format placeholders // in that message can be provided for if a failure occurs. -func AssertNotNil(t TestingT, value any, msgAndArgs ...any) { +func NotNil(t TestingT, value any, msgAndArgs ...any) { if ht, ok := t.(helperT); ok { ht.Helper() } @@ -46,10 +46,10 @@ func AssertNotNil(t TestingT, value any, msgAndArgs ...any) { Assert(t, value != nil, msgAndArgs...) } -// AssertTrue checks whether the condition is true. +// True checks whether the condition is true. // An optional message and arguments for any format placeholders // in that message can be provided for if a failure occurs. -func AssertTrue(t TestingT, condition bool, msgAndArgs ...any) { +func True(t TestingT, condition bool, msgAndArgs ...any) { if ht, ok := t.(helperT); ok { ht.Helper() } @@ -61,10 +61,10 @@ func AssertTrue(t TestingT, condition bool, msgAndArgs ...any) { Assert(t, condition, msgAndArgs...) } -// AssertFalse checks whether the condition is false. +// False checks whether the condition is false. // An optional message and arguments for any format placeholders // in that message can be provided for if a failure occurs. -func AssertFalse(t TestingT, condition bool, msgAndArgs ...any) { +func False(t TestingT, condition bool, msgAndArgs ...any) { if ht, ok := t.(helperT); ok { ht.Helper() } @@ -76,11 +76,11 @@ func AssertFalse(t TestingT, condition bool, msgAndArgs ...any) { Assert(t, !condition, msgAndArgs...) } -// AssertErrorIs checks whether any error in the actual error's tree +// ErrorIs checks whether any error in the actual error's tree // matches the expected error. An optional message and arguments for // any format placeholders in that message can be provided for if a // failure occurs. -func AssertErrorIs(t TestingT, actual error, expected error, msgAndArgs ...any) { +func ErrorIs(t TestingT, actual error, expected error, msgAndArgs ...any) { if ht, ok := t.(helperT); ok { ht.Helper() } @@ -92,10 +92,10 @@ func AssertErrorIs(t TestingT, actual error, expected error, msgAndArgs ...any) Assert(t, errors.Is(actual, expected), msgAndArgs...) } -// AssertErrorContains checks whether the given err contains the expectedErrText +// ErrorContains checks whether the given err contains the expectedErrText // in the err.Error() output. An optional message and arguments for any format // placeholders in that message can be provided for if a failure occurs. -func AssertErrorContains(t TestingT, err error, expectedErrText string, msgAndArgs ...any) { +func ErrorContains(t TestingT, err error, expectedErrText string, msgAndArgs ...any) { if ht, ok := t.(helperT); ok { ht.Helper() } @@ -107,11 +107,11 @@ func AssertErrorContains(t TestingT, err error, expectedErrText string, msgAndAr Assert(t, strings.Contains(err.Error(), expectedErrText), msgAndArgs...) } -// AssertDeepEqual checks if two values are deeply equal using Google's +// DeepEqual checks if two values are deeply equal using Google's // go-cmp cmp.Equal. If they are not equal, it logs the differences // using cmp.Diff. An optional message and arguments for any format // placeholders in that message can be provided for if a failure occurs. -func AssertDeepEqual(t TestingT, actual, expected any, msgAndArgs ...any) { +func DeepEqual(t TestingT, actual, expected any, msgAndArgs ...any) { if ht, ok := t.(helperT); ok { ht.Helper() } @@ -124,10 +124,10 @@ func AssertDeepEqual(t TestingT, actual, expected any, msgAndArgs ...any) { } } -// AssertEqual checks if two primitive values are equal. An optional +// Equal checks if two primitive values are equal. An optional // message and arguments for any format placeholders in that message // can be provided for if a failure occurs. -func AssertEqual(t TestingT, actual, expected any, msgAndArgs ...any) { +func Equal(t TestingT, actual, expected any, msgAndArgs ...any) { if ht, ok := t.(helperT); ok { ht.Helper() } @@ -140,10 +140,10 @@ func AssertEqual(t TestingT, actual, expected any, msgAndArgs ...any) { } } -// AssertNotEqual checks if two values are not equal. It fails the test if +// NotEqual checks if two values are not equal. It fails the test if // the values are equal. An optional message and arguments for any format // placeholders in that message can be provided if a failure occurs. -func AssertNotEqual(t TestingT, actual, expected any, msgAndArgs ...any) { +func NotEqual(t TestingT, actual, expected any, msgAndArgs ...any) { if ht, ok := t.(helperT); ok { ht.Helper() } @@ -156,11 +156,11 @@ func AssertNotEqual(t TestingT, actual, expected any, msgAndArgs ...any) { } } -// AssertLenEqual checks if the length of a given slice or array equals the +// LenEqual checks if the length of a given slice or array equals the // expected length. It fails the test if the lengths are not equal. An optional // message and arguments for any format placeholders in that message can be provided // if a failure occurs. -func AssertLenEqual[T any](t TestingT, l []T, expectedLen int, msgAndArgs ...any) { +func LenEqual[T any](t TestingT, l []T, expectedLen int, msgAndArgs ...any) { if ht, ok := t.(helperT); ok { ht.Helper() } diff --git a/assert_test.go b/assert/assert_test.go index 88e9e0b..a21f00c 100644 --- a/assert_test.go +++ b/assert/assert_test.go @@ -1,4 +1,4 @@ -package checkmate +package assert import ( "errors" @@ -59,28 +59,28 @@ func wrapAssert(t TestingT, args []any) { func wrapAssertEqual(t TestingT, args []any) { if len(args) > 2 { - AssertEqual(t, args[0], args[1], args[2:]...) + Equal(t, args[0], args[1], args[2:]...) } else { - AssertEqual(t, args[0], args[1]) + Equal(t, args[0], args[1]) } } func wrapAssertDeepEqual(t TestingT, args []any) { if len(args) > 2 { - AssertDeepEqual(t, args[0], args[1], args[2:]...) + DeepEqual(t, args[0], args[1], args[2:]...) } else { - AssertDeepEqual(t, args[0], args[1]) + DeepEqual(t, args[0], args[1]) } } func wrapAssertNotEqual(t TestingT, args []any) { if len(args) > 2 { - AssertNotEqual(t, args[0], args[1], args[2:]...) + NotEqual(t, args[0], args[1], args[2:]...) } else { - AssertNotEqual(t, args[0], args[1]) + NotEqual(t, args[0], args[1]) } } @@ -96,64 +96,64 @@ func wrapAssertLenEqualInt(t TestingT, args []any) { } if len(args) > 2 { - AssertLenEqual(t, slice, expectedLen, args[2:]...) + LenEqual(t, slice, expectedLen, args[2:]...) } else { - AssertLenEqual(t, slice, expectedLen) + LenEqual(t, slice, expectedLen) } } func wrapAssertErrorIs(t TestingT, args []any) { if len(args) > 2 { - AssertErrorIs(t, args[0].(error), args[1].(error), args[2:]...) + ErrorIs(t, args[0].(error), args[1].(error), args[2:]...) } else { - AssertErrorIs(t, args[0].(error), args[1].(error)) + ErrorIs(t, args[0].(error), args[1].(error)) } } func wrapAssertErrorContains(t TestingT, args []any) { if len(args) > 2 { - AssertErrorContains(t, args[0].(error), args[1].(string), args[2:]...) + ErrorContains(t, args[0].(error), args[1].(string), args[2:]...) } else { - AssertErrorContains(t, args[0].(error), args[1].(string)) + ErrorContains(t, args[0].(error), args[1].(string)) } } func wrapAssertTrue(t TestingT, args []any) { if len(args) > 1 { - AssertTrue(t, args[0].(bool), args[1:]...) + True(t, args[0].(bool), args[1:]...) } else { - AssertTrue(t, args[0].(bool)) + True(t, args[0].(bool)) } } func wrapAssertFalse(t TestingT, args []any) { if len(args) > 1 { - AssertFalse(t, args[0].(bool), args[1:]...) + False(t, args[0].(bool), args[1:]...) } else { - AssertFalse(t, args[0].(bool)) + False(t, args[0].(bool)) } } func wrapAssertNil(t TestingT, args []any) { if len(args) > 1 { - AssertNil(t, args[0], args[1:]...) + Nil(t, args[0], args[1:]...) } else { - AssertNil(t, args[0]) + Nil(t, args[0]) } } func wrapAssertNotNil(t TestingT, args []any) { if len(args) > 1 { - AssertNotNil(t, args[0], args[1:]...) + NotNil(t, args[0], args[1:]...) } else { - AssertNotNil(t, args[0]) + NotNil(t, args[0]) } } @@ -322,7 +322,7 @@ func TestAssertEqual(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { mockT := &MockT{} - AssertEqual(mockT, tc.actual, tc.expected) + Equal(mockT, tc.actual, tc.expected) if mockT.FailNowCalled != tc.shouldFail { t.Errorf( @@ -358,7 +358,7 @@ func TestAssertDeepEqual(t *testing.T) { t.Run("Equal structs", func(t *testing.T) { mockT := &MockT{} - AssertDeepEqual(mockT, TestStruct{"Alice", 30}, TestStruct{"Alice", 30}) + DeepEqual(mockT, TestStruct{"Alice", 30}, TestStruct{"Alice", 30}) if mockT.FailNowCalled { t.Error("AssertDeepEqual failed when it should have passed") } @@ -366,7 +366,7 @@ func TestAssertDeepEqual(t *testing.T) { t.Run("Not equal structs", func(t *testing.T) { mockT := &MockT{} - AssertDeepEqual(mockT, TestStruct{"Alice", 30}, TestStruct{"Bob", 30}) + DeepEqual(mockT, TestStruct{"Alice", 30}, TestStruct{"Bob", 30}) if !mockT.FailNowCalled { t.Error("AssertDeepEqual passed when it should have failed") } @@ -385,7 +385,7 @@ func TestAssertErrorIsChecksErrTree(t *testing.T) { actual := fmt.Errorf("wrapped: %w", os.ErrInvalid) expected := os.ErrInvalid - AssertErrorIs(mockT, actual, expected) + ErrorIs(mockT, actual, expected) if mockT.FailNowCalled { t.Fatal("AssertErrorIs failed when it should have passed") |