| 46 | 46 | | pub bye: bool, |
| 47 | 47 | | /// Whether to print a wave after the greeting (`--wave`). |
| 48 | 48 | | pub wave: bool, |
| 49 | + | /// Whether to print the greeting and a goodbye on one line (`--both`). |
| 50 | + | pub both: bool, |
| 49 | 51 | | } |
| 50 | 52 | | |
| 51 | 53 | | impl Default for Options { |
| ⋯ |
| 62 | 64 | | loud: false, |
| 63 | 65 | | bye: false, |
| 64 | 66 | | wave: false, |
| 67 | + | both: false, |
| 65 | 68 | | } |
| 66 | 69 | | } |
| 67 | 70 | | } |
| ⋯ |
| 99 | 102 | | options.bye = true; |
| 100 | 103 | | } else if arg == "--wave" { |
| 101 | 104 | | options.wave = true; |
| 105 | + | } else if arg == "--both" { |
| 106 | + | options.both = true; |
| 102 | 107 | | } else if arg == "--times" { |
| 103 | 108 | | let value = args |
| 104 | 109 | | .next() |
| ⋯ |
| 141 | 146 | | } |
| 142 | 147 | | |
| 143 | 148 | | /// The greeting for `name`. |
| 144 | | − | pub fn hail(name: &str) -> String { |
| 149 | + | pub fn greet(name: &str) -> String { |
| 145 | 150 | | greeting_in(name, Lang::En) |
| 146 | 151 | | } |
| 147 | 152 | | |
| ⋯ |
| 162 | 167 | | } |
| 163 | 168 | | |
| 164 | 169 | | /// The farewell for `name`. |
| 165 | | − | pub fn part(name: &str) -> String { |
| 170 | + | pub fn farewell(name: &str) -> String { |
| 166 | 171 | | let trimmed = name.trim(); |
| 167 | 172 | | let display_name = if trimmed.is_empty() { "world" } else { trimmed }; |
| 168 | 173 | | format!("Goodbye, {display_name}!") |
| ⋯ |
| 195 | 200 | | } |
| 196 | 201 | | |
| 197 | 202 | | #[test] |
| 203 | + | fn parse_args_both() { |
| 204 | + | assert!(parse_args(args(&["--both", "Ada"])).unwrap().both); |
| 205 | + | assert!(!parse_args(args(&["Ada"])).unwrap().both); |
| 206 | + | } |
| 207 | + | |
| 208 | + | #[test] |
| 198 | 209 | | fn wave_is_o_slash() { |
| 199 | 210 | | assert_eq!(wave(), "o/"); |
| 200 | 211 | | } |
| ⋯ |
| 208 | 219 | | |
| 209 | 220 | | #[test] |
| 210 | 221 | | fn loud_uses_three_exclamation_marks() { |
| 211 | | − | assert_eq!(loud(&hail("Ada")), "Hi, Ada!!!"); |
| 222 | + | assert_eq!(loud(&greet("Ada")), "Hi, Ada!!!"); |
| 212 | 223 | | } |
| 213 | 224 | | |
| 214 | 225 | | #[test] |
| ⋯ |
| 339 | 350 | | |
| 340 | 351 | | #[test] |
| 341 | 352 | | fn shout_upper_cases_greeting() { |
| 342 | | − | assert_eq!(shout(&hail("Ada")), "HI, ADA!"); |
| 353 | + | assert_eq!(shout(&greet("Ada")), "HI, ADA!"); |
| 343 | 354 | | } |
| 344 | 355 | | |
| 345 | 356 | | #[test] |
| 346 | 357 | | fn whisper_lower_cases_and_trails_off() { |
| 347 | | − | assert_eq!(whisper(&hail("Ada")), "hi, ada..."); |
| 358 | + | assert_eq!(whisper(&greet("Ada")), "hi, ada..."); |
| 348 | 359 | | assert_eq!(whisper(&greeting_in("Luc", Lang::Fr)), "bonjour, luc..."); |
| 349 | 360 | | } |
| 350 | 361 | | |
| ⋯ |
| 366 | 377 | | |
| 367 | 378 | | #[test] |
| 368 | 379 | | fn reverse_spells_backwards() { |
| 369 | | − | assert_eq!(hail(&reverse("Ada")), "Hi, adA!"); |
| 380 | + | assert_eq!(greet(&reverse("Ada")), "Hi, adA!"); |
| 370 | 381 | | } |
| 371 | 382 | | |
| 372 | 383 | | #[test] |
| 373 | 384 | | fn greets_by_name() { |
| 374 | | − | assert_eq!(hail("Ada"), "Hi, Ada!"); |
| 385 | + | assert_eq!(greet("Ada"), "Hi, Ada!"); |
| 375 | 386 | | } |
| 376 | 387 | | |
| 377 | 388 | | #[test] |
| 378 | 389 | | fn greets_world_with_empty_string() { |
| 379 | | − | assert_eq!(hail(""), "Hi, world!"); |
| 390 | + | assert_eq!(greet(""), "Hi, world!"); |
| 380 | 391 | | } |
| 381 | 392 | | |
| 382 | 393 | | #[test] |
| 383 | 394 | | fn greets_world_with_whitespace_only() { |
| 384 | | − | assert_eq!(hail(" "), "Hi, world!"); |
| 395 | + | assert_eq!(greet(" "), "Hi, world!"); |
| 385 | 396 | | } |
| 386 | 397 | | |
| 387 | 398 | | #[test] |
| 388 | 399 | | fn trims_whitespace_from_name() { |
| 389 | | − | assert_eq!(hail(" Ada "), "Hi, Ada!"); |
| 400 | + | assert_eq!(greet(" Ada "), "Hi, Ada!"); |
| 390 | 401 | | } |
| 391 | 402 | | |
| 392 | 403 | | #[test] |
| 393 | 404 | | fn says_goodbye_by_name() { |
| 394 | | − | assert_eq!(part("Ada"), "Goodbye, Ada!"); |
| 405 | + | assert_eq!(farewell("Ada"), "Goodbye, Ada!"); |
| 395 | 406 | | } |
| 396 | 407 | | |
| 397 | 408 | | #[test] |
| 398 | 409 | | fn says_goodbye_to_world_when_blank() { |
| 399 | | − | assert_eq!(part(" "), "Goodbye, world!"); |
| 410 | + | assert_eq!(farewell(" "), "Goodbye, world!"); |
| 400 | 411 | | } |
| 401 | 412 | | |
| 402 | 413 | | #[test] |
| 403 | 414 | | fn farewell_trims_whitespace() { |
| 404 | | − | assert_eq!(part(" Ada "), "Goodbye, Ada!"); |
| 415 | + | assert_eq!(farewell(" Ada "), "Goodbye, Ada!"); |
| 405 | 416 | | } |
| 406 | 417 | | } |