Skip to content

Commit 110d5cc

Browse files
committed
refactor comparison function, provide diff of solution & attempt
1 parent f9c72ca commit 110d5cc

23 files changed

Lines changed: 172 additions & 113 deletions

File tree

compare-solution.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
require("colors");
2+
3+
var path = require("path");
4+
var diff = require("diff");
5+
var run = require(path.join(__dirname, "run-solution"));
6+
7+
module.exports = function(solution, attempt, cb) {
8+
9+
run(solution, function(err, solutionResult) {
10+
11+
if(err) {
12+
console.error(err);
13+
return cb(false);
14+
}
15+
16+
run(attempt, function(err, attemptResult) {
17+
18+
if(err) {
19+
if(err.code !== 8) {
20+
console.error(err);
21+
}
22+
return cb(false);
23+
}
24+
25+
if(solutionResult === attemptResult) {
26+
return cb(true);
27+
}
28+
29+
console.error("\nSolution:\n------------------------");
30+
console.error(solutionResult);
31+
console.error("Your attempt:\n------------------------");
32+
console.error(attemptResult);
33+
console.error("Difference:\n------------------------");
34+
console.error(generateDiff(solutionResult, attemptResult));
35+
36+
cb(false);
37+
38+
});
39+
40+
});
41+
42+
}
43+
44+
function generateDiff(solution, attempt) {
45+
46+
var parts = diff.diffChars(solution, attempt);
47+
48+
var result = "";
49+
50+
parts.forEach(function(part) {
51+
52+
if(part.added) {
53+
result += part.value["bgRed"];
54+
} else if(part.removed) {
55+
result += part.value["bgGreen"];
56+
} else {
57+
result += part.value;
58+
}
59+
60+
});
61+
62+
return result;
63+
64+
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
"preferGlobal": true,
1313
"dependencies": {
1414
"adventure": "^2.8.0",
15-
"cli-md": "^0.1.0"
15+
"cli-md": "^0.1.0",
16+
"colors": "^1.0.3",
17+
"diff": "^1.2.1"
1618
}
1719
}

problems/array-filtering/index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
var path = require('path');
22
var getFile = require('../../get-file');
3-
var run = require('../../run-solution');
3+
var compare = require('../../compare-solution');
44

55
exports.problem = getFile(path.join(__dirname, 'problem.md'));
66

77
exports.solution = getFile(path.join(__dirname, 'solution.md'));
88

99
exports.fail = getFile(path.join(__dirname, 'troubleshooting.md'));
1010

11+
var solutionPath = path.resolve(__dirname, "../../solutions/array-filtering/index.js");
12+
1113
exports.verify = function (args, cb) {
12-
run(args[0], function (err, result) {
13-
var expected = "[ 2, 4, 6, 8, 10 ]\n";
14-
if (result === expected) cb(true);
15-
else cb(false);
16-
});
14+
var attemptPath = path.resolve(process.cwd(), args[0]);
15+
compare(solutionPath, attemptPath, cb);
1716
};
1817

1918
exports.run = function (args) {

problems/arrays/index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
var path = require('path');
22
var getFile = require('../../get-file');
3-
var run = require('../../run-solution');
3+
var compare = require('../../compare-solution');
44

55
exports.problem = getFile(path.join(__dirname, 'problem.md'));
66

77
exports.solution = getFile(path.join(__dirname, 'solution.md'));
88

99
exports.fail = getFile(path.join(__dirname, 'troubleshooting.md'));
1010

11+
var solutionPath = path.resolve(__dirname, "../../solutions/arrays/index.js");
12+
1113
exports.verify = function (args, cb) {
12-
run(args[0], function (err, result) {
13-
var expected = "[ 'tomato sauce', 'cheese', 'pepperoni' ]\n";
14-
if (result && result.replace('"', "'") === expected) cb(true);
15-
else cb(false);
16-
});
14+
var attemptPath = path.resolve(process.cwd(), args[0]);
15+
compare(solutionPath, attemptPath, cb);
1716
};
1817

1918
exports.run = function (args) {

problems/for-loop/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
var path = require('path');
22
var getFile = require('../../get-file');
3-
var run = require('../../run-solution');
3+
var compare = require('../../compare-solution');
44

55
exports.problem = getFile(path.join(__dirname, 'problem.md'));
66

77
exports.solution = getFile(path.join(__dirname, 'solution.md'));
88

99
exports.fail = getFile(path.join(__dirname, 'troubleshooting.md'));
1010

11+
var solutionPath = path.resolve(__dirname, "../../solutions/for-loop/index.js");
12+
1113
exports.verify = function (args, cb) {
12-
run(args[0], function (err, result) {
13-
if (/^45\n$/.test(result)) cb(true);
14-
else cb(false);
15-
});
14+
var attemptPath = path.resolve(process.cwd(), args[0]);
15+
compare(solutionPath, attemptPath, cb);
1616
};
1717

1818
exports.run = function (args) {

problems/function-arguments/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
var path = require('path');
22
var getFile = require('../../get-file');
3-
var run = require('../../run-solution');
3+
var compare = require('../../compare-solution');
44

55
exports.problem = getFile(path.join(__dirname, 'problem.md'));
66

77
exports.solution = getFile(path.join(__dirname, 'solution.md'));
88

99
exports.fail = getFile(path.join(__dirname, 'troubleshooting.md'));
1010

11+
var solutionPath = path.resolve(__dirname, "../../solutions/function-arguments/index.js");
12+
1113
exports.verify = function (args, cb) {
12-
run(args[0], function (err, result) {
13-
if (/^4140\n$/.test(result)) cb(true);
14-
else cb(false);
15-
});
14+
var attemptPath = path.resolve(process.cwd(), args[0]);
15+
compare(solutionPath, attemptPath, cb);
1616
};
1717

1818
exports.run = function (args) {

problems/function-return-values/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
var path = require('path');
22
var getFile = require('../../get-file');
3-
var run = require('../../run-solution');
3+
var compare = require('../../compare-solution');
44

55
exports.problem = getFile(path.join(__dirname, 'problem.md'));
66

77
exports.solution = getFile(path.join(__dirname, 'solution.md'));
88

99
exports.fail = getFile(path.join(__dirname, 'troubleshooting.md'));
1010

11+
var solutionPath = path.resolve(__dirname, "../../solutions/function-return-values/index.js");
12+
1113
exports.verify = function (args, cb) {
12-
run(args[0], function (err, result) {
13-
if (/hello/.test(result)) cb(true);
14-
else cb(false);
15-
});
14+
var attemptPath = path.resolve(process.cwd(), args[0]);
15+
compare(solutionPath, attemptPath, cb);
1616
};
1717

1818
exports.run = function (args) {

problems/functions/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
var path = require('path');
22
var getFile = require('../../get-file');
3-
var run = require('../../run-solution');
3+
var compare = require('../../compare-solution');
44

55
exports.problem = getFile(path.join(__dirname, 'problem.md'));
66

77
exports.solution = getFile(path.join(__dirname, 'solution.md'));
88

99
exports.fail = getFile(path.join(__dirname, 'troubleshooting.md'));
1010

11+
var solutionPath = path.resolve(__dirname, "../../solutions/functions/index.js");
12+
1113
exports.verify = function (args, cb) {
12-
run(args[0], function (err, result) {
13-
if (/^bananas tasted really good.\n$/.test(result)) cb(true);
14-
else cb(false);
15-
});
14+
var attemptPath = path.resolve(process.cwd(), args[0]);
15+
compare(solutionPath, attemptPath, cb);
1616
};
1717

1818
exports.run = function (args) {

problems/if-statement/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
var path = require('path');
22
var getFile = require('../../get-file');
3-
var run = require('../../run-solution');
3+
var compare = require('../../compare-solution');
44

55
exports.problem = getFile(path.join(__dirname, 'problem.md'));
66

77
exports.solution = getFile(path.join(__dirname, 'solution.md'));
88

99
exports.fail = getFile(path.join(__dirname, 'troubleshooting.md'));
1010

11+
var solutionPath = path.resolve(__dirname, "../../solutions/if-statement/index.js");
12+
1113
exports.verify = function (args, cb) {
12-
run(args[0], function (err, result) {
13-
if (/^The fruit name has more than five characters.\n$/.test(result)) cb(true);
14-
else cb(false);
15-
});
14+
var attemptPath = path.resolve(process.cwd(), args[0]);
15+
compare(solutionPath, attemptPath, cb);
1616
};
1717

1818
exports.run = function (args) {

problems/introduction/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
var path = require('path');
22
var getFile = require('../../get-file');
3-
var run = require('../../run-solution');
3+
var compare = require('../../compare-solution');
44

55
exports.problem = getFile(path.join(__dirname, 'problem.md'));
66

77
exports.solution = getFile(path.join(__dirname, 'solution.md'));
88

99
exports.fail = getFile(path.join(__dirname, 'troubleshooting.md'));
1010

11+
var solutionPath = path.resolve(__dirname, "../../solutions/introduction/index.js");
12+
1113
exports.verify = function (args, cb) {
12-
run(args[0], function (err, result) {
13-
if (/^hello\n$/.test(result)) cb(true);
14-
else cb(false);
15-
});
14+
var attemptPath = path.resolve(process.cwd(), args[0]);
15+
compare(solutionPath, attemptPath, cb);
1616
};
1717

1818
exports.run = function (args) {

0 commit comments

Comments
 (0)