Skip to content

Commit dec366d

Browse files
authored
Merge pull request Ada-C12#29 from AdaGold/be/update-tests
Be/update tests
2 parents 49c3799 + 265d6dd commit dec366d

File tree

4 files changed

+153
-74
lines changed

4 files changed

+153
-74
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ In this project, we will build the same Adagrams project from the Python curricu
1616

1717
A test suite and sample game project is provided in order to practice TDD and verify the correct implementation. A complete project will pass all of the provided tests and have coverage of at least 90%.
1818

19+
Note there are a handful of incomplete tests that currently throw exceptions. You should complete these tests and remove the exception.
20+
1921
## Getting Started
2022

2123
After forking and cloning this repo you should `cd` to the project directory and run:

src/adagrams.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
const Adagrams = {
2-
drawLetters() {
3-
// Implement this method for wave 1
4-
},
1+
export const drawLetters = () => {
2+
// Implement this method for wave 1
53
};
64

7-
// Do not remove this line or your tests will break!
8-
export default Adagrams;
5+
export const usesAvailableLetters = (input, lettersInHand) => {
6+
// Implement this method for wave 2
7+
};
8+
9+
export const scoreWord = (word) => {
10+
// Implement this method for wave 3
11+
};
12+
13+
export const highestScoreFrom = (words) => {
14+
// Implement this method for wave 1
15+
};

src/demo/adagrams.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,50 @@
1-
import Real from 'adagrams';
1+
import {
2+
drawLetters,
3+
usesAvailableLetters,
4+
scoreWord,
5+
highestScoreFrom,
6+
} from "adagrams";
7+
8+
const Real = {
9+
drawLetters,
10+
usesAvailableLetters,
11+
scoreWord,
12+
highestScoreFrom,
13+
};
214

315
const Stub = {
416
drawLetters() {
5-
const defaultLetters = ['H', 'E', 'L', 'L', 'O', 'W', 'O', 'R', 'L', 'D'];
17+
const defaultLetters = ["H", "E", "L", "L", "O", "W", "O", "R", "L", "D"];
618

7-
if(typeof Real.drawLetters === 'function') {
19+
if (typeof Real.drawLetters === "function") {
820
return Real.drawLetters() || defaultLetters;
921
}
1022

1123
return defaultLetters;
1224
},
1325

1426
usesAvailableLetters(input, lettersInHand) {
15-
if(typeof Real.usesAvailableLetters === 'function') {
27+
if (typeof Real.usesAvailableLetters === "function") {
1628
return Real.usesAvailableLetters(input, lettersInHand);
1729
}
1830

1931
return true;
2032
},
2133

2234
scoreWord(word) {
23-
if(typeof Real.scoreWord === 'function') {
35+
if (typeof Real.scoreWord === "function") {
2436
return Real.scoreWord(word);
2537
}
2638

2739
return -1;
2840
},
2941

3042
highestScoreFrom(words) {
31-
if(typeof Real.highestScoreFrom === 'function') {
43+
if (typeof Real.highestScoreFrom === "function") {
3244
return Real.highestScoreFrom(words);
3345
}
3446

35-
if(words.length < 1) return null;
47+
if (words.length < 1) return null;
3648

3749
return {
3850
word: words[0],

test/adagrams.test.js

Lines changed: 119 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,129 @@
1-
import Adagrams from 'adagrams';
2-
3-
describe('Adagrams', () => {
4-
describe('drawLetters', () => {
5-
it('draws ten letters from the letter pool', () => {
6-
const drawn = Adagrams.drawLetters();
1+
import {
2+
drawLetters,
3+
usesAvailableLetters,
4+
scoreWord,
5+
highestScoreFrom,
6+
} from "adagrams";
7+
8+
const LETTER_POOL = {
9+
A: 9,
10+
B: 2,
11+
C: 2,
12+
D: 4,
13+
E: 12,
14+
F: 2,
15+
G: 3,
16+
H: 2,
17+
I: 9,
18+
J: 1,
19+
K: 1,
20+
L: 4,
21+
M: 2,
22+
N: 6,
23+
O: 8,
24+
P: 2,
25+
Q: 1,
26+
R: 6,
27+
S: 4,
28+
T: 6,
29+
U: 4,
30+
V: 2,
31+
W: 2,
32+
X: 1,
33+
Y: 2,
34+
Z: 1,
35+
};
36+
37+
describe("Adagrams", () => {
38+
describe("drawLetters", () => {
39+
it("draws ten letters from the letter pool", () => {
40+
const drawn = drawLetters();
741

842
expect(drawn).toHaveLength(10);
943
});
1044

11-
it('returns an array, and each item is a single-letter string', () => {
12-
const drawn = Adagrams.drawLetters();
45+
it("returns an array, and each item is a single-letter string", () => {
46+
const drawn = drawLetters();
1347

1448
expect(Array.isArray(drawn)).toBe(true);
1549
drawn.forEach((l) => {
1650
expect(l).toMatch(/^[A-Z]$/);
1751
});
1852
});
53+
54+
it("does not draw a letter too many times", () => {
55+
for (let i = 0; i < 1000; i++) {
56+
const drawn = drawLetters();
57+
const letter_freq = {};
58+
for (let letter of drawn) {
59+
if (letter in letter_freq) {
60+
letter_freq[letter] += 1;
61+
} else {
62+
letter_freq[letter] = 1;
63+
}
64+
}
65+
66+
for (let letter of drawn) {
67+
expect(letter_freq[letter]).toBeLessThanOrEqual(LETTER_POOL[letter]);
68+
}
69+
}
70+
});
1971
});
2072

21-
describe('usesAvailableLetters', () => {
22-
it('returns true if the submitted letters are valid against the drawn letters', () => {
23-
const drawn = ['D', 'O', 'G', 'X', 'X', 'X', 'X', 'X', 'X', 'X'];
24-
const word = 'DOG';
73+
describe("usesAvailableLetters", () => {
74+
it("returns true if the submitted letters are valid against the drawn letters", () => {
75+
const drawn = ["D", "O", "G", "X", "X", "X", "X", "X", "X", "X"];
76+
const word = "DOG";
2577

26-
const isValid = Adagrams.usesAvailableLetters(word, drawn);
78+
const isValid = usesAvailableLetters(word, drawn);
2779
expect(isValid).toBe(true);
2880
});
2981

30-
it('returns false when word contains letters not in the drawn letters', () => {
31-
const drawn = ['D', 'O', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'];
32-
const word = 'DOG';
82+
it("returns false when word contains letters not in the drawn letters", () => {
83+
const drawn = ["D", "O", "X", "X", "X", "X", "X", "X", "X", "X"];
84+
const word = "DOG";
3385

34-
const isValid = Adagrams.usesAvailableLetters(word, drawn);
86+
const isValid = usesAvailableLetters(word, drawn);
3587
expect(isValid).toBe(false);
3688
});
3789

38-
it('returns false when word contains repeated letters more than in the drawn letters', () => {
39-
const drawn = ['D', 'O', 'G', 'X', 'X', 'X', 'X', 'X', 'X', 'X'];
40-
const word = 'GOOD';
90+
it("returns false when word contains repeated letters more than in the drawn letters", () => {
91+
const drawn = ["D", "O", "G", "X", "X", "X", "X", "X", "X", "X"];
92+
const word = "GOOD";
4193

42-
const isValid = Adagrams.usesAvailableLetters(word, drawn);
94+
const isValid = usesAvailableLetters(word, drawn);
4395
expect(isValid).toBe(false);
44-
4596
});
4697
});
4798

48-
describe('scoreWord', () => {
99+
describe("scoreWord", () => {
49100
const expectScores = (wordScores) => {
50101
Object.entries(wordScores).forEach(([word, score]) => {
51-
expect(Adagrams.scoreWord(word)).toBe(score);
102+
expect(scoreWord(word)).toBe(score);
52103
});
53104
};
54105

55-
it('returns an accurate numerical score according to the score chart', () => {
106+
it("returns an accurate numerical score according to the score chart", () => {
56107
expectScores({
57108
A: 1,
58109
DOG: 5,
59-
WHIMSY: 17
110+
WHIMSY: 17,
60111
});
61112
});
62113

63-
it('returns a score regardless of the input case', () => {
114+
it("returns a score regardless of the input case", () => {
64115
expectScores({
65116
a: 1,
66117
dog: 5,
67-
wHiMsY: 17
118+
wHiMsY: 17,
68119
});
69120
});
70121

71-
it('returns a score of 0 if given an empty input', () => {
72-
expectScores({
73-
'': 0
74-
});
122+
it("returns a score of 0 if given an empty input", () => {
123+
throw "Complete test";
75124
});
76125

77-
it('adds an extra 8 points if word is 7 or more characters long', () => {
126+
it("adds an extra 8 points if word is 7 or more characters long", () => {
78127
expectScores({
79128
XXXXXXX: 64,
80129
XXXXXXXX: 72,
@@ -84,57 +133,66 @@ describe('Adagrams', () => {
84133
});
85134
});
86135

87-
describe.skip('highestScoreFrom', () => {
88-
it('returns a hash that contains the word and score of best word in an array', () => {
89-
const words = ['X', 'XX', 'XXX', 'XXXX'];
90-
const correct = { word: 'XXXX', score: Adagrams.scoreWord('XXXX') };
136+
describe("highestScoreFrom", () => {
137+
it("returns a hash that contains the word and score of best word in an array", () => {
138+
const words = ["X", "XX", "XXX", "XXXX"];
139+
const correct = { word: "XXXX", score: scoreWord("XXXX") };
91140

92-
expect(Adagrams.highestScoreFrom(words)).toEqual(correct);
141+
expect(highestScoreFrom(words)).toEqual(correct);
93142
});
94143

95-
it('accurately finds best scoring word even if not sorted', () => {
96-
const words = ['XXX', 'XXXX', 'X', 'XX'];
97-
const correct = { word: 'XXXX', score: Adagrams.scoreWord('XXXX') };
144+
it("accurately finds best scoring word even if not sorted", () => {
145+
const words = ["XXX", "XXXX", "X", "XX"];
146+
const correct = { word: "XXXX", score: scoreWord("XXXX") };
98147

99-
expect(Adagrams.highestScoreFrom(words)).toEqual(correct);
148+
throw "Complete test by adding an assertion";
100149
});
101150

102-
describe('in case of tied score', () => {
151+
describe("in case of tied score", () => {
103152
const expectTie = (words) => {
104-
const scores = words.map(word => Adagrams.scoreWord(word));
105-
const highScore = scores.reduce((h, s) => h < s ? s : h, 0);
153+
const scores = words.map((word) => scoreWord(word));
154+
const highScore = scores.reduce((h, s) => (h < s ? s : h), 0);
106155
const tiedWords = scores.filter((s) => s == highScore);
107156

108157
// Expect at least two tied words
109158
expect(tiedWords.length).toBeGreaterThan(1);
110159
};
111160

112-
it('selects the word with 10 letters', () => {
113-
const words = ['AAAAAAAAAA', 'BBBBBB'];
114-
const correct = { word: 'AAAAAAAAAA', score: Adagrams.scoreWord('AAAAAAAAAA') };
161+
it("selects the word with 10 letters", () => {
162+
const words = ["AAAAAAAAAA", "BBBBBB"];
163+
const correct = {
164+
word: "AAAAAAAAAA",
165+
score: scoreWord("AAAAAAAAAA"),
166+
};
115167
expectTie(words);
116168

117-
expect(Adagrams.highestScoreFrom(words)).toEqual(correct);
118-
expect(Adagrams.highestScoreFrom(words.reverse())).toEqual(correct);
169+
expect(highestScoreFrom(words)).toEqual(correct);
170+
expect(highestScoreFrom(words.reverse())).toEqual(correct);
119171
});
120172

121-
it('selects the word with fewer letters when neither are 10 letters', () => {
122-
const words = ['MMMM', 'WWW'];
123-
const correct = { word: 'WWW', score: Adagrams.scoreWord('WWW') };
173+
it("selects the word with fewer letters when neither are 10 letters", () => {
174+
const words = ["MMMM", "WWW"];
175+
const correct = { word: "WWW", score: scoreWord("WWW") };
124176
expectTie(words);
125177

126-
expect(Adagrams.highestScoreFrom(words)).toEqual(correct);
127-
expect(Adagrams.highestScoreFrom(words.reverse())).toEqual(correct);
178+
expect(highestScoreFrom(words)).toEqual(correct);
179+
expect(highestScoreFrom(words.reverse())).toEqual(correct);
128180
});
129181

130-
it('selects the first word when both have same length', () => {
131-
const words = ['AAAAAAAAAA', 'EEEEEEEEEE'];
132-
const first = { word: 'AAAAAAAAAA', score: Adagrams.scoreWord('AAAAAAAAAA') };
133-
const second = { word: 'EEEEEEEEEE', score: Adagrams.scoreWord('EEEEEEEEEE') };
182+
it("selects the first word when both have same length", () => {
183+
const words = ["AAAAAAAAAA", "EEEEEEEEEE"];
184+
const first = {
185+
word: "AAAAAAAAAA",
186+
score: scoreWord("AAAAAAAAAA"),
187+
};
188+
const second = {
189+
word: "EEEEEEEEEE",
190+
score: scoreWord("EEEEEEEEEE"),
191+
};
134192
expectTie(words);
135193

136-
expect(Adagrams.highestScoreFrom(words)).toEqual(first);
137-
expect(Adagrams.highestScoreFrom(words.reverse())).toEqual(second);
194+
expect(highestScoreFrom(words)).toEqual(first);
195+
expect(highestScoreFrom(words.reverse())).toEqual(second);
138196
});
139197
});
140198
});

0 commit comments

Comments
 (0)