Skip to content

Commit

Permalink
chore(test): remove lots of code, add message tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed May 17, 2018
1 parent ab9ce17 commit de45157
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 102 deletions.
103 changes: 1 addition & 102 deletions dsl/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"log"
"reflect"
"regexp"
"strconv"
"strings"
"time"
)
Expand Down Expand Up @@ -188,109 +187,9 @@ func (m Matcher) isMatcher() {}
// GetValue returns the raw generated value for the matcher
// without any of the matching detail context
func (m Matcher) GetValue() interface{} {
class, ok := m["json_class"]

if !ok {
return nil
}

// extract out the value
switch class {
case "Pact::ArrayLike":
contents := m["contents"]
min, err := strconv.Atoi(fmt.Sprintf("%d", m["min"]))
if err != nil {
min = 1
}

data := make([]interface{}, min)

for i := 0; i < min; i++ {
data[i] = contents
}
return data

case "Pact::SomethingLike":
return m["contents"]
case "Pact::Term":
data := m["data"].(map[string]interface{})
return data["generate"]
}

return nil
}

// GetValue returns the raw generated value for the matcher
// without any of the matching detail context
func getMatcherValue(m interface{}) interface{} {
matcher, ok := getMatcher(m)
if !ok {
return nil
}

class, ok := matcher["json_class"]

if !ok {
return nil
}

// extract out the value
switch class {
case "Pact::ArrayLike":
contents := matcher["contents"]
min := matcher["min"].(int)
data := make([]interface{}, min)

for i := 0; i < min; i++ {
data[i] = contents
}
return data

case "Pact::SomethingLike":
return matcher["contents"]
case "Pact::Term":
data := matcher["data"].(map[string]interface{})
return data["generate"]
}

return nil
}

// func isMatcher(obj map[string]interface{}) bool {
func isMatcher(obj interface{}) bool {
m, ok := obj.(map[string]interface{})

if ok {
if _, match := m["json_class"]; match {
return true
}
}

if _, match := obj.(Matcher); match {
return true
}

return false
}

func getMatcher(obj interface{}) (Matcher, bool) {
// If an object, but not a map[string]interface{} then just return?
m, ok := obj.(map[string]interface{})

if ok {
if _, match := m["json_class"]; match {
return m, true
}
}

m, ok = obj.(Matcher)
if ok {
return m, true
}

return nil, false
}

// MapMatcher allows a map[string]string-like object
// to also contain complex matchers
type MapMatcher map[string]StringMatcher
Expand Down Expand Up @@ -420,7 +319,7 @@ func pluckParams(srcType reflect.Type, pactTag string) params {
} else if exampleRegex.Match([]byte(pactTag)) {
components := strings.Split(pactTag, "example=")

if len(components) != 2 {
if len(components) != 2 || strings.TrimSpace(components[1]) == "" {
triggerInvalidPactTagPanic(pactTag, fmt.Errorf("invalid format: example must not be empty"))
}

Expand Down
8 changes: 8 additions & 0 deletions dsl/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,14 @@ func Test_pluckParams(t *testing.T) {
},
wantPanic: true,
},
{
name: "invalid string tag - empty example",
args: args{
srcType: reflect.TypeOf(""),
pactTag: "example=",
},
wantPanic: true,
},
{
name: "invalid string tag - example typo",
args: args{
Expand Down
20 changes: 20 additions & 0 deletions dsl/message_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dsl

import "testing"

type t struct {
ID int
}

func TestMessage_DSL(t *testing.T) {
m := &Message{}
m.Given("state string").
ExpectsToReceive("description string").
WithMetadata(MapMatcher{
"content-type": String("application/json"),
}).
WithContent(map[string]interface{}{
"foo": "bar",
}).
AsType(t)
}

0 comments on commit de45157

Please sign in to comment.