Skip to content

Commit

Permalink
fix and improve test
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent authored and darkdrag00nv2 committed Dec 11, 2023
1 parent 9c6d4fc commit 4c928dd
Showing 1 changed file with 94 additions and 27 deletions.
121 changes: 94 additions & 27 deletions runtime/tests/checker/builtinfunctions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,48 +280,51 @@ func TestCheckRevertibleRandom(t *testing.T) {

t.Parallel()

baseValueActivation := sema.NewVariableActivation(sema.BaseValueActivation)
baseValueActivation.DeclareValue(stdlib.NewRevertibleRandomFunction(&testRandomGenerator{}))
options := ParseAndCheckOptions{
Config: &sema.Config{
BaseValueActivationHandler: func(_ common.Location) *sema.VariableActivation {
return baseValueActivation
newOptions := func() ParseAndCheckOptions {
baseValueActivation := sema.NewVariableActivation(sema.BaseValueActivation)
baseValueActivation.DeclareValue(stdlib.NewRevertibleRandomFunction(&testRandomGenerator{}))
return ParseAndCheckOptions{
Config: &sema.Config{
BaseValueActivationHandler: func(_ common.Location) *sema.VariableActivation {
return baseValueActivation
},
},
},
}
}

runCase := func(t *testing.T, ty sema.Type, code string, options ParseAndCheckOptions) {
checker, err := ParseAndCheckWithOptions(t, code, options)
runCase := func(t *testing.T, ty sema.Type, code string) {

checker, err := ParseAndCheckWithOptions(t, code, newOptions())

require.NoError(t, err)

resType := RequireGlobalValue(t, checker.Elaboration, "rand")
require.Equal(t, ty, resType)
}

runValidCaseWithoutModulo := func(t *testing.T, ty sema.Type, options ParseAndCheckOptions) {
t.Run(fmt.Sprintf("revertibleRandom<%s>_no_modulo", ty), func(t *testing.T) {
runValidCaseWithoutModulo := func(t *testing.T, ty sema.Type) {
t.Run(fmt.Sprintf("revertibleRandom<%s>, no modulo", ty), func(t *testing.T) {
t.Parallel()

code := fmt.Sprintf("let rand = revertibleRandom<%s>()", ty)
runCase(t, ty, code, options)
runCase(t, ty, code)
})
}

runValidCaseWithModulo := func(t *testing.T, ty sema.Type, options ParseAndCheckOptions) {
t.Run(fmt.Sprintf("revertibleRandom<%s>_modulo", ty), func(t *testing.T) {
runValidCaseWithModulo := func(t *testing.T, ty sema.Type) {
t.Run(fmt.Sprintf("revertibleRandom<%s>, modulo", ty), func(t *testing.T) {
t.Parallel()

code := fmt.Sprintf("let rand = revertibleRandom<%[1]s>(modulo: %[1]s(1))", ty)
runCase(t, ty, code, options)
runCase(t, ty, code)
})
}

runInvalidCase := func(t *testing.T, testName string, code string, options ParseAndCheckOptions, expectedErrors []error) {
runInvalidCase := func(t *testing.T, testName string, code string, expectedErrors []error) {
t.Run(testName, func(t *testing.T) {
t.Parallel()

_, err := ParseAndCheckWithOptions(t, code, options)
_, err := ParseAndCheckWithOptions(t, code, newOptions())

errs := RequireCheckerErrors(t, err, len(expectedErrors))
for i := range expectedErrors {
Expand All @@ -336,19 +339,83 @@ func TestCheckRevertibleRandom(t *testing.T) {
continue

default:
runValidCaseWithoutModulo(t, ty, options)
runValidCaseWithModulo(t, ty, options)
runValidCaseWithoutModulo(t, ty)
runValidCaseWithModulo(t, ty)
}
}

runInvalidCase(t, "revertibleRandom<Int>", "let rand = revertibleRandom<Int>()", options, []error{&sema.TypeMismatchError{}})
runInvalidCase(t, "revertibleRandom<String>", "let rand = revertibleRandom<String>(modulo: \"abcd\")", options, []error{&sema.TypeMismatchError{}})
runInvalidCase(t, "missing_argument_label", "let rand = revertibleRandom<UInt256>(UInt256(1))", options, []error{&sema.MissingArgumentLabelError{}})
runInvalidCase(t, "incorrect_argument_label", "let rand = revertibleRandom<UInt256>(typo: UInt256(1))", options, []error{&sema.IncorrectArgumentLabelError{}})
runInvalidCase(t, "too_many_args", "let rand = revertibleRandom<UInt256>(modulo: UInt256(1), 2, 3)", options, []error{&sema.ExcessiveArgumentsError{}})
runInvalidCase(t, "modulo type mismatch", "let rand = revertibleRandom<UInt256>(modulo: UInt128(1))", options, []error{&sema.TypeParameterTypeMismatchError{}, &sema.TypeMismatchError{}})
runInvalidCase(t, "string modulo", "let rand = revertibleRandom<UInt256>(modulo: \"abcd\")", options, []error{&sema.TypeParameterTypeMismatchError{}, &sema.TypeMismatchError{}})
runInvalidCase(
t,
"revertibleRandom<Int>",
"let rand = revertibleRandom<Int>()",
[]error{
&sema.TypeMismatchError{},
},
)

runInvalidCase(
t,
"revertibleRandom<String>",
`let rand = revertibleRandom<String>(modulo: "abcd")`,
[]error{
&sema.TypeMismatchError{},
},
)

runInvalidCase(
t,
"missing_argument_label",
"let rand = revertibleRandom<UInt256>(UInt256(1))",
[]error{
&sema.MissingArgumentLabelError{},
},
)

runInvalidCase(
t,
"incorrect_argument_label",
"let rand = revertibleRandom<UInt256>(typo: UInt256(1))",
[]error{
&sema.IncorrectArgumentLabelError{},
},
)

runInvalidCase(
t,
"too_many_args",
"let rand = revertibleRandom<UInt256>(modulo: UInt256(1), 2, 3)",
[]error{
&sema.ExcessiveArgumentsError{},
},
)

runInvalidCase(
t,
"modulo type mismatch",
"let rand = revertibleRandom<UInt256>(modulo: UInt128(1))",
[]error{
&sema.TypeParameterTypeMismatchError{},
&sema.TypeMismatchError{},
},
)

runInvalidCase(
t,
"string modulo",
`let rand = revertibleRandom<UInt256>(modulo: "abcd")`,
[]error{
&sema.TypeParameterTypeMismatchError{},
&sema.TypeMismatchError{},
},
)

// This is an error since we do not support type inference of function arguments.
runInvalidCase(t, "missing_typeinference", "let rand = revertibleRandom<UInt256>(modulo: 1)", options, []error{&sema.TypeParameterTypeMismatchError{}})
runInvalidCase(
t,
"missing type inference",
"let rand = revertibleRandom<UInt256>(modulo: 1)",
[]error{
&sema.TypeParameterTypeMismatchError{},
},
)
}

0 comments on commit 4c928dd

Please sign in to comment.