-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
arrays.re
147 lines (144 loc) 路 3.96 KB
/
arrays.re
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
open Grain_tests.TestFramework;
open Grain_tests.Runner;
describe("arrays", ({test, testSkip}) => {
let test_or_skip =
Sys.backend_type == Other("js_of_ocaml") ? testSkip : test;
let assertSnapshot = makeSnapshotRunner(test);
let assertCompileError = makeCompileErrorRunner(test);
let assertRun = makeRunner(test_or_skip);
let assertRunError = makeErrorRunner(test_or_skip);
let assertParse = makeParseRunner(test);
assertRun("array1", "print([> 1, 2, 3])", "[> 1, 2, 3]\n");
assertRun("array2", "print([>])", "[> ]\n");
assertSnapshot("array3", "[>\n1, 2, 3]");
assertCompileError("array_error", "[> 1, false, 2]", "has type Bool but");
assertSnapshot("array_access", "let x = [> 1, 2, 3]; x[0]");
assertSnapshot("array_access2", "let x = [> 1, 2, 3]; x[1]");
assertSnapshot("array_access3", "let x = [> 1, 2, 3]; x[2]");
assertSnapshot("array_access4", "let x = [> 1, 2, 3]; x[-2]");
assertSnapshot("array_access5", "let x = [> 1, 2, 3]; x[-3]");
assertSnapshot("array_access5", "[> 1, 2, 3][-3]");
assertRunError(
"array_access_err",
"let x = [> 1, 2, 3]; x[3]",
"Index out of bounds",
);
assertRunError(
"array_access_err2",
"let x = [> 1, 2, 3]; x[-4]",
"Index out of bounds",
);
assertRunError(
"array_access_err3",
"let x = [> 1, 2, 3]; x[99]",
"Index out of bounds",
);
assertRunError(
"array_access_err4",
"let x = [> 1, 2, 3]; x[-99]",
"Index out of bounds",
);
assertRunError(
"array_access_err5",
"let x = [> 1, 2, 3]; x[1.5]",
"Index not an integer",
);
assertRunError(
"array_access_err6",
"let x = [> 1, 2, 3]; x[1/3]",
"Index not an integer",
);
assertRunError(
"array_access_err7",
"let x = [> 1, 2, 3]; x[987654321987654321]",
"Index out of bounds",
);
assertCompileError(
"array_access_err8",
"let x = [> 1, 2, 3]; x[false]",
"has type Bool but",
);
assertRun(
"array_set",
"let x = [> 1, 2, 3]; x[0] = 4; print(x)",
"[> 4, 2, 3]\n",
);
assertRun(
"array_set2",
"let x = [> 1, 2, 3]; x[-2] = 4; print(x)",
"[> 1, 4, 3]\n",
);
assertCompileError(
"array_set_err",
"let x = [> 1, 2, 3]; x[-2] = false",
"has type Bool but",
);
assertRunError(
"array_set_err2",
"let x = [> 1, 2, 3]; x[-12] = 4",
"Index out of bounds",
);
assertRunError(
"array_set_err3",
"let x = [> 1, 2, 3]; x[1.5] = 4",
"Index not an integer",
);
assertRunError(
"array_set_err4",
"let x = [> 1, 2, 3]; x[1/3] = 4",
"Index not an integer",
);
assertRunError(
"array_set_err5",
"let x = [> 1, 2, 3]; x[987654321987654321] = 4",
"Index out of bounds",
);
assertCompileError(
"array_type",
"let x = [> true, false, false]; x[1] + 3",
"has type Bool but",
);
assertCompileError(
"array_type2",
"let x = [> true, false, false]; (x[1] = true) + 3",
"has type Void but",
);
// trailing commas
assertSnapshot("array1_trailing", "[> 1, 2, 3,]");
assertSnapshot("array1_trailing_space", "[> 1, 2, 3, ]");
assertCompileError(
"invalid_empty_trailing",
"[> ,]",
"Error: Syntax error",
);
// parsing
Grain_parsing.(
Ast_helper.(
assertParse(
"issue_925_parse_array_set_newline",
"module Test
state[0] =
5",
{
attributes: Grain_tests.Test_utils.default_module_attributes,
module_name: Location.mknoloc("Test"),
statements: [
Toplevel.expr(
Expression.array_set(
Expression.ident(
Location.mknoloc(
Identifier.IdentName(Location.mknoloc("state")),
),
),
Expression.constant(Constant.number(PConstNumberInt("0"))),
Expression.constant(Constant.number(PConstNumberInt("5"))),
),
),
],
comments: [],
prog_loc: Location.dummy_loc,
},
)
)
);
});