diff --git a/crates/forge-core/src/config.rs b/crates/forge-core/src/config.rs index ccedc2d..cc4dec2 100644 --- a/crates/forge-core/src/config.rs +++ b/crates/forge-core/src/config.rs @@ -806,36 +806,75 @@ mod tests { } #[test] - fn missing_user_content_listen_uses_default() { + fn user_content_listen_defaults_and_parses() { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("forge.toml"); - let text: String = DEV_DEFAULTS + let without_line: String = DEV_DEFAULTS .lines() .filter(|line| !line.starts_with("user_content_listen")) .collect::>() .join("\n"); - std::fs::write(&path, text).unwrap(); - let cfg = Config::load(&path).unwrap(); - assert_eq!(cfg.user_content_listen, "127.0.0.1:7661".parse().unwrap()); - } - - #[test] - fn user_content_listen_is_parsed() { - let dir = tempfile::tempdir().unwrap(); - let path = dir.path().join("forge.toml"); - let text = DEV_DEFAULTS.replace("127.0.0.1:7661\"\n# SSH", "0.0.0.0:9000\"\n# SSH"); - std::fs::write(&path, text).unwrap(); - let cfg = Config::load(&path).unwrap(); - assert_eq!(cfg.user_content_listen, "0.0.0.0:9000".parse().unwrap()); + let replaced = DEV_DEFAULTS.replace("127.0.0.1:7661\"\n# SSH", "0.0.0.0:9000\"\n# SSH"); + let cases: Vec<(&str, String, &str)> = vec![ + ("missing line uses default", without_line, "127.0.0.1:7661"), + ("explicit value is parsed", replaced, "0.0.0.0:9000"), + ]; + for (label, text, want) in cases { + std::fs::write(&path, text).unwrap(); + let cfg = Config::load(&path).unwrap(); + assert_eq!( + cfg.user_content_listen, + want.parse().unwrap(), + "case {label}" + ); + } } #[test] - fn unknown_keys_are_rejected() { + fn bad_configs_are_refused() { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("forge.toml"); - let text = DEV_DEFAULTS.replace("registration", "bogus = 1\nregistration"); - std::fs::write(&path, text).unwrap(); - assert!(matches!(Config::load(&path), Err(Error::Config(_)))); + let cases: Vec<(&str, String)> = vec![ + ( + "unknown top-level key", + DEV_DEFAULTS.replace("registration", "bogus = 1\nregistration"), + ), + ( + "invalid trusted proxy", + DEV_DEFAULTS.replace("trusted_proxies = []", "trusted_proxies = [\"not-an-ip\"]"), + ), + ( + "self_update unknown key", + format!("{DEV_DEFAULTS}\n[self_update]\nbogus = 1\n"), + ), + ( + "storage unknown key", + format!("{DEV_DEFAULTS}\n[storage]\nbogus = 1\n"), + ), + ( + "storage.s3 secret_access_key", + format!("{DEV_DEFAULTS}\n[storage.s3]\nsecret_access_key = \"x\"\n"), + ), + ( + "r2 endpoint", + format!("{DEV_DEFAULTS}\n[storage.r2]\nendpoint = \"https://x\"\n"), + ), + ( + "r2 region", + format!("{DEV_DEFAULTS}\n[storage.r2]\nregion = \"auto\"\n"), + ), + ( + "r2 unknown key", + format!("{DEV_DEFAULTS}\n[storage.r2]\nbogus = 1\n"), + ), + ]; + for (label, text) in cases { + std::fs::write(&path, text).unwrap(); + assert!( + matches!(Config::load(&path), Err(Error::Config(_))), + "case {label}" + ); + } } #[test] @@ -873,39 +912,45 @@ mod tests { } #[test] - fn invalid_trusted_proxy_is_rejected() { - let dir = tempfile::tempdir().unwrap(); - let path = dir.path().join("forge.toml"); - let text = - DEV_DEFAULTS.replace("trusted_proxies = []", "trusted_proxies = [\"not-an-ip\"]"); - std::fs::write(&path, text).unwrap(); - assert!(matches!(Config::load(&path), Err(Error::Config(_)))); - } - - #[test] - fn backup_section_defaults_to_disabled() { + fn optional_sections_default_to_disabled() { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("forge.toml"); std::fs::write(&path, DEV_DEFAULTS).unwrap(); let cfg = Config::load(&path).unwrap(); - assert!(!cfg.backup.enabled()); - assert_eq!(cfg.backup.interval_hours, 24); - assert_eq!(cfg.backup.keep, 7); - assert_eq!(cfg.backup.fsck_interval_days, 7); - assert!(cfg.backup.healthcheck_url.is_empty()); - } - - #[test] - fn replication_section_defaults_to_disabled() { - let dir = tempfile::tempdir().unwrap(); - let path = dir.path().join("forge.toml"); - std::fs::write(&path, DEV_DEFAULTS).unwrap(); - let cfg = Config::load(&path).unwrap(); - assert!(!cfg.replication.enabled()); - assert_eq!(cfg.replication.ssh_port, 22); - assert_eq!(cfg.replication.db_snapshot_minutes, 15); - assert_eq!(cfg.replication.reconcile_minutes, 60); - assert!(cfg.replication.healthcheck_url.is_empty()); + // backup + assert!(!cfg.backup.enabled(), "backup enabled"); + assert_eq!(cfg.backup.interval_hours, 24, "backup interval"); + assert_eq!(cfg.backup.keep, 7, "backup keep"); + assert_eq!(cfg.backup.fsck_interval_days, 7, "backup fsck"); + assert!(cfg.backup.healthcheck_url.is_empty(), "backup healthcheck"); + // replication + assert!(!cfg.replication.enabled(), "replication enabled"); + assert_eq!(cfg.replication.ssh_port, 22, "replication ssh port"); + assert_eq!( + cfg.replication.db_snapshot_minutes, 15, + "replication snapshot" + ); + assert_eq!( + cfg.replication.reconcile_minutes, 60, + "replication reconcile" + ); + assert!( + cfg.replication.healthcheck_url.is_empty(), + "replication healthcheck" + ); + // self_update + assert!(!cfg.self_update.enabled, "self_update enabled"); + assert_eq!(cfg.self_update.branch, "main", "self_update branch"); + assert_eq!(cfg.self_update.job, "build", "self_update job"); + assert_eq!( + cfg.self_update.artifact, "dist_forge", + "self_update artifact" + ); + // storage + assert_eq!(cfg.storage.backend, StorageBackend::Fs, "storage backend"); + assert!(cfg.storage.s3.bucket.is_empty(), "s3 bucket"); + assert!(!cfg.storage.s3.allow_http, "s3 allow_http"); + assert!(!cfg.storage.s3.virtual_hosted_style, "s3 virtual hosted"); } #[test] @@ -933,18 +978,6 @@ mod tests { assert!(matches!(Config::load(&path), Err(Error::Config(_)))); } - #[test] - fn self_update_section_defaults_to_disabled() { - let dir = tempfile::tempdir().unwrap(); - let path = dir.path().join("forge.toml"); - std::fs::write(&path, DEV_DEFAULTS).unwrap(); - let cfg = Config::load(&path).unwrap(); - assert!(!cfg.self_update.enabled); - assert_eq!(cfg.self_update.branch, "main"); - assert_eq!(cfg.self_update.job, "build"); - assert_eq!(cfg.self_update.artifact, "dist_forge"); - } - #[test] fn self_update_enabled_requires_owner_name_repo() { let dir = tempfile::tempdir().unwrap(); @@ -963,14 +996,6 @@ mod tests { assert_eq!(cfg.self_update.repo, "Tahinli/forge"); } - #[test] - fn self_update_rejects_unknown_keys() { - let dir = tempfile::tempdir().unwrap(); - let path = dir.path().join("forge.toml"); - std::fs::write(&path, format!("{DEV_DEFAULTS}\n[self_update]\nbogus = 1\n")).unwrap(); - assert!(Config::load(&path).is_err()); - } - #[test] fn backup_section_is_parsed_and_validated() { let dir = tempfile::tempdir().unwrap(); @@ -991,18 +1016,6 @@ mod tests { assert!(matches!(Config::load(&path), Err(Error::Config(_)))); } - #[test] - fn storage_section_defaults_to_fs() { - let dir = tempfile::tempdir().unwrap(); - let path = dir.path().join("forge.toml"); - std::fs::write(&path, DEV_DEFAULTS).unwrap(); - let cfg = Config::load(&path).unwrap(); - assert_eq!(cfg.storage.backend, StorageBackend::Fs); - assert!(cfg.storage.s3.bucket.is_empty()); - assert!(!cfg.storage.s3.allow_http); - assert!(!cfg.storage.s3.virtual_hosted_style); - } - #[test] fn s3_backend_requires_bucket_region_and_credentials() { let dir = tempfile::tempdir().unwrap(); @@ -1076,28 +1089,6 @@ mod tests { assert!(Config::load(&path).is_ok()); } - #[test] - fn storage_rejects_unknown_keys() { - let dir = tempfile::tempdir().unwrap(); - let path = dir.path().join("forge.toml"); - std::fs::write(&path, format!("{DEV_DEFAULTS}\n[storage]\nbogus = 1\n")).unwrap(); - assert!(matches!(Config::load(&path), Err(Error::Config(_)))); - std::fs::write( - &path, - format!("{DEV_DEFAULTS}\n[storage.s3]\nsecret_access_key = \"x\"\n"), - ) - .unwrap(); - assert!(matches!(Config::load(&path), Err(Error::Config(_)))); - // R2 users never set an endpoint or region, so those keys are refused outright. - for key in ["endpoint = \"https://x\"", "region = \"auto\"", "bogus = 1"] { - std::fs::write(&path, format!("{DEV_DEFAULTS}\n[storage.r2]\n{key}\n")).unwrap(); - assert!( - matches!(Config::load(&path), Err(Error::Config(_))), - "{key}" - ); - } - } - /// A complete `[storage.r2]` table with a real secret file, minus any lines the caller drops. fn r2_config_text(dir: &Path, drop: &[&str], extra: &str) -> String { let secret = dir.join("r2-secret"); @@ -1151,74 +1142,85 @@ mod tests { } #[test] - fn r2_backend_requires_its_fields() { - let dir = tempfile::tempdir().unwrap(); - let path = dir.path().join("forge.toml"); - std::fs::write(&path, r2_config_text(dir.path(), &[], "")).unwrap(); - assert!(Config::load(&path).is_ok()); - for missing in [ - "account_id", - "bucket", - "access_key_id", - "secret_access_key_file", - ] { - std::fs::write(&path, r2_config_text(dir.path(), &[missing], "")).unwrap(); - assert!( - matches!(Config::load(&path), Err(Error::Config(_))), - "{missing} should be required" - ); - } - } - - #[test] - fn r2_account_id_must_be_hex() { + fn r2_fields_are_validated() { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("forge.toml"); - for bad in ["evil.example.com/", "acct id", "xyz"] { - let text = r2_config_text( - dir.path(), + let absent = dir.path().join("absent").display().to_string(); + // (label, lines to drop from the complete table, extra lines, expected to load) + let cases: Vec<(&str, &[&str], String, bool)> = vec![ + ("complete", &[], String::new(), true), + ("account_id missing", &["account_id"], String::new(), false), + ("bucket missing", &["bucket"], String::new(), false), + ( + "access_key_id missing", + &["access_key_id"], + String::new(), + false, + ), + ( + "secret_access_key_file missing", + &["secret_access_key_file"], + String::new(), + false, + ), + ( + "account_id with a host", &["account_id"], - &format!("account_id = \"{bad}\"\n"), - ); - std::fs::write(&path, text).unwrap(); - assert!( - matches!(Config::load(&path), Err(Error::Config(_))), - "`{bad}`" - ); - } - } - - #[test] - fn r2_jurisdiction_must_be_known() { - let dir = tempfile::tempdir().unwrap(); - let path = dir.path().join("forge.toml"); - for (value, ok) in [ - ("", true), - ("eu", true), - ("fedramp", true), - ("us", false), - ("EU", false), - ] { - let text = r2_config_text( - dir.path(), + "account_id = \"evil.example.com/\"\n".into(), + false, + ), + ( + "account_id with a space", + &["account_id"], + "account_id = \"acct id\"\n".into(), + false, + ), + ( + "account_id not hex", + &["account_id"], + "account_id = \"xyz\"\n".into(), + false, + ), + ( + "secret file absent", + &["secret_access_key_file"], + format!("secret_access_key_file = \"{absent}\"\n"), + false, + ), + ( + "jurisdiction empty", &["jurisdiction"], - &format!("jurisdiction = \"{value}\"\n"), - ); - std::fs::write(&path, text).unwrap(); - assert_eq!(Config::load(&path).is_ok(), ok, "jurisdiction `{value}`"); + "jurisdiction = \"\"\n".into(), + true, + ), + ( + "jurisdiction eu", + &["jurisdiction"], + "jurisdiction = \"eu\"\n".into(), + true, + ), + ( + "jurisdiction fedramp", + &["jurisdiction"], + "jurisdiction = \"fedramp\"\n".into(), + true, + ), + ( + "jurisdiction us", + &["jurisdiction"], + "jurisdiction = \"us\"\n".into(), + false, + ), + ( + "jurisdiction EU", + &["jurisdiction"], + "jurisdiction = \"EU\"\n".into(), + false, + ), + ]; + for (label, drop, extra, ok) in cases { + std::fs::write(&path, r2_config_text(dir.path(), drop, &extra)).unwrap(); + assert_eq!(Config::load(&path).is_ok(), ok, "case {label}"); } } - - #[test] - fn r2_secret_file_must_exist() { - let dir = tempfile::tempdir().unwrap(); - let path = dir.path().join("forge.toml"); - let text = r2_config_text(dir.path(), &["secret_access_key_file"], "") - + &format!( - "secret_access_key_file = \"{}\"\n", - dir.path().join("absent").display() - ); - std::fs::write(&path, text).unwrap(); - assert!(matches!(Config::load(&path), Err(Error::Config(_)))); - } } diff --git a/crates/forge-core/src/issue_refs.rs b/crates/forge-core/src/issue_refs.rs index 7cb9e00..d8d93b7 100644 --- a/crates/forge-core/src/issue_refs.rs +++ b/crates/forge-core/src/issue_refs.rs @@ -115,80 +115,42 @@ mod tests { use super::*; #[test] - fn plain_reference() { - assert_eq!(find_issue_refs("fixes #12"), vec![12]); - } - - #[test] - fn multiple_references_in_order() { - assert_eq!(find_issue_refs("Fixes: #3, closes #4"), vec![3, 4]); - } - - #[test] - fn prefix_before_hash_is_not_a_reference() { - assert!(find_issue_refs("prefix#5").is_empty()); - } - - #[test] - fn html_entity_is_not_a_reference() { - assert!(find_issue_refs("'").is_empty()); - } - - #[test] - fn slash_before_hash_is_not_a_reference() { - assert!(find_issue_refs("a/#7").is_empty()); - } - - #[test] - fn leading_zeros_are_parsed() { - assert_eq!(find_issue_refs("#0012"), vec![12]); - } - - #[test] - fn ten_digits_is_not_a_reference() { - assert!(find_issue_refs("#1234567890").is_empty()); - } - - #[test] - fn nine_digits_is_a_reference() { - assert_eq!(find_issue_refs("#123456789"), vec![123_456_789]); - } - - #[test] - fn zero_is_not_a_reference() { - assert!(find_issue_refs("#0").is_empty()); - } - - #[test] - fn parentheses_are_allowed_around_reference() { - assert_eq!(find_issue_refs("(#8)"), vec![8]); - } - - #[test] - fn trailing_punctuation_is_allowed() { - assert_eq!(find_issue_refs("#9."), vec![9]); - } - - #[test] - fn alphanumeric_suffix_is_not_a_reference() { - assert!(find_issue_refs("#12abc").is_empty()); - } - - #[test] - fn duplicates_are_removed() { - assert_eq!(find_issue_refs("#2 and #1 and #2"), vec![2, 1]); - assert_eq!(find_closing_refs("fixes #2, closes #2"), vec![2]); - } - - #[test] - fn unfixes_is_not_closing() { - assert!(find_closing_refs("unfixes #2").is_empty()); - assert_eq!(find_issue_refs("unfixes #2"), vec![2]); + fn issue_references_table() { + let cases: &[(&str, Vec)] = &[ + ("fixes #12", vec![12]), + ("Fixes: #3, closes #4", vec![3, 4]), + ("prefix#5", vec![]), + ("'", vec![]), + ("a/#7", vec![]), + ("#0012", vec![12]), + ("#1234567890", vec![]), + ("#123456789", vec![123_456_789]), + ("#0", vec![]), + ("(#8)", vec![8]), + ("#9.", vec![9]), + ("#12abc", vec![]), + ("#2 and #1 and #2", vec![2, 1]), + ("unfixes #2", vec![2]), + ]; + for (text, want) in cases { + assert_eq!(find_issue_refs(text), *want, "case {text:?}"); + } } #[test] - fn fixed_is_closing() { - assert_eq!(find_closing_refs("fixed #2"), vec![2]); + fn closing_references_table() { + let cases: &[(&str, Vec)] = &[ + ("fixes #2, closes #2", vec![2]), + ("unfixes #2", vec![]), + ("fixed #2", vec![2]), + ("fixes#2", vec![]), + ("fixes:#2", vec![]), + ("see #2", vec![]), + ("prefixes #2", vec![]), + ]; + for (text, want) in cases { + assert_eq!(find_closing_refs(text), *want, "case {text:?}"); + } } #[test] @@ -200,20 +162,4 @@ mod tests { assert_eq!(find_closing_refs(&text), vec![5], "{kw}"); } } - - #[test] - fn keyword_needs_whitespace_before_hash() { - assert!(find_closing_refs("fixes#2").is_empty()); - assert!(find_closing_refs("fixes:#2").is_empty()); - } - - #[test] - fn plain_reference_is_not_closing() { - assert!(find_closing_refs("see #2").is_empty()); - } - - #[test] - fn keyword_must_be_a_whole_word() { - assert!(find_closing_refs("prefixes #2").is_empty()); - } } diff --git a/crates/forge-core/src/names.rs b/crates/forge-core/src/names.rs index 368ca2f..681228a 100644 --- a/crates/forge-core/src/names.rs +++ b/crates/forge-core/src/names.rs @@ -152,16 +152,33 @@ mod tests { } #[test] - fn accepts_valid_names_and_returns_folded_form() { - assert_eq!(validate_name("Tahinli").unwrap(), "tahinli"); - assert_eq!(validate_name("my-repo_2.x").unwrap(), "my-repo_2.x"); - assert_eq!(validate_name("a").unwrap(), "a"); - assert_eq!(validate_name(&"a".repeat(64)).unwrap().len(), 64); - assert_eq!(validate_name("x.gitx").unwrap(), "x.gitx"); + fn accepted_names_return_folded_form() { + let good: [(&str, &str); 5] = [ + ("Tahinli", "tahinli"), + ("my-repo_2.x", "my-repo_2.x"), + ("a", "a"), + ("x.gitx", "x.gitx"), + ("Tahinli.Repo", "tahinli.repo"), + ]; + for (name, want) in good { + assert_eq!(validate_name(name).unwrap(), want, "case {name:?}"); + } + let long = "a".repeat(64); + assert_eq!(validate_name(&long).unwrap(), long, "case 64 chars"); + assert_eq!( + validate_username("Tahinli").unwrap(), + ("Tahinli".to_string(), "tahinli".to_string()), + "case username display case" + ); + assert_eq!( + validate_username("TAHINLI").unwrap(), + ("TAHINLI".to_string(), "tahinli".to_string()), + "case username fullwidth" + ); } #[test] - fn rejects_invalid_names() { + fn rejected_names_and_usernames_are_invalid_name_errors() { let bad = [ "".to_string(), "a".repeat(65), @@ -186,22 +203,6 @@ mod tests { "expected rejection for {name:?}" ); } - } - - #[test] - fn username_keeps_display_case_and_folds_key() { - assert_eq!( - validate_username("Tahinli").unwrap(), - ("Tahinli".to_string(), "tahinli".to_string()) - ); - assert_eq!( - validate_username("TAHINLI").unwrap(), - ("TAHINLI".to_string(), "tahinli".to_string()) - ); - } - - #[test] - fn username_rejects_reserved_and_invalid() { for bad in [ "ADMIN", "Settings", @@ -214,7 +215,7 @@ mod tests { ] { assert!( matches!(validate_username(bad), Err(Error::InvalidName(_))), - "expected rejection for {bad:?}" + "expected rejection for username {bad:?}" ); } } diff --git a/crates/forge-core/src/suggestions.rs b/crates/forge-core/src/suggestions.rs index ac1b555..f4b3518 100644 --- a/crates/forge-core/src/suggestions.rs +++ b/crates/forge-core/src/suggestions.rs @@ -114,43 +114,36 @@ mod tests { } #[test] - fn body_without_a_suggestion_is_one_text_part() { - assert_eq!( - split_body("looks fine\n\n```rust\nlet x = 1;\n```"), - vec![Part::Text("looks fine\n\n```rust\nlet x = 1;\n```".into())] - ); - } - - #[test] - fn suggestion_block_is_split_out_with_its_surrounding_text() { - let body = "Try this:\n```suggestion\nlet y = 2;\n```\nThanks"; - assert_eq!( - split_body(body), - vec![ - Part::Text("Try this:".into()), - Part::Suggestion(lines(&["let y = 2;"])), - Part::Text("Thanks".into()), - ] - ); - } - - #[test] - fn empty_suggestion_and_several_blocks_are_parsed_in_order() { - let body = "```suggestion\n```\n\n````suggestion\na\n```\nb\n````"; - assert_eq!( - split_body(body), - vec![ - Part::Suggestion(vec![]), - Part::Text("".into()), - Part::Suggestion(lines(&["a", "```", "b"])), - ] - ); - } - - #[test] - fn unclosed_suggestion_fence_stays_text() { - let body = "```suggestion\nlet y = 2;"; - assert_eq!(split_body(body), vec![Part::Text(body.into())]); + fn split_body_cases() { + let cases: Vec<(&str, Vec)> = vec![ + ( + "looks fine\n\n```rust\nlet x = 1;\n```", + vec![Part::Text("looks fine\n\n```rust\nlet x = 1;\n```".into())], + ), + ( + "Try this:\n```suggestion\nlet y = 2;\n```\nThanks", + vec![ + Part::Text("Try this:".into()), + Part::Suggestion(lines(&["let y = 2;"])), + Part::Text("Thanks".into()), + ], + ), + ( + "```suggestion\n```\n\n````suggestion\na\n```\nb\n````", + vec![ + Part::Suggestion(vec![]), + Part::Text("".into()), + Part::Suggestion(lines(&["a", "```", "b"])), + ], + ), + ( + "```suggestion\nlet y = 2;", + vec![Part::Text("```suggestion\nlet y = 2;".into())], + ), + ]; + for (body, want) in cases { + assert_eq!(split_body(body), want, "case {body:?}"); + } } #[test] @@ -162,48 +155,28 @@ mod tests { } #[test] - fn replace_line_swaps_one_line_and_keeps_the_rest() { - let text = "a\nb\nc\n"; - assert_eq!( - replace_line(text, 2, "b", &lines(&["B"])), - Ok("a\nB\nc\n".into()) - ); - } - - #[test] - fn replace_line_can_expand_delete_and_keep_a_missing_newline() { - assert_eq!( - replace_line("a\nb", 2, "b", &lines(&["b1", "b2"])), - Ok("a\nb1\nb2".into()) - ); - assert_eq!(replace_line("a\nb\nc\n", 2, "b", &[]), Ok("a\nc\n".into())); - } - - #[test] - fn replace_line_refuses_a_changed_or_missing_line() { - assert_eq!( - replace_line("a\nb\n", 2, "old", &lines(&["x"])), - Err(ApplyError::Changed) - ); - assert_eq!( - replace_line("a\nb\n", 3, "b", &lines(&["x"])), - Err(ApplyError::OutOfRange) - ); - assert_eq!( - replace_line("a\n", 0, "a", &lines(&["x"])), - Err(ApplyError::OutOfRange) - ); - assert_eq!( - replace_line("", 1, "", &lines(&["x"])), - Err(ApplyError::OutOfRange) - ); - } - - #[test] - fn replace_line_ignores_carriage_returns_on_the_expected_line() { - assert_eq!( - replace_line("a\r\nb\r\n", 1, "a", &lines(&["A"])), - Ok("A\nb\r\n".into()) + fn replace_line_cases() { + // (text, 1-based line, expected line text, replacement, want) + type Case<'a> = ( + &'a str, + u32, + &'a str, + &'a [&'a str], + Result<&'a str, ApplyError>, ); + let cases: Vec> = vec![ + ("a\nb\nc\n", 2, "b", &["B"], Ok("a\nB\nc\n")), + ("a\nb", 2, "b", &["b1", "b2"], Ok("a\nb1\nb2")), + ("a\nb\nc\n", 2, "b", &[], Ok("a\nc\n")), + ("a\nb\n", 2, "old", &["x"], Err(ApplyError::Changed)), + ("a\nb\n", 3, "b", &["x"], Err(ApplyError::OutOfRange)), + ("a\n", 0, "a", &["x"], Err(ApplyError::OutOfRange)), + ("", 1, "", &["x"], Err(ApplyError::OutOfRange)), + ("a\r\nb\r\n", 1, "a", &["A"], Ok("A\nb\r\n")), + ]; + for (text, line, expect, new, want) in cases { + let got = replace_line(text, line, expect, &lines(new)); + assert_eq!(got, want.map(str::to_string), "case {text:?} line {line}"); + } } }