From 9c4420df17f80538e3061ab105c725f20bd68a9d Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Mon, 7 Oct 2019 18:33:51 +0800 Subject: [PATCH] Fix the bug that LeetCode cannot test (#27) --- lib/commands/test.js | 7 +++++-- lib/plugins/leetcode.js | 8 +++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/commands/test.js b/lib/commands/test.js index cd1ad4d9..609a18fd 100644 --- a/lib/commands/test.js +++ b/lib/commands/test.js @@ -86,11 +86,14 @@ function runTest(argv) { results[0].your_input = problem.testcase; results[0].output = results[0].answer; - results[1].expected = results[1].answer; + // LeetCode-CN returns the actual and expected answer into two separate responses + if (results[1]) { + results[0].expected_answer = results[1].answer; + } results[0].stdout = results[0].stdout.slice(1, -1).replace(/\\n/g, '\n'); printResult(results[0], null, 'your_input'); printResult(results[0], results[0].runtime, 'output'); - printResult(results[1], results[1].runtime, 'expected'); + printResult(results[0], null, 'expected_answer'); printResult(results[0], null, 'stdout'); }); }); diff --git a/lib/plugins/leetcode.js b/lib/plugins/leetcode.js index 4b90412d..bc418f6f 100644 --- a/lib/plugins/leetcode.js +++ b/lib/plugins/leetcode.js @@ -266,6 +266,8 @@ function formatResult(result) { } x.stdout = util.inspect(output); x.answer = result.code_answer; + // LeetCode use 'expected_code_answer' to store the expected answer + x.expected_answer = result.expected_code_answer; } else { // It's submitting x.answer = result.code_output; @@ -291,8 +293,12 @@ plugin.testProblem = function(problem, cb) { const tasks = [ {type: 'Actual', id: task.interpret_id}, - {type: 'Expected', id: task.interpret_expected_id} ]; + + // Used by LeetCode-CN + if (task.interpret_expected_id) { + tasks.push({type: 'Expected', id: task.interpret_expected_id}); + } const q = new Queue(tasks, {opts: opts, results: []}, verifyResult); q.run(null, function(e, ctx) { return cb(e, ctx.results);