#![allow(clippy::expect_used, clippy::unwrap_used, clippy::panic)]
use std::sync::Arc;
use brink_runtime::{DotNetRng, Step, Story};
fn play(source: &str) -> Vec<String> {
let output = brink_compiler::compile("story.ink", |_| Ok(source.to_owned()));
assert!(
output.is_ok(),
"compile failed: {:?}\n{source}",
output.as_ref().err()
);
let output = output.expect("just asserted above");
let (program, line_tables) = brink_runtime::link(&output.data).expect("link");
let mut story = Story::<DotNetRng>::new(Arc::new(program), line_tables);
let mut lines = Vec::new();
for _ in 0..200 {
match story.continue_single().expect("runtime") {
Step::Line(l) => lines.push(l.text.clone()),
Step::Choices(_) => panic!("unexpected choices in {source}"),
Step::Done | Step::End | Step::Suspended => return lines,
}
}
panic!("story did not reach a terminal step in 200 steps");
}
const PRELUDE: &str = "LIST l = li\nVAR e = ()\n-> k\n\n=== k ===\n";
#[test]
fn a_functions_empty_output_is_trimmed() {
let f = "\n=== function f() ===\n{l}\n";
assert_eq!(
play(&format!("{PRELUDE}a\n~ f()\nb\n-> END\n{f}")),
["a\n", "b\n"]
);
assert_eq!(play(&format!("{PRELUDE}a\n~ f()\n-> END\n{f}")), ["a\n"]);
assert_eq!(
play(&format!("{PRELUDE}~ f()\n-> END\n{f}")),
Vec::<String>::new()
);
}
#[test]
fn every_whitespace_rendering_value_is_trimmed() {
for body in ["{e}", "{\"\"}", "{\" \"}", "{l}\n{e}\n{\" \"}"] {
let src = format!("{PRELUDE}a\n~ f()\nb\n-> END\n\n=== function f() ===\n{body}\n");
assert_eq!(play(&src), ["a\n", "b\n"], "body was {body}");
}
}
#[test]
fn a_visible_value_is_not_trimmed() {
let src = format!("{PRELUDE}a\n~ f()\nb\n-> END\n\n=== function f() ===\n{{l}}x\n");
assert_eq!(play(&src), ["a\n", "x\n", "b\n"]);
let src =
format!("{PRELUDE}a\n~ f()\nb\n-> END\n\n=== function f() ===\n~ temp t = (li)\n{{t}}\n");
assert_eq!(play(&src), ["a\n", "li\n", "b\n"]);
}
#[test]
fn a_value_functions_empty_output_is_trimmed() {
let src =
format!("{PRELUDE}a\n{{f()}}\nb\n-> END\n\n=== function f() ===\n{{l}}\n~ return 7\n");
assert_eq!(play(&src), ["a\n", "7\n", "b\n"]);
}