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

Commit

Permalink
Added bool matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
iv-p committed Feb 16, 2022
1 parent 8bfd7d7 commit 65c11db
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 96 deletions.
6 changes: 6 additions & 0 deletions file/jsonnet_lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ local spec(steps) = std.manifestJson(
local type = {
int: type_matcher('int'),
bool: type_matcher('bool'),
float: type_matcher('float'),
string: type_matcher('string'),
object: type_matcher('object'),
Expand Down Expand Up @@ -44,6 +45,11 @@ local int(int) = {
'$$matcher_params$$': int,
};
local bool(bool) = {
'$$matcher_type$$': 'bool',
'$$matcher_params$$': bool,
};
local float(float) = {
'$$matcher_type$$': 'float',
'$$matcher_params$$': float,
Expand Down
32 changes: 23 additions & 9 deletions spec/matcher/bool_matcher.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
package matcher

import (
"fmt"

"github.com/getapid/apid/log"
)

type boolMatcher struct {
value bool
}

func BoolMatcher(value bool) Matcher {
return boolMatcher{value}
func BoolMatcherWithOptions(params interface{}) Matcher {
switch v := params.(type) {
case bool:
return boolMatcher{v}
default:
log.L.Fatalf("invalid bool matcher, got %v", params)
}
return nil
}

func (m boolMatcher) Match(data interface{}, location string) (bool, []string, []string) {
// switch val := data.(type) {
// case bool:
// return v.value == val, ""
// }
// return false, ""
return false, nil, nil
switch val := data.(type) {
case bool:
if m.value == val {
return true, []string{fmt.Sprintf("%s is %t", location, m.value)}, nil
}
}

return false, nil, []string{fmt.Sprintf("%s: wanted %t, got %v", location, m.value, data)}
}

func (m boolMatcher) String() string {
return "false"
return fmt.Sprintf("%t", m.value)
}
6 changes: 5 additions & 1 deletion spec/matcher/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func tryGetMatcherFromMap(m map[string]interface{}) Matcher {
return IntMatcherWithOptions(params)
case "float":
return FloatMatcherWithOptions(params)
case "bool":
return BoolMatcherWithOptions(params)
case "json":
return JSONMatcherWithOptions(params)
case "array":
Expand All @@ -60,6 +62,8 @@ func tryGetMatcherFromMap(m map[string]interface{}) Matcher {
return RangeMatcherWithOptions(params)
case "type::int":
return TypeIntMatcherWithOptions(params)
case "type::bool":
return TypeBoolMatcherWithOptions(params)
case "type::float":
return TypeFloatMatcherWithOptions(params)
case "type::string":
Expand All @@ -85,7 +89,7 @@ func GetMatcher(i interface{}) Matcher {
case []interface{}:
return ArrayMatcher(val)
case bool:
return BoolMatcher(val)
return BoolMatcherWithOptions(val)
case float64:
return FloatMatcherWithOptions(val)
case string:
Expand Down
24 changes: 24 additions & 0 deletions spec/matcher/type_bool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package matcher

import (
"fmt"
)

type typeBoolMatcher struct{}

func TypeBoolMatcherWithOptions(value interface{}) Matcher {
return &typeBoolMatcher{}
}

func (m typeBoolMatcher) Match(data interface{}, location string) (bool, []string, []string) {
switch data.(type) {
case bool:
return true, []string{fmt.Sprintf("%s is bool", location)}, nil
}

return false, nil, []string{fmt.Sprintf("%s wanted bool, got %v", location, data)}
}

func (m typeBoolMatcher) String() string {
return "type::bool"
}
59 changes: 59 additions & 0 deletions tests/failing/body_bool_fail.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
local vars = import 'vars.libsonnet';

local text_body_spec(method, body, expectedBody) = std.manifestJson(
{
steps: [
{
name: 'first request',
request: {
method: method,
url: vars.url,
body: body,
},
expect: {
code: 200,
body: expectedBody,
},
},
{
name: 'second request',
request: {
method: method,
url: vars.url,
body: body,
},
expect: {
code: 200,
body: expectedBody,
},
},
],
}
);

{
['true-bool-%s-%s-%s' % [method, body, expectedBody]]: text_body_spec(method, body, expectedBody)
for method in ['POST', 'PUT', 'PATCH', 'DELETE']
for body in [true]
for expectedBody in [
false,
]
}
{
['false-bool-%s-%s-%s' % [method, body, expectedBody]]: text_body_spec(method, body, expectedBody)
for method in ['POST', 'PUT', 'PATCH', 'DELETE']
for body in [false]
for expectedBody in [
true,
]
}
{
['non-bool-%s-%s-%s' % [method, body, expectedBody]]: text_body_spec(method, body, expectedBody)
for method in ['POST', 'PUT', 'PATCH', 'DELETE']
for body in ['false', 12, 13.1247, {}, []]
for expectedBody in [
type.bool,
false,
true,
]
}
86 changes: 0 additions & 86 deletions tests/passing/apid.libsonnet

This file was deleted.

8 changes: 8 additions & 0 deletions tests/passing/body_pass.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ local steps(method, body, expected) = [
float(66.861),
type.float,
]),
bool: and([
true,
type.bool,
]),
boolFalse: and([
false,
type.bool,
]),
[key(string('random'))]: int(88),
[key(regex('first\\w+'))]: 'Lilith',
[key(
Expand Down
1 change: 1 addition & 0 deletions tests/passing/vars.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
random: 88,
'random float': 66.861,
bool: true,
boolFalse: false,
date: '1990-09-24',
regEx: 'helloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo to you',
enum: 'generator',
Expand Down

0 comments on commit 65c11db

Please sign in to comment.