Skip to content

Commit f679e46

Browse files
committed
Sync word-count tests
1 parent 041e6f3 commit f679e46

File tree

2 files changed

+61
-39
lines changed

2 files changed

+61
-39
lines changed
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
function! WordCount(phrase) abort
1+
function! WordCount(phrase)
22
let words = {}
33

44
for word in split(a:phrase, "[^[:alnum:]']\\+")
5-
let word = substitute(tolower(word), "^'\\(.*\\)'$", '\1', '')
6-
let words[word] = get(words, word) + 1
5+
let word = substitute(tolower(word), "^'\\+\\|'\\+$", '', 'g')
6+
if strlen(word) > 0
7+
let words[word] = get(words, word) + 1
8+
endif
79
endfor
810

911
return words
10-
endfunction
12+
endfunction
Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,74 @@
1-
Before:
2-
if exists('expected')
3-
unlet expected
4-
endif
5-
61
Execute (count one word):
7-
let phrase = 'word'
8-
let expected = {'word': 1}
9-
AssertEqual expected, WordCount(phrase)
2+
let g:sentence = "word"
3+
let g:expected = {'word': 1}
4+
AssertEqual g:expected, WordCount(g:sentence)
105

116
Execute (count one of each word):
12-
let phrase = 'one of each'
13-
let expected = {'one': 1, 'of': 1, 'each': 1}
14-
AssertEqual expected, WordCount(phrase)
7+
let g:sentence = "one of each"
8+
let g:expected = {'of': 1, 'one': 1, 'each': 1}
9+
AssertEqual g:expected, WordCount(g:sentence)
1510

1611
Execute (multiple occurrences of a word):
17-
let phrase = 'one fish two fish red fish blue fish'
18-
let expected = {'one': 1, 'fish': 4, 'two': 1, 'red': 1, 'blue': 1}
19-
AssertEqual expected, WordCount(phrase)
12+
let g:sentence = "one fish two fish red fish blue fish"
13+
let g:expected = {'one': 1, 'blue': 1, 'two': 1, 'fish': 4, 'red': 1}
14+
AssertEqual g:expected, WordCount(g:sentence)
2015

2116
Execute (handles cramped lists):
22-
let phrase = 'one,two,three'
23-
let expected = {'one': 1, 'two': 1, 'three': 1}
24-
AssertEqual expected, WordCount(phrase)
17+
let g:sentence = "one,two,three"
18+
let g:expected = {'one': 1, 'two': 1, 'three': 1}
19+
AssertEqual g:expected, WordCount(g:sentence)
2520

2621
Execute (handles expanded lists):
27-
let phrase = "one,\ntwo,\nthree"
28-
let expected = {'one': 1, 'two': 1, 'three': 1}
29-
AssertEqual expected, WordCount(phrase)
22+
let g:sentence = "one,\ntwo,\nthree"
23+
let g:expected = {'one': 1, 'two': 1, 'three': 1}
24+
AssertEqual g:expected, WordCount(g:sentence)
3025

3126
Execute (ignore punctuation):
32-
let phrase = 'car: carpet as java: javascript!!&@$%^&'
33-
let expected = {'car': 1, 'carpet': 1, 'as': 1, 'java': 1, 'javascript': 1}
34-
AssertEqual expected, WordCount(phrase)
27+
let g:sentence = "car: carpet as java: javascript!!&@$%^&"
28+
let g:expected = {'car': 1, 'carpet': 1, 'as': 1, 'java': 1, 'javascript': 1}
29+
AssertEqual g:expected, WordCount(g:sentence)
3530

3631
Execute (include numbers):
37-
let phrase = 'testing, 1, 2 testing'
38-
let expected = {'testing': 2, '1': 1, '2': 1}
39-
AssertEqual expected, WordCount(phrase)
32+
let g:sentence = "testing, 1, 2 testing"
33+
let g:expected = {'1': 1, 'testing': 2, '2': 1}
34+
AssertEqual g:expected, WordCount(g:sentence)
4035

4136
Execute (normalize case):
42-
let phrase = 'go Go GO Stop stop'
43-
let expected = {'go': 3, 'stop': 2}
44-
AssertEqual expected, WordCount(phrase)
37+
let g:sentence = "go Go GO Stop stop"
38+
let g:expected = {'go': 3, 'stop': 2}
39+
AssertEqual g:expected, WordCount(g:sentence)
40+
41+
Execute (with apostrophes):
42+
let g:sentence = "First: don't laugh. Then: don't cry."
43+
let g:expected = {'first': 1, 'laugh': 1, 'then': 1, "don't": 2, 'cry': 1}
44+
AssertEqual g:expected, WordCount(g:sentence)
4545

4646
Execute (with apostrophes):
47-
let phrase = "First: don't laugh. Then: don't cry."
48-
let expected = {'first': 1, 'don''t': 2, 'laugh': 1, 'then': 1, 'cry': 1}
49-
AssertEqual expected, WordCount(phrase)
47+
let g:sentence = "'First: don't laugh. Then: don't cry. You're getting it.'"
48+
let g:expected = {'first': 1, 'laugh': 1, 'then': 1, 'getting': 1, 'it': 1, "you're": 1, "don't": 2, 'cry': 1}
49+
AssertEqual g:expected, WordCount(g:sentence)
5050

5151
Execute (with quotations):
52-
let phrase = "Joe can't tell between 'large' and large."
53-
let expected = {'joe': 1, 'can''t': 1, 'tell': 1, 'between': 1, 'large': 2, 'and': 1}
54-
AssertEqual expected, WordCount(phrase)
52+
let g:sentence = "Joe can't tell between 'large' and large."
53+
let g:expected = {'large': 2, "can't": 1, 'and': 1, 'tell': 1, 'joe': 1, 'between': 1}
54+
AssertEqual g:expected, WordCount(g:sentence)
55+
56+
Execute (substrings from the beginning):
57+
let g:sentence = "Joe can't tell between app, apple and a."
58+
let g:expected = {'a': 1, 'apple': 1, 'and': 1, "can't": 1, 'app': 1, 'tell': 1, 'joe': 1, 'between': 1}
59+
AssertEqual g:expected, WordCount(g:sentence)
60+
61+
Execute (multiple spaces not detected as a word):
62+
let g:sentence = " multiple whitespaces"
63+
let g:expected = {'whitespaces': 1, 'multiple': 1}
64+
AssertEqual g:expected, WordCount(g:sentence)
65+
66+
Execute (alternating word separators not detected as a word):
67+
let g:sentence = ",\n,one,\n ,two \n 'three'"
68+
let g:expected = {'one': 1, 'two': 1, 'three': 1}
69+
AssertEqual g:expected, WordCount(g:sentence)
70+
71+
Execute (quotation for word with apostrophe):
72+
let g:sentence = "can, can't, 'can't'"
73+
let g:expected = {"can't": 2, "can": 1}
74+
AssertEqual g:expected, WordCount(g:sentence)

0 commit comments

Comments
 (0)