Rewrite tests for consistency
This commit is contained in:
parent
5015cf4cc4
commit
9b1dc6c55c
@ -1,9 +1,10 @@
|
|||||||
use dust_lang::*;
|
use dust_lang::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn add() {
|
fn add() {
|
||||||
|
let source = "1 + 2";
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse("1 + 2"),
|
parse(source),
|
||||||
Ok(Chunk::with_data(
|
Ok(Chunk::with_data(
|
||||||
vec![
|
vec![
|
||||||
(
|
(
|
||||||
@ -19,13 +20,15 @@ fn add() {
|
|||||||
))
|
))
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(run("1 + 2"), Ok(Some(Value::integer(3))));
|
assert_eq!(run(source), Ok(Some(Value::integer(3))));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn and() {
|
fn and() {
|
||||||
|
let source = "true && false";
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse("true && false"),
|
parse(source),
|
||||||
Ok(Chunk::with_data(
|
Ok(Chunk::with_data(
|
||||||
vec![
|
vec![
|
||||||
(Instruction::load_boolean(0, true, false), Span(0, 4)),
|
(Instruction::load_boolean(0, true, false), Span(0, 4)),
|
||||||
@ -39,7 +42,7 @@ fn and() {
|
|||||||
))
|
))
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(run("true && false"), Ok(Some(Value::boolean(false))));
|
assert_eq!(run(source), Ok(Some(Value::boolean(false))));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -93,8 +96,9 @@ fn block_scope() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn constant() {
|
fn constant() {
|
||||||
|
let source = "42";
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse("42"),
|
parse(source),
|
||||||
Ok(Chunk::with_data(
|
Ok(Chunk::with_data(
|
||||||
vec![
|
vec![
|
||||||
(Instruction::load_constant(0, 0, false), Span(0, 2)),
|
(Instruction::load_constant(0, 0, false), Span(0, 2)),
|
||||||
@ -105,13 +109,14 @@ fn constant() {
|
|||||||
))
|
))
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(run("42"), Ok(Some(Value::integer(42))));
|
assert_eq!(run(source), Ok(Some(Value::integer(42))));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn define_local() {
|
fn define_local() {
|
||||||
|
let source = "let x = 42;";
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse("let x = 42;"),
|
parse(source),
|
||||||
Ok(Chunk::with_data(
|
Ok(Chunk::with_data(
|
||||||
vec![
|
vec![
|
||||||
(Instruction::load_constant(0, 0, false), Span(8, 10)),
|
(Instruction::load_constant(0, 0, false), Span(8, 10)),
|
||||||
@ -122,13 +127,14 @@ fn define_local() {
|
|||||||
)),
|
)),
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(run("let x = 42;"), Ok(None));
|
assert_eq!(run(source), Ok(None));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn divide() {
|
fn divide() {
|
||||||
|
let source = "2 / 2";
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse("2 / 2"),
|
parse(source),
|
||||||
Ok(Chunk::with_data(
|
Ok(Chunk::with_data(
|
||||||
vec![
|
vec![
|
||||||
(
|
(
|
||||||
@ -144,13 +150,14 @@ fn divide() {
|
|||||||
))
|
))
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(run("2 / 2"), Ok(Some(Value::integer(1))));
|
assert_eq!(run(source), Ok(Some(Value::integer(1))));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn empty() {
|
fn empty() {
|
||||||
assert_eq!(parse(""), Ok(Chunk::with_data(vec![], vec![], vec![])),);
|
let source = "";
|
||||||
assert_eq!(run(""), Ok(None));
|
assert_eq!(parse(source), Ok(Chunk::with_data(vec![], vec![], vec![])),);
|
||||||
|
assert_eq!(run(source), Ok(None));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -506,8 +513,9 @@ fn list_with_simple_expression() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn math_operator_precedence() {
|
fn math_operator_precedence() {
|
||||||
|
let source = "1 + 2 - 3 * 4 / 5";
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse("1 + 2 - 3 * 4 / 5"),
|
parse(source),
|
||||||
Ok(Chunk::with_data(
|
Ok(Chunk::with_data(
|
||||||
vec![
|
vec![
|
||||||
(
|
(
|
||||||
@ -539,13 +547,14 @@ fn math_operator_precedence() {
|
|||||||
))
|
))
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(run("1 + 2 - 3 * 4 / 5"), Ok(Some(Value::integer(1))));
|
assert_eq!(run(source), Ok(Some(Value::integer(1))));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn multiply() {
|
fn multiply() {
|
||||||
|
let source = "1 * 2";
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse("1 * 2"),
|
parse(source),
|
||||||
Ok(Chunk::with_data(
|
Ok(Chunk::with_data(
|
||||||
vec![(
|
vec![(
|
||||||
*Instruction::multiply(0, 0, 1)
|
*Instruction::multiply(0, 0, 1)
|
||||||
@ -558,7 +567,7 @@ fn multiply() {
|
|||||||
))
|
))
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(run("1 * 2"), Ok(Some(Value::integer(2))));
|
assert_eq!(run(source), Ok(Some(Value::integer(2))));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -624,8 +633,9 @@ fn not_equal() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn or() {
|
fn or() {
|
||||||
|
let source = "true || false";
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse("true || false"),
|
parse(source),
|
||||||
Ok(Chunk::with_data(
|
Ok(Chunk::with_data(
|
||||||
vec![
|
vec![
|
||||||
(Instruction::load_boolean(0, true, false), Span(0, 4)),
|
(Instruction::load_boolean(0, true, false), Span(0, 4)),
|
||||||
@ -638,13 +648,14 @@ fn or() {
|
|||||||
))
|
))
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(run("true || false"), Ok(Some(Value::boolean(true))));
|
assert_eq!(run(source), Ok(Some(Value::boolean(true))));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parentheses_precedence() {
|
fn parentheses_precedence() {
|
||||||
|
let source = "(1 + 2) * 3";
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse("(1 + 2) * 3"),
|
parse(source),
|
||||||
Ok(Chunk::with_data(
|
Ok(Chunk::with_data(
|
||||||
vec![
|
vec![
|
||||||
(
|
(
|
||||||
@ -663,13 +674,14 @@ fn parentheses_precedence() {
|
|||||||
))
|
))
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(run("(1 + 2) * 3"), Ok(Some(Value::integer(9))));
|
assert_eq!(run(source), Ok(Some(Value::integer(9))));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn set_local() {
|
fn set_local() {
|
||||||
|
let source = "let mut x = 41; x = 42;";
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse("let mut x = 41; x = 42;"),
|
parse(source),
|
||||||
Ok(Chunk::with_data(
|
Ok(Chunk::with_data(
|
||||||
vec![
|
vec![
|
||||||
(Instruction::load_constant(0, 0, false), Span(12, 14)),
|
(Instruction::load_constant(0, 0, false), Span(12, 14)),
|
||||||
@ -682,13 +694,14 @@ fn set_local() {
|
|||||||
)),
|
)),
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(run("let mut x = 41; x = 42;"), Ok(Some(Value::integer(42))));
|
assert_eq!(run(source), Ok(Some(Value::integer(42))));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn subtract() {
|
fn subtract() {
|
||||||
|
let source = "1 - 2";
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse("1 - 2"),
|
parse(source),
|
||||||
Ok(Chunk::with_data(
|
Ok(Chunk::with_data(
|
||||||
vec![(
|
vec![(
|
||||||
*Instruction::subtract(0, 0, 1)
|
*Instruction::subtract(0, 0, 1)
|
||||||
@ -701,13 +714,14 @@ fn subtract() {
|
|||||||
))
|
))
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(run("1 - 2"), Ok(Some(Value::integer(-1))));
|
assert_eq!(run(source), Ok(Some(Value::integer(-1))));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn variable_and() {
|
fn variable_and() {
|
||||||
|
let source = "let a = true; let b = false; a && b";
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse("let a = true; let b = false; a && b"),
|
parse(source),
|
||||||
Ok(Chunk::with_data(
|
Ok(Chunk::with_data(
|
||||||
vec![
|
vec![
|
||||||
(Instruction::load_boolean(0, true, false), Span(8, 12)),
|
(Instruction::load_boolean(0, true, false), Span(8, 12)),
|
||||||
@ -727,10 +741,7 @@ fn variable_and() {
|
|||||||
))
|
))
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(run(source), Ok(Some(Value::boolean(false))));
|
||||||
run("let a = true; let b = false; a && b"),
|
|
||||||
Ok(Some(Value::boolean(false)))
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
Reference in New Issue
Block a user