Cotton code
80 lines · 3.9 KB · RustRaw
use super::*;fn act(text: &str) -> Beat {    Beat { kind: BeatKind::Action, who: None, aside: None, form: None, text: text.into() }}fn say(who: &str, text: &str) -> Beat {    Beat { kind: BeatKind::Dialogue, who: Some(who.into()), aside: None, form: None, text: text.into() }}fn held(lines: &[(&str, Beat)]) -> Vec<Held> {    lines.iter().map(|(id, beat)| Held { id: (*id).into(), beat: beat.clone() }).collect()}fn ids(laid: &Laid) -> Vec<Option<&str>> {    laid.lines.iter().map(|line| line.id.as_deref()).collect()}/// RULE: A body written again keeps every unchanged line's id, an edited line's id where it stands, and gives ids only to new lines.#[test]fn a_rewrite_keeps_the_ids_of_lines_kept_and_edited() {    let rows = held(&[("a", act("Rain on the tin roof.")), ("b", say("ASHA", "You came back.")), ("c", say("RAVI", "I never left.")), ("d", act("She turns away."))]);    let new = vec![act("Rain on the tin roof."), say("ASHA", "You came back, then."), act("A dog barks."), say("RAVI", "I never left."), act("She turns away.")];    let laid = reconcile(&rows, &new);    assert_eq!(ids(&laid), [Some("a"), Some("b"), None, Some("c"), Some("d")]);    assert!(laid.removed.is_empty());}/// RULE: A line taken out goes; a speech given to another speaker with the same words keeps its id.#[test]fn a_line_taken_out_goes_and_a_speech_given_to_another_keeps_its_id() {    let rows = held(&[("a", act("Night.")), ("b", say("ASHA", "Who is there?")), ("c", act("Silence."))]);    let laid = reconcile(&rows, &[say("MEERA", "Who is there?"), act("Silence.")]);    assert_eq!(ids(&laid), [Some("b"), Some("c")]);    assert_eq!(laid.removed, ["a"]);}/// RULE: Lines moved past each other keep their ids, any script.#[test]fn lines_in_any_script_keep_their_ids_when_moved() {    let rows = held(&[("a", say("రవి", "నువ్వు వచ్చావా?")), ("b", act("दरवाज़ा खुलता है।")), ("c", say("ハナ", "おかえり。"))]);    let laid = reconcile(&rows, &[act("दरवाज़ा खुलता है।"), say("ハナ", "おかえり。"), say("రవి", "నువ్వు వచ్చావా?")]);    assert_eq!(ids(&laid), [Some("b"), Some("c"), Some("a")]);    assert!(laid.removed.is_empty());}fn at(scene: &str, heading: &str, lines: &[(&str, Beat)]) -> SceneAt {    SceneAt { scene: scene.into(), heading: heading.into(), summary: None, lines: held(lines) }}/// RULE: What changed since a draft is told by identity: an added scene, a removed one, a moved one, a changed one, each with its numbers then and now.#[test]fn changes_since_a_draft_are_told_by_identity() {    let then = vec![        at("s1", "INT. HOUSE - DAY", &[("a", act("Morning."))]),        at("s2", "EXT. ROAD - DAY", &[("b", act("A bus."))]),        at("s3", "INT. BUS - DAY", &[("c", say("ASHA", "Hello."))]),        at("s4", "EXT. FIELD - NIGHT", &[("d", act("Stars."))]),    ];    let now = vec![        at("s1", "INT. HOUSE - DAY", &[("a", act("Morning."))]),        at("s3", "INT. BUS - DAY", &[("c", say("ASHA", "Hello there.")), ("e", act("She sits."))]),        at("s2", "EXT. ROAD - DAY", &[("b", act("A bus."))]),        at("s5", "INT. SHOP - DAY", &[("f", act("Shelves."))]),    ];    let found = changes(&then, &now);    let told: Vec<(&str, Change, Option<usize>, Option<usize>)> = found.scenes.iter().map(|s| (s.scene.as_str(), s.change, s.number_then, s.number_now)).collect();    assert_eq!(        told,        [            ("s1", Change::Same, Some(1), Some(1)),            ("s3", Change::Changed, Some(3), Some(2)),            ("s2", Change::Moved, Some(2), Some(3)),            ("s5", Change::Added, None, Some(4)),            ("s4", Change::Removed, Some(4), None),        ]    );    assert_eq!(found.scenes[1].lines, LineCount { added: 1, removed: 0, changed: 1 });    assert_eq!((found.since.scenes_added, found.since.scenes_removed, found.since.scenes_changed, found.since.scenes_moved), (1, 1, 1, 1));}