#![allow(clippy::expect_used, clippy::unwrap_used, clippy::panic)]
use std::sync::Arc;
use brink_runtime::{DotNetRng, Step, Story};
fn play_choosing(source: &str, choices: &[usize]) -> 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();
let mut picks = choices.iter().copied();
for _ in 0..200 {
match story.continue_single().expect("runtime") {
Step::Line(l) => lines.push(l.text.clone()),
Step::Choices(_) => {
let pick = picks
.next()
.unwrap_or_else(|| panic!("unexpected choices in {source}"));
story.choose(pick).expect("choose");
}
Step::Done | Step::End | Step::Suspended => return lines,
}
}
panic!("story did not reach a terminal step in 200 steps");
}
#[test]
fn temp_written_after_a_thread_spawn_survives_the_choice() {
let src = "-> k\n\n=== k ===\n<- t\n~ temp t0 = 1\n* [a]\n+ -> END\n- {t0}\n-> END\n\n=== t ===\n-> DONE\n";
assert_eq!(play_choosing(src, &[0]), vec!["1\n"]);
}
#[test]
fn temp_after_a_printing_thread_survives_the_choice() {
let src = "-> k\n\n=== k ===\n<- t\n~ temp t0 = 1\n* [a]\n+ -> END\n- {t0}\n-> END\n\n=== t ===\nthread text\n-> DONE\n";
assert_eq!(play_choosing(src, &[0]), vec!["thread text\n", "1\n"]);
}
#[test]
fn temp_written_before_the_spawn_control() {
let src = "-> k\n\n=== k ===\n~ temp t0 = 1\n<- t\n* [a]\n+ -> END\n- {t0}\n-> END\n\n=== t ===\n-> DONE\n";
assert_eq!(play_choosing(src, &[0]), vec!["1\n"]);
}
#[test]
fn temp_reassigned_after_the_spawn_survives_the_choice() {
let src = "-> k\n\n=== k ===\n~ temp t0 = 1\n<- t\n~ t0 = 2\n* [a]\n+ -> END\n- {t0}\n-> END\n\n=== t ===\n-> DONE\n";
assert_eq!(play_choosing(src, &[0]), vec!["2\n"]);
}