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

71 lines
1.6 KiB
Rust
Raw Normal View History

2024-11-05 21:33:56 +00:00
use dust_lang::*;
#[test]
fn constant() {
let source = "42";
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(0, 2)),
(Instruction::r#return(true), Span(2, 2))
],
2024-11-11 00:28:21 +00:00
vec![ValueOwned::Primitive(Primitive::Integer(42))],
2024-11-05 21:33:56 +00:00
vec![]
))
);
2024-11-11 00:28:21 +00:00
assert_eq!(run(source), Ok(Some(ValueOwned::integer(42))));
2024-11-05 21:33:56 +00:00
}
#[test]
fn empty() {
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::r#return(false), Span(0, 0))],
vec![],
vec![]
))
);
assert_eq!(run(source), Ok(None));
}
#[test]
fn parentheses_precedence() {
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::add(0, 0, 1)
.set_b_is_constant()
.set_c_is_constant(),
Span(3, 4)
),
(
*Instruction::multiply(1, 0, 2).set_c_is_constant(),
Span(8, 9)
),
(Instruction::r#return(true), Span(11, 11)),
],
2024-11-11 00:28:21 +00:00
vec![
ValueOwned::integer(1),
ValueOwned::integer(2),
ValueOwned::integer(3)
],
2024-11-05 21:33:56 +00:00
vec![]
))
);
2024-11-11 00:28:21 +00:00
assert_eq!(run(source), Ok(Some(ValueOwned::integer(9))));
2024-11-05 21:33:56 +00:00
}