inflector/cases/pascalcase/
mod.rs

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