-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb02a7e
commit 9db75ce
Showing
2 changed files
with
150 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package extra | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/golang/mock/gomock" | ||
) | ||
|
||
type intRangeMatcher struct { | ||
lowerBound, upperBound int | ||
} | ||
|
||
func (i *intRangeMatcher) String() string { | ||
return fmt.Sprintf("it upper than %d and lower than %d", i.lowerBound, i.upperBound) | ||
} | ||
|
||
func (i *intRangeMatcher) Matches(x interface{}) bool { | ||
// Try to cast input as int | ||
inp, ok := x.(int) | ||
// Check | ||
if !ok { | ||
return false | ||
} | ||
|
||
return i.lowerBound <= inp && inp <= i.upperBound | ||
} | ||
|
||
func IntRangeMatcher(lowerBound, upperBound int) gomock.Matcher { | ||
return &intRangeMatcher{lowerBound: lowerBound, upperBound: upperBound} | ||
} |
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,120 @@ | ||
package extra | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
) | ||
|
||
func TestIntRangeMatcher(t *testing.T) { | ||
type args struct { | ||
lowerBound int | ||
upperBound int | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want gomock.Matcher | ||
}{ | ||
{ | ||
name: "constructor", | ||
args: args{lowerBound: 5, upperBound: 15}, | ||
want: &intRangeMatcher{ | ||
lowerBound: 5, | ||
upperBound: 15, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := IntRangeMatcher(tt.args.lowerBound, tt.args.upperBound); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("IntRangeMatcher() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_intRange_String(t *testing.T) { | ||
type fields struct { | ||
lowerBound int | ||
upperBound int | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
want string | ||
}{ | ||
{ | ||
name: "display", | ||
fields: fields{lowerBound: 5, upperBound: 15}, | ||
want: "it upper than 5 and lower than 15", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
i := &intRangeMatcher{ | ||
lowerBound: tt.fields.lowerBound, | ||
upperBound: tt.fields.upperBound, | ||
} | ||
if got := i.String(); got != tt.want { | ||
t.Errorf("intRange.String() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_intRange_Matches(t *testing.T) { | ||
type fields struct { | ||
lowerBound int | ||
upperBound int | ||
} | ||
type args struct { | ||
x interface{} | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want bool | ||
}{ | ||
{ | ||
name: "not an int", | ||
args: args{x: "string"}, | ||
want: false, | ||
}, | ||
{ | ||
name: "not an int 2", | ||
args: args{x: true}, | ||
want: false, | ||
}, | ||
{ | ||
name: "should match 0 case", | ||
args: args{x: 0}, | ||
want: true, | ||
}, | ||
{ | ||
name: "should match in range", | ||
args: args{x: 5}, | ||
fields: fields{lowerBound: 1, upperBound: 15}, | ||
want: true, | ||
}, | ||
{ | ||
name: "shouldn't match not in range", | ||
args: args{x: 25}, | ||
fields: fields{lowerBound: 1, upperBound: 15}, | ||
want: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
i := &intRangeMatcher{ | ||
lowerBound: tt.fields.lowerBound, | ||
upperBound: tt.fields.upperBound, | ||
} | ||
if got := i.Matches(tt.args.x); got != tt.want { | ||
t.Errorf("intRange.Matches() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |