Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdrag00nv2 committed Dec 1, 2023
1 parent d51c035 commit f0c20a7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 69 deletions.
16 changes: 11 additions & 5 deletions runtime/program_params_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/onflow/cadence/encoding/json"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/sema"
"github.com/onflow/cadence/runtime/tests/checker"
. "github.com/onflow/cadence/runtime/tests/utils"
)

Expand Down Expand Up @@ -323,7 +324,6 @@ func TestRuntimeScriptParameterTypeValidation(t *testing.T) {
assert.NoError(t, err)
})

// Since InclusiveRange isn't covariant.
t.Run("Invalid InclusiveRange<Integer>", func(t *testing.T) {
t.Parallel()

Expand All @@ -338,8 +338,11 @@ func TestRuntimeScriptParameterTypeValidation(t *testing.T) {
cadence.NewInclusiveRange(cadence.NewInt16(1), cadence.NewInt16(2), cadence.NewInt16(1)),
)

var entryPointErr *InvalidEntryPointArgumentError
require.ErrorAs(t, err, &entryPointErr)
var checkerError *sema.CheckerError
require.ErrorAs(t, err, &checkerError)

errs := checker.RequireCheckerErrors(t, checkerError, 1)
assert.IsType(t, &sema.InvalidTypeArgumentError{}, errs[0])
})

t.Run("Invalid InclusiveRange<Int16> with mixed value types", func(t *testing.T) {
Expand Down Expand Up @@ -374,8 +377,11 @@ func TestRuntimeScriptParameterTypeValidation(t *testing.T) {
cadence.NewInclusiveRange(cadence.NewInt16(1), cadence.NewUInt(2), cadence.NewUInt(1)),
)

var entryPointErr *InvalidEntryPointArgumentError
require.ErrorAs(t, err, &entryPointErr)
var checkerError *sema.CheckerError
require.ErrorAs(t, err, &checkerError)

errs := checker.RequireCheckerErrors(t, checkerError, 1)
assert.IsType(t, &sema.InvalidTypeArgumentError{}, errs[0])
})

t.Run("Capability", func(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions runtime/tests/checker/for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ func TestCheckForInclusiveRange(t *testing.T) {
baseValueActivation.DeclareValue(stdlib.InclusiveRangeConstructorFunction)

for _, typ := range sema.AllIntegerTypes {
// Only test leaf integer types
switch typ {
case sema.IntegerType, sema.SignedIntegerType:
continue
}

code := fmt.Sprintf(`
fun test() {
let start : %[1]s = 1
Expand Down
66 changes: 2 additions & 64 deletions runtime/tests/interpreter/dynamic_casting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1471,15 +1471,6 @@ func TestInterpretDynamicCastingInclusiveRange(t *testing.T) {

t.Parallel()

types := []sema.Type{
&sema.InclusiveRangeType{
MemberType: sema.IntType,
},
&sema.InclusiveRangeType{
MemberType: sema.IntegerType,
},
}

baseValueActivation := sema.NewVariableActivation(sema.BaseValueActivation)
baseValueActivation.DeclareValue(stdlib.InclusiveRangeConstructorFunction)

Expand All @@ -1498,66 +1489,13 @@ func TestInterpretDynamicCastingInclusiveRange(t *testing.T) {
for operation, returnsOptional := range dynamicCastingOperations {

t.Run(operation.Symbol(), func(t *testing.T) {

for _, fromType := range types {
for _, targetType := range types {

t.Run(fmt.Sprintf("valid: from %s to %s", fromType, targetType), func(t *testing.T) {

inter, err := parseCheckAndInterpretWithOptions(t,
fmt.Sprintf(
`
let x: InclusiveRange<Int> = InclusiveRange(10, 20)
let y: %[1]s = x
let z: %[2]s? = y %[3]s %[2]s
`,
fromType,
targetType,
operation.Symbol(),
),
options,
)
require.NoError(t, err)

expectedInclusiveRange := interpreter.NewInclusiveRangeValue(
inter,
interpreter.EmptyLocationRange,
interpreter.NewUnmeteredIntValueFromInt64(10),
interpreter.NewUnmeteredIntValueFromInt64(20),
interpreter.InclusiveRangeStaticType{
ElementType: interpreter.PrimitiveStaticTypeInt,
},
sema.NewInclusiveRangeType(nil, sema.IntType),
)

AssertValuesEqual(
t,
inter,
expectedInclusiveRange,
inter.Globals.Get("y").GetValue(),
)

AssertValuesEqual(
t,
inter,
interpreter.NewUnmeteredSomeValueNonCopying(
expectedInclusiveRange,
),
inter.Globals.Get("z").GetValue(),
)
})
}

// We cannot test for invalid casts for T since InclusiveRange<T> has a type bound Integer on T.
}

t.Run("invalid upcast", func(t *testing.T) {
t.Run("invalid cast", func(t *testing.T) {

inter, err := parseCheckAndInterpretWithOptions(t,
fmt.Sprintf(
`
fun test(): InclusiveRange<UInt256>? {
let x: InclusiveRange<Integer> = InclusiveRange(10, 20)
let x: InclusiveRange<Int> = InclusiveRange(10, 20)
return x %s InclusiveRange<UInt256>
}
`,
Expand Down

0 comments on commit f0c20a7

Please sign in to comment.