Solved and fixed tests

This commit is contained in:
J. Fernando Sánchez 2019-02-05 16:04:28 +01:00
parent 5a71a12c6b
commit 4d40dc0e02
3 changed files with 65 additions and 8 deletions

60
cmds.js
View File

@ -151,10 +151,40 @@ exports.editCmd = (rl, id) => {
* @param id Clave del quiz a probar. * @param id Clave del quiz a probar.
*/ */
exports.testCmd = (rl, id) => { exports.testCmd = (rl, id) => {
log('Probar el quiz indicado.', 'red'); if (typeof id === "undefined") {
errorlog(`Falta el parámetro id.`);
rl.prompt(); 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. * 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. * @param rl Objeto readline usado para implementar el CLI.
*/ */
exports.playCmd = rl => { exports.playCmd = rl => {
log('Jugar.', 'red'); let aciertos = 0;
let questions = shuffle(model.getAll());
const keepAsking = () => {
if (questions.length<1){
log(`Fin del juego - Aciertos: ${aciertos}`, "red");
rl.prompt(); 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();
}; };

View File

@ -96,7 +96,7 @@ describe("mooc_node-mod5_quiz_cmd", function () {
// replace answers file // replace answers file
[_, path_ok] = await to(fs.move(path.join(path_assignment, 'quizzes.json'), path.join(path_assignment, 'quizzes.original.json'), {"overwrite": true})); [_, 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) { if (error_deps) {
this.msg_err = "Error copying the answers file: " + error_deps; this.msg_err = "Error copying the answers file: " + error_deps;
error_critical = this.msg_err; error_critical = this.msg_err;
@ -301,7 +301,7 @@ describe("mooc_node-mod5_quiz_cmd", function () {
should.not.exist(error_critical); should.not.exist(error_critical);
} else { } else {
const input = ["play", "OK"]; const input = ["play", "OK"];
const expected = "/1/"; const expected = /1/;
let output = ""; let output = "";
let error_std = ""; let error_std = "";
const client = spawn("node", ["main.js"], {cwd: path_assignment}); 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_ok = `Found '${expected}' in ${path_assignment}`;
this.msg_err = `Couldn't find '${expected}' in ${path_assignment}\nError:${error_std}\nReceived:${output}`; 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(""); error_std.should.be.equal("");
Utils.search(expected, output).should.be.equal(true); Utils.search(expected, output).should.be.equal(true);
} }