Cotton code
97 lines · 4.3 KB · RustRaw
use super::*;#[test]fn a_receipt_names_its_merchant_amount_date_and_order() {    let fields = read_record(        "Your receipt from Synthetic Coffee",        "Thanks for your order.\nOrder number: SC-10442\nPaid on 3 Oct 2026\nLatte ₹180.00\nCroissant ₹120.00\nTotal: ₹300.00\n",        Some("Synthetic Coffee"),    );    assert_eq!(        fields,        RecordFields {            kind: RECEIPT,            amount_minor: Some(30_000),            currency: Some("INR".into()),            merchant: Some("Synthetic Coffee".into()),            on: Some("2026-10-03".into()),            reference: Some("SC-10442".into()),        }    );}#[test]fn a_ticket_is_about_its_travel_date_never_when_it_was_sent() {    let fields = read_record(        "E-ticket: Bengaluru to Pune",        "Booked on 01/10/2026.\nPNR: 4711XQ\nDate of journey: 18 Oct 2026\nFare INR 5,240\n",        Some("Example Rail"),    );    assert_eq!(fields.kind, TICKET);    assert_eq!(fields.on.as_deref(), Some("2026-10-18"));    assert_eq!(fields.reference.as_deref(), Some("4711XQ"));    assert_eq!((fields.currency.as_deref(), fields.amount_minor), (Some("INR"), Some(524_000)));}#[test]fn a_statement_is_about_its_period_end_and_a_delivery_its_day() {    let statement = read_record(        "Your credit card statement is ready",        "Statement period 1 Sep 2026 to 30 Sep 2026\nTotal due: $412.50\n",        Some("Example Bank"),    );    assert_eq!(statement.kind, STATEMENT);    assert_eq!(statement.on.as_deref(), Some("2026-09-30"));    assert_eq!((statement.currency.as_deref(), statement.amount_minor), (Some("USD"), Some(41_250)));    assert_eq!(statement.reference, None, "a statement's reference is its account, which is not lifted");    let delivery = read_record("Your parcel is out for delivery", "Expected delivery: October 12, 2026\nTracking number: ZX9981234\n", Some("Example Post"));    assert_eq!(delivery.kind, DELIVERY);    assert_eq!(delivery.on.as_deref(), Some("2026-10-12"));    assert_eq!(delivery.reference.as_deref(), Some("ZX9981234"));    assert_eq!(delivery.amount_minor, None);}// RULE: A one-time code is never stored: a code mail is typed `code` and keeps// only its merchant — never the digits, a reference or an amount.#[test]fn a_one_time_code_is_typed_and_never_stored() {    let fields = read_record(        "Your verification code",        "Your one-time password is 482913. Order ABC12345 total ₹999. Do not share it.",        Some("Example Shop"),    );    assert_eq!(fields, RecordFields { kind: CODE, merchant: Some("Example Shop".into()), ..RecordFields::default() });    let columns = fields.columns();    assert!(!serde_json::to_string(&columns).unwrap().contains("482913"));    // In the body alone, a code still makes it one.    assert_eq!(read_record("Sign in to Example", "Use 551200 as your OTP to sign in.", Some("Example")).kind, CODE);}// RULE: An unsure parse yields a notice with every field empty: a subject// that names no kind, or a receipt whose several amounts lead to no one total.#[test]fn an_unsure_parse_is_a_notice_or_an_empty_field() {    let notice = read_record("Updates to our terms", "We paid ₹100 to charity. Order 12345 date 3 Oct 2026.", Some("Example"));    assert_eq!(notice, RecordFields::notice());    assert_eq!(notice.columns()["merchant"], Value::Null);    let unsure = read_record("Your receipt", "Item one ₹100\nItem two ₹250\n", Some("Example"));    assert_eq!(unsure.kind, RECEIPT);    assert_eq!((unsure.amount_minor, unsure.currency), (None, None), "two amounts and no total: unsure");    assert_eq!(unsure.on, None);    // 03/04/2026 is March or April depending on the writer: unread.    let ambiguous = read_record("Booking confirmed", "Check-in: 03/04/2026", Some("Example Stays"));    assert_eq!((ambiguous.kind, ambiguous.on), (BOOKING, None));    let clear = read_record("Booking confirmed", "Check-in: 23/04/2026", Some("Example Stays"));    assert_eq!(clear.on.as_deref(), Some("2026-04-23"));}#[test]fn a_cleared_row_names_every_typed_column() {    let cleared = cleared();    assert_eq!(cleared.len(), COLUMNS.len());    assert!(cleared.values().all(Value::is_null));    assert_eq!(RecordFields::notice().columns().keys().collect::<Vec<_>>(), cleared.keys().collect::<Vec<_>>());}