Skip to content
This repository was archived by the owner on Sep 17, 2022. It is now read-only.

Commit fb7a02f

Browse files
committed
Added length matcher
1 parent 0802adf commit fb7a02f

File tree

6 files changed

+71
-2
lines changed

6 files changed

+71
-2
lines changed

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,10 @@ test/positive: build
2222

2323
test/negative: build
2424
@docker-compose -f tests/echo/docker-compose.yaml up -d &>/dev/null
25-
$(BIN) check -s "tests/**/*_fail.jsonnet"
25+
$(BIN) check -s "tests/**/*_fail.jsonnet" --silent
26+
@docker-compose -f tests/echo/docker-compose.yaml down &>/dev/null
27+
28+
test/local: build
29+
@docker-compose -f tests/echo/docker-compose.yaml up -d &>/dev/null
30+
$(BIN) check -s "$(target)"
2631
@docker-compose -f tests/echo/docker-compose.yaml down &>/dev/null

spec/matcher/json_matcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (m jsonMatcher) Match(data interface{}, location string) (ok bool, pass []s
6767
keysMatched[name] = true
6868
break
6969
} else {
70-
errStr = fmt.Sprintf("%s does not match %s, got %v", key, valueMatcher, value)
70+
errStr = fmt.Sprintf("%s is not %s, got %v", key, valueMatcher, value)
7171
}
7272
}
7373
if !found {

spec/matcher/len_matcher.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package matcher
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/getapid/apid/log"
7+
)
8+
9+
type lenMatcher struct {
10+
value int
11+
}
12+
13+
func LenMatcherWithOptions(params interface{}) Matcher {
14+
switch v := params.(type) {
15+
case float64:
16+
// check if value is int, json unmarshal puts
17+
// numbers in float64
18+
if v == float64(int(v)) {
19+
return lenMatcher{int(v)}
20+
}
21+
log.L.Fatalf("invalid len matcher, got %v", params)
22+
default:
23+
log.L.Fatalf("invalid len matcher, got %v", params)
24+
}
25+
return nil
26+
}
27+
28+
func (m lenMatcher) Match(data interface{}, location string) (bool, []string, []string) {
29+
switch val := data.(type) {
30+
case []interface{}:
31+
if len(val) == m.value {
32+
return true, []string{fmt.Sprintf("`%s` has length %d", location, m.value)}, nil
33+
}
34+
return true, nil, []string{fmt.Sprintf("`%s` of length %d wanted, got %d", location, m.value, len(val))}
35+
case map[string]interface{}:
36+
if len(val) == m.value {
37+
return true, []string{fmt.Sprintf("`%s` has length %d", location, m.value)}, nil
38+
}
39+
return true, nil, []string{fmt.Sprintf("`%s` of length %d wanted, got %d", location, m.value, len(val))}
40+
case string:
41+
if len(val) == m.value {
42+
return true, []string{fmt.Sprintf("`%s` has length %d", location, m.value)}, nil
43+
}
44+
return false, nil, []string{fmt.Sprintf("`%s` of length %d wanted, got %d", location, m.value, len(val))}
45+
default:
46+
return false, nil, []string{fmt.Sprintf("`%s` not a map, array or string", location)}
47+
}
48+
49+
}
50+
51+
func (m lenMatcher) String() string {
52+
return fmt.Sprintf("of lentgh %d", m.value)
53+
}

spec/matcher/matcher.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ func tryGetMatcherFromMap(m map[string]interface{}) Matcher {
5050
return JSONMatcherWithOptions(params)
5151
case "array":
5252
return ArrayMatcherWithOptions(params)
53+
case "len":
54+
return LenMatcherWithOptions(params)
5355
default:
5456
log.L.Fatalf("unknown matcher type %s", t)
5557
return nil

tests/passing/apid/is.libsonnet

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ local array_matcher(array, subset=false) =
5151
},
5252
};
5353

54+
local len_matcher(len) =
55+
{
56+
'$$matcher_type$$': 'len',
57+
'$$matcher_params$$': len,
58+
};
59+
5460
local json_to_string(json) = std.manifestJsonEx(json, '');
5561

5662
{
@@ -59,6 +65,7 @@ local json_to_string(json) = std.manifestJsonEx(json, '');
5965
string(string, case_sensitive=true):: '%s%s' % [SHORTHAND_MATCHER_PREFIX, json_to_string(string_matcher(string, case_sensitive))],
6066
int(int):: '%s%s' % [SHORTHAND_MATCHER_PREFIX, json_to_string(int_matcher(int))],
6167
float(float):: '%s%s' % [SHORTHAND_MATCHER_PREFIX, json_to_string(float_matcher(float))],
68+
len(len):: '%s%s' % [SHORTHAND_MATCHER_PREFIX, json_to_string(len_matcher(len))],
6269
},
6370

6471
any():: any_matcher(),
@@ -68,4 +75,5 @@ local json_to_string(json) = std.manifestJsonEx(json, '');
6875
float(float):: float_matcher(float),
6976
json(json, subset=false):: json_matcher(json, subset),
7077
array(array, subset=false):: array_matcher(array, subset),
78+
len(len):: len_matcher(len),
7179
}

tests/passing/body_pass.jsonnet

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ local steps(method, body, expected) = [
5353
'Marline',
5454
'Catharine',
5555
],
56+
countryCode: is.len(2),
5657
},
5758
]
5859
}

0 commit comments

Comments
 (0)