2024-09-12 04:39:31 +00:00
|
|
|
use dust_lang::*;
|
|
|
|
|
2024-09-25 13:55:10 +00:00
|
|
|
#[test]
|
|
|
|
fn r#while() {
|
|
|
|
assert_eq!(
|
|
|
|
run("let mut x = 0; while x < 5 { x = x + 1 } x"),
|
|
|
|
Ok(Some(Value::integer(5)))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-09-24 04:24:09 +00:00
|
|
|
#[test]
|
2024-09-24 20:49:17 +00:00
|
|
|
fn if_else() {
|
|
|
|
assert_eq!(run("if true { 1 } else { 2 }"), Ok(Some(Value::integer(1))));
|
|
|
|
assert_eq!(
|
|
|
|
run("if false { 1 } else { 2 }"),
|
|
|
|
Ok(Some(Value::integer(2)))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn r#if() {
|
2024-09-24 04:24:09 +00:00
|
|
|
assert_eq!(run("if true { 1 }"), Ok(Some(Value::integer(1))));
|
2024-09-24 05:30:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
run("if 42 == 42 { 1 } else { 2 }"),
|
2024-09-24 12:29:33 +00:00
|
|
|
Ok(Some(Value::integer(1)))
|
2024-09-24 05:30:21 +00:00
|
|
|
);
|
2024-09-24 04:24:09 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 23:16:15 +00:00
|
|
|
#[test]
|
|
|
|
fn less_than() {
|
|
|
|
assert_eq!(run("1 < 2"), Ok(Some(Value::boolean(true))));
|
2024-09-25 00:32:52 +00:00
|
|
|
assert_eq!(run("2 < 1"), Ok(Some(Value::boolean(false))));
|
2024-09-23 23:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn greater_than() {
|
|
|
|
assert_eq!(run("1 > 2"), Ok(Some(Value::boolean(false))));
|
2024-09-25 00:32:52 +00:00
|
|
|
assert_eq!(run("2 > 1"), Ok(Some(Value::boolean(true))));
|
2024-09-23 23:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn less_than_or_equal() {
|
|
|
|
assert_eq!(run("1 <= 2"), Ok(Some(Value::boolean(true))));
|
|
|
|
assert_eq!(run("1 <= 1"), Ok(Some(Value::boolean(true))));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn greater_than_or_equal() {
|
|
|
|
assert_eq!(run("1 >= 2"), Ok(Some(Value::boolean(false))));
|
|
|
|
assert_eq!(run("1 >= 1"), Ok(Some(Value::boolean(true))));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn equal() {
|
|
|
|
assert_eq!(run("1 == 1"), Ok(Some(Value::boolean(true))));
|
2024-09-25 00:32:52 +00:00
|
|
|
assert_eq!(run("1 == 2"), Ok(Some(Value::boolean(false))));
|
2024-09-23 23:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn not_equal() {
|
|
|
|
assert_eq!(run("1 != 1"), Ok(Some(Value::boolean(false))));
|
2024-09-25 00:32:52 +00:00
|
|
|
assert_eq!(run("2 != 1"), Ok(Some(Value::boolean(true))));
|
2024-09-23 23:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn and() {
|
|
|
|
assert_eq!(run("true && true"), Ok(Some(Value::boolean(true))));
|
|
|
|
assert_eq!(run("true && false"), Ok(Some(Value::boolean(false))));
|
|
|
|
assert_eq!(run("false && true"), Ok(Some(Value::boolean(false))));
|
|
|
|
assert_eq!(run("false && false"), Ok(Some(Value::boolean(false))));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn or() {
|
|
|
|
assert_eq!(run("true || true"), Ok(Some(Value::boolean(true))));
|
|
|
|
assert_eq!(run("true || false"), Ok(Some(Value::boolean(true))));
|
|
|
|
assert_eq!(run("false || true"), Ok(Some(Value::boolean(true))));
|
|
|
|
assert_eq!(run("false || false"), Ok(Some(Value::boolean(false))));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn not() {
|
|
|
|
assert_eq!(run("!true"), Ok(Some(Value::boolean(false))));
|
|
|
|
assert_eq!(run("!false"), Ok(Some(Value::boolean(true))));
|
|
|
|
}
|
|
|
|
|
2024-09-17 21:51:39 +00:00
|
|
|
#[test]
|
|
|
|
fn long_math() {
|
2024-09-19 22:07:11 +00:00
|
|
|
assert_eq!(
|
|
|
|
run("1 + 2 * 3 - 4 / 2"),
|
2024-09-22 16:57:58 +00:00
|
|
|
Ok(Some(Value::integer(5).into_reference()))
|
2024-09-19 22:07:11 +00:00
|
|
|
);
|
2024-09-17 21:51:39 +00:00
|
|
|
}
|
|
|
|
|
2024-09-12 04:39:31 +00:00
|
|
|
#[test]
|
|
|
|
fn add() {
|
|
|
|
assert_eq!(run("1 + 2"), Ok(Some(Value::integer(3))));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn subtract() {
|
|
|
|
assert_eq!(run("1 - 2"), Ok(Some(Value::integer(-1))));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiply() {
|
|
|
|
assert_eq!(run("2 * 3"), Ok(Some(Value::integer(6))));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn divide() {
|
|
|
|
assert_eq!(run("6 / 3"), Ok(Some(Value::integer(2))));
|
|
|
|
}
|