🐦‍⬛ Snapshot testing in Gleam
0

Configure Feed

Select the types of activity you want to include in your feed.

do not escape newlines in titles when displaying them

author
Giacomo Cavalieri
date (Jun 15, 2026, 10:56 AM +0200) commit 793f99cc parent 1404298b change-id xlzvoryn
+42 -3
+5
CHANGELOG.md
··· 1 1 # Changelog 2 2 3 + ## 2.0.1 - 2026-06-15 4 + 5 + - Fixed a bug where newline characters would be displayed as escaped when 6 + displaying a snapshot title containing one. 7 + 3 8 ## 2.0.0 - 2026-05-03 4 9 5 10 - Birdie snapshots are now located under the `test/birdie_snapshots` directory.
+17 -3
src/birdie.gleam
··· 386 386 content, 387 387 )) -> 388 388 Ok(Snapshot( 389 - title: string.trim(title), 389 + title: unescape_title(title) |> string.trim, 390 390 content: trim_content(content, based_on: version), 391 391 info: None, 392 392 )) ··· 416 416 content, 417 417 )) -> 418 418 Ok(Snapshot( 419 - title: string.trim(title), 419 + title: unescape_title(title) |> string.trim, 420 420 content: trim_content(content, based_on: version), 421 421 info: Some(SnapshotInfo( 422 422 file: string.trim(file), ··· 429 429 } 430 430 } 431 431 432 + fn unescape_title(title: String) -> String { 433 + // All backslashes have been escaped as "\\\\" 434 + string.split(title, on: "\\\\") 435 + |> list.map(string.replace(in: _, each: "\\n", with: "\n")) 436 + |> string.join(with: "\\") 437 + } 438 + 439 + fn escape_title(title: String) -> String { 440 + // We escape newlines and backslash so that the title will be on a single line 441 + // in the file once it's saved. 442 + string.replace(title, each: "\n", with: "\\n") 443 + |> string.replace(each: "\\", with: "\\\\") 444 + } 445 + 432 446 /// Birdie started adding newlines to the end of files starting from `1.4.0`, 433 447 /// so if we're reading a snapshot created from `1.4.0` onwards then we want to 434 448 /// make sure to remove that newline! ··· 466 480 // easier to parse. 467 481 // Is this the best course of action? Probably not. 468 482 // Does this make my life a lot easier? Absolutely! 😁 469 - "title: " <> string.replace(title, each: "\n", with: "\\n"), 483 + "title: " <> escape_title(title), 470 484 ], 471 485 info_lines, 472 486 ["---", content],
+5
test/birdie_snapshots/this_title_contains_a_newline.accepted
··· 1 + --- 2 + version: 2.0.0 3 + title: this title\n contains a newline 4 + --- 5 + "this snapshot's title should be on two lines"
+5
test/birdie_snapshots/this_title_n_is_displayed_on_a_single_line.accepted
··· 1 + --- 2 + version: 2.0.0 3 + title: this title\\n is displayed on a single line 4 + --- 5 + "this snapshot's title should be on two lines"
+10
test/birdie_test.gleam
··· 16 16 |> birdie.snap(title: "a result test") 17 17 } 18 18 19 + pub fn newlines_in_title_are_shown_normally_test() { 20 + string.inspect("this snapshot's title should be on two lines") 21 + |> birdie.snap(title: "this title\n contains a newline") 22 + } 23 + 24 + pub fn escaped_newlines_in_title_stay_escaped_test() { 25 + string.inspect("this snapshot's title should be on two lines") 26 + |> birdie.snap(title: "this title\\n is displayed on a single line") 27 + } 28 + 19 29 pub fn list_test() { 20 30 "[ 1, 2, 3, 4 ]" 21 31 |> birdie.snap(title: "list test")