Lines
95.12 %
Functions
99.16 %
Branches
100 %
//! The reader of PERSIST rows: a row is `(label, units)`, and what each unit
//! of it prints is compared with the row's pinned values.
use nms::interpreter::Interpreter;
use scripting::nomiscript::Value;
pub type Row = (&'static str, Vec<String>);
pub fn row(label: &'static str, units: &[&str]) -> Row {
(label, units.iter().map(ToString::to_string).collect())
}
fn show(value: &Value) -> String {
match value {
Value::Nil => "nil".to_string(),
Value::T => "t".to_string(),
Value::Number(n) => n.to_string(),
Value::String(text) => format!("{text:?}"),
Value::Symbol(name) => name.to_lowercase(),
Value::Struct { .. } | Value::Closure(_) | Value::Condition => {
scripting::nomiscript::format_value(value)
other => match scripting::nomiscript::list_to_vec(other) {
Some(items) => format!("({})", items.iter().map(show).collect::<Vec<_>>().join(" ")),
None => format!("{other:?}"),
},
fn shown(units: &[String], width: usize) -> Vec<String> {
let mut interp = Interpreter::new(false).unwrap();
units
.iter()
.map(|unit| match interp.eval(unit) {
Ok(values) => values.last().map_or("-".to_string(), show),
Err(err) => {
let text = err.to_string();
let text: Vec<&str> = text.split_whitespace().collect();
format!(
"err: {}",
text.join(" ").chars().take(width).collect::<String>()
)
})
.collect()
/// Asserts that every row reads as `pinned`, naming each row that moved.
pub fn assert_pinned(rows: &[Row], pinned: &[(&str, &[&str])]) {
assert_pinned_wide(rows, pinned, 44);
/// [`assert_pinned`] with an error text kept to `width` characters.
pub fn assert_pinned_wide(rows: &[Row], pinned: &[(&str, &[&str])], width: usize) {
assert_eq!(rows.len(), pinned.len());
let mut moved = Vec::new();
for ((label, units), (pinned_label, values)) in rows.iter().zip(pinned) {
assert_eq!(label, pinned_label);
let now = shown(units, width);
if now != *values {
moved.push(format!("{label}: {now:?}"));
assert!(moved.is_empty(), "{}", moved.join("\n"));