inflector/cases/traincase/
mod.rs1#![deny(warnings)]
2use cases::case::*;
3pub fn is_train_case(test_string: &str) -> bool {
61 test_string == to_train_case(test_string.clone())
62}
63
64
65pub 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}