1
0
dust/dust-lang/tests/lists.rs

128 lines
3.4 KiB
Rust
Raw Normal View History

2024-11-05 21:33:56 +00:00
use dust_lang::*;
#[test]
fn empty_list() {
let source = "[]";
assert_eq!(
2024-11-06 20:40:37 +00:00
compile(source),
2024-11-05 21:33:56 +00:00
Ok(Chunk::with_data(
None,
vec![
(Instruction::load_list(0, 0), Span(0, 2)),
2024-11-05 21:33:56 +00:00
(Instruction::r#return(true), Span(2, 2)),
],
vec![],
vec![]
)),
);
assert_eq!(run(source), Ok(Some(Value::abstract_list(0, 0, Type::Any))));
2024-11-05 21:33:56 +00:00
}
#[test]
fn list() {
let source = "[1, 2, 3]";
assert_eq!(
2024-11-06 20:40:37 +00:00
compile(source),
2024-11-05 21:33:56 +00:00
Ok(Chunk::with_data(
None,
vec![
(Instruction::load_constant(0, 0, false), Span(1, 2)),
(Instruction::load_constant(1, 1, false), Span(4, 5)),
(Instruction::load_constant(2, 2, false), Span(7, 8)),
(Instruction::load_list(3, 0), Span(0, 9)),
2024-11-05 21:33:56 +00:00
(Instruction::r#return(true), Span(9, 9)),
],
vec![Value::integer(1), Value::integer(2), Value::integer(3)],
2024-11-05 21:33:56 +00:00
vec![]
)),
);
assert_eq!(
run(source),
Ok(Some(Value::abstract_list(0, 3, Type::Integer)))
);
2024-11-05 21:33:56 +00:00
}
#[test]
fn list_with_complex_expression() {
let source = "[1, 2 + 3 - 4 * 5]";
assert_eq!(
2024-11-06 20:40:37 +00:00
compile(source),
2024-11-05 21:33:56 +00:00
Ok(Chunk::with_data(
None,
vec![
(Instruction::load_constant(0, 0, false), Span(1, 2)),
(
*Instruction::add(1, 1, 2)
.set_b_is_constant()
.set_c_is_constant(),
Span(6, 7)
),
(
*Instruction::multiply(2, 3, 4)
.set_b_is_constant()
.set_c_is_constant(),
Span(14, 15)
),
(Instruction::subtract(3, 1, 2), Span(10, 11)),
(Instruction::close(1, 3), Span(17, 18)),
(Instruction::load_list(4, 0), Span(0, 18)),
2024-11-05 21:33:56 +00:00
(Instruction::r#return(true), Span(18, 18)),
],
vec![
Value::integer(1),
Value::integer(2),
Value::integer(3),
Value::integer(4),
Value::integer(5)
],
vec![]
)),
);
assert_eq!(
run(source),
Ok(Some(Value::abstract_list(0, 4, Type::Integer)))
);
2024-11-05 21:33:56 +00:00
}
#[test]
fn list_with_simple_expression() {
let source = "[1, 2 + 3, 4]";
assert_eq!(
2024-11-06 20:40:37 +00:00
compile(source),
2024-11-05 21:33:56 +00:00
Ok(Chunk::with_data(
None,
vec![
(Instruction::load_constant(0, 0, false), Span(1, 2)),
(
*Instruction::add(1, 1, 2)
.set_b_is_constant()
.set_c_is_constant(),
Span(6, 7)
),
(Instruction::load_constant(2, 3, false), Span(11, 12)),
(Instruction::load_list(3, 0), Span(0, 13)),
2024-11-05 21:33:56 +00:00
(Instruction::r#return(true), Span(13, 13)),
],
vec![
Value::integer(1),
Value::integer(2),
Value::integer(3),
Value::integer(4),
],
vec![]
)),
);
assert_eq!(
run(source),
Ok(Some(Value::abstract_list(0, 3, Type::Integer)))
);
2024-11-05 21:33:56 +00:00
}