Skip to content
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

fix: support struct tags for pointers #198

Merged
merged 1 commit into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dsl/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,8 @@ func pluckParams(srcType reflect.Type, pactTag string) params {

params.str.example = components[1]
}
case reflect.Ptr:
return pluckParams(srcType.Elem(), pactTag)
}

return params
Expand Down
126 changes: 126 additions & 0 deletions dsl/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,19 +557,33 @@ func TestMatch(t *testing.T) {
Word string `json:"word"`
Length int `json:"length"`
}
type wordPtrDTO struct {
Word *string `json:"word,omitempty"`
Length *int `json:"length,omitempty"`
}
type dateDTO struct {
Date string `json:"date" pact:"example=2000-01-01,regex=^\\d{4}-\\d{2}-\\d{2}$"`
}
type wordsDTO struct {
Words []string `json:"words" pact:"min=2"`
}
type wordsPtrDTO struct {
Words *[]string `json:"words,omitempty" pact:"min=2"`
}
type boolDTO struct {
Boolean bool `json:"boolean" pact:"example=true"`
}
type boolPtrDTO struct {
Boolean *bool `json:"boolean,omitempty" pact:"example=true"`
}
type numberDTO struct {
Integer int `json:"integer" pact:"example=42"`
Float float32 `json:"float" pact:"example=6.66"`
}
type numberPtrDTO struct {
Integer *int `json:"integer,omitempty" pact:"example=42"`
Float *float32 `json:"float,omitempty" pact:"example=6.66"`
}
type jsonTagOmitemptyDTO struct {
Word string `json:"word,omitempty"`
}
Expand Down Expand Up @@ -625,6 +639,17 @@ func TestMatch(t *testing.T) {
"length": Like(1),
},
},
{
name: "recursive case - struct with pointers",
args: args{
src: wordPtrDTO{},
},
want: StructMatcher{
"word": Like("string"),
"length": Like(1),
},
},

{
name: "recursive case - struct with custom string tag",
args: args{
Expand All @@ -643,6 +668,15 @@ func TestMatch(t *testing.T) {
"words": EachLike(Like("string"), 2),
},
},
{
name: "recursive case - struct with custom slice tag and pointers",
args: args{
src: wordsPtrDTO{},
},
want: StructMatcher{
"words": EachLike(Like("string"), 2),
},
},
{
name: "recursive case - struct with bool",
args: args{
Expand All @@ -652,6 +686,15 @@ func TestMatch(t *testing.T) {
"boolean": Like(true),
},
},
{
name: "recursive case - struct with bool pointer",
args: args{
src: boolPtrDTO{},
},
want: StructMatcher{
"boolean": Like(true),
},
},
{
name: "recursive case - struct with int and float",
args: args{
Expand All @@ -662,6 +705,16 @@ func TestMatch(t *testing.T) {
"float": Like(float32(6.66)),
},
},
{
name: "recursive case - struct with int and float pointers",
args: args{
src: numberPtrDTO{},
},
want: StructMatcher{
"integer": Like(42),
"float": Like(float32(6.66)),
},
},
{
name: "recursive case - struct with json tag including omitempty",
args: args{
Expand Down Expand Up @@ -828,6 +881,12 @@ func TestMatch(t *testing.T) {
}

func Test_pluckParams(t *testing.T) {
var (
s string
i int
b bool
ss []string
)
type args struct {
srcType reflect.Type
pactTag string
Expand Down Expand Up @@ -949,6 +1008,73 @@ func Test_pluckParams(t *testing.T) {
},
wantPanic: false,
},
{
name: "expected use - example with string pointer",
args: args{
srcType: reflect.TypeOf(&s),
pactTag: "example=aBcD123,regex=[A-Za-z0-9]",
},
want: params{
slice: sliceParams{
min: getDefaults().slice.min,
},
str: stringParams{
example: "aBcD123",
regEx: "[A-Za-z0-9]",
},
},
wantPanic: false,
},
{
name: "expected use - example with number pointer",
args: args{
srcType: reflect.TypeOf(&i),
pactTag: "example=7",
},
want: params{
slice: sliceParams{
min: getDefaults().slice.min,
},
number: numberParams{
integer: 7,
},
},
wantPanic: false,
},
{
name: "expected use - example with boolean pointer",
args: args{
srcType: reflect.TypeOf(&b),
pactTag: "example=true",
},
want: params{
slice: sliceParams{
min: getDefaults().slice.min,
},
boolean: boolParams{
value: true,
defined: true,
},
},
wantPanic: false,
},
{
name: "expected use - example with string slice pointer",
args: args{
srcType: reflect.TypeOf(&ss),
pactTag: "min=2",
},
want: params{
slice: sliceParams{
min: 2,
},
str: stringParams{
example: getDefaults().str.example,
regEx: getDefaults().str.regEx,
},
},
wantPanic: false,
},
{
name: "empty string tag",
args: args{
Expand Down