116 lines
2.4 KiB
Rust
116 lines
2.4 KiB
Rust
use super::harness::*;
|
|
|
|
#[test]
|
|
fn define_and_use_word() {
|
|
expect_int(": double 2 * ; 5 double", 10);
|
|
}
|
|
|
|
#[test]
|
|
fn define_word_with_multiple_ops() {
|
|
expect_int(": triple dup dup + + ; 3 triple", 9);
|
|
}
|
|
|
|
#[test]
|
|
fn define_word_using_another_user_word() {
|
|
expect_int(": double 2 * ; : quad double double ; 3 quad", 12);
|
|
}
|
|
|
|
#[test]
|
|
fn redefine_word_overwrites() {
|
|
expect_int(": foo 10 ; : foo 20 ; foo", 20);
|
|
}
|
|
|
|
#[test]
|
|
fn word_with_param() {
|
|
let outputs = expect_outputs(": loud 0.9 gain ; \"kick\" s loud .", 1);
|
|
assert!(outputs[0].contains("gain/0.9"));
|
|
}
|
|
|
|
#[test]
|
|
fn word_available_across_evaluations() {
|
|
let f = forth();
|
|
let ctx = default_ctx();
|
|
f.evaluate(": hi 42 ;", &ctx).unwrap();
|
|
f.evaluate("hi", &ctx).unwrap();
|
|
assert_eq!(stack_int(&f), 42);
|
|
}
|
|
|
|
#[test]
|
|
fn word_defined_in_one_forth_available_in_same() {
|
|
let f = forth();
|
|
let ctx = default_ctx();
|
|
f.evaluate(": ten 10 ;", &ctx).unwrap();
|
|
f.clear_stack();
|
|
f.evaluate("ten ten +", &ctx).unwrap();
|
|
assert_eq!(stack_int(&f), 20);
|
|
}
|
|
|
|
#[test]
|
|
fn unknown_word_becomes_string() {
|
|
expect_str("nosuchword", "nosuchword");
|
|
}
|
|
|
|
#[test]
|
|
fn missing_semicolon_errors() {
|
|
expect_error(": foo 10", "missing ';'");
|
|
}
|
|
|
|
#[test]
|
|
fn missing_name_errors() {
|
|
expect_error(":", "expected word name");
|
|
}
|
|
|
|
#[test]
|
|
fn unexpected_semicolon_errors() {
|
|
expect_error(";", "unexpected ;");
|
|
}
|
|
|
|
#[test]
|
|
fn apply_executes_quotation() {
|
|
expect_int("5 { 2 * } apply", 10);
|
|
}
|
|
|
|
#[test]
|
|
fn apply_with_stack_ops() {
|
|
expect_int("3 4 { + } apply", 7);
|
|
}
|
|
|
|
#[test]
|
|
fn apply_empty_stack_errors() {
|
|
expect_error("apply", "stack underflow");
|
|
}
|
|
|
|
#[test]
|
|
fn apply_non_quotation_errors() {
|
|
expect_error("42 apply", "expected quotation");
|
|
}
|
|
|
|
#[test]
|
|
fn apply_nested() {
|
|
expect_int("2 { { 3 * } apply } apply", 6);
|
|
}
|
|
|
|
#[test]
|
|
fn define_word_containing_quotation() {
|
|
expect_int(": dbl { 2 * } apply ; 7 dbl", 14);
|
|
}
|
|
|
|
#[test]
|
|
fn define_word_with_sound() {
|
|
let outputs = expect_outputs(": kick \"kick\" s . ; kick", 1);
|
|
assert!(outputs[0].contains("sound/kick"));
|
|
}
|
|
|
|
#[test]
|
|
fn define_word_with_conditional() {
|
|
let f = forth();
|
|
let ctx = default_ctx();
|
|
f.evaluate(": maybe-double dup 5 gt if 2 * then ;", &ctx).unwrap();
|
|
f.clear_stack();
|
|
f.evaluate("3 maybe-double", &ctx).unwrap();
|
|
assert_eq!(stack_int(&f), 3);
|
|
f.clear_stack();
|
|
f.evaluate("10 maybe-double", &ctx).unwrap();
|
|
assert_eq!(stack_int(&f), 20);
|
|
}
|