⭐️ A friendly language for building type-safe, scalable systems!

Add more tests for list formatting

authored by giacomocavalieri.me and committed by Louis Pilfold 5c0f404f 26587999

Changed files
+276
compiler-core
src
format
+1
compiler-core/src/format/tests.rs
··· 12 12 mod function; 13 13 mod guards; 14 14 mod imports; 15 + mod lists; 15 16 mod pipeline; 16 17 mod record_update; 17 18 mod tuple;
+275
compiler-core/src/format/tests/lists.rs
··· 1 + use crate::{assert_format, assert_format_rewrite}; 2 + 3 + #[test] 4 + fn list_with_trailing_comma_is_broken() { 5 + assert_format_rewrite!( 6 + "pub fn main() { [ 1, 2, a, ] }", 7 + r#"pub fn main() { 8 + [ 9 + 1, 10 + 2, 11 + a, 12 + ] 13 + } 14 + "# 15 + ); 16 + } 17 + 18 + #[test] 19 + fn constant_list_with_trailing_comma_is_broken() { 20 + assert_format_rewrite!( 21 + "const list = [ 1, 2, a, ]", 22 + r#"const list = [ 23 + 1, 24 + 2, 25 + a, 26 + ] 27 + "# 28 + ); 29 + } 30 + 31 + #[test] 32 + fn list_with_trailing_comma_is_kept_broken() { 33 + assert_format!( 34 + r#"pub fn main() { 35 + [ 36 + 1, 37 + 2, 38 + a, 39 + ] 40 + } 41 + "# 42 + ); 43 + } 44 + 45 + #[test] 46 + fn constant_list_with_trailing_comma_is_kept_broken() { 47 + assert_format!( 48 + r#"const list = [ 49 + 1, 50 + 2, 51 + a, 52 + ] 53 + "# 54 + ); 55 + } 56 + 57 + #[test] 58 + fn list_with_no_trailing_comma_is_packed_on_a_single_line() { 59 + assert_format_rewrite!( 60 + r#"pub fn main() { 61 + [ 62 + 1, 63 + 2, 64 + a 65 + ] 66 + } 67 + "#, 68 + r#"pub fn main() { 69 + [1, 2, a] 70 + } 71 + "# 72 + ); 73 + } 74 + 75 + #[test] 76 + fn constant_list_with_no_trailing_comma_is_packed_on_a_single_line() { 77 + assert_format_rewrite!( 78 + r#"const list = [ 79 + 1, 80 + 2, 81 + a 82 + ]"#, 83 + "const list = [1, 2, a]\n" 84 + ); 85 + } 86 + 87 + #[test] 88 + fn list_with_no_comma_is_packed_on_a_single_line_or_split_one_item_per_line() { 89 + assert_format_rewrite!( 90 + "pub fn main() { 91 + [ 92 + 1, 93 + a, 94 + 12_312_312_312_312_312_312_312, 95 + 12_312_312_312_312_312_312_312, 96 + 12_312_312_312_312_312_312_312, 97 + b, 98 + 12_312_312_312_312_312_312_312, 99 + 12_312_312_312_312_312_312_312, 100 + 12_312_312_312_312_312_312_312 101 + ] 102 + } 103 + ", 104 + "pub fn main() { 105 + [ 106 + 1, 107 + a, 108 + 12_312_312_312_312_312_312_312, 109 + 12_312_312_312_312_312_312_312, 110 + 12_312_312_312_312_312_312_312, 111 + b, 112 + 12_312_312_312_312_312_312_312, 113 + 12_312_312_312_312_312_312_312, 114 + 12_312_312_312_312_312_312_312, 115 + ] 116 + } 117 + " 118 + ); 119 + } 120 + 121 + #[test] 122 + fn constant_list_with_no_comma_is_packed_on_a_single_line_or_split_one_item_per_line() { 123 + assert_format_rewrite!( 124 + "const list = [ 125 + 1, 126 + a, 127 + 12_312_312_312_312_312_312_312, 128 + 12_312_312_312_312_312_312_312, 129 + 12_312_312_312_312_312_312_312, 130 + b, 131 + 12_312_312_312_312_312_312_312, 132 + 12_312_312_312_312_312_312_312, 133 + 12_312_312_312_312_312_312_312 134 + ] 135 + ", 136 + "const list = [ 137 + 1, 138 + a, 139 + 12_312_312_312_312_312_312_312, 140 + 12_312_312_312_312_312_312_312, 141 + 12_312_312_312_312_312_312_312, 142 + b, 143 + 12_312_312_312_312_312_312_312, 144 + 12_312_312_312_312_312_312_312, 145 + 12_312_312_312_312_312_312_312, 146 + ] 147 + " 148 + ); 149 + } 150 + 151 + #[test] 152 + fn simple_list_with_no_comma_is_packed_on_a_single_line_or_split_one_item_per_line() { 153 + assert_format_rewrite!( 154 + r#"pub fn main() { 155 + [ 156 + "hello", 157 + "wibble wobble", 158 + "these are all simple strings", 159 + "but the list won't be packed", 160 + "the formatter will keep", 161 + "one item", 162 + "per line", 163 + "since there's no trailing comma here ->" 164 + ] 165 + } 166 + "#, 167 + r#"pub fn main() { 168 + [ 169 + "hello", 170 + "wibble wobble", 171 + "these are all simple strings", 172 + "but the list won't be packed", 173 + "the formatter will keep", 174 + "one item", 175 + "per line", 176 + "since there's no trailing comma here ->", 177 + ] 178 + } 179 + "# 180 + ); 181 + } 182 + 183 + #[test] 184 + fn simple_list_with_trailing_comma_and_multiple_items_per_line_is_packed() { 185 + assert_format_rewrite!( 186 + r#"pub fn main() { 187 + [ 188 + "hello", 189 + "wibble wobble", 190 + "these are all simple strings", 191 + "and the list will be packed since the following strings are", 192 + "on the same", "line", "and there's a trailing comma ->", 193 + ] 194 + } 195 + "#, 196 + r#"pub fn main() { 197 + [ 198 + "hello", "wibble wobble", "these are all simple strings", 199 + "and the list will be packed since the following strings are", "on the same", 200 + "line", "and there's a trailing comma ->", 201 + ] 202 + } 203 + "# 204 + ); 205 + } 206 + 207 + #[test] 208 + fn simple_constant_list_with_trailing_comma_and_multiple_items_per_line_is_packed() { 209 + assert_format_rewrite!( 210 + r#"pub const list = [ 211 + "hello", 212 + "wibble wobble", 213 + "these are all simple strings", 214 + "and the list will be packed since the following strings are", 215 + "on the same", "line", "and there's a trailing comma ->", 216 + ] 217 + "#, 218 + r#"pub const list = [ 219 + "hello", "wibble wobble", "these are all simple strings", 220 + "and the list will be packed since the following strings are", "on the same", 221 + "line", "and there's a trailing comma ->", 222 + ] 223 + "# 224 + ); 225 + } 226 + 227 + #[test] 228 + fn simple_packed_list_with_trailing_comma_is_kept_with_multiple_items_per_line() { 229 + assert_format!( 230 + r#"pub fn main() { 231 + [ 232 + "hello", "wibble wobble", "these are all simple strings", 233 + "and the list will be kept packed since it ends with a trailing comma", 234 + "right here! ->", 235 + ] 236 + } 237 + "# 238 + ); 239 + } 240 + 241 + #[test] 242 + fn simple_single_line_list_with_trailing_comma_is_split_one_item_per_line() { 243 + assert_format_rewrite!( 244 + r#"pub fn main() { 245 + ["these are all simple strings", "but the list won't be packed", "since it ends with a trailing comma ->",] 246 + } 247 + "#, 248 + r#"pub fn main() { 249 + [ 250 + "these are all simple strings", 251 + "but the list won't be packed", 252 + "since it ends with a trailing comma ->", 253 + ] 254 + } 255 + "# 256 + ); 257 + } 258 + 259 + #[test] 260 + fn simple_single_line_list_with_no_trailing_comma_is_split_one_item_per_line() { 261 + assert_format_rewrite!( 262 + r#"pub fn main() { 263 + ["these are all simple strings", "but the list won't be packed", "even if it doesn't end with a trailing comma!"] 264 + } 265 + "#, 266 + r#"pub fn main() { 267 + [ 268 + "these are all simple strings", 269 + "but the list won't be packed", 270 + "even if it doesn't end with a trailing comma!", 271 + ] 272 + } 273 + "# 274 + ); 275 + }