use crate::storage::{PersistedEntry, PersistedState};
const DEMO_INPUTS: &[&str] = &[
"# calcli demo - press F12 or ? for help, Ctrl+Q to quit",
"2 + 3 * 4",
"ans ^ 2",
"r = 5 # a radius",
"2 * pi * r # circumference",
"# Units: write `value unit`, convert with ->",
"123 MPa -> bar",
"100 km/h -> m/s",
"20 kN + 300 N",
"2 kN / 4 m^2 -> kN/m^2",
"x = 50 kN",
"x -> N",
];
pub fn demo_state() -> PersistedState {
PersistedState {
settings: None,
ui: Default::default(),
variables: Default::default(),
history: DEMO_INPUTS
.iter()
.map(|input| PersistedEntry {
input: (*input).to_string(),
value: None,
unit: None,
})
.collect(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::domain::evaluator::MevalEvaluator;
use crate::domain::history::{History, HistoryEntry};
use crate::domain::variables::VariableStore;
use crate::services::CalcService;
fn replay() -> CalcService {
let state = demo_state();
let entries = state
.history
.iter()
.map(|entry| HistoryEntry {
input: entry.input.clone(),
value: None,
error: None,
})
.collect();
let mut service = CalcService::new(
Box::new(MevalEvaluator::new()),
crate::config::Config::default().format_settings(),
History::from_entries(entries, 500),
VariableStore::new(),
);
service.recompute_all();
service
}
#[test]
fn every_demo_line_evaluates_without_error() {
let service = replay();
for entry in service.history().entries() {
assert!(
entry.error.is_none(),
"demo line {:?} errored: {:?}",
entry.input,
entry.error,
);
}
}
#[test]
fn demo_showcases_chaining_variables_and_units() {
let service = replay();
let value_at = |index: usize| {
service.history().entries()[index]
.value
.as_ref()
.map(|q| q.display_value())
};
assert_eq!(value_at(1), Some(14.0));
assert_eq!(value_at(2), Some(196.0));
let last = service.history().entries().last().unwrap();
assert_eq!(last.value.as_ref().unwrap().unit_symbol(), Some("N"));
assert!(
(last.value.as_ref().unwrap().display_value() - 50_000.0).abs()
< 1e-6
);
}
}