-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #403 from ysugimoto/feature/testing-assert-not-state
implement assert.not_state assertion function
- Loading branch information
Showing
4 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package function | ||
|
||
import ( | ||
"fmt" | ||
|
||
"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_not_state_Name = "assert.not_state" | ||
|
||
var Assert_not_state_ArgumentTypes = []value.Type{value.IdentType} | ||
|
||
func Assert_not_state_Validate(args []value.Value) error { | ||
if len(args) > 2 { | ||
return errors.ArgumentNotInRange(Assert_not_state_Name, 1, 2, args) | ||
} | ||
|
||
for i := range Assert_not_state_ArgumentTypes { | ||
if args[i].Type() != Assert_not_state_ArgumentTypes[i] { | ||
return errors.TypeMismatch(Assert_not_state_Name, i+1, Assert_not_state_ArgumentTypes[i], args[i].Type()) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func Assert_not_state( | ||
ctx *context.Context, | ||
i *interpreter.Interpreter, | ||
args ...value.Value, | ||
) (value.Value, error) { | ||
|
||
if err := Assert_not_state_Validate(args); err != nil { | ||
return nil, errors.NewTestingError("%s", err.Error()) | ||
} | ||
|
||
state := value.Unwrap[*value.Ident](args[0]) | ||
expect := interpreter.StateFromString(state.Value) | ||
|
||
var message string | ||
if len(args) == 2 { | ||
message = value.Unwrap[*value.String](args[0]).Value | ||
} else { | ||
message = fmt.Sprintf("state should not be %s", expect) | ||
} | ||
|
||
return assert_not(state, expect.String(), i.TestingState.String(), message) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package function | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
"github.com/ysugimoto/falco/interpreter" | ||
"github.com/ysugimoto/falco/interpreter/context" | ||
"github.com/ysugimoto/falco/interpreter/function/errors" | ||
"github.com/ysugimoto/falco/interpreter/value" | ||
) | ||
|
||
func Test_Assert_not_state(t *testing.T) { | ||
|
||
tests := []struct { | ||
args []value.Value | ||
ip *interpreter.Interpreter | ||
err error | ||
expect *value.Boolean | ||
}{ | ||
{ | ||
args: []value.Value{ | ||
&value.Ident{Value: "error"}, | ||
}, | ||
ip: &interpreter.Interpreter{ | ||
TestingState: interpreter.ERROR, | ||
}, | ||
expect: &value.Boolean{Value: false}, | ||
err: &errors.AssertionError{}, | ||
}, | ||
{ | ||
args: []value.Value{ | ||
&value.Ident{Value: "error"}, | ||
}, | ||
ip: &interpreter.Interpreter{ | ||
TestingState: interpreter.LOOKUP, | ||
}, | ||
expect: &value.Boolean{Value: true}, | ||
}, | ||
} | ||
|
||
for i := range tests { | ||
_, err := Assert_not_state( | ||
&context.Context{}, | ||
tests[i].ip, | ||
tests[i].args..., | ||
) | ||
if diff := cmp.Diff( | ||
tests[i].err, | ||
err, | ||
cmpopts.IgnoreFields(errors.AssertionError{}, "Message", "Actual"), | ||
cmpopts.IgnoreFields(errors.TestingError{}, "Message"), | ||
); diff != "" { | ||
t.Errorf("Assert_not_state()[%d] error: diff=%s", i, diff) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters