Cotton code
56 lines · 4.2 KB · RustRaw
use super::*;fn worked(total: i64, currency: Option<&str>) -> Option<Worked> {    Some(Worked { total: FigureTotal { total, currency: currency.map(str::to_string), mixed: false, on: None }, amount: true })}fn figure(many: &str) -> ToolFigure {    ToolFigure { read: "worth".into(), one: Some("one".into()), many: many.into(), summary: None, formula: None, term: false }}/// RULE: A formula figure is its plus terms less its minus terms, in one currency; a missing term or a second currency makes no total, so a card never says a sum across rupees and euros.#[test]fn a_formula_adds_and_takes_away_in_one_currency() {    let mut totals: BTreeMap<&str, Option<Worked>> = BTreeMap::new();    totals.insert("held", worked(500_000, Some("INR")));    totals.insert("owed_to", worked(20_000, Some("INR")));    totals.insert("owed_by", worked(5_000, Some("INR")));    totals.insert("empty", worked(0, None));    let net = combine(&totals, &["held".into(), "owed_to".into(), "empty".into()], &["owed_by".into()]).unwrap();    assert_eq!(net.total.total, 515_000);    assert_eq!(net.total.currency.as_deref(), Some("INR"));    totals.insert("euros", worked(100, Some("EUR")));    assert!(combine(&totals, &["held".into(), "euros".into()], &[]).is_none(), "two currencies");    assert!(combine(&totals, &["held".into(), "nowhere".into()], &[]).is_none(), "a term not worked out");    totals.insert("failed", None);    assert!(combine(&totals, &["held".into()], &["failed".into()]).is_none(), "a term that failed");}/// RULE: A worked figure says nothing at zero or across currencies, says an amount in its own currency for {n}, and a count of one in its one words.#[test]fn a_worked_figure_says_its_total_or_nothing() {    // {n} is the amount transform's own words (its grouping is the money set's data, rupees in lakhs), so this checks the fill, not a grouping.    let words = cotton_core::records::amount::MoneyWords::shipped();    let rupees = Worked { total: FigureTotal { total: 12_000_050, currency: Some("INR".into()), mixed: false, on: None }, amount: true };    let money = cotton_core::records::amount::amount(12_000_050, Some("INR"), words).said(cotton_core::records::amount::MoneyForm::Money, words);    assert_eq!(said(&figure("Worth {n}"), &rupees, words), Some(format!("Worth {money}")));    let below = Worked { total: FigureTotal { total: -500, currency: Some("INR".into()), mixed: false, on: None }, amount: true };    assert_eq!(said(&figure("Worth {n}"), &below, cotton_core::records::amount::MoneyWords::shipped()).as_deref(), Some("Worth \u{2212}\u{20B9}5"));    let zero = Worked { total: FigureTotal { total: 0, currency: Some("INR".into()), mixed: false, on: None }, amount: true };    assert_eq!(said(&figure("Worth {n}"), &zero, cotton_core::records::amount::MoneyWords::shipped()), None);    let mixed = Worked { total: FigureTotal { total: 10, currency: Some("INR".into()), mixed: true, on: None }, amount: true };    assert_eq!(said(&figure("Worth {n}"), &mixed, cotton_core::records::amount::MoneyWords::shipped()), None);    let one = Worked { total: FigureTotal { total: 1, currency: None, mixed: false, on: None }, amount: false };    assert_eq!(said(&figure("{n} owed"), &one, cotton_core::records::amount::MoneyWords::shipped()).as_deref(), Some("one"));    let three = Worked { total: FigureTotal { total: 3, currency: None, mixed: false, on: None }, amount: false };    assert_eq!(said(&figure("{n} owed"), &three, cotton_core::records::amount::MoneyWords::shipped()).as_deref(), Some("3 owed"));}/// RULE: A lowest or highest says the day of its row for {date}, and nothing when it has no day — never a figure with a hole in its words.#[test]fn a_lowest_point_says_its_day() {    let low = Worked { total: FigureTotal { total: -150_000, currency: Some("INR".into()), mixed: false, on: Some("2026-10-20".into()) }, amount: true };    assert_eq!(said(&figure("Lowest {n} on {date}"), &low, cotton_core::records::amount::MoneyWords::shipped()).as_deref(), Some("Lowest \u{2212}\u{20B9}1,500 on 20 Oct"));    let undated = Worked { total: FigureTotal { on: None, ..low.total.clone() }, amount: true };    assert_eq!(said(&figure("Lowest {n} on {date}"), &undated, cotton_core::records::amount::MoneyWords::shipped()), None);}