Skip to content

Commit

Permalink
feat: Add int range matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
oxyno-zeta committed Sep 6, 2020
1 parent fb02a7e commit 9db75ce
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 0 deletions.
30 changes: 30 additions & 0 deletions int-range-matcher.go
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}
}
120 changes: 120 additions & 0 deletions int-range-matcher_test.go
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)
}
})
}
}

0 comments on commit 9db75ce

Please sign in to comment.