-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
assert_error.go
94 lines (81 loc) · 2.36 KB
/
assert_error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package function
import (
"github.com/ysugimoto/falco/interpreter"
"github.com/ysugimoto/falco/interpreter/context"
"github.com/ysugimoto/falco/interpreter/function/errors"
"github.com/ysugimoto/falco/interpreter/value"
)
const Assert_error_Name = "assert.error"
var Assert_error_ArgumentTypes = []value.Type{value.IntegerType}
func Assert_error_Validate(args []value.Value) error {
if len(args) < 1 || len(args) > 3 {
return errors.ArgumentNotInRange(Assert_error_Name, 1, 3, args)
}
for i := range Assert_error_ArgumentTypes {
if args[i].Type() != Assert_error_ArgumentTypes[i] {
return errors.TypeMismatch(Assert_error_Name, i+1, Assert_error_ArgumentTypes[i], args[i].Type())
}
}
return nil
}
func Assert_error(
ctx *context.Context,
i *interpreter.Interpreter,
args ...value.Value,
) (value.Value, error) {
if err := Assert_error_Validate(args); err != nil {
return nil, errors.NewTestingError(err.Error())
}
// extract arguments
var message string
if len(args) == 3 {
message = value.Unwrap[*value.String](args[2]).Value
}
// First, check state moves to ERROR internally
if i.TestingState != interpreter.ERROR {
if message == "" {
return &value.Boolean{}, errors.NewAssertionError(
&value.String{Value: i.TestingState.String()},
"State does not move to ERROR, current state is %s",
i.TestingState.String(),
)
}
return &value.Boolean{}, errors.NewAssertionError(
&value.String{Value: i.TestingState.String()},
message,
)
}
// status code check
code := value.Unwrap[*value.Integer](args[0]).Value
if ctx.ObjectStatus.Value != code {
if message == "" {
return &value.Boolean{}, errors.NewAssertionError(
ctx.ObjectStatus,
"Error response code mismatch: expects %d, got %d",
code, ctx.ObjectStatus.Value,
)
}
return &value.Boolean{}, errors.NewAssertionError(
ctx.ObjectStatus,
message,
)
}
// response string check
if len(args) > 1 {
response := value.Unwrap[*value.String](args[1]).Value
if ctx.ObjectResponse.Value != response {
if message == "" {
return &value.Boolean{}, errors.NewAssertionError(
ctx.ObjectResponse,
"Error response text mismatch: expects %s, got %s",
response, ctx.ObjectResponse.Value,
)
}
return &value.Boolean{}, errors.NewAssertionError(
ctx.ObjectResponse,
message,
)
}
}
return &value.Boolean{Value: true}, nil
}