From 4d40dc0e0241a86b250d68a4c041b469bd896823 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Tue, 5 Feb 2019 16:04:28 +0100 Subject: [PATCH] Solved and fixed tests --- cmds.js | 64 +++++++++++++++++++++++++++++++++++++++++--- tests/checks.test.js | 7 ++--- tests/quizzes.json | 2 +- 3 files changed, 65 insertions(+), 8 deletions(-) diff --git a/cmds.js b/cmds.js index 480f94b..33734eb 100644 --- a/cmds.js +++ b/cmds.js @@ -151,10 +151,40 @@ exports.editCmd = (rl, id) => { * @param id Clave del quiz a probar. */ exports.testCmd = (rl, id) => { - log('Probar el quiz indicado.', 'red'); - rl.prompt(); + if (typeof id === "undefined") { + errorlog(`Falta el parĂ¡metro id.`); + rl.prompt(); + } else { + try { + const quiz = model.getByIndex(id); + + rl.question(colorize(quiz.question + " ", 'red'), answer => { + if ( quiz.answer == answer ) { + biglog("correct", "green"); + } else { + biglog("incorrect", "red"); + } + rl.prompt(); + }); + } catch (error) { + errorlog(error.message); + rl.prompt(); + } + } }; +/** + * Shuffles array in place. ES6 version + * @param {Array} a items An array containing the items. + */ +function shuffle(a) { + for (let i = a.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [a[i], a[j]] = [a[j], a[i]]; + } + return a; +} + /** * Pregunta todos los quizzes existentes en el modelo en orden aleatorio. @@ -163,8 +193,34 @@ exports.testCmd = (rl, id) => { * @param rl Objeto readline usado para implementar el CLI. */ exports.playCmd = rl => { - log('Jugar.', 'red'); - rl.prompt(); + let aciertos = 0; + let questions = shuffle(model.getAll()); + + const keepAsking = () => { + if (questions.length<1){ + log(`Fin del juego - Aciertos: ${aciertos}`, "red"); + rl.prompt(); + return + } + try { + let quiz = questions.pop(); + + rl.question(colorize(quiz.question + " ", 'red'), answer => { + if ( quiz.answer == answer ) { + aciertos += 1; + log(`CORRECTO - lleva ${aciertos} aciertos`, "green"); + } else { + log("INCORRECTO", "red"); + questions = []; + } + keepAsking() + }); + } catch (error) { + errorlog(error.message); + rl.prompt(); + } + } + keepAsking(); }; diff --git a/tests/checks.test.js b/tests/checks.test.js index 21bb17d..611485c 100644 --- a/tests/checks.test.js +++ b/tests/checks.test.js @@ -96,7 +96,7 @@ describe("mooc_node-mod5_quiz_cmd", function () { // replace answers file [_, path_ok] = await to(fs.move(path.join(path_assignment, 'quizzes.json'), path.join(path_assignment, 'quizzes.original.json'), {"overwrite": true})); - [error_deps, path_ok] = await to(fs.move(path.join(path_assignment, 'tests', 'quizzes.json'), path.join(path_assignment, 'quizzes.json'), {"overwrite": true})); + [error_deps, path_ok] = await to(fs.copy(path.join(path_assignment, 'tests', 'quizzes.json'), path.join(path_assignment, 'quizzes.json'), {"overwrite": true})); if (error_deps) { this.msg_err = "Error copying the answers file: " + error_deps; error_critical = this.msg_err; @@ -301,7 +301,7 @@ describe("mooc_node-mod5_quiz_cmd", function () { should.not.exist(error_critical); } else { const input = ["play", "OK"]; - const expected = "/1/"; + const expected = /1/; let output = ""; let error_std = ""; const client = spawn("node", ["main.js"], {cwd: path_assignment}); @@ -327,6 +327,7 @@ describe("mooc_node-mod5_quiz_cmd", function () { } this.msg_ok = `Found '${expected}' in ${path_assignment}`; this.msg_err = `Couldn't find '${expected}' in ${path_assignment}\nError:${error_std}\nReceived:${output}`; + console.log(expected, "OUT", output); error_std.should.be.equal(""); Utils.search(expected, output).should.be.equal(true); } @@ -411,4 +412,4 @@ describe("mooc_node-mod5_quiz_cmd", function () { after("Restoring the original file", async function () { [error_copy, path_ok] = await to(fs.move(path.join(path_assignment, 'quizzes.original.json'), path.join(path_assignment, 'quizzes.json'), {"overwrite": true})); }); -}); \ No newline at end of file +}); diff --git a/tests/quizzes.json b/tests/quizzes.json index 66bc1c2..58d73fc 100644 --- a/tests/quizzes.json +++ b/tests/quizzes.json @@ -1 +1 @@ -[{"question": "Answer Number 1","answer": "OK"},{"question": "Answer Number 2","answer": "OK"}] \ No newline at end of file +[{"question":"Answer Number 1","answer":"OK"},{"question":"Answer Number 2","answer":"OK"}] \ No newline at end of file