#![deny(missing_docs)]
#![deny(unreachable_patterns)]
#[macro_use]
extern crate derive_is_enum_variant;
extern crate diff;
use std::env;
use std::fs::File;
use std::io::Read;
use std::process::Command;
#[test]
fn cargo_readme_up_to_date() {
if env::var("CI").is_ok() {
return;
}
let expected = Command::new("cargo")
.arg("readme")
.current_dir(env!("CARGO_MANIFEST_DIR"))
.output()
.expect("should run `cargo readme` OK")
.stdout;
let expected = String::from_utf8_lossy(&expected);
let actual = {
let mut file = File::open(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"))
.expect("should open README.md file");
let mut s = String::new();
file.read_to_string(&mut s)
.expect("should read contents of file to string");
s
};
if actual != expected {
println!();
println!("+++ expected README.md");
println!("--- actual README.md");
for d in diff::lines(&expected, &actual) {
match d {
diff::Result::Left(l) => println!("+{}", l),
diff::Result::Right(r) => println!("-{}", r),
diff::Result::Both(b, _) => println!(" {}", b),
}
}
panic!("Run `cargo readme > README.md` to update README.md")
}
}
#[derive(is_enum_variant)]
pub enum Pet {
Doggo,
Kitteh,
FlyingSquirrel,
}
#[test]
fn basic_enum_predicates() {
let doggo = Pet::Doggo;
assert!(doggo.is_doggo());
assert!(!doggo.is_kitteh());
assert!(!doggo.is_flying_squirrel());
let kitteh = Pet::Kitteh;
assert!(!kitteh.is_doggo());
assert!(kitteh.is_kitteh());
assert!(!kitteh.is_flying_squirrel());
let squirrel = Pet::FlyingSquirrel;
assert!(!squirrel.is_doggo());
assert!(!squirrel.is_kitteh());
assert!(squirrel.is_flying_squirrel());
}
#[derive(is_enum_variant)]
pub enum VariantKinds {
Struct { x: usize, y: usize },
Tuple(usize, usize),
Unit,
}
#[test]
fn variant_kinds() {
assert!(VariantKinds::Struct { x: 1, y: 2 }.is_struct());
assert!(VariantKinds::Tuple(1, 2).is_tuple());
assert!(VariantKinds::Unit.is_unit());
}
#[allow(non_camel_case_types)]
#[derive(is_enum_variant)]
pub enum Funky {
CAPS,
SHOUTING_SNAKE,
snake_case,
littleCamel,
WithACRONYM,
}
#[test]
fn funky_variant_names() {
assert!(Funky::CAPS.is_caps());
assert!(Funky::SHOUTING_SNAKE.is_shouting_snake());
assert!(Funky::snake_case.is_snake_case());
assert!(Funky::littleCamel.is_little_camel());
assert!(Funky::WithACRONYM.is_with_acronym());
}
#[derive(is_enum_variant)]
pub enum CustomNames {
#[is_enum_variant(name = "i_dont_know_why_you_say")] Goodbye,
#[is_enum_variant(name = "i_say")] Hello,
}
#[test]
fn custom_predicate_names() {
assert!(CustomNames::Goodbye.i_dont_know_why_you_say());
assert!(CustomNames::Hello.i_say());
}
#[derive(is_enum_variant)]
pub enum Skip {
#[is_enum_variant(skip)] NoPredicate,
YesPredicate,
}
#[test]
fn skip_variants() {
assert!(Skip::YesPredicate.is_yes_predicate());
}
#[derive(is_enum_variant)]
pub enum GeneratedCodeHasNoWarnings {
OnlyOneVariant,
}