|
@@ -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();
|
|
|
|
-
|
|
|
|
- let _parsed: Element = syn::parse2(quote::quote! { div {} }).unwrap();
|
|
|
|
- let _parsed: Element = 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();
|
|
|
|
|
|
+#[cfg(test)]
|
|
|
|
+mod tests {
|
|
|
|
+ use super::*;
|
|
|
|
+ use prettier_please::PrettyUnparse;
|
|
|
|
+
|
|
|
|
+ #[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! { div {} }).unwrap();
|
|
|
|
+ let _parsed: Element = 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();
|
|
|
|
|
|
- dbg!(parsed);
|
|
|
|
-}
|
|
|
|
|
|
+ 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
|
|
|
|
- }
|
|
|
|
- };
|
|
|
|
|
|
+ #[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);
|
|
|
|
-}
|
|
|
|
|
|
+ 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",
|
|
|
|
|
|
+ #[test]
|
|
|
|
+ fn to_tokens_properly() {
|
|
|
|
+ let input = quote::quote! {
|
|
div {
|
|
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 {}
|
|
|
|
|
|
+ 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());
|
|
|
|
-}
|
|
|
|
|
|
+ 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,
|
|
|
|
|
|
+ #[test]
|
|
|
|
+ fn to_tokens_with_diagnostic() {
|
|
|
|
+ let input = quote::quote! {
|
|
div {
|
|
div {
|
|
- ..attrs,
|
|
|
|
class: "hello world",
|
|
class: "hello world",
|
|
id: "my-id",
|
|
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());
|
|
|
|
-}
|
|
|
|
|
|
+ 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" }
|
|
|
|
- }
|
|
|
|
- };
|
|
|
|
|
|
+ #[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 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;
|
|
|
|
|
|
+ let attr = &parsed.merged_attributes[0].value;
|
|
|
|
|
|
- println!("{}", attr.to_token_stream().pretty_unparse());
|
|
|
|
|
|
+ println!("{}", attr.to_token_stream().pretty_unparse());
|
|
|
|
|
|
- let _attr = match attr {
|
|
|
|
- AttributeValue::AttrLiteral(lit) => lit,
|
|
|
|
- _ => panic!("expected literal"),
|
|
|
|
- };
|
|
|
|
-}
|
|
|
|
|
|
+ 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,
|
|
|
|
- }
|
|
|
|
- };
|
|
|
|
|
|
+ /// 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 },
|
|
|
|
|
|
- let parsed: Element = syn::parse2(input).unwrap();
|
|
|
|
|
|
+ style: "color: red;",
|
|
|
|
+ style: "color: blue;",
|
|
|
|
|
|
- assert_eq!(parsed.merged_attributes.len(), 4);
|
|
|
|
- assert_eq!(parsed.diagnostics.len(), 3);
|
|
|
|
|
|
+ width: "1px",
|
|
|
|
+ width: 1,
|
|
|
|
+ width: false,
|
|
|
|
+ contenteditable: true,
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
|
|
- // style should not generate a diagnostic
|
|
|
|
- assert!(!parsed
|
|
|
|
- .diagnostics
|
|
|
|
- .diagnostics
|
|
|
|
- .into_iter()
|
|
|
|
- .any(|f| f.emit_as_item_tokens().to_string().contains("style")));
|
|
|
|
-}
|
|
|
|
|
|
+ let parsed: Element = syn::parse2(input).unwrap();
|
|
|
|
|
|
-#[test]
|
|
|
|
-fn diagnostics() {
|
|
|
|
- let input = quote::quote! {
|
|
|
|
- p {
|
|
|
|
- class: "foo bar"
|
|
|
|
- "Hello world"
|
|
|
|
- }
|
|
|
|
- };
|
|
|
|
|
|
+ assert_eq!(parsed.merged_attributes.len(), 4);
|
|
|
|
+ assert_eq!(parsed.diagnostics.len(), 3);
|
|
|
|
|
|
- let _parsed: Element = syn::parse2(input).unwrap();
|
|
|
|
-}
|
|
|
|
|
|
+ // 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 parses_raw_elements() {
|
|
|
|
- let input = quote::quote! {
|
|
|
|
- use {
|
|
|
|
- "hello"
|
|
|
|
- }
|
|
|
|
- };
|
|
|
|
|
|
+ #[test]
|
|
|
|
+ fn diagnostics() {
|
|
|
|
+ let input = quote::quote! {
|
|
|
|
+ p {
|
|
|
|
+ class: "foo bar"
|
|
|
|
+ "Hello world"
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ let _parsed: Element = syn::parse2(input).unwrap();
|
|
|
|
+ }
|
|
|
|
|
|
- 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();
|
|
|
|
+ }
|
|
}
|
|
}
|