fix: use prettier-please just in tests - no custom wrapper (#2767)

* fix: use prettier-please just in tests - no custom wrapper
This commit is contained in:
Jonathan Kelley authored and GitHub committed 2024-08-01 17:46:51 -07:00
1 parent e5e578d27b
commit ae4a2464b3
12 files changed
+377 -368

No files matched your search

+211 -205
View File
@@ -404,216 +404,222 @@ impl Display for ElementName {
}
}
#[test]
fn parses_name() {
let _parsed: ElementName = syn::parse2(quote::quote! { div }).unwrap();
let _parsed: ElementName = syn::parse2(quote::quote! { some-cool-element }).unwrap();
#[cfg(test)]
mod tests {
use super::*;
use prettier_please::PrettyUnparse;
let _parsed: Element = syn::parse2(quote::quote! { div {} }).unwrap();
let _parsed: Element = syn::parse2(quote::quote! { some-cool-element {} }).unwrap();
#[test]
fn parses_name() {
let _parsed: ElementName = syn::parse2(quote::quote! { div }).unwrap();
let _parsed: ElementName = syn::parse2(quote::quote! { some-cool-element }).unwrap();
let parsed: Element = syn::parse2(quote::quote! {
some-cool-div {
id: "hi",
id: "hi {abc}",
id: "hi {def}",
class: 123,
something: bool,
data_attr: "data",
data_attr: "data2",
data_attr: "data3",
exp: { some_expr },
something: {cool},
something: bool,
something: 123,
onclick: move |_| {
println!("hello world");
},
"some-attr": "hello world",
onclick: move |_| {},
class: "hello world",
id: "my-id",
data_attr: "data",
data_attr: "data2",
data_attr: "data3",
"somte_attr3": "hello world",
something: {cool},
something: bool,
something: 123,
onclick: move |_| {
println!("hello world");
},
..attrs1,
..attrs2,
..attrs3
}
})
.unwrap();
let _parsed: Element = syn::parse2(quote::quote! { div {} }).unwrap();
let _parsed: Element = syn::parse2(quote::quote! { some-cool-element {} }).unwrap();
dbg!(parsed);
}
#[test]
fn parses_variety() {
let input = quote::quote! {
div {
class: "hello world",
id: "my-id",
data_attr: "data",
data_attr: "data2",
data_attr: "data3",
"somte_attr3": "hello world",
something: {cool},
something: bool,
something: 123,
onclick: move |_| {
println!("hello world");
},
..attrs,
..attrs2,
..attrs3
}
};
let parsed: Element = syn::parse2(input).unwrap();
dbg!(parsed);
}
#[test]
fn to_tokens_properly() {
let input = quote::quote! {
div {
class: "hello world",
class2: "hello {world}",
class3: "goodbye {world}",
class4: "goodbye world",
"something": "cool {blah}",
"something2": "cooler",
div {
div {
h1 { class: "h1 col" }
h2 { class: "h2 col" }
h3 { class: "h3 col" }
div {}
}
}
}
};
let parsed: Element = syn::parse2(input).unwrap();
println!("{}", parsed.to_token_stream().pretty_unparse());
}
#[test]
fn to_tokens_with_diagnostic() {
let input = quote::quote! {
div {
class: "hello world",
id: "my-id",
..attrs,
div {
..attrs,
let parsed: Element = syn::parse2(quote::quote! {
some-cool-div {
id: "hi",
id: "hi {abc}",
id: "hi {def}",
class: 123,
something: bool,
data_attr: "data",
data_attr: "data2",
data_attr: "data3",
exp: { some_expr },
something: {cool},
something: bool,
something: 123,
onclick: move |_| {
println!("hello world");
},
"some-attr": "hello world",
onclick: move |_| {},
class: "hello world",
id: "my-id",
data_attr: "data",
data_attr: "data2",
data_attr: "data3",
"somte_attr3": "hello world",
something: {cool},
something: bool,
something: 123,
onclick: move |_| {
println!("hello world");
},
..attrs1,
..attrs2,
..attrs3
}
}
};
})
.unwrap();
let parsed: Element = syn::parse2(input).unwrap();
println!("{}", parsed.to_token_stream().pretty_unparse());
}
#[test]
fn merges_attributes() {
let input = quote::quote! {
div {
class: "hello world",
class: if count > 3 { "abc {def}" },
class: if count < 50 { "small" } else { "big" }
}
};
let parsed: Element = syn::parse2(input).unwrap();
assert_eq!(parsed.diagnostics.len(), 0);
assert_eq!(parsed.merged_attributes.len(), 1);
assert_eq!(
parsed.merged_attributes[0].name.to_string(),
"class".to_string()
);
let attr = &parsed.merged_attributes[0].value;
println!("{}", attr.to_token_stream().pretty_unparse());
let _attr = match attr {
AttributeValue::AttrLiteral(lit) => lit,
_ => panic!("expected literal"),
};
}
/// There are a number of cases where merging attributes doesn't make sense
/// - merging two expressions together
/// - merging two literals together
/// - merging a literal and an expression together
///
/// etc
///
/// We really only want to merge formatted things together
///
/// IE
/// class: "hello world ",
/// class: if some_expr { "abc" }
///
/// Some open questions - should the delimiter be explicit?
#[test]
fn merging_weird_fails() {
let input = quote::quote! {
div {
class: "hello world",
class: if some_expr { 123 },
style: "color: red;",
style: "color: blue;",
width: "1px",
width: 1,
width: false,
contenteditable: true,
}
};
let parsed: Element = syn::parse2(input).unwrap();
assert_eq!(parsed.merged_attributes.len(), 4);
assert_eq!(parsed.diagnostics.len(), 3);
// style should not generate a diagnostic
assert!(!parsed
.diagnostics
.diagnostics
.into_iter()
.any(|f| f.emit_as_item_tokens().to_string().contains("style")));
}
#[test]
fn diagnostics() {
let input = quote::quote! {
p {
class: "foo bar"
"Hello world"
}
};
let _parsed: Element = syn::parse2(input).unwrap();
}
#[test]
fn parses_raw_elements() {
let input = quote::quote! {
use {
"hello"
}
};
let _parsed: Element = syn::parse2(input).unwrap();
dbg!(parsed);
}
#[test]
fn parses_variety() {
let input = quote::quote! {
div {
class: "hello world",
id: "my-id",
data_attr: "data",
data_attr: "data2",
data_attr: "data3",
"somte_attr3": "hello world",
something: {cool},
something: bool,
something: 123,
onclick: move |_| {
println!("hello world");
},
..attrs,
..attrs2,
..attrs3
}
};
let parsed: Element = syn::parse2(input).unwrap();
dbg!(parsed);
}
#[test]
fn to_tokens_properly() {
let input = quote::quote! {
div {
class: "hello world",
class2: "hello {world}",
class3: "goodbye {world}",
class4: "goodbye world",
"something": "cool {blah}",
"something2": "cooler",
div {
div {
h1 { class: "h1 col" }
h2 { class: "h2 col" }
h3 { class: "h3 col" }
div {}
}
}
}
};
let parsed: Element = syn::parse2(input).unwrap();
println!("{}", parsed.to_token_stream().pretty_unparse());
}
#[test]
fn to_tokens_with_diagnostic() {
let input = quote::quote! {
div {
class: "hello world",
id: "my-id",
..attrs,
div {
..attrs,
class: "hello world",
id: "my-id",
}
}
};
let parsed: Element = syn::parse2(input).unwrap();
println!("{}", parsed.to_token_stream().pretty_unparse());
}
#[test]
fn merges_attributes() {
let input = quote::quote! {
div {
class: "hello world",
class: if count > 3 { "abc {def}" },
class: if count < 50 { "small" } else { "big" }
}
};
let parsed: Element = syn::parse2(input).unwrap();
assert_eq!(parsed.diagnostics.len(), 0);
assert_eq!(parsed.merged_attributes.len(), 1);
assert_eq!(
parsed.merged_attributes[0].name.to_string(),
"class".to_string()
);
let attr = &parsed.merged_attributes[0].value;
println!("{}", attr.to_token_stream().pretty_unparse());
let _attr = match attr {
AttributeValue::AttrLiteral(lit) => lit,
_ => panic!("expected literal"),
};
}
/// There are a number of cases where merging attributes doesn't make sense
/// - merging two expressions together
/// - merging two literals together
/// - merging a literal and an expression together
///
/// etc
///
/// We really only want to merge formatted things together
///
/// IE
/// class: "hello world ",
/// class: if some_expr { "abc" }
///
/// Some open questions - should the delimiter be explicit?
#[test]
fn merging_weird_fails() {
let input = quote::quote! {
div {
class: "hello world",
class: if some_expr { 123 },
style: "color: red;",
style: "color: blue;",
width: "1px",
width: 1,
width: false,
contenteditable: true,
}
};
let parsed: Element = syn::parse2(input).unwrap();
assert_eq!(parsed.merged_attributes.len(), 4);
assert_eq!(parsed.diagnostics.len(), 3);
// style should not generate a diagnostic
assert!(!parsed
.diagnostics
.diagnostics
.into_iter()
.any(|f| f.emit_as_item_tokens().to_string().contains("style")));
}
#[test]
fn diagnostics() {
let input = quote::quote! {
p {
class: "foo bar"
"Hello world"
}
};
let _parsed: Element = syn::parse2(input).unwrap();
}
#[test]
fn parses_raw_elements() {
let input = quote::quote! {
use {
"hello"
}
};
let _parsed: Element = syn::parse2(input).unwrap();
}
}