Skip to content

Commit 11196c2

Browse files
committed
Improve test
1 parent 8364591 commit 11196c2

2 files changed

Lines changed: 142 additions & 12 deletions

File tree

src/case.rs

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,133 @@ impl Case {
7878
s.to_case(self.0)
7979
}
8080
}
81+
82+
#[cfg(test)]
83+
mod tests {
84+
use super::*;
85+
use convert_case::Case as CC;
86+
87+
/// Helper to wrap a convert_case::Case into our Case type.
88+
fn wrap(c: CC) -> Case {
89+
Case(c)
90+
}
91+
92+
/// List of all supported case strings and their expected enum variants.
93+
fn all_cases() -> Vec<(&'static str, CC)> {
94+
vec![
95+
("\"upper\"", CC::Upper),
96+
("\"lower\"", CC::Lower),
97+
("\"title\"", CC::Title),
98+
("\"toggle\"", CC::Toggle),
99+
("\"camel\"", CC::Camel),
100+
("\"pascal\"", CC::Pascal),
101+
("\"upper_camel\"", CC::UpperCamel),
102+
("\"snake\"", CC::Snake),
103+
("\"upper_snake\"", CC::UpperSnake),
104+
("\"screaming_snake\"", CC::ScreamingSnake),
105+
("\"kebab\"", CC::Kebab),
106+
("\"cobol\"", CC::Cobol),
107+
("\"upper_kebab\"", CC::UpperKebab),
108+
("\"train\"", CC::Train),
109+
("\"flat\"", CC::Flat),
110+
("\"upper_flat\"", CC::UpperFlat),
111+
("\"alternating\"", CC::Alternating),
112+
]
113+
}
114+
115+
// ------------------------------------------------------------------------
116+
// Parsing Tests
117+
// ------------------------------------------------------------------------
118+
119+
#[test]
120+
fn test_try_from_string_parses_all_cases() {
121+
for (input, expected_variant) in all_cases() {
122+
let parsed: Case = input.to_string().try_into().unwrap();
123+
assert_eq!(
124+
parsed,
125+
wrap(expected_variant),
126+
"Parsing string {input} should yield {expected_variant:?}"
127+
);
128+
}
129+
}
130+
131+
#[test]
132+
fn test_try_from_string_rejects_invalid_input() {
133+
let err = Case::try_from("invalid_value".to_string());
134+
assert!(err.is_err());
135+
}
136+
137+
#[test]
138+
fn test_try_from_tuple_parses_when_first_value_is_case() {
139+
let (key, val) = ("case".to_string(), "\"upper\"".to_string());
140+
let result: Result<Case, _> = (key, val).try_into();
141+
assert!(result.is_ok());
142+
}
143+
144+
#[test]
145+
fn test_try_from_tuple_rejects_when_first_value_is_not_case() {
146+
let (key, val) = ("not_case".to_string(), "\"upper\"".to_string());
147+
let result: Result<Case, _> = (key, val).try_into();
148+
assert!(result.is_err());
149+
}
150+
151+
// ------------------------------------------------------------------------
152+
// Display Tests
153+
// ------------------------------------------------------------------------
154+
155+
#[test]
156+
fn test_display_outputs_all_expected_strings() {
157+
for (input_literal, variant) in all_cases() {
158+
let c = wrap(variant);
159+
let expected_display = input_literal.trim_matches('"');
160+
assert_eq!(
161+
c.to_string(),
162+
expected_display,
163+
"Display for {:?} should be `{}`",
164+
variant,
165+
expected_display
166+
);
167+
}
168+
}
169+
170+
// ------------------------------------------------------------------------
171+
// to_case() Behavior Documentation Tests
172+
// ------------------------------------------------------------------------
173+
174+
#[test]
175+
fn test_to_case_produces_expected_conversions() {
176+
let example = "hello world";
177+
178+
let examples = vec![
179+
(CC::Upper, "HELLO WORLD"),
180+
(CC::Lower, "hello world"),
181+
(CC::Title, "Hello World"),
182+
(CC::Toggle, "hELLO wORLD"),
183+
(CC::Camel, "helloWorld"),
184+
(CC::Pascal, "HelloWorld"),
185+
(CC::UpperCamel, "HelloWorld"),
186+
(CC::Snake, "hello_world"),
187+
(CC::UpperSnake, "HELLO_WORLD"),
188+
(CC::ScreamingSnake, "HELLO_WORLD"),
189+
(CC::Kebab, "hello-world"),
190+
(CC::Cobol, "HELLO-WORLD"),
191+
(CC::UpperKebab, "HELLO-WORLD"),
192+
(CC::Train, "Hello-World"),
193+
(CC::Flat, "helloworld"),
194+
(CC::UpperFlat, "HELLOWORLD"),
195+
(CC::Alternating, "hElLo WoRlD"),
196+
];
197+
198+
for (variant, expected_output) in examples {
199+
let c = Case(variant);
200+
assert_eq!(
201+
c.to_case(example),
202+
expected_output,
203+
"Case {:?} should convert `{}` to `{}`",
204+
variant,
205+
example,
206+
expected_output
207+
);
208+
}
209+
}
210+
}

tests/case.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
use enum_stringify::EnumStringify;
22

33
#[derive(EnumStringify, Debug, PartialEq)]
4-
#[enum_stringify(case = "upper")]
4+
#[enum_stringify(case = "lower")]
55
enum Letters {
66
A,
77
B,
88
}
99

1010
#[test]
11-
fn test_case_conversion_upper() {
12-
assert_eq!(Letters::A.to_string(), "A");
13-
assert_eq!(Letters::B.to_string(), "B");
11+
fn test_case_conversion_loer() {
12+
assert_eq!(Letters::A.to_string(), "a");
13+
assert_eq!(Letters::B.to_string(), "b");
1414

15-
assert_eq!(Letters::try_from("A").unwrap(), Letters::A);
16-
assert_eq!(Letters::try_from("B").unwrap(), Letters::B);
15+
assert_eq!(Letters::try_from("a").unwrap(), Letters::A);
16+
assert_eq!(Letters::try_from("b").unwrap(), Letters::B);
1717
}
1818

1919
#[derive(EnumStringify, Debug, PartialEq)]
20-
#[enum_stringify(case = "lower")]
20+
#[enum_stringify(case = "upper")]
2121
enum Colors {
2222
Red,
2323
Blue,
2424
}
2525

2626
#[test]
27-
fn test_case_conversion_lower() {
28-
assert_eq!(Colors::Red.to_string(), "red");
29-
assert_eq!(Colors::Blue.to_string(), "blue");
27+
fn test_case_conversion_upper() {
28+
assert_eq!(Colors::Red.to_string(), "RED");
29+
assert_eq!(Colors::Blue.to_string(), "BLUE");
3030

31-
assert_eq!(Colors::try_from("red").unwrap(), Colors::Red);
32-
assert_eq!(Colors::try_from("blue").unwrap(), Colors::Blue);
31+
assert_eq!(Colors::try_from("RED").unwrap(), Colors::Red);
32+
assert_eq!(Colors::try_from("BLUE").unwrap(), Colors::Blue);
3333
}
3434

3535
#[derive(EnumStringify, Debug, PartialEq)]

0 commit comments

Comments
 (0)