inflector/cases/sentencecase/
mod.rs1#![deny(warnings)]
2use cases::case::*;
3pub fn to_sentence_case(non_sentence_case_string: &str) -> String {
54 let options = CamelOptions {
55 new_word: true,
56 last_char: ' ',
57 first_word: true,
58 injectable_char: ' ',
59 has_seperator: true,
60 inverted: true,
61 };
62 to_case_camel_like(non_sentence_case_string, options)
63}
64pub fn is_sentence_case(test_string: &str) -> bool {
130 test_string == to_sentence_case(test_string.clone())
131}
132
133#[cfg(all(feature = "unstable", test))]
134mod benchmarks {
135 extern crate test;
136 use self::test::Bencher;
137
138 #[bench]
139 fn bench_sentence(b: &mut Bencher) {
140 b.iter(|| super::to_sentence_case("Foo BAR"));
141 }
142
143 #[bench]
144 fn bench_is_sentence(b: &mut Bencher) {
145 b.iter(|| super::is_sentence_case("Foo bar"));
146 }
147
148 #[bench]
149 fn bench_sentence_from_snake(b: &mut Bencher) {
150 b.iter(|| super::to_sentence_case("foo_bar"));
151 }
152
153}
154
155#[cfg(test)]
156mod tests {
157 use ::to_sentence_case;
158 use ::is_sentence_case;
159
160 #[test]
161 fn from_camel_case() {
162 let convertable_string: String = "fooBar".to_owned();
163 let expected: String = "Foo bar".to_owned();
164 assert_eq!(to_sentence_case(&convertable_string), expected)
165 }
166
167 #[test]
168 fn from_pascal_case() {
169 let convertable_string: String = "FooBar".to_owned();
170 let expected: String = "Foo bar".to_owned();
171 assert_eq!(to_sentence_case(&convertable_string), expected)
172 }
173
174 #[test]
175 fn from_kebab_case() {
176 let convertable_string: String = "foo-bar".to_owned();
177 let expected: String = "Foo bar".to_owned();
178 assert_eq!(to_sentence_case(&convertable_string), expected)
179 }
180
181 #[test]
182 fn from_sentence_case() {
183 let convertable_string: String = "Foo bar".to_owned();
184 let expected: String = "Foo bar".to_owned();
185 assert_eq!(to_sentence_case(&convertable_string), expected)
186 }
187
188 #[test]
189 fn from_title_case() {
190 let convertable_string: String = "Foo Bar".to_owned();
191 let expected: String = "Foo bar".to_owned();
192 assert_eq!(to_sentence_case(&convertable_string), expected)
193 }
194
195 #[test]
196 fn from_train_case() {
197 let convertable_string: String = "Foo-Bar".to_owned();
198 let expected: String = "Foo bar".to_owned();
199 assert_eq!(to_sentence_case(&convertable_string), expected)
200 }
201
202 #[test]
203 fn from_screaming_snake_case() {
204 let convertable_string: String = "FOO_BAR".to_owned();
205 let expected: String = "Foo bar".to_owned();
206 assert_eq!(to_sentence_case(&convertable_string), expected)
207 }
208
209 #[test]
210 fn from_snake_case() {
211 let convertable_string: String = "foo_bar".to_owned();
212 let expected: String = "Foo bar".to_owned();
213 assert_eq!(to_sentence_case(&convertable_string), expected)
214 }
215
216 #[test]
217 fn from_case_with_loads_of_space() {
218 let convertable_string: String = "foo bar".to_owned();
219 let expected: String = "Foo bar".to_owned();
220 assert_eq!(to_sentence_case(&convertable_string), expected)
221 }
222
223 #[test]
224 fn a_name_with_a_dot() {
225 let convertable_string: String = "Robert C. Martin".to_owned();
226 let expected: String = "Robert c martin".to_owned();
227 assert_eq!(to_sentence_case(&convertable_string), expected)
228 }
229
230 #[test]
231 fn random_text_with_bad_chars() {
232 let convertable_string: String = "Random text with *(bad) chars".to_owned();
233 let expected: String = "Random text with bad chars".to_owned();
234 assert_eq!(to_sentence_case(&convertable_string), expected)
235 }
236
237 #[test]
238 fn trailing_bad_chars() {
239 let convertable_string: String = "trailing bad_chars*(()())".to_owned();
240 let expected: String = "Trailing bad chars".to_owned();
241 assert_eq!(to_sentence_case(&convertable_string), expected)
242 }
243
244 #[test]
245 fn leading_bad_chars() {
246 let convertable_string: String = "-!#$%leading bad chars".to_owned();
247 let expected: String = "Leading bad chars".to_owned();
248 assert_eq!(to_sentence_case(&convertable_string), expected)
249 }
250
251 #[test]
252 fn wrapped_in_bad_chars() {
253 let convertable_string: String = "-!#$%wrapped in bad chars&*^*&(&*^&(<><?>><?><>))".to_owned();
254 let expected: String = "Wrapped in bad chars".to_owned();
255 assert_eq!(to_sentence_case(&convertable_string), expected)
256 }
257
258 #[test]
259 fn has_a_sign() {
260 let convertable_string: String = "has a + sign".to_owned();
261 let expected: String = "Has a sign".to_owned();
262 assert_eq!(to_sentence_case(&convertable_string), expected)
263 }
264
265
266 #[test]
267 fn is_correct_from_camel_case() {
268 let convertable_string: String = "fooBar".to_owned();
269 assert_eq!(is_sentence_case(&convertable_string), false)
270 }
271
272 #[test]
273 fn is_correct_from_pascal_case() {
274 let convertable_string: String = "FooBar".to_owned();
275 assert_eq!(is_sentence_case(&convertable_string), false)
276 }
277
278 #[test]
279 fn is_correct_from_kebab_case() {
280 let convertable_string: String = "foo-bar".to_owned();
281 assert_eq!(is_sentence_case(&convertable_string), false)
282 }
283
284 #[test]
285 fn is_correct_from_sentence_case() {
286 let convertable_string: String = "Foo bar".to_owned();
287 assert_eq!(is_sentence_case(&convertable_string), true)
288 }
289
290 #[test]
291 fn is_correct_from_title_case() {
292 let convertable_string: String = "Foo Bar".to_owned();
293 assert_eq!(is_sentence_case(&convertable_string), false)
294 }
295
296 #[test]
297 fn is_correct_from_train_case() {
298 let convertable_string: String = "Foo-Bar".to_owned();
299 assert_eq!(is_sentence_case(&convertable_string), false)
300 }
301
302 #[test]
303 fn is_correct_from_screaming_snake_case() {
304 let convertable_string: String = "FOO_BAR".to_owned();
305 assert_eq!(is_sentence_case(&convertable_string), false)
306 }
307
308 #[test]
309 fn is_correct_from_snake_case() {
310 let convertable_string: String = "foo_bar".to_owned();
311 assert_eq!(is_sentence_case(&convertable_string), false)
312 }
313}