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

Commit 65c11db

Browse files
committed
Added bool matchers
1 parent 8bfd7d7 commit 65c11db

File tree

8 files changed

+126
-96
lines changed

8 files changed

+126
-96
lines changed

file/jsonnet_lib.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ local spec(steps) = std.manifestJson(
1515
1616
local type = {
1717
int: type_matcher('int'),
18+
bool: type_matcher('bool'),
1819
float: type_matcher('float'),
1920
string: type_matcher('string'),
2021
object: type_matcher('object'),
@@ -44,6 +45,11 @@ local int(int) = {
4445
'$$matcher_params$$': int,
4546
};
4647
48+
local bool(bool) = {
49+
'$$matcher_type$$': 'bool',
50+
'$$matcher_params$$': bool,
51+
};
52+
4753
local float(float) = {
4854
'$$matcher_type$$': 'float',
4955
'$$matcher_params$$': float,

spec/matcher/bool_matcher.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
11
package matcher
22

3+
import (
4+
"fmt"
5+
6+
"github.com/getapid/apid/log"
7+
)
8+
39
type boolMatcher struct {
410
value bool
511
}
612

7-
func BoolMatcher(value bool) Matcher {
8-
return boolMatcher{value}
13+
func BoolMatcherWithOptions(params interface{}) Matcher {
14+
switch v := params.(type) {
15+
case bool:
16+
return boolMatcher{v}
17+
default:
18+
log.L.Fatalf("invalid bool matcher, got %v", params)
19+
}
20+
return nil
921
}
1022

1123
func (m boolMatcher) Match(data interface{}, location string) (bool, []string, []string) {
12-
// switch val := data.(type) {
13-
// case bool:
14-
// return v.value == val, ""
15-
// }
16-
// return false, ""
17-
return false, nil, nil
24+
switch val := data.(type) {
25+
case bool:
26+
if m.value == val {
27+
return true, []string{fmt.Sprintf("%s is %t", location, m.value)}, nil
28+
}
29+
}
30+
31+
return false, nil, []string{fmt.Sprintf("%s: wanted %t, got %v", location, m.value, data)}
1832
}
1933

2034
func (m boolMatcher) String() string {
21-
return "false"
35+
return fmt.Sprintf("%t", m.value)
2236
}

spec/matcher/matcher.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ func tryGetMatcherFromMap(m map[string]interface{}) Matcher {
4646
return IntMatcherWithOptions(params)
4747
case "float":
4848
return FloatMatcherWithOptions(params)
49+
case "bool":
50+
return BoolMatcherWithOptions(params)
4951
case "json":
5052
return JSONMatcherWithOptions(params)
5153
case "array":
@@ -60,6 +62,8 @@ func tryGetMatcherFromMap(m map[string]interface{}) Matcher {
6062
return RangeMatcherWithOptions(params)
6163
case "type::int":
6264
return TypeIntMatcherWithOptions(params)
65+
case "type::bool":
66+
return TypeBoolMatcherWithOptions(params)
6367
case "type::float":
6468
return TypeFloatMatcherWithOptions(params)
6569
case "type::string":
@@ -85,7 +89,7 @@ func GetMatcher(i interface{}) Matcher {
8589
case []interface{}:
8690
return ArrayMatcher(val)
8791
case bool:
88-
return BoolMatcher(val)
92+
return BoolMatcherWithOptions(val)
8993
case float64:
9094
return FloatMatcherWithOptions(val)
9195
case string:

spec/matcher/type_bool.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package matcher
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type typeBoolMatcher struct{}
8+
9+
func TypeBoolMatcherWithOptions(value interface{}) Matcher {
10+
return &typeBoolMatcher{}
11+
}
12+
13+
func (m typeBoolMatcher) Match(data interface{}, location string) (bool, []string, []string) {
14+
switch data.(type) {
15+
case bool:
16+
return true, []string{fmt.Sprintf("%s is bool", location)}, nil
17+
}
18+
19+
return false, nil, []string{fmt.Sprintf("%s wanted bool, got %v", location, data)}
20+
}
21+
22+
func (m typeBoolMatcher) String() string {
23+
return "type::bool"
24+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
local vars = import 'vars.libsonnet';
2+
3+
local text_body_spec(method, body, expectedBody) = std.manifestJson(
4+
{
5+
steps: [
6+
{
7+
name: 'first request',
8+
request: {
9+
method: method,
10+
url: vars.url,
11+
body: body,
12+
},
13+
expect: {
14+
code: 200,
15+
body: expectedBody,
16+
},
17+
},
18+
{
19+
name: 'second request',
20+
request: {
21+
method: method,
22+
url: vars.url,
23+
body: body,
24+
},
25+
expect: {
26+
code: 200,
27+
body: expectedBody,
28+
},
29+
},
30+
],
31+
}
32+
);
33+
34+
{
35+
['true-bool-%s-%s-%s' % [method, body, expectedBody]]: text_body_spec(method, body, expectedBody)
36+
for method in ['POST', 'PUT', 'PATCH', 'DELETE']
37+
for body in [true]
38+
for expectedBody in [
39+
false,
40+
]
41+
}
42+
{
43+
['false-bool-%s-%s-%s' % [method, body, expectedBody]]: text_body_spec(method, body, expectedBody)
44+
for method in ['POST', 'PUT', 'PATCH', 'DELETE']
45+
for body in [false]
46+
for expectedBody in [
47+
true,
48+
]
49+
}
50+
{
51+
['non-bool-%s-%s-%s' % [method, body, expectedBody]]: text_body_spec(method, body, expectedBody)
52+
for method in ['POST', 'PUT', 'PATCH', 'DELETE']
53+
for body in ['false', 12, 13.1247, {}, []]
54+
for expectedBody in [
55+
type.bool,
56+
false,
57+
true,
58+
]
59+
}

tests/passing/apid.libsonnet

Lines changed: 0 additions & 86 deletions
This file was deleted.

tests/passing/body_pass.jsonnet

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ local steps(method, body, expected) = [
3939
float(66.861),
4040
type.float,
4141
]),
42+
bool: and([
43+
true,
44+
type.bool,
45+
]),
46+
boolFalse: and([
47+
false,
48+
type.bool,
49+
]),
4250
[key(string('random'))]: int(88),
4351
[key(regex('first\\w+'))]: 'Lilith',
4452
[key(

tests/passing/vars.libsonnet

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
random: 88,
55
'random float': 66.861,
66
bool: true,
7+
boolFalse: false,
78
date: '1990-09-24',
89
regEx: 'helloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo to you',
910
enum: 'generator',

0 commit comments

Comments
 (0)