|
1 | 1 | use std::collections::HashMap; |
2 | 2 |
|
3 | | -fn check_word_count(s: &str, pairs: &[(&str, u32)]) { |
| 3 | +fn check_word_count(mut output: HashMap<String, u32>, pairs: &[(&str, u32)]) { |
4 | 4 | // The reason for the awkward code in here is to ensure that the failure |
5 | 5 | // message for assert_eq! is as informative as possible. A simpler |
6 | 6 | // solution would simply check the length of the map, and then |
7 | 7 | // check for the presence and value of each key in the given pairs vector. |
8 | | - let mut m: HashMap<String, u32> = word_count::word_count(s); |
9 | 8 | for &(k, v) in pairs.iter() { |
10 | | - assert_eq!((k, m.remove(&k.to_string()).unwrap_or(0)), (k, v)); |
| 9 | + assert_eq!((k, output.remove(&k.to_string()).unwrap_or(0)), (k, v)); |
11 | 10 | } |
12 | 11 | // may fail with a message that clearly shows all extra pairs in the map |
13 | | - assert_eq!(m.iter().collect::<Vec<(&String, &u32)>>(), vec![]); |
| 12 | + assert_eq!(output.iter().collect::<Vec<(&String, &u32)>>(), vec![]); |
14 | 13 | } |
15 | 14 |
|
16 | 15 | #[test] |
17 | 16 | fn count_one_word() { |
18 | | - check_word_count("word", &[("word", 1)]); |
| 17 | + let input = "word"; |
| 18 | + let output = word_count::word_count(input); |
| 19 | + let expected = &[("word", 1)]; |
| 20 | + check_word_count(output, expected); |
19 | 21 | } |
20 | 22 |
|
21 | 23 | #[test] |
22 | 24 | #[ignore] |
23 | | -fn count_one_of_each() { |
24 | | - check_word_count("one of each", &[("one", 1), ("of", 1), ("each", 1)]); |
| 25 | +fn count_one_of_each_word() { |
| 26 | + let input = "one of each"; |
| 27 | + let output = word_count::word_count(input); |
| 28 | + let expected = &[("one", 1), ("of", 1), ("each", 1)]; |
| 29 | + check_word_count(output, expected); |
25 | 30 | } |
26 | 31 |
|
27 | 32 | #[test] |
28 | 33 | #[ignore] |
29 | | -fn count_multiple_occurrences() { |
30 | | - check_word_count( |
31 | | - "one fish two fish red fish blue fish", |
32 | | - &[("one", 1), ("fish", 4), ("two", 1), ("red", 1), ("blue", 1)], |
33 | | - ); |
| 34 | +fn multiple_occurrences_of_a_word() { |
| 35 | + let input = "one fish two fish red fish blue fish"; |
| 36 | + let output = word_count::word_count(input); |
| 37 | + let expected = &[("one", 1), ("fish", 4), ("two", 1), ("red", 1), ("blue", 1)]; |
| 38 | + check_word_count(output, expected); |
34 | 39 | } |
35 | 40 |
|
36 | 41 | #[test] |
37 | 42 | #[ignore] |
38 | | -fn cramped_lists() { |
39 | | - check_word_count("one,two,three", &[("one", 1), ("two", 1), ("three", 1)]); |
| 43 | +fn handles_cramped_lists() { |
| 44 | + let input = "one,two,three"; |
| 45 | + let output = word_count::word_count(input); |
| 46 | + let expected = &[("one", 1), ("two", 1), ("three", 1)]; |
| 47 | + check_word_count(output, expected); |
40 | 48 | } |
41 | 49 |
|
42 | 50 | #[test] |
43 | 51 | #[ignore] |
44 | | -fn expanded_lists() { |
45 | | - check_word_count("one\ntwo\nthree", &[("one", 1), ("two", 1), ("three", 1)]); |
| 52 | +fn handles_expanded_lists() { |
| 53 | + let input = "one,\ntwo,\nthree"; |
| 54 | + let output = word_count::word_count(input); |
| 55 | + let expected = &[("one", 1), ("two", 1), ("three", 1)]; |
| 56 | + check_word_count(output, expected); |
46 | 57 | } |
47 | 58 |
|
48 | 59 | #[test] |
49 | 60 | #[ignore] |
50 | 61 | fn ignore_punctuation() { |
51 | | - check_word_count( |
52 | | - "car : carpet as java : javascript!!&@$%^&", |
53 | | - &[ |
54 | | - ("car", 1), |
55 | | - ("carpet", 1), |
56 | | - ("as", 1), |
57 | | - ("java", 1), |
58 | | - ("javascript", 1), |
59 | | - ], |
60 | | - ); |
| 62 | + let input = "car: carpet as java: javascript!!&@$%^&"; |
| 63 | + let output = word_count::word_count(input); |
| 64 | + let expected = &[ |
| 65 | + ("car", 1), |
| 66 | + ("carpet", 1), |
| 67 | + ("as", 1), |
| 68 | + ("java", 1), |
| 69 | + ("javascript", 1), |
| 70 | + ]; |
| 71 | + check_word_count(output, expected); |
61 | 72 | } |
62 | 73 |
|
63 | 74 | #[test] |
64 | 75 | #[ignore] |
65 | 76 | fn include_numbers() { |
66 | | - check_word_count( |
67 | | - "testing, 1, 2 testing", |
68 | | - &[("testing", 2), ("1", 1), ("2", 1)], |
69 | | - ); |
| 77 | + let input = "testing, 1, 2 testing"; |
| 78 | + let output = word_count::word_count(input); |
| 79 | + let expected = &[("testing", 2), ("1", 1), ("2", 1)]; |
| 80 | + check_word_count(output, expected); |
70 | 81 | } |
71 | 82 |
|
72 | 83 | #[test] |
73 | 84 | #[ignore] |
74 | 85 | fn normalize_case() { |
75 | | - check_word_count("go Go GO Stop stop", &[("go", 3), ("stop", 2)]); |
| 86 | + let input = "go Go GO Stop stop"; |
| 87 | + let output = word_count::word_count(input); |
| 88 | + let expected = &[("go", 3), ("stop", 2)]; |
| 89 | + check_word_count(output, expected); |
76 | 90 | } |
77 | 91 |
|
78 | 92 | #[test] |
79 | 93 | #[ignore] |
80 | 94 | fn with_apostrophes() { |
81 | | - check_word_count( |
82 | | - "First: don't laugh. Then: don't cry.", |
83 | | - &[ |
84 | | - ("first", 1), |
85 | | - ("don't", 2), |
86 | | - ("laugh", 1), |
87 | | - ("then", 1), |
88 | | - ("cry", 1), |
89 | | - ], |
90 | | - ); |
| 95 | + let input = "'First: don't laugh. Then: don't cry. You're getting it.'"; |
| 96 | + let output = word_count::word_count(input); |
| 97 | + let expected = &[ |
| 98 | + ("first", 1), |
| 99 | + ("don't", 2), |
| 100 | + ("laugh", 1), |
| 101 | + ("then", 1), |
| 102 | + ("cry", 1), |
| 103 | + ("you're", 1), |
| 104 | + ("getting", 1), |
| 105 | + ("it", 1), |
| 106 | + ]; |
| 107 | + check_word_count(output, expected); |
91 | 108 | } |
92 | 109 |
|
93 | 110 | #[test] |
94 | 111 | #[ignore] |
95 | 112 | fn with_quotations() { |
96 | | - check_word_count( |
97 | | - "Joe can't tell between 'large' and large.", |
98 | | - &[ |
99 | | - ("joe", 1), |
100 | | - ("can't", 1), |
101 | | - ("tell", 1), |
102 | | - ("between", 1), |
103 | | - ("large", 2), |
104 | | - ("and", 1), |
105 | | - ], |
106 | | - ); |
| 113 | + let input = "Joe can't tell between 'large' and large."; |
| 114 | + let output = word_count::word_count(input); |
| 115 | + let expected = &[ |
| 116 | + ("joe", 1), |
| 117 | + ("can't", 1), |
| 118 | + ("tell", 1), |
| 119 | + ("between", 1), |
| 120 | + ("large", 2), |
| 121 | + ("and", 1), |
| 122 | + ]; |
| 123 | + check_word_count(output, expected); |
| 124 | +} |
| 125 | + |
| 126 | +#[test] |
| 127 | +#[ignore] |
| 128 | +fn substrings_from_the_beginning() { |
| 129 | + let input = "Joe can't tell between app, apple and a."; |
| 130 | + let output = word_count::word_count(input); |
| 131 | + let expected = &[ |
| 132 | + ("joe", 1), |
| 133 | + ("can't", 1), |
| 134 | + ("tell", 1), |
| 135 | + ("between", 1), |
| 136 | + ("app", 1), |
| 137 | + ("apple", 1), |
| 138 | + ("and", 1), |
| 139 | + ("a", 1), |
| 140 | + ]; |
| 141 | + check_word_count(output, expected); |
107 | 142 | } |
108 | 143 |
|
109 | 144 | #[test] |
110 | 145 | #[ignore] |
111 | 146 | fn multiple_spaces_not_detected_as_a_word() { |
112 | | - check_word_count( |
113 | | - " multiple whitespaces", |
114 | | - &[("multiple", 1), ("whitespaces", 1)], |
115 | | - ); |
| 147 | + let input = " multiple whitespaces"; |
| 148 | + let output = word_count::word_count(input); |
| 149 | + let expected = &[("multiple", 1), ("whitespaces", 1)]; |
| 150 | + check_word_count(output, expected); |
| 151 | +} |
| 152 | + |
| 153 | +#[test] |
| 154 | +#[ignore] |
| 155 | +fn alternating_word_separators_not_detected_as_a_word() { |
| 156 | + let input = ",\n,one,\n ,two \n 'three'"; |
| 157 | + let output = word_count::word_count(input); |
| 158 | + let expected = &[("one", 1), ("two", 1), ("three", 1)]; |
| 159 | + check_word_count(output, expected); |
| 160 | +} |
| 161 | + |
| 162 | +#[test] |
| 163 | +#[ignore] |
| 164 | +fn quotation_for_word_with_apostrophe() { |
| 165 | + let input = "can, can't, 'can't'"; |
| 166 | + let output = word_count::word_count(input); |
| 167 | + let expected = &[("can", 1), ("can't", 2)]; |
| 168 | + check_word_count(output, expected); |
116 | 169 | } |
0 commit comments