1
0
mirror of https://github.com/danog/strum.git synced 2024-12-04 10:28:13 +01:00
strum/strum_tests/tests/enum_try_as.rs

49 lines
996 B
Rust
Raw Normal View History

2023-07-17 04:59:38 +02:00
use strum::EnumTryAs;
#[derive(EnumTryAs)]
enum Foo {
Unnamed0(),
Unnamed1(u128),
Unnamed2(bool, String),
#[strum(disabled)]
#[allow(dead_code)]
Disabled(u32),
2023-07-31 22:44:38 +02:00
#[allow(dead_code)]
Unit,
#[allow(dead_code)]
Named { _a: u32, _b: String },
2023-07-17 04:59:38 +02:00
}
#[test]
fn unnamed_0() {
let foo = Foo::Unnamed0();
assert_eq!(Some(()), foo.try_as_unnamed_0());
}
#[test]
fn unnamed_1() {
let foo = Foo::Unnamed1(128);
assert_eq!(Some(&128), foo.try_as_unnamed_1_ref());
}
#[test]
fn unnamed_2() {
let foo = Foo::Unnamed2(true, String::from("Hay"));
assert_eq!(Some((true, String::from("Hay"))), foo.try_as_unnamed_2());
}
#[test]
fn can_mutate() {
let mut foo = Foo::Unnamed1(128);
if let Some(value) = foo.try_as_unnamed_1_mut() {
*value = 44_u128;
}
assert_eq!(foo.try_as_unnamed_1(), Some(44));
}
#[test]
fn doesnt_match_other_variations() {
let foo = Foo::Unnamed1(66);
assert_eq!(None, foo.try_as_unnamed_0());
}