-
Notifications
You must be signed in to change notification settings - Fork 26
Add arithmetic compasion checkers #178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
babakks
wants to merge
8
commits into
frankban:master
Choose a base branch
from
babakks:add-arithmetic-checkers
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a627caf
Add `binaryArithmeticChecker`
babakks 1940365
Add `IsLessThan` checker
babakks 5fb794e
Add `IsLessThanOrEqual` checker
babakks d2a0d22
Add tests for `IsLessThan` and `IsLessThanOrEqual`
babakks 31992e6
Add tests for `binaryArithmeticChecker`
babakks 9ead6e9
Add `IsGreaterThan` checker
babakks 8937e00
Add `IsGreaterThanOrEqual` checker
babakks 08b1b42
Add tests for `IsGreaterThan` and `IsGreaterThanOrEqual`
babakks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| // Licensed under the MIT license, see LICENSE file for details. | ||
|
|
||
| package quicktest_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "math/big" | ||
| "testing" | ||
|
|
||
| "github.com/frankban/quicktest" | ||
| ) | ||
|
|
||
| // TestBinaryArithmeticCheckerMatrix verifies that the binary arithmetic checker | ||
| // correctly handles all possible types of input arguments (got/reference). | ||
| func TestBinaryArithmeticCheckerMatrix(t *testing.T) { | ||
| numerics := []interface{}{ | ||
| int(99), | ||
| int8(99), | ||
| int16(99), | ||
| int32(99), | ||
| int64(99), | ||
| uint(99), | ||
| uint8(99), | ||
| uint16(99), | ||
| uint32(99), | ||
| uint64(99), | ||
| float32(99), | ||
| float64(99), | ||
| } | ||
|
|
||
| asBigFloat := big.NewFloat(99) | ||
|
|
||
| for _, got := range numerics { | ||
| for _, reference := range numerics { | ||
| t.Run(fmt.Sprintf("%T and %T", got, reference), func(t *testing.T) { | ||
| checker := quicktest.NewBinaryArithmeticChecker(func(value, reference *big.Float) error { | ||
| if value.Cmp(asBigFloat) != 0 { | ||
| t.Fatal("unexpected value") | ||
| } | ||
| if reference.Cmp(asBigFloat) != 0 { | ||
| t.Fatal("unexpected value") | ||
| } | ||
| return nil | ||
| }) | ||
|
|
||
| err := checker.Check(got, []interface{}{reference}, func(key string, value interface{}) {}) | ||
| if err != nil { | ||
| t.Fatal("expected nil error from checker") | ||
| } | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestBinaryArithmeticCheckerValidation(t *testing.T) { | ||
| tests := []struct { | ||
| about string | ||
| value interface{} | ||
| reference interface{} | ||
| expectedError string | ||
| }{{ | ||
| about: "value is boolean", | ||
| value: true, | ||
| reference: 0, | ||
| expectedError: "bad check: value should be of a numeric type but it is bool", | ||
| }, { | ||
| about: "reference is boolean", | ||
| value: 0, | ||
| reference: true, | ||
| expectedError: "bad check: reference should be of a numeric type but it is bool", | ||
| }, { | ||
| about: "value is string", | ||
| value: "foo", | ||
| reference: 0, | ||
| expectedError: "bad check: value should be of a numeric type but it is string", | ||
| }, { | ||
| about: "reference is string", | ||
| value: 0, | ||
| reference: "foo", | ||
| expectedError: "bad check: reference should be of a numeric type but it is string", | ||
| }, { | ||
| about: "value is function", | ||
| value: func() {}, | ||
| reference: 0, | ||
| expectedError: "bad check: value should be of a numeric type but it is func()", | ||
| }, { | ||
| about: "reference is function", | ||
| value: 0, | ||
| reference: func() {}, | ||
| expectedError: "bad check: reference should be of a numeric type but it is func()", | ||
| }, { | ||
| about: "value is slice", | ||
| value: []int{}, | ||
| reference: 0, | ||
| expectedError: "bad check: value should be of a numeric type but it is []int", | ||
| }, { | ||
| about: "reference is slice", | ||
| value: 0, | ||
| reference: []int{}, | ||
| expectedError: "bad check: reference should be of a numeric type but it is []int", | ||
| }, { | ||
| about: "value is map", | ||
| value: map[string]string{}, | ||
| reference: 0, | ||
| expectedError: "bad check: value should be of a numeric type but it is map[string]string", | ||
| }, { | ||
| about: "reference is map", | ||
| value: 0, | ||
| reference: map[string]string{}, | ||
| expectedError: "bad check: reference should be of a numeric type but it is map[string]string", | ||
| }, { | ||
| about: "value is struct", | ||
| value: struct{}{}, | ||
| reference: 0, | ||
| expectedError: "bad check: value should be of a numeric type but it is struct {}", | ||
| }, { | ||
| about: "reference is struct", | ||
| value: 0, | ||
| reference: struct{}{}, | ||
| expectedError: "bad check: reference should be of a numeric type but it is struct {}", | ||
| }, { | ||
| about: "value is pointer", | ||
| value: &struct{}{}, | ||
| reference: 0, | ||
| expectedError: "bad check: value should be of a numeric type but it is *struct {}", | ||
| }, { | ||
| about: "reference is pointer", | ||
| value: 0, | ||
| reference: &struct{}{}, | ||
| expectedError: "bad check: reference should be of a numeric type but it is *struct {}", | ||
| }, { | ||
| about: "value is nil", | ||
| value: nil, | ||
| reference: 0, | ||
| expectedError: "bad check: value should be of a numeric type but it is <nil>", | ||
| }, { | ||
| about: "reference is nil", | ||
| value: 0, | ||
| reference: nil, | ||
| expectedError: "bad check: reference should be of a numeric type but it is <nil>", | ||
| }, | ||
| } | ||
|
|
||
| noopChecker := quicktest.NewBinaryArithmeticChecker(func(value, reference *big.Float) error { | ||
| return nil | ||
| }) | ||
|
|
||
| for _, test := range tests { | ||
| t.Run(test.about, func(t *testing.T) { | ||
| err := noopChecker.Check(test.value, []interface{}{test.reference}, func(key string, value interface{}) {}) | ||
| if err == nil { | ||
| t.Fatal("unexpected nil error") | ||
| } | ||
| if err.Error() != test.expectedError { | ||
| t.Fatal("unexpected error message") | ||
| } | ||
| }) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need all these tests? Isn't one test for the value and one for the reference sufficient? Also, can this be just part of the checker tests?