Skip to content

Commit 907b43d

Browse files
authored
feat: Pass if all lines match when trimmed (#42)
1 parent 37bcaf5 commit 907b43d

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/Runner.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from './models';
1313
import getOutput from './utils/getOutput';
1414
import saveCode from './utils/saveCode';
15+
import matchLines from './utils/matchLines';
1516

1617
interface RunnerOpts {
1718
id: string;
@@ -93,7 +94,7 @@ export default class Runner {
9394
if (exitCode === 124) {
9495
remarks = 'Time limit exceeded';
9596
} else if (exitCode === 0) {
96-
remarks = expectedOutput.trim() === obtainedOutput.trim() ? 'Pass' : 'Fail';
97+
remarks = matchLines(expectedOutput, obtainedOutput);
9798
} else {
9899
remarks = 'Error';
99100
}

src/utils/matchLines.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export default function matchLines(expected: string, obtained: string): 'Pass' | 'Fail' {
2+
function splitAndTrim(code: string) {
3+
return code.split('\n').map((sentence) => sentence.trim());
4+
}
5+
6+
const expectedArray = splitAndTrim(expected);
7+
const obtainedArray = splitAndTrim(obtained);
8+
9+
const minLength = Math.min(expectedArray.length, obtainedArray.length);
10+
11+
for (let i = 0; i < minLength; i += 1) {
12+
if (expectedArray[i] !== obtainedArray[i]) {
13+
return 'Fail';
14+
}
15+
}
16+
17+
return 'Pass';
18+
}

0 commit comments

Comments
 (0)