Skip to content

Commit

Permalink
adds GCSEtest() as more GCSE compatible replacement for assert()
Browse files Browse the repository at this point in the history
Fixes #26
  • Loading branch information
stretchyboy committed Feb 28, 2021
1 parent 4807399 commit 1b9d7ed
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 21 deletions.
2 changes: 1 addition & 1 deletion dist/exercises.json

Large diffs are not rendered by default.

41 changes: 36 additions & 5 deletions exercises/central_heating/tests.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
assert( calcBoilerStatus(19, 22, 18, "OFF") == "ON", 'Boiler should turn ON when too cold.', "logic");
GCSEtest({
"text":'Boiler should turn ON when too cold.',
"func": calcBoilerStatus,
"type":"logic",
"input_type":"valid",
"inputs":[19, 22, 18, "OFF"],
"expected":"ON",
"task":1
})

assert( calcBoilerStatus(19, 22, 23, "ON") == "OFF", 'Boiler should turn OFF when too hot.', "logic");
GCSEtest({
"text":'Boiler should turn OFF when too hot.',
"func": calcBoilerStatus,
"type":"logic",
"input_type":"valid",
"inputs":[19, 22, 23, "ON"],
"expected":"OFF",
"task":2
})

assert( calcBoilerStatus(19, 22, 20, "ON") == "ON", 'Boiler should stay ON when temp is OK.', "logic");

assert( calcBoilerStatus(19, 22, 20, "OFF") == "OFF", 'Boiler should stay OFF when temp is OK.', "logic");
GCSEtest({
"text":'Boiler should stay ON when temp is OK.',
"func": calcBoilerStatus,
"type":"logic",
"input_type":"valid",
"inputs":[19, 22, 20, "ON"],
"expected":"ON",
"task":3
})

GCSEtest({
"text":'Boiler should stay OFF when temp is OK.',
"func": calcBoilerStatus,
"type":"logic",
"input_type":"valid",
"inputs":[19, 22, 20, "OFF"],
"expected":"OFF",
"task":4
})
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ <h2>Result</h2>
<div id="tests">
<h2>Test Results</h2>
<ul id="testoutput"></ul>
<table id="testtable">
</table>
<h3 id="result"></h3>
<a href="" id="next" >Next Exercise</a>
<!--<a href="" id="next" target="_blank">Next Exercise</a>-->
Expand Down
111 changes: 96 additions & 15 deletions js/executer.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,31 @@ var Executer = function (sTestScript, sContextScript){

if(this.bTest) {
script += "var assert = function(outcome, description, type ) {aTests.push({text:description,type:type,label:outcome?'Pass':'Fail',pass:outcome, row: false, col: false});};";
script += "var GCSEtest = function(options){";
script += "var actual = options.func.apply(null, options.inputs);";
script += "var outcome = (actual == options.expected);";
script += "var result = new Object(options);";
script += "result.actual = actual;";
script += "result.label = outcome?'Pass':'Fail';";
script += "result.pass = outcome;";
script += "result.row = false; ";
script += "result.col = false;";
script += "aTests.push(result);";
script += "};";
/*
var GCSEtest = function(options){
var actual = options.func.apply(null, options.inputs);
var outcome = (actual == options.expected);
var result = new Object(options);
result.actual = actual;
result.label = outcome?'Pass':'Fail';
result.pass = outcome;
result.row = false;
result.col = false;
aTests.push(result);
};
*/

}
script += this.sContextScript.replace("//CODE//", this.sCode);

Expand All @@ -67,6 +92,25 @@ var Executer = function (sTestScript, sContextScript){

};


this.getExecutionPython = function() {
var script = "aTests = [];";

if(this.bTest) {
script += "def assert(outcome, description, type ) {aTests.push({text:description,type:type,label:outcome?'Pass':'Fail',pass:outcome, row: false, col: false});};";

}
script += this.sContextScript.replace("##CODE##", this.sCode);

if(this.bTest) {
// TODO : if()//TESTS// not in script add it at end
script = script.replace("##TESTS##", this.sTestScript);
}
script += "\n\nreturn aTests";
return script;

};

this.execute = function(){
window.clearInterval(intervalID);
this.calcAnnoErrors();
Expand Down Expand Up @@ -109,22 +153,59 @@ var Executer = function (sTestScript, sContextScript){

this.resultsToHTML = function(){
if(this.aTests.length) {
$("#testtable").html("<tr><th>Test</th><th>Function</th><th>Input Type</th><th>Inputs</th><th>Expected</th><th>Actual</th><th>Outcome</th></tr>");
this.aTests.forEach(function(oMess, iKey) {
var li = document.createElement('li');
li.className = oMess.label.toLowerCase();

var sText = oMess.text;

if(oMess.row !== false) {
$(li).click(function(){
editor.gotoLine(oMess.row + 1);
});
sText = "Line "+(oMess.row+1)+" : "+sText;
}

li.appendChild( document.createTextNode( sText ) );
var eOut = $("#testoutput");
eOut.append(li);
if(oMess.input_type){
var tr = document.createElement('tr');
tr.className = oMess.label.toLowerCase();
//var aNum = document.createElement('td');

var eTest = document.createElement('td');
eTest.appendChild( document.createTextNode( oMess.text ) );

tr.appendChild(eTest)
var eFunc = document.createElement('td');
eFunc.appendChild( document.createTextNode( oMess.func.name ) );
tr.appendChild(eFunc)

var eIT = document.createElement('td');
eIT.appendChild( document.createTextNode( oMess.input_type ) );
tr.appendChild(eIT)

var eInputs = document.createElement('td');
eInputs.appendChild( document.createTextNode( oMess.inputs.join(", ") ) );
tr.appendChild(eInputs)

var eExpected = document.createElement('td');
eExpected.appendChild( document.createTextNode( oMess.expected ) );
tr.appendChild(eExpected)

var eActual = document.createElement('td');
eActual.appendChild( document.createTextNode( oMess.actual ) );
tr.appendChild(eActual)

var eOutcome = document.createElement('td');
eOutcome.appendChild( document.createTextNode( oMess.label ) );
tr.appendChild(eOutcome)

var eOut = $("#testtable");
eOut.append(tr);
}else{
var li = document.createElement('li');
li.className = oMess.label.toLowerCase();

var sText = oMess.text;

if(oMess.row !== false) {
$(li).click(function(){
editor.gotoLine(oMess.row + 1);
});
sText = "Line "+(oMess.row+1)+" : "+sText;
}
li.appendChild( document.createTextNode( sText ) );
var eOut = $("#testoutput");
eOut.append(li);
}
});
}
};
Expand Down
1 change: 1 addition & 0 deletions js/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ var Exercise = function (aData, sExercise){
this.resetGUI =function () {
$("#output").html("");
$("#testoutput").html("");
$("#testtable").html("");
$("#result").html("");
$("#simulation").html(this.aData.simulation);
$("title").text("Active Javascript : " + this.aData.info.name);
Expand Down

0 comments on commit 1b9d7ed

Please sign in to comment.