Skip to content

Commit

Permalink
Add tests to util::IdentList
Browse files Browse the repository at this point in the history
  • Loading branch information
TedDriggs committed Jan 21, 2019
1 parent e0347f7 commit 66cde1e
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion core/src/util/ident_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,50 @@ impl FromMeta for IdentList {
if let NestedMeta::Meta(Meta::Word(ref ident)) = *nmi {
idents.push(ident.clone());
} else {
return Err(Error::unexpected_type("non-word"));
return Err(Error::unexpected_type("non-word").with_span(nmi));
}
}

Ok(IdentList(idents))
}
}

#[cfg(test)]
mod tests {
use super::IdentList;
use proc_macro2::TokenStream;
use FromMeta;

/// parse a string as a syn::Meta instance.
fn pm(tokens: TokenStream) -> ::std::result::Result<syn::Meta, String> {
let attribute: syn::Attribute = parse_quote!(#[#tokens]);
attribute.interpret_meta().ok_or("Unable to parse".into())
}

fn fm<T: FromMeta>(tokens: TokenStream) -> T {
FromMeta::from_meta(&pm(tokens).expect("Tests should pass well-formed input"))
.expect("Tests should pass valid input")
}

#[test]
fn succeeds() {
let idents = fm::<IdentList>(quote!(ignore(Debug, Clone, Eq)));
assert_eq!(
idents.to_strings(),
vec![
String::from("Debug"),
String::from("Clone"),
String::from("Eq")
]
);
}

/// Check that the parser rejects non-word members of the list, and that the error
/// has an associated span.
#[test]
fn fails_non_word() {
let input = IdentList::from_meta(&pm(quote!(ignore(Debug, Clone = false))).unwrap());
let err = input.unwrap_err();
assert!(err.has_span());
}
}

0 comments on commit 66cde1e

Please sign in to comment.