|
19 | 19 | package interpreter_test |
20 | 20 |
|
21 | 21 | import ( |
| 22 | + "fmt" |
22 | 23 | "testing" |
23 | | -) |
24 | 24 |
|
25 | | -func TestRangeDeclaration(t *testing.T) { |
| 25 | + "github.com/onflow/cadence/runtime/activations" |
| 26 | + "github.com/onflow/cadence/runtime/interpreter" |
| 27 | + "github.com/onflow/cadence/runtime/sema" |
| 28 | + "github.com/onflow/cadence/runtime/stdlib" |
| 29 | + "github.com/stretchr/testify/require" |
| 30 | +) |
26 | 31 |
|
| 32 | +func TestRange(t *testing.T) { |
27 | 33 | t.Parallel() |
28 | 34 |
|
29 | | - _ = parseCheckAndInterpret(t, ` |
30 | | - let a = 1 .. 10 |
31 | | - `) |
| 35 | + baseValueActivation := sema.NewVariableActivation(sema.BaseValueActivation) |
| 36 | + baseValueActivation.DeclareValue(stdlib.RangeConstructorFunction) |
| 37 | + |
| 38 | + baseActivation := activations.NewActivation(nil, interpreter.BaseActivation) |
| 39 | + interpreter.Declare(baseActivation, stdlib.RangeConstructorFunction) |
| 40 | + |
| 41 | + runValidCase := func(t *testing.T, memberType sema.Type, withStep bool) { |
| 42 | + t.Run(memberType.String(), func(t *testing.T) { |
| 43 | + t.Parallel() |
| 44 | + |
| 45 | + var code string |
| 46 | + if withStep { |
| 47 | + code = fmt.Sprintf( |
| 48 | + ` |
| 49 | + let s : %s = 10 |
| 50 | + let e : %s = 20 |
| 51 | + let step : %s = 2 |
| 52 | + let r = Range(s, e, step: step) |
| 53 | + `, |
| 54 | + memberType.String(), memberType.String(), memberType.String()) |
| 55 | + } else { |
| 56 | + code = fmt.Sprintf( |
| 57 | + ` |
| 58 | + let s : %s = 10 |
| 59 | + let e : %s = 20 |
| 60 | + let r = Range(s, e) |
| 61 | + `, |
| 62 | + memberType.String(), memberType.String()) |
| 63 | + } |
| 64 | + |
| 65 | + _, err := parseCheckAndInterpretWithOptions(t, code, |
| 66 | + ParseCheckAndInterpretOptions{ |
| 67 | + CheckerConfig: &sema.Config{ |
| 68 | + BaseValueActivation: baseValueActivation, |
| 69 | + }, |
| 70 | + Config: &interpreter.Config{ |
| 71 | + BaseActivation: baseActivation, |
| 72 | + }, |
| 73 | + }, |
| 74 | + ) |
| 75 | + |
| 76 | + require.NoError(t, err) |
| 77 | + }) |
| 78 | + } |
| 79 | + |
| 80 | + runValidCaseWithoutStep := func(t *testing.T, memberType sema.Type) { |
| 81 | + runValidCase(t, memberType, false) |
| 82 | + } |
| 83 | + runValidCaseWithStep := func(t *testing.T, memberType sema.Type) { |
| 84 | + runValidCase(t, memberType, true) |
| 85 | + } |
| 86 | + |
| 87 | + for _, integerType := range sema.AllIntegerTypes { |
| 88 | + switch integerType { |
| 89 | + case sema.IntegerType, sema.SignedIntegerType: |
| 90 | + continue |
| 91 | + } |
| 92 | + |
| 93 | + runValidCaseWithStep(t, integerType) |
| 94 | + runValidCaseWithoutStep(t, integerType) |
| 95 | + } |
32 | 96 | } |
0 commit comments