-
Notifications
You must be signed in to change notification settings - Fork 140
/
display.rs
65 lines (58 loc) · 1.57 KB
/
display.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
extern crate strum;
#[macro_use]
extern crate strum_macros;
#[derive(Debug, Eq, PartialEq, EnumString, Display)]
enum Color {
#[strum(to_string = "RedRed")]
Red,
#[strum(serialize = "b", to_string = "blue")]
Blue { hue: usize },
#[strum(serialize = "y", serialize = "yellow")]
Yellow,
#[strum(default = "true")]
Green(String),
}
#[test]
fn to_blue_string() {
assert_eq!(String::from("blue"), format!("{}", Color::Blue { hue: 0 }));
}
#[test]
fn test_formatters() {
assert_eq!(String::from(" blue"), format!("{:>6}", Color::Blue { hue: 0 }));
assert_eq!(String::from("blue "), format!("{:<6}", Color::Blue { hue: 0 }));
assert_eq!(String::from(" blue "), format!("{:^6}", Color::Blue { hue: 0 }));
assert_eq!(String::from("bl"), format!("{:.2}", Color::Blue { hue: 0 }));
}
#[test]
fn to_yellow_string() {
assert_eq!(String::from("yellow"), format!("{}", Color::Yellow));
}
#[test]
fn to_red_string() {
assert_eq!(String::from("RedRed"), format!("{}", Color::Red));
}
#[derive(Display, Debug, Eq, PartialEq)]
#[strum(serialize_all = "snake_case")]
enum Brightness {
DarkBlack,
Dim {
glow: usize,
},
#[strum(serialize = "bright")]
BrightWhite,
}
#[test]
fn brightness_to_string() {
assert_eq!(
String::from("dark_black"),
Brightness::DarkBlack.to_string().as_ref()
);
assert_eq!(
String::from("dim"),
Brightness::Dim { glow: 0 }.to_string().as_ref()
);
assert_eq!(
String::from("bright"),
Brightness::BrightWhite.to_string().as_ref()
);
}