1use crate::case::*;
2pub fn is_train_case(test_string: &str) -> bool {
16 test_string == to_train_case(test_string)
17}
18
19pub fn to_train_case(non_train_case_string: &str) -> String {
33 let options = CamelOptions {
34 new_word: true,
35 last_char: ' ',
36 first_word: true,
37 injectable_char: '-',
38 has_seperator: true,
39 inverted: false,
40 concat_num: true,
41 };
42 to_case_camel_like(non_train_case_string, options)
43}
44
45#[cfg(test)]
46mod benchmarks {
47 extern crate test;
48 use self::test::Bencher;
49
50 #[bench]
51 fn bench_train(b: &mut Bencher) {
52 b.iter(|| super::to_train_case("Foo bar"));
53 }
54
55 #[bench]
56 fn bench_is_train(b: &mut Bencher) {
57 b.iter(|| super::is_train_case("Foo bar"));
58 }
59
60 #[bench]
61 fn bench_train_from_snake(b: &mut Bencher) {
62 b.iter(|| super::to_train_case("test_test_test"));
63 }
64}
65
66#[cfg(test)]
67mod tests {
68 use super::is_train_case;
69 use super::to_train_case;
70
71 #[test]
72 fn from_camel_case() {
73 let convertable_string: String = "fooBar".to_owned();
74 let expected: String = "Foo-Bar".to_owned();
75 assert_eq!(to_train_case(&convertable_string), expected)
76 }
77
78 #[test]
79 fn from_pascal_case() {
80 let convertable_string: String = "FooBar".to_owned();
81 let expected: String = "Foo-Bar".to_owned();
82 assert_eq!(to_train_case(&convertable_string), expected)
83 }
84
85 #[test]
86 fn from_kebab_case() {
87 let convertable_string: String = "foo-bar".to_owned();
88 let expected: String = "Foo-Bar".to_owned();
89 assert_eq!(to_train_case(&convertable_string), expected)
90 }
91
92 #[test]
93 fn from_sentence_case() {
94 let convertable_string: String = "Foo bar".to_owned();
95 let expected: String = "Foo-Bar".to_owned();
96 assert_eq!(to_train_case(&convertable_string), expected)
97 }
98
99 #[test]
100 fn from_title_case() {
101 let convertable_string: String = "Foo Bar".to_owned();
102 let expected: String = "Foo-Bar".to_owned();
103 assert_eq!(to_train_case(&convertable_string), expected)
104 }
105
106 #[test]
107 fn from_train_case() {
108 let convertable_string: String = "Foo-Bar".to_owned();
109 let expected: String = "Foo-Bar".to_owned();
110 assert_eq!(to_train_case(&convertable_string), expected)
111 }
112
113 #[test]
114 fn from_screaming_snake_case() {
115 let convertable_string: String = "FOO_BAR".to_owned();
116 let expected: String = "Foo-Bar".to_owned();
117 assert_eq!(to_train_case(&convertable_string), expected)
118 }
119
120 #[test]
121 fn from_snake_case() {
122 let convertable_string: String = "foo_bar".to_owned();
123 let expected: String = "Foo-Bar".to_owned();
124 assert_eq!(to_train_case(&convertable_string), expected)
125 }
126
127 #[test]
128 fn from_case_with_loads_of_space() {
129 let convertable_string: String = "foo bar".to_owned();
130 let expected: String = "Foo-Bar".to_owned();
131 assert_eq!(to_train_case(&convertable_string), expected)
132 }
133
134 #[test]
135 fn a_name_with_a_dot() {
136 let convertable_string: String = "Robert C. Martin".to_owned();
137 let expected: String = "Robert-C-Martin".to_owned();
138 assert_eq!(to_train_case(&convertable_string), expected)
139 }
140
141 #[test]
142 fn random_text_with_bad_chars() {
143 let convertable_string: String = "Random text with *(bad) chars".to_owned();
144 let expected: String = "Random-Text-With-Bad-Chars".to_owned();
145 assert_eq!(to_train_case(&convertable_string), expected)
146 }
147
148 #[test]
149 fn trailing_bad_chars() {
150 let convertable_string: String = "trailing bad_chars*(()())".to_owned();
151 let expected: String = "Trailing-Bad-Chars".to_owned();
152 assert_eq!(to_train_case(&convertable_string), expected)
153 }
154
155 #[test]
156 fn leading_bad_chars() {
157 let convertable_string: String = "-!#$%leading bad chars".to_owned();
158 let expected: String = "Leading-Bad-Chars".to_owned();
159 assert_eq!(to_train_case(&convertable_string), expected)
160 }
161
162 #[test]
163 fn wrapped_in_bad_chars() {
164 let convertable_string: String =
165 "-!#$%wrapped in bad chars&*^*&(&*^&(<><?>><?><>))".to_owned();
166 let expected: String = "Wrapped-In-Bad-Chars".to_owned();
167 assert_eq!(to_train_case(&convertable_string), expected)
168 }
169
170 #[test]
171 fn has_a_sign() {
172 let convertable_string: String = "has a + sign".to_owned();
173 let expected: String = "Has-A-Sign".to_owned();
174 assert_eq!(to_train_case(&convertable_string), expected)
175 }
176
177 #[test]
178 fn is_correct_from_camel_case() {
179 let convertable_string: String = "fooBar".to_owned();
180 assert_eq!(is_train_case(&convertable_string), false)
181 }
182
183 #[test]
184 fn is_correct_from_pascal_case() {
185 let convertable_string: String = "FooBar".to_owned();
186 assert_eq!(is_train_case(&convertable_string), false)
187 }
188
189 #[test]
190 fn is_correct_from_kebab_case() {
191 let convertable_string: String = "foo-bar".to_owned();
192 assert_eq!(is_train_case(&convertable_string), false)
193 }
194
195 #[test]
196 fn is_correct_from_sentence_case() {
197 let convertable_string: String = "Foo bar".to_owned();
198 assert_eq!(is_train_case(&convertable_string), false)
199 }
200
201 #[test]
202 fn is_correct_from_title_case() {
203 let convertable_string: String = "Foo Bar".to_owned();
204 assert_eq!(is_train_case(&convertable_string), false)
205 }
206
207 #[test]
208 fn is_correct_from_train_case() {
209 let convertable_string: String = "Foo-Bar".to_owned();
210 assert_eq!(is_train_case(&convertable_string), true)
211 }
212
213 #[test]
214 fn is_correct_from_screaming_snake_case() {
215 let convertable_string: String = "FOO_BAR".to_owned();
216 assert_eq!(is_train_case(&convertable_string), false)
217 }
218
219 #[test]
220 fn is_correct_from_snake_case() {
221 let convertable_string: String = "foo_bar".to_owned();
222 assert_eq!(is_train_case(&convertable_string), false)
223 }
224}