-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Motivation Testing validation defined with `govy` can be a daunting task. Govy structured errors are information rich, while this is great for end users, it can be a tedious task to verify if one `govy.ValidatorError` is equal to another `govy.ValidatorError`. Often times we might not care about fields like description or even message, we might just want to verify error codes for given properties. Govy's goal is to not only make the end-user's life better but the programmer's just as well, it would benefit the second party to have a ready-to-use utility which could make the testing process of govy-defined validation a breeze. ## Summary - Added `govytest` package. - Added tests to some of the internal helpers. - Added new builtin rule `OneOfProperties` which ensures that at least one of the properties provided by getters is set. ## Release Notes Added `govytest` package which exposes utilities which help test govy validation rules. It comes with two functions `AssertNoError`, which ensures no error was produced, and `AssertError` which checks that the expected errors are equal to the actual `govy.ValidatorError`. Added `OneOfProperties` rule which checks if at least one of the properties is set.
- Loading branch information
1 parent
be841aa
commit 74b9792
Showing
18 changed files
with
946 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ words: | |
- govulncheck | ||
- govy | ||
- govyconfig | ||
- govytest | ||
- ldflags | ||
- nobl | ||
- pkgs | ||
|
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package internal | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/nobl9/govy/internal/assert" | ||
) | ||
|
||
func TestJoinErrors(t *testing.T) { | ||
tests := []struct { | ||
in []error | ||
out string | ||
}{ | ||
{nil, ""}, | ||
{[]error{nil, nil}, ""}, | ||
// Incorrect formatting, this test case ensures the function does not panic. | ||
{[]error{nil, errors.New("some error"), nil}, " - some error\n"}, | ||
{[]error{errors.New("- some error")}, " - some error"}, | ||
{[]error{errors.New("- some error"), errors.New("some other error")}, " - some error\n - some other error"}, | ||
} | ||
for _, tc := range tests { | ||
b := strings.Builder{} | ||
JoinErrors(&b, tc.in, " ") | ||
assert.Equal(t, tc.out, b.String()) | ||
} | ||
t.Run("custom indent", func(t *testing.T) { | ||
b := strings.Builder{} | ||
JoinErrors(&b, []error{errors.New("some error")}, " ") | ||
assert.Equal(t, " - some error", b.String()) | ||
}) | ||
} | ||
|
||
func TestPropertyValueString(t *testing.T) { | ||
tests := []struct { | ||
in any | ||
out string | ||
}{ | ||
{nil, ""}, | ||
{any(nil), ""}, | ||
{false, "false"}, | ||
{true, "true"}, | ||
{any("this"), "this"}, | ||
{func() {}, "func"}, | ||
{ptr("this"), "this"}, | ||
{struct{ This string }{This: "this"}, `{"This":"this"}`}, | ||
{ptr(struct{ This string }{This: "this"}), `{"This":"this"}`}, | ||
{struct { | ||
This string `json:"this"` | ||
}{This: "this"}, `{"this":"this"}`}, | ||
{map[string]string{"this": "this"}, `{"this":"this"}`}, | ||
{[]string{"this", "that"}, `["this","that"]`}, | ||
{0, "0"}, | ||
{0.0, "0"}, | ||
{2, "2"}, | ||
{0.123, "0.123"}, | ||
{time.Second, "1s"}, | ||
{time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), "2024-01-01T00:00:00Z"}, | ||
{mockEmptyStringer{}, "mock"}, | ||
{mockStringerWithTags{}, ""}, | ||
{mockStringerWithTags{Mock: "mock"}, `{"mock":"mock"}`}, | ||
{ptr(mockEmptyStringer{}), "mock"}, | ||
} | ||
for _, tc := range tests { | ||
got := PropertyValueString(tc.in) | ||
assert.Equal(t, tc.out, got) | ||
} | ||
} | ||
|
||
type mockEmptyStringer struct{} | ||
|
||
func (m mockEmptyStringer) String() string { | ||
return "mock" | ||
} | ||
|
||
type mockStringerWithTags struct { | ||
Mock string `json:"mock"` | ||
} | ||
|
||
func (m mockStringerWithTags) String() string { | ||
return "stringer" | ||
} | ||
|
||
func ptr[T any](v T) *T { return &v } |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package internal | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/nobl9/govy/internal/assert" | ||
) | ||
|
||
func TestIsEmpty(t *testing.T) { | ||
tests := []struct { | ||
in any | ||
out bool | ||
}{ | ||
{nil, true}, | ||
{any(nil), true}, | ||
{any(""), true}, | ||
{"", true}, | ||
{0, true}, | ||
{0.0, true}, | ||
{false, true}, | ||
{struct{}{}, true}, | ||
{map[int]string{}, false}, | ||
{[]int{}, false}, | ||
{ptr(struct{}{}), false}, | ||
{ptr(""), false}, | ||
{make(chan int), false}, | ||
{any("this"), false}, | ||
{0.123, false}, | ||
{true, false}, | ||
{struct{ This string }{This: "this"}, false}, | ||
{map[int]string{0: ""}, false}, | ||
{ptr(struct{ This string }{This: "this"}), false}, | ||
{[]int{0}, false}, | ||
} | ||
for _, tc := range tests { | ||
assert.Equal(t, tc.out, IsEmpty(tc.in)) | ||
} | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.