Skip to content

Commit

Permalink
feat(matcher): add support for int, float + bool to dsl.Match
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Fellows committed Oct 23, 2019
1 parent cb2a377 commit 649c7a8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
38 changes: 36 additions & 2 deletions dsl/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,20 @@ func match(srcType reflect.Type, params params) Matcher {

return Like("string")
case reflect.Bool:
if params.boolean.defined {
return Like(params.boolean.value)
}
return Like(true)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if params.number.integer != 0 {
return Like(params.number.integer)
}
return Like(1)
case reflect.Float32, reflect.Float64:
if params.number.float != 0 {
return Like(params.number.float)
}
return Like(1.1)
default:
panic(fmt.Sprintf("match: unhandled type: %v", srcType))
Expand All @@ -327,8 +336,19 @@ func match(srcType reflect.Type, params params) Matcher {
// struct fields. They are passed back into match() along with their
// associated type to serve as parameters for the dsl functions.
type params struct {
slice sliceParams
str stringParams
slice sliceParams
str stringParams
number numberParams
boolean boolParams
}

type numberParams struct {
integer int
float float32
}
type boolParams struct {
value bool
defined bool
}

type sliceParams struct {
Expand Down Expand Up @@ -360,6 +380,20 @@ func pluckParams(srcType reflect.Type, pactTag string) params {
}

switch kind := srcType.Kind(); kind {
case reflect.Bool:
if _, err := fmt.Sscanf(pactTag, "example=%t", &params.boolean.value); err != nil {
triggerInvalidPactTagPanic(pactTag, err)
}
params.boolean.defined = true
case reflect.Float32, reflect.Float64:
if _, err := fmt.Sscanf(pactTag, "example=%g", &params.number.float); err != nil {
triggerInvalidPactTagPanic(pactTag, err)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if _, err := fmt.Sscanf(pactTag, "example=%d", &params.number.integer); err != nil {
triggerInvalidPactTagPanic(pactTag, err)
}
case reflect.Slice:
if _, err := fmt.Sscanf(pactTag, "min=%d", &params.slice.min); err != nil {
triggerInvalidPactTagPanic(pactTag, err)
Expand Down
26 changes: 26 additions & 0 deletions dsl/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,13 @@ func TestMatch(t *testing.T) {
type wordsDTO struct {
Words []string `json:"words" pact:"min=2"`
}
type boolDTO struct {
Boolean bool `json:"boolean" pact:"example=true"`
}
type numberDTO struct {
Integer int `json:"integer" pact:"example=42"`
Float float32 `json:"float" pact:"example=6.66"`
}
str := "str"
type args struct {
src interface{}
Expand Down Expand Up @@ -622,6 +629,25 @@ func TestMatch(t *testing.T) {
"words": EachLike(Like("string"), 2),
},
},
{
name: "recursive case - struct with bool",
args: args{
src: boolDTO{},
},
want: StructMatcher{
"boolean": Like(true),
},
},
{
name: "recursive case - struct with int and float",
args: args{
src: numberDTO{},
},
want: StructMatcher{
"integer": Like(42),
"float": Like(float32(6.66)),
},
},
{
name: "base case - string",
args: args{
Expand Down

0 comments on commit 649c7a8

Please sign in to comment.