Skip to content

Commit 9d3e826

Browse files
committed
Fixing output answer assertion
1 parent 2ab8b22 commit 9d3e826

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
## [Unreleased]
7+
## [1.2.0] - 2022-03-02
88
### Added
99
- Tests for "Conway sequence".
1010
- Tests for "Dwarfs standing on the shoulders of giants".
1111
- Tests for "Mayan calculation".
1212

13+
### Fixed
14+
- Output answer assertion.
15+
1316
## [1.1.0] - 2022-02-28
1417
### Added
1518
- Tests for "Defibrillators".

test/assertOutputAnswer.js

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,47 @@ import File from './File.js';
33

44
function assertOutputAnswer(fileName) {
55
let outputFile = new File(fileName);
6+
let userAnswer = '';
67
let line = outputFile.readline();
7-
let answerIndex = 0;
8+
let answer = "";
9+
10+
for(const [consoleCallIndex, consoleCall] of Object.entries(console.log.getCalls())) {
11+
userAnswer += consoleCall.args[0] + "\n";
12+
}
813

914
while(line !== "false") {
10-
assert.strictEqual(
11-
console.log.getCall(answerIndex).args[0],
12-
line
13-
);
15+
answer += line + "\n";
1416
line = outputFile.readline();
15-
answerIndex++;
17+
}
18+
19+
userAnswer = removeTraillingNewLine(userAnswer);
20+
21+
assert.strictEqual(
22+
userAnswer,
23+
answer
24+
);
25+
}
26+
27+
function removeTraillingNewLine(answer) {
28+
let index = answer.length - 1;
29+
30+
while(answer.charAt(index) === "\n") {
31+
index--;
32+
}
33+
34+
return answer.slice(0, (index + 2));
35+
}
36+
37+
function removeTraillingNewLineBAK(answer) {
38+
39+
if(
40+
answer.charAt(answer.length - 1) === "\n"
41+
&& answer.charAt(answer.length - 2) === "\n"
42+
) {
43+
return removeTraillingNewLine(answer.slice(0, answer.length - 1));
44+
}
45+
else {
46+
return answer;
1647
}
1748
}
1849

0 commit comments

Comments
 (0)