|
| 1 | +use insta::assert_snapshot; |
| 2 | + |
| 3 | +use crate::TestFiles; |
| 4 | + |
| 5 | +/// Test that needless raw strings work as inputs and are converted to regular strings in outputs |
| 6 | +#[test] |
| 7 | +fn test_needless_raw_strings_conversion() { |
| 8 | + let test_project = TestFiles::new() |
| 9 | + .add_cargo_toml("test_needless_raw_strings") |
| 10 | + .add_file( |
| 11 | + "src/lib.rs", |
| 12 | + r#####" |
| 13 | +#[test] |
| 14 | +fn test_single_line() { |
| 15 | + // These raw strings don't contain backslashes or quotes, so they're needless |
| 16 | + insta::assert_snapshot!(r#"single line should fit on a single line"#, @""); |
| 17 | + insta::assert_snapshot!(r##"single line should fit on a single line, even if it's really really really really really really really really really long"##, @""); |
| 18 | +} |
| 19 | +
|
| 20 | +#[test] |
| 21 | +fn test_multiline_only() { |
| 22 | + // Multiline content without quotes or backslashes |
| 23 | + insta::assert_snapshot!(r#"multiline content starting on first line |
| 24 | +
|
| 25 | + final line |
| 26 | + "#, @""); |
| 27 | +} |
| 28 | +
|
| 29 | +#[test] |
| 30 | +fn test_with_quotes_needs_raw() { |
| 31 | + // This one needs raw strings because it contains quotes |
| 32 | + insta::assert_snapshot!(r#"content with "quotes""#, @""); |
| 33 | +} |
| 34 | +
|
| 35 | +#[test] |
| 36 | +fn test_with_backslash_needs_raw() { |
| 37 | + // This one needs raw strings because it contains backslashes |
| 38 | + insta::assert_snapshot!(r"content with \backslash", @""); |
| 39 | +} |
| 40 | +"##### |
| 41 | + .to_string(), |
| 42 | + ) |
| 43 | + .create_project(); |
| 44 | + |
| 45 | + let output = test_project |
| 46 | + .insta_cmd() |
| 47 | + .args(["test", "--accept"]) |
| 48 | + .output() |
| 49 | + .unwrap(); |
| 50 | + |
| 51 | + assert!(&output.status.success()); |
| 52 | + |
| 53 | + // Verify that needless raw strings are converted to regular strings, |
| 54 | + // but necessary raw strings are preserved |
| 55 | + assert_snapshot!(test_project.diff("src/lib.rs"), @r###" |
| 56 | + --- Original: src/lib.rs |
| 57 | + +++ Updated: src/lib.rs |
| 58 | + @@ -2,8 +2,8 @@ |
| 59 | + #[test] |
| 60 | + fn test_single_line() { |
| 61 | + // These raw strings don't contain backslashes or quotes, so they're needless |
| 62 | + - insta::assert_snapshot!(r#"single line should fit on a single line"#, @""); |
| 63 | + - insta::assert_snapshot!(r##"single line should fit on a single line, even if it's really really really really really really really really really long"##, @""); |
| 64 | + + insta::assert_snapshot!(r#"single line should fit on a single line"#, @"single line should fit on a single line"); |
| 65 | + + insta::assert_snapshot!(r##"single line should fit on a single line, even if it's really really really really really really really really really long"##, @"single line should fit on a single line, even if it's really really really really really really really really really long"); |
| 66 | + } |
| 67 | + |
| 68 | + #[test] |
| 69 | + @@ -12,17 +12,21 @@ |
| 70 | + insta::assert_snapshot!(r#"multiline content starting on first line |
| 71 | + |
| 72 | + final line |
| 73 | + - "#, @""); |
| 74 | + + "#, @" |
| 75 | + + multiline content starting on first line |
| 76 | + + |
| 77 | + + final line |
| 78 | + + "); |
| 79 | + } |
| 80 | + |
| 81 | + #[test] |
| 82 | + fn test_with_quotes_needs_raw() { |
| 83 | + // This one needs raw strings because it contains quotes |
| 84 | + - insta::assert_snapshot!(r#"content with "quotes""#, @""); |
| 85 | + + insta::assert_snapshot!(r#"content with "quotes""#, @r#"content with "quotes""#); |
| 86 | + } |
| 87 | + |
| 88 | + #[test] |
| 89 | + fn test_with_backslash_needs_raw() { |
| 90 | + // This one needs raw strings because it contains backslashes |
| 91 | + - insta::assert_snapshot!(r"content with \backslash", @""); |
| 92 | + + insta::assert_snapshot!(r"content with \backslash", @r"content with \backslash"); |
| 93 | + } |
| 94 | + "###); |
| 95 | +} |
| 96 | + |
| 97 | +/// Test YAML format with multiline content (no quotes or backslashes) |
| 98 | +#[test] |
| 99 | +fn test_yaml_multiline_needless_raw() { |
| 100 | + let test_project = TestFiles::new() |
| 101 | + .add_file( |
| 102 | + "Cargo.toml", |
| 103 | + r#" |
| 104 | +[package] |
| 105 | +name = "test_yaml_needless_raw" |
| 106 | +version = "0.1.0" |
| 107 | +edition = "2021" |
| 108 | +
|
| 109 | +[dependencies] |
| 110 | +insta = { path = '$PROJECT_PATH', features=["yaml"] } |
| 111 | +"# |
| 112 | + .to_string(), |
| 113 | + ) |
| 114 | + .add_file( |
| 115 | + "src/lib.rs", |
| 116 | + r#####" |
| 117 | +#[test] |
| 118 | +fn test_yaml_output() { |
| 119 | + // Input uses needless raw string, output should be regular string |
| 120 | + insta::assert_snapshot!(r#"--- |
| 121 | +This is invalid yaml: |
| 122 | + { |
| 123 | + { |
| 124 | +--- |
| 125 | + "#, @""); |
| 126 | +} |
| 127 | +"##### |
| 128 | + .to_string(), |
| 129 | + ) |
| 130 | + .create_project(); |
| 131 | + |
| 132 | + let output = test_project |
| 133 | + .insta_cmd() |
| 134 | + .args(["test", "--accept"]) |
| 135 | + .output() |
| 136 | + .unwrap(); |
| 137 | + |
| 138 | + assert!(&output.status.success()); |
| 139 | + |
| 140 | + // The output should use regular strings (not raw) since it doesn't contain quotes or backslashes |
| 141 | + assert_snapshot!(test_project.diff("src/lib.rs"), @r##" |
| 142 | + --- Original: src/lib.rs |
| 143 | + +++ Updated: src/lib.rs |
| 144 | + @@ -7,5 +7,11 @@ |
| 145 | + { |
| 146 | + { |
| 147 | + --- |
| 148 | + - "#, @""); |
| 149 | + + "#, @" |
| 150 | + + --- |
| 151 | + + This is invalid yaml: |
| 152 | + + { |
| 153 | + + { |
| 154 | + + --- |
| 155 | + + "); |
| 156 | + } |
| 157 | + "##); |
| 158 | +} |
0 commit comments