1use dprint_core::formatting::PrintOptions;2use indoc::indoc;34use crate::Printable;56fn reformat(input: &str) -> String {7 let (source, _) = jrsonnet_rowan_parser::parse(input);89 dprint_core::formatting::format(10 || source.print(),11 PrintOptions {12 indent_width: 2,13 max_width: 100,14 use_tabs: false,15 new_line_text: "\n",16 },17 )18}1920macro_rules! assert_formatted {21 ($input:literal, $output:literal) => {22 let formatted = reformat(indoc!($input));23 let mut expected = indoc!($output).to_owned();24 expected.push('\n');25 if formatted != expected {26 panic!(27 "bad formatting, expected\n```\n{formatted}\n```\nto be equal to\n```\n{expected}\n```",28 )29 }30 };31}3233#[test]34fn padding_stripped_for_multiline_comment() {35 assert_formatted!(36 "{37 /*38 Hello39 World40 */41 _: null,42 }",43 "{44 /*45 Hello46 World47 */48 _: null,49 }"50 );51}5253#[test]54fn last_comment_respects_spacing_with_inline_comment_above() {55 assert_formatted!(56 "{57 a: '', // Inline5859 // Comment60 }",61 "{62 a: '', // Inline6364 // Comment65 }"66 );67}6869#[test]70fn complex_comments_snapshot() {71 insta::assert_display_snapshot!(reformat(indoc!(72 "{73 comments: {74 _: '',75 // Plain comment76 a: '',7778 # Plain comment with empty line before79 b: '',80 /*Single-line multiline comment8182 */83 c: '',8485 /**Single-line multiline doc comment8687 */88 c: '',8990 /**Multiline doc91 Comment92 */93 c: '',9495 /*9697 Multi-line9899 comment100 */101 d: '',102103 e: '', // Inline comment104105 k: '',106107 // Text after everything108 },109 comments2: {110 k: '',111 // Text after everything, but no newline above112 },113 spacing: {114 a: '',115116 b: '',117 },118 noSpacing: {119 a: '',120 b: '',121 },122 }"123 )))124}