Skip to content

Commit 91899b6

Browse files
committed
tuple for tests
1 parent 1ee453a commit 91899b6

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

tests/parser_tests.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,45 @@ mod expression_tests {
109109
assert_eq!(rest, "");
110110
assert_eq!(result, expected);
111111
}
112+
113+
#[test]
114+
fn test_tuple_literals() {
115+
let cases = vec![
116+
(
117+
"(1, 2, 3)",
118+
Expression::Tuple(vec![
119+
Expression::CInt(1),
120+
Expression::CInt(2),
121+
Expression::CInt(3),
122+
]),
123+
),
124+
(
125+
"(\"hello\", True)",
126+
Expression::Tuple(vec![
127+
Expression::CString("hello".to_string()),
128+
Expression::CTrue,
129+
]),
130+
),
131+
("()", Expression::Tuple(vec![])),
132+
(
133+
"(42,)",
134+
Expression::Tuple(vec![Expression::CInt(42)]),
135+
),
136+
(
137+
"((1, 2), (3, 4))",
138+
Expression::Tuple(vec![
139+
Expression::Tuple(vec![Expression::CInt(1), Expression::CInt(2)]),
140+
Expression::Tuple(vec![Expression::CInt(3), Expression::CInt(4)]),
141+
]),
142+
),
143+
];
144+
145+
for (input, expected) in cases {
146+
let (rest, result) = parse_expression(input).unwrap();
147+
assert_eq!(rest, "");
148+
assert_eq!(result, expected);
149+
}
150+
}
112151
}
113152

114153
// Statement Tests
@@ -204,6 +243,75 @@ mod statement_tests {
204243
assert_eq!(result, expected);
205244
}
206245

246+
#[test]
247+
fn test_for_over_tuple() {
248+
let cases = vec![
249+
(
250+
"for x in (1, 2, 3): x = x + 1; end",
251+
Statement::For(
252+
"x".to_string(),
253+
Box::new(Expression::Tuple(vec![
254+
Expression::CInt(1),
255+
Expression::CInt(2),
256+
Expression::CInt(3),
257+
])),
258+
Box::new(Statement::Block(vec![Statement::Assignment(
259+
"x".to_string(),
260+
Box::new(Expression::Add(
261+
Box::new(Expression::Var("x".to_string())),
262+
Box::new(Expression::CInt(1)),
263+
)),
264+
)])),
265+
),
266+
),
267+
(
268+
"for item in (\"a\", \"b\"): val x = item; end",
269+
Statement::For(
270+
"item".to_string(),
271+
Box::new(Expression::Tuple(vec![
272+
Expression::CString("a".to_string()),
273+
Expression::CString("b".to_string()),
274+
])),
275+
Box::new(Statement::Block(vec![Statement::ValDeclaration(
276+
"x".to_string(),
277+
Box::new(Expression::Var("item".to_string())),
278+
)])),
279+
),
280+
),
281+
(
282+
"for x in (): val y = 1; end",
283+
Statement::For(
284+
"x".to_string(),
285+
Box::new(Expression::Tuple(vec![])),
286+
Box::new(Statement::Block(vec![Statement::ValDeclaration(
287+
"y".to_string(),
288+
Box::new(Expression::CInt(1)),
289+
)])),
290+
),
291+
),
292+
(
293+
"for t in ((1,2), (3,4)): val x = t; end",
294+
Statement::For(
295+
"t".to_string(),
296+
Box::new(Expression::Tuple(vec![
297+
Expression::Tuple(vec![Expression::CInt(1), Expression::CInt(2)]),
298+
Expression::Tuple(vec![Expression::CInt(3), Expression::CInt(4)]),
299+
])),
300+
Box::new(Statement::Block(vec![Statement::ValDeclaration(
301+
"x".to_string(),
302+
Box::new(Expression::Var("t".to_string())),
303+
)])),
304+
),
305+
),
306+
];
307+
308+
for (input, expected) in cases {
309+
let (rest, result) = parse_statement(input).unwrap();
310+
assert_eq!(rest, "");
311+
assert_eq!(result, expected);
312+
}
313+
}
314+
207315
#[test]
208316
#[ignore]
209317
fn test_function_definitions() {

0 commit comments

Comments
 (0)