Files
dioxus/packages/autofmt/tests/samples.rs
T
Sebastian Flügge 82c3cab25f fix(autofmt): preserve indentation of commented-out RSX blocks (#5464)
* test: add failing idempotency tests for commented-out RSX blocks

Two variants of the regression introduced in v0.7.4 (#5257):
- commented_rsx_block: first line keeps indent, rest gets dedented
- commented_rsx_block_nested: entire block gets dedented

See #5463

* fix(autofmt): preserve indentation of commented-out RSX blocks

Trailing comments before `}` in write_rsx_block were indented at
parent level instead of child level. Truncate the tab from
tabbed_line() and bump indent to child level before
apply_line_comments, same approach as write_trailing_body_comments.

Fixes #5463
2026-04-21 17:16:20 -07:00

112 lines
2.6 KiB
Rust

#![allow(deprecated)]
macro_rules! twoway {
(
$(
// doc attrs
$( #[doc = $doc:expr] )*
$name:ident,
)*
) => {
$(
$( #[doc = $doc] )*
#[test]
fn $name() {
let src = include_str!(concat!("./samples/", stringify!($name), ".rsx"));
let formatted = dioxus_autofmt::fmt_file(src, Default::default());
let out = dioxus_autofmt::apply_formats(src, formatted);
// normalize line endings
let out = out.replace("\r", "");
let src = src.replace("\r", "");
pretty_assertions::assert_eq!(&src, &out);
}
)*
};
}
twoway![
attributes,
basic_expr,
collapse_expr,
comments_attr_expr_blocks,
comments,
comments_async_closure,
comments_expr_with_strings,
comments_nested_closures,
commentshard,
complex,
docsite,
emoji,
fat_exprs,
ifchain_forloop,
immediate_expr,
key,
letsome,
long_exprs,
long,
manual_props,
many_exprs,
messy_indent,
misplaced,
multirsx,
nested,
raw_strings,
reallylong,
shorthand,
simple,
skip,
spaces,
staged,
t2,
tiny,
tinynoopt,
trailing_expr,
oneline,
prop_rsx,
asset,
collapse,
expr_on_conditional,
blank_lines,
blank_lines_preserved,
forloop_tuple,
commented_rsx_block,
commented_rsx_block_nested,
commented_rsx_block_only,
commented_rsx_block_between,
commented_rsx_block_deep,
];
fn assert_idempotent(src: &str) {
let src = src.replace("\r", "");
let once =
dioxus_autofmt::apply_formats(&src, dioxus_autofmt::fmt_file(&src, Default::default()))
.replace("\r", "");
let twice =
dioxus_autofmt::apply_formats(&once, dioxus_autofmt::fmt_file(&once, Default::default()))
.replace("\r", "");
pretty_assertions::assert_eq!(src, once);
pretty_assertions::assert_eq!(once, twice);
}
#[test]
fn comments_async_closure_is_idempotent() {
assert_idempotent(include_str!("./samples/comments_async_closure.rsx"));
}
#[test]
fn comments_nested_closures_is_idempotent() {
assert_idempotent(include_str!("./samples/comments_nested_closures.rsx"));
}
#[test]
fn comments_attr_expr_blocks_is_idempotent() {
assert_idempotent(include_str!("./samples/comments_attr_expr_blocks.rsx"));
}
#[test]
fn comments_expr_with_strings_is_idempotent() {
assert_idempotent(include_str!("./samples/comments_expr_with_strings.rsx"));
}