Skip to content

Commit

Permalink
chore: Add tests for fn binding syntax.
Browse files Browse the repository at this point in the history
  • Loading branch information
vldm committed Jul 18, 2023
1 parent c4ccde4 commit 400f047
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/node/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub enum KeyedAttributeValue {
impl KeyedAttributeValue {
pub fn to_value(&self) -> Option<&AttributeValueExpr> {
match self {
KeyedAttributeValue::Value(v) => Some(&v),
KeyedAttributeValue::Value(v) => Some(v),
KeyedAttributeValue::None => None,
KeyedAttributeValue::Binding(_) => None,
}
Expand Down Expand Up @@ -265,7 +265,7 @@ impl Parse for FnBinding {
fn parse(stream: ParseStream) -> syn::Result<Self> {
let content;
let paren = syn::parenthesized!(content in stream);
let inputs = Punctuated::parse_terminated_with(&content, |stream| closure_arg(stream))?;
let inputs = Punctuated::parse_terminated_with(&content, closure_arg)?;
Ok(Self { paren, inputs })
}
}
Expand Down
1 change: 1 addition & 0 deletions src/node/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ fn block_transform(input: ParseStream, transform_fn: &TransformBlockFn) -> syn::
})
}

#[allow(clippy::needless_pass_by_ref_mut)]
fn parse_valid_block_expr(
parser: &mut RecoverableContext,
input: syn::parse::ParseStream,
Expand Down
66 changes: 64 additions & 2 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rstml::{
node::{KeyedAttribute, KeyedAttributeValue, Node, NodeAttribute, NodeElement, NodeType},
parse2, Parser, ParserConfig,
};
use syn::{parse_quote, Block, LifetimeParam, TypeParam};
use syn::{parse_quote, token::Colon, Block, LifetimeParam, Pat, PatType, Token, TypeParam};

#[test]
fn test_single_empty_element() -> Result<()> {
Expand Down Expand Up @@ -354,7 +354,7 @@ fn test_fn_bind_with_type() -> Result<()> {
assert_eq!(binding.inputs.len(), 1);
match binding.inputs.first().unwrap() {
syn::Pat::Type(x) => {
assert!(matches!(&*x.pat, syn::Pat::Ident(x) if x.ident.to_string() == "x"));
assert!(matches!(&*x.pat, syn::Pat::Ident(x) if x.ident == "x"));
match &*x.ty {
syn::Type::Path(p) => {
assert_eq!(p.path.segments.first().unwrap().ident.to_string(), "Y")
Expand Down Expand Up @@ -603,6 +603,7 @@ fn test_generics() -> Result<()> {

Ok(())
}

#[test]
fn test_generics_lifetime() -> Result<()> {
let tokens = quote! {
Expand All @@ -617,6 +618,7 @@ fn test_generics_lifetime() -> Result<()> {

Ok(())
}

#[test]
fn test_generics_closed() -> Result<()> {
let tokens = quote! {
Expand Down Expand Up @@ -662,6 +664,66 @@ fn test_generics_closed_not_match() -> Result<()> {
Ok(())
}

#[test]
fn test_attribute_fn_binding() -> Result<()> {
let tokens = quote! {
<tag attribute(x:u32, // bind variable
Foo{y: Bar}:Foo, // destructing
baz // variable without type (like in closure)
) />
};

let nodes = parse2(tokens)?;

assert_eq!(nodes.len(), 1);
let Node::Element(e) = &nodes[0] else {
unreachable!()
};
assert_eq!(e.attributes().len(), 1);
let NodeAttribute::Attribute(a) = &e.open_tag.attributes[0] else {
unreachable!()
};
assert_eq!(a.key.to_string(), "attribute");
let KeyedAttributeValue::Binding(b) = &a.possible_value else {
unreachable!()
};
assert_eq!(b.inputs.len(), 3);
let pat = &b.inputs[0];

let expected: PatType = PatType {
pat: parse_quote!(x),
colon_token: Colon::default(),
ty: parse_quote!(u32),
attrs: vec![],
};
assert_eq!(pat, &Pat::Type(expected));

let pat = &b.inputs[1];

let expected: PatType = PatType {
pat: parse_quote!(Foo { y: Bar }),
colon_token: Colon::default(),
ty: parse_quote!(Foo),
attrs: vec![],
};
assert_eq!(pat, &Pat::Type(expected));

let pat = &b.inputs[2];
assert_eq!(pat, &parse_quote!(baz));
Ok(())
}

#[test]
// during parsing ="foo"
#[should_panic = "invalid tag name or attribute key"]
fn test_attribute_fn_binding_with_value_is_not_expected() {
let tokens = quote! {
<tag attribute(x:u32)="foo" />
};

parse2(tokens).unwrap();
}

#[test]
fn test_reserved_keywords() -> Result<()> {
let tokens = quote! {
Expand Down

0 comments on commit 400f047

Please sign in to comment.