1use dprint_core::formatting::{PrintOptions, PrintItems};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 || {11 let mut out = PrintItems::new();12 source.print(&mut out);13 out14 },15 PrintOptions {16 indent_width: 2,17 max_width: 100,18 use_tabs: true,19 new_line_text: "\n",20 },21 )22}2324macro_rules! assert_formatted {25 ($input:literal, $output:literal) => {26 let formatted = reformat(indoc!($input));27 let mut expected = indoc!($output).to_owned();28 expected.push('\n');29 if formatted != expected {30 panic!(31 "bad formatting, expected\n```\n{formatted}\n```\nto be equal to\n```\n{expected}\n```",32 )33 }34 };35}3637#[test]38fn padding_stripped_for_multiline_comment() {39 assert_formatted!(40 "{41 /*42 Hello43 World44 */45 _: null,46 }",47 "{48 /*49 Hello50 World51 */52 _: null,53 }"54 );55}5657#[test]58fn last_comment_respects_spacing_with_inline_comment_above() {59 assert_formatted!(60 "{61 a: '', // Inline6263 // Comment64 }",65 "{66 a: '', // Inline6768 // Comment69 }"70 );71}7273#[test]74fn complex_comments_snapshot() {75 insta::assert_display_snapshot!(reformat(indoc!(76 "{77 comments: {78 _: '',79 // Plain comment80 a: '',8182 # Plain comment with empty line before83 b: '',84 /*Single-line multiline comment8586 */87 c: '',8889 /**Single-line multiline doc comment9091 */92 c: '',9394 /**Multiline doc95 Comment96 */97 c: '',9899 /*100101 Multi-line102103 comment104 */105 d: '',106107 e: '', // Inline comment108109 k: '',110111 // Text after everything112 },113 comments2: {114 k: '',115 // Text after everything, but no newline above116 },117 spacing: {118 a: '',119120 b: '',121 },122 noSpacing: {123 a: '',124 b: '',125 },126 }"127 )))128}