diff --git a/runtime/tests/checker/builtinfunctions_test.go b/runtime/tests/checker/builtinfunctions_test.go index 3db3d4f320..1bff1ba334 100644 --- a/runtime/tests/checker/builtinfunctions_test.go +++ b/runtime/tests/checker/builtinfunctions_test.go @@ -280,18 +280,21 @@ 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) @@ -299,29 +302,29 @@ func TestCheckRevertibleRandom(t *testing.T) { 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 { @@ -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", "let rand = revertibleRandom()", options, []error{&sema.TypeMismatchError{}}) - runInvalidCase(t, "revertibleRandom", "let rand = revertibleRandom(modulo: \"abcd\")", options, []error{&sema.TypeMismatchError{}}) - runInvalidCase(t, "missing_argument_label", "let rand = revertibleRandom(UInt256(1))", options, []error{&sema.MissingArgumentLabelError{}}) - runInvalidCase(t, "incorrect_argument_label", "let rand = revertibleRandom(typo: UInt256(1))", options, []error{&sema.IncorrectArgumentLabelError{}}) - runInvalidCase(t, "too_many_args", "let rand = revertibleRandom(modulo: UInt256(1), 2, 3)", options, []error{&sema.ExcessiveArgumentsError{}}) - runInvalidCase(t, "modulo type mismatch", "let rand = revertibleRandom(modulo: UInt128(1))", options, []error{&sema.TypeParameterTypeMismatchError{}, &sema.TypeMismatchError{}}) - runInvalidCase(t, "string modulo", "let rand = revertibleRandom(modulo: \"abcd\")", options, []error{&sema.TypeParameterTypeMismatchError{}, &sema.TypeMismatchError{}}) + runInvalidCase( + t, + "revertibleRandom", + "let rand = revertibleRandom()", + []error{ + &sema.TypeMismatchError{}, + }, + ) + + runInvalidCase( + t, + "revertibleRandom", + `let rand = revertibleRandom(modulo: "abcd")`, + []error{ + &sema.TypeMismatchError{}, + }, + ) + + runInvalidCase( + t, + "missing_argument_label", + "let rand = revertibleRandom(UInt256(1))", + []error{ + &sema.MissingArgumentLabelError{}, + }, + ) + + runInvalidCase( + t, + "incorrect_argument_label", + "let rand = revertibleRandom(typo: UInt256(1))", + []error{ + &sema.IncorrectArgumentLabelError{}, + }, + ) + + runInvalidCase( + t, + "too_many_args", + "let rand = revertibleRandom(modulo: UInt256(1), 2, 3)", + []error{ + &sema.ExcessiveArgumentsError{}, + }, + ) + + runInvalidCase( + t, + "modulo type mismatch", + "let rand = revertibleRandom(modulo: UInt128(1))", + []error{ + &sema.TypeParameterTypeMismatchError{}, + &sema.TypeMismatchError{}, + }, + ) + + runInvalidCase( + t, + "string modulo", + `let rand = revertibleRandom(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(modulo: 1)", options, []error{&sema.TypeParameterTypeMismatchError{}}) + runInvalidCase( + t, + "missing type inference", + "let rand = revertibleRandom(modulo: 1)", + []error{ + &sema.TypeParameterTypeMismatchError{}, + }, + ) }