inflector/cases/traincase/
mod.rs

1#![deny(warnings)]
2use cases::case::*;
3/// Determines if a `&str` is `Train-Case`
4///
5/// ```
6///     use inflector::cases::traincase::is_train_case;
7///     let mock_string: &str = "Foo-Bar-String-That-Is-Really-Really-Long";
8///     let asserted_bool: bool = is_train_case(mock_string);
9///     assert!(asserted_bool == true);
10///
11/// ```
12///
13/// ```
14///     use inflector::cases::traincase::is_train_case;
15///     let mock_string: &str = "foo-bar-string-that-is-really-really-long";
16///     let asserted_bool: bool = is_train_case(mock_string);
17///     assert!(asserted_bool == false);
18///
19/// ```
20///
21/// ```
22///     use inflector::cases::traincase::is_train_case;
23///     let mock_string: &str = "FooBarIsAReallyReallyLongString";
24///     let asserted_bool: bool = is_train_case(mock_string);
25///     assert!(asserted_bool == false);
26///
27/// ```
28///
29/// ```
30///     use inflector::cases::traincase::is_train_case;
31///     let mock_string: &str = "fooBarIsAReallyReallyLongString";
32///     let asserted_bool: bool = is_train_case(mock_string);
33///     assert!(asserted_bool == false);
34///
35/// ```
36///
37/// ```
38///     use inflector::cases::traincase::is_train_case;
39///     let mock_string: &str = "foo_bar_string_that_is_really_really_long";
40///     let asserted_bool: bool = is_train_case(mock_string);
41///     assert!(asserted_bool == false);
42///
43/// ```
44///
45/// ```
46///     use inflector::cases::traincase::is_train_case;
47///     let mock_string: &str = "Foo bar string that is really really long";
48///     let asserted_bool: bool = is_train_case(mock_string);
49///     assert!(asserted_bool == false);
50///
51/// ```
52///
53/// ```
54///     use inflector::cases::traincase::is_train_case;
55///     let mock_string: &str = "Foo Bar Is A Really Really Long String";
56///     let asserted_bool: bool = is_train_case(mock_string);
57///     assert!(asserted_bool == false);
58///
59/// ```
60pub fn is_train_case(test_string: &str) -> bool {
61    test_string == to_train_case(test_string.clone())
62}
63
64
65/// Converts a `&str` to `Train-Case` `String`
66///
67/// ```
68///     use inflector::cases::traincase::to_train_case;
69///     let mock_string: &str = "foo-bar";
70///     let expected_string: String = "Foo-Bar".to_string();
71///     let asserted_string: String = to_train_case(mock_string);
72///     assert!(asserted_string == expected_string);
73///
74/// ```
75///
76/// ```
77///     use inflector::cases::traincase::to_train_case;
78///     let mock_string: &str = "FOO_BAR";
79///     let expected_string: String = "Foo-Bar".to_string();
80///     let asserted_string: String = to_train_case(mock_string);
81///     assert!(asserted_string == expected_string);
82///
83/// ```
84///
85/// ```
86///     use inflector::cases::traincase::to_train_case;
87///     let mock_string: &str = "foo_bar";
88///     let expected_string: String = "Foo-Bar".to_string();
89///     let asserted_string: String = to_train_case(mock_string);
90///     assert!(asserted_string == expected_string);
91///
92/// ```
93///
94/// ```
95///     use inflector::cases::traincase::to_train_case;
96///     let mock_string: &str = "Foo Bar";
97///     let expected_string: String = "Foo-Bar".to_string();
98///     let asserted_string: String = to_train_case(mock_string);
99///     assert!(asserted_string == expected_string);
100///
101/// ```
102///
103/// ```
104///     use inflector::cases::traincase::to_train_case;
105///     let mock_string: &str = "Foo bar";
106///     let expected_string: String = "Foo-Bar".to_string();
107///     let asserted_string: String = to_train_case(mock_string);
108///     assert!(asserted_string == expected_string);
109///
110/// ```
111///
112/// ```
113///     use inflector::cases::traincase::to_train_case;
114///     let mock_string: &str = "FooBar";
115///     let expected_string: String = "Foo-Bar".to_string();
116///     let asserted_string: String = to_train_case(mock_string);
117///     assert!(asserted_string == expected_string);
118///
119/// ```
120///
121/// ```
122///     use inflector::cases::traincase::to_train_case;
123///     let mock_string: &str = "fooBar";
124///     let expected_string: String = "Foo-Bar".to_string();
125///     let asserted_string: String = to_train_case(mock_string);
126///     assert!(asserted_string == expected_string);
127///
128/// ```
129pub fn to_train_case(non_train_case_string: &str) -> String {
130    let options = CamelOptions {
131        new_word: true,
132        last_char: ' ',
133        first_word: true,
134        injectable_char: '-',
135        has_seperator: true,
136        inverted: false,
137    };
138    to_case_camel_like(non_train_case_string, options)
139}
140
141#[cfg(all(feature = "unstable", test))]
142mod benchmarks {
143    extern crate test;
144    use self::test::Bencher;
145
146    #[bench]
147    fn bench_train(b: &mut Bencher) {
148        b.iter(|| super::to_train_case("Foo bar"));
149    }
150
151    #[bench]
152    fn bench_is_train(b: &mut Bencher) {
153        b.iter(|| super::is_train_case("Foo bar"));
154    }
155
156    #[bench]
157    fn bench_train_from_snake(b: &mut Bencher) {
158        b.iter(|| super::to_train_case("test_test_test"));
159    }
160
161}
162
163#[cfg(test)]
164mod tests {
165    use ::to_train_case;
166    use ::is_train_case;
167
168    #[test]
169    fn from_camel_case() {
170        let convertable_string: String = "fooBar".to_owned();
171        let expected: String = "Foo-Bar".to_owned();
172        assert_eq!(to_train_case(&convertable_string), expected)
173    }
174
175    #[test]
176    fn from_pascal_case() {
177        let convertable_string: String = "FooBar".to_owned();
178        let expected: String = "Foo-Bar".to_owned();
179        assert_eq!(to_train_case(&convertable_string), expected)
180    }
181
182    #[test]
183    fn from_kebab_case() {
184        let convertable_string: String = "foo-bar".to_owned();
185        let expected: String = "Foo-Bar".to_owned();
186        assert_eq!(to_train_case(&convertable_string), expected)
187    }
188
189    #[test]
190    fn from_sentence_case() {
191        let convertable_string: String = "Foo bar".to_owned();
192        let expected: String = "Foo-Bar".to_owned();
193        assert_eq!(to_train_case(&convertable_string), expected)
194    }
195
196    #[test]
197    fn from_title_case() {
198        let convertable_string: String = "Foo Bar".to_owned();
199        let expected: String = "Foo-Bar".to_owned();
200        assert_eq!(to_train_case(&convertable_string), expected)
201    }
202
203    #[test]
204    fn from_train_case() {
205        let convertable_string: String = "Foo-Bar".to_owned();
206        let expected: String = "Foo-Bar".to_owned();
207        assert_eq!(to_train_case(&convertable_string), expected)
208    }
209
210    #[test]
211    fn from_screaming_snake_case() {
212        let convertable_string: String = "FOO_BAR".to_owned();
213        let expected: String = "Foo-Bar".to_owned();
214        assert_eq!(to_train_case(&convertable_string), expected)
215    }
216
217    #[test]
218    fn from_snake_case() {
219        let convertable_string: String = "foo_bar".to_owned();
220        let expected: String = "Foo-Bar".to_owned();
221        assert_eq!(to_train_case(&convertable_string), expected)
222    }
223
224    #[test]
225    fn from_case_with_loads_of_space() {
226        let convertable_string: String = "foo           bar".to_owned();
227        let expected: String = "Foo-Bar".to_owned();
228        assert_eq!(to_train_case(&convertable_string), expected)
229    }
230
231    #[test]
232    fn a_name_with_a_dot() {
233        let convertable_string: String = "Robert C. Martin".to_owned();
234        let expected: String = "Robert-C-Martin".to_owned();
235        assert_eq!(to_train_case(&convertable_string), expected)
236    }
237
238    #[test]
239    fn random_text_with_bad_chars() {
240        let convertable_string: String = "Random text with *(bad) chars".to_owned();
241        let expected: String = "Random-Text-With-Bad-Chars".to_owned();
242        assert_eq!(to_train_case(&convertable_string), expected)
243    }
244
245    #[test]
246    fn trailing_bad_chars() {
247        let convertable_string: String = "trailing bad_chars*(()())".to_owned();
248        let expected: String = "Trailing-Bad-Chars".to_owned();
249        assert_eq!(to_train_case(&convertable_string), expected)
250    }
251
252    #[test]
253    fn leading_bad_chars() {
254        let convertable_string: String = "-!#$%leading bad chars".to_owned();
255        let expected: String = "Leading-Bad-Chars".to_owned();
256        assert_eq!(to_train_case(&convertable_string), expected)
257    }
258
259    #[test]
260    fn wrapped_in_bad_chars() {
261        let convertable_string: String = "-!#$%wrapped in bad chars&*^*&(&*^&(<><?>><?><>))".to_owned();
262        let expected: String = "Wrapped-In-Bad-Chars".to_owned();
263        assert_eq!(to_train_case(&convertable_string), expected)
264    }
265
266    #[test]
267    fn has_a_sign() {
268        let convertable_string: String = "has a + sign".to_owned();
269        let expected: String = "Has-A-Sign".to_owned();
270        assert_eq!(to_train_case(&convertable_string), expected)
271    }
272
273    #[test]
274    fn is_correct_from_camel_case() {
275        let convertable_string: String = "fooBar".to_owned();
276        assert_eq!(is_train_case(&convertable_string), false)
277    }
278
279    #[test]
280    fn is_correct_from_pascal_case() {
281        let convertable_string: String = "FooBar".to_owned();
282        assert_eq!(is_train_case(&convertable_string), false)
283    }
284
285    #[test]
286    fn is_correct_from_kebab_case() {
287        let convertable_string: String = "foo-bar".to_owned();
288        assert_eq!(is_train_case(&convertable_string), false)
289    }
290
291    #[test]
292    fn is_correct_from_sentence_case() {
293        let convertable_string: String = "Foo bar".to_owned();
294        assert_eq!(is_train_case(&convertable_string), false)
295    }
296
297    #[test]
298    fn is_correct_from_title_case() {
299        let convertable_string: String = "Foo Bar".to_owned();
300        assert_eq!(is_train_case(&convertable_string), false)
301    }
302
303    #[test]
304    fn is_correct_from_train_case() {
305        let convertable_string: String = "Foo-Bar".to_owned();
306        assert_eq!(is_train_case(&convertable_string), true)
307    }
308
309    #[test]
310    fn is_correct_from_screaming_snake_case() {
311        let convertable_string: String = "FOO_BAR".to_owned();
312        assert_eq!(is_train_case(&convertable_string), false)
313    }
314
315    #[test]
316    fn is_correct_from_snake_case() {
317        let convertable_string: String = "foo_bar".to_owned();
318        assert_eq!(is_train_case(&convertable_string), false)
319    }
320}