forked from AdaGold/adagrams
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathadagrams_test.rb
196 lines (147 loc) · 6.18 KB
/
adagrams_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
require 'minitest/autorun'
require 'minitest/reporters'
require 'minitest/skip_dsl'
require_relative '../lib/adagrams'
# Get that nice colorized output
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
describe 'Adagrams' do
describe 'draw_letters method' do
it 'draws ten letters from the letter pool' do
drawn_letters = draw_letters
expect(drawn_letters.size).must_equal 10
end
it 'returns an array, and each item is a single-letter string' do
drawn_letters = draw_letters
expect(drawn_letters.size).must_equal 10
expect(drawn_letters).must_be_instance_of Array
drawn_letters.each do |letter|
expect(letter).must_be_instance_of String
expect(letter.length).must_equal 1
end
end
end
describe 'uses_available_letters? method' do
it 'returns true if the submitted letters are valid against the drawn letters' do
drawn_letters = ['D', 'O', 'G', 'X', 'X', 'X', 'X', 'X', 'X', 'X']
test_word = 'DOG'
is_valid = uses_available_letters? test_word, drawn_letters
expect(is_valid).must_equal true
end
it 'returns false word contains letters not in the drawn letters' do
drawn_letters = ['D', 'O', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X']
test_word = 'DOG'
is_valid = uses_available_letters? test_word, drawn_letters
expect(is_valid).must_equal false
end
it 'returns false word contains repeated letters more than in the drawn letters' do
drawn_letters = ['A', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X']
test_word = 'AAA'
is_valid = uses_available_letters? test_word, drawn_letters
expect(is_valid).must_equal false
end
it 'does not change the letters in hand' do
drawn_letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
dl_copy = drawn_letters.dup
# Hint: if your implementation of uses_available_letters?
# needs to change the hand, you should make a copy first
uses_available_letters? 'ABCD', drawn_letters
expect(drawn_letters).must_equal dl_copy
end
end
describe 'score_word method' do
it 'returns an accurate numerical score according to the score chart' do
expect(score_word("A")).must_equal 1
expect(score_word("DOG")).must_equal 5
expect(score_word("WHIMSY")).must_equal 17
end
it 'returns a score regardless of input case' do
expect(score_word("a")).must_equal 1
expect(score_word("dog")).must_equal 5
expect(score_word("wHiMsY")).must_equal 17
end
it 'returns a score of 0 if given an empty input' do
expect(score_word("")).must_equal 0
end
it 'adds an extra 8 points if the word is 7 or more characters long' do
expect(score_word("XXXXXXX")).must_equal 64
expect(score_word("XXXXXXXX")).must_equal 72
expect(score_word("XXXXXXXXX")).must_equal 80
end
end
describe 'highest_score_from method' do
it 'returns a hash that contains the word and score of best word in an array' do
words = ['X', 'XX', 'XXX', 'XXXX']
best_word = highest_score_from words
expect(best_word[:word]).must_equal 'XXXX'
expect(best_word[:score]).must_equal 32
end
it 'accurately finds best scoring word even if not sorted' do
words = ['XXX', 'XXXX', 'XX', 'X']
best_word = highest_score_from words
expect(best_word[:word]).must_equal 'XXXX'
expect(best_word[:score]).must_equal 32
end
it 'in case of tied score, prefers the word with fewer letters' do
# the character 'M' is worth 3 points, 'W' is 4 points
words = ['MMMM', 'WWW']
# verify both have a score of 12
expect(score_word(words.first)).must_equal 12
expect(score_word(words.last)).must_equal 12
best_word = highest_score_from words
expect(best_word[:word]).must_equal 'WWW'
expect(best_word[:score]).must_equal 12
end
it 'in case of tied score, prefers the word with fewer letters regardless of order' do
# the character 'M' is worth 3 points, 'W' is 4 points
words = ['WWW', 'MMMM']
# verify both have a score of 12
expect(score_word(words.first)).must_equal 12
expect(score_word(words.last)).must_equal 12
best_word = highest_score_from words
expect(best_word[:word]).must_equal 'WWW'
expect(best_word[:score]).must_equal 12
end
it 'in case of tied score, prefers most the word with 10 letters' do
# the character 'A' is worth 1 point, 'B' is 3 points
words = ['AAAAAAAAAA', 'BBBBBB']
# verify both have a score of 10
expect(score_word(words.first)).must_equal 18
expect(score_word(words.last)).must_equal 18
best_word = highest_score_from words
expect(best_word[:word]).must_equal 'AAAAAAAAAA'
expect(best_word[:score]).must_equal 18
end
it 'in case of tied score, prefers most the word with 10 letters regardless of order' do
# the character 'A' is worth 1 point, 'B' is 3 points
words = ['BBBBBB', 'AAAAAAAAAA']
# verify both have a score of 10
expect(score_word(words.first)).must_equal 18
expect(score_word(words.last)).must_equal 18
best_word = highest_score_from words
expect(best_word[:word]).must_equal 'AAAAAAAAAA'
expect(best_word[:score]).must_equal 18
end
it 'in case of tied score and same length words, prefers the first word' do
# the character 'A' is worth 1 point, 'E' is 1 point
words = ['AAAAAAAAAA', 'EEEEEEEEEE']
# verify both have a score of 10
expect(score_word(words.first)).must_equal 18
expect(score_word(words.last)).must_equal 18
best_word = highest_score_from words
expect(best_word[:word]).must_equal words.first
expect(best_word[:score]).must_equal 18
end
end
describe 'is_in_english_dict?' do
it 'checks an input string against a list of valid English words' do
word1 = "APPLE"
word2 = "pencil"
word3 = "pppppppppp"
word4 = "PROGRAMMING"
expect(is_in_english_dict?(word1)).must_equal true
expect(is_in_english_dict?(word2)).must_equal true
expect(is_in_english_dict?(word3)).must_equal false
expect(is_in_english_dict?(word4)).must_equal false
end
end
end