Skip to content

Commit

Permalink
add a unit test for rounding numbers in matrix entry pre-filled cells
Browse files Browse the repository at this point in the history
see #1133
  • Loading branch information
christianp committed Jan 10, 2025
1 parent 1c97720 commit 242dbe5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
14 changes: 8 additions & 6 deletions tests/numbas-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -32886,6 +32886,13 @@ MatrixEntryPart.prototype = /** @lends Numbas.parts.MatrixEntryPart.prototype */
['minColumns','maxColumns','minRows','maxRows'].map(eval_setting);
}

settings.tolerance = Math.max(settings.tolerance,0.00000000001);
if(settings.precisionType != 'none') {
settings.allowFractions = false;
}

this.getCorrectAnswer(scope);

var prefilled_fractions = settings.allowFractions && settings.correctAnswerFractions;
if(settings.prefilledCellsString) {
var prefilledCells = jme.castToType(scope.evaluate(jme.subvars(settings.prefilledCellsString+'',scope)), 'list');
Expand Down Expand Up @@ -32916,7 +32923,7 @@ MatrixEntryPart.prototype = /** @lends Numbas.parts.MatrixEntryPart.prototype */
return frac.toString();
} else {
cell = jme.castToType(cell,'number');
return math.niceRealNumber(cell.value,scope);
return math.niceRealNumber(cell.value, {precisionType: settings.precisionType, precision: settings.precision, style: settings.correctAnswerStyle});
}
}
p.error('part.matrix.invalid type in prefilled',{type: cell.type});
Expand All @@ -32925,10 +32932,6 @@ MatrixEntryPart.prototype = /** @lends Numbas.parts.MatrixEntryPart.prototype */
}
}

settings.tolerance = Math.max(settings.tolerance,0.00000000001);
if(settings.precisionType!='none') {
settings.allowFractions = false;
}
this.studentAnswer = [];
for(var i=0;i<this.settings.numRows;i++) {
var row = [];
Expand All @@ -32937,7 +32940,6 @@ MatrixEntryPart.prototype = /** @lends Numbas.parts.MatrixEntryPart.prototype */
}
this.studentAnswer.push(row);
}
this.getCorrectAnswer(scope);
if(!settings.allowResize && (settings.correctAnswer.rows!=settings.numRows || settings.correctAnswer.columns != settings.numColumns)) {
var correctSize = settings.correctAnswer.rows+'×'+settings.correctAnswer.columns;
var answerSize = settings.numRows+'×'+settings.numColumns;
Expand Down
4 changes: 4 additions & 0 deletions tests/parts/part-tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,10 @@ Numbas.queueScript('part_tests',['qunit','json','jme','localisation','parts/numb
assert.ok(contains_note(res,{note:jme.normaliseName('all_same_precision'),message: R('part.matrix.not all cells same precision')}),'not all cells same precision warning');
});

QUnit.test('Pre-filled cells', async function(assert) {
var p = createPartFromJSON({type:'matrix',correctAnswer:'matrix([1.222,1.227],[3,4])', prefilledCells: '[[1+2/9,1.227], ["3", "???"]]', precisionType: 'dp', precision: 2});
assert.deepEqual(p.settings.prefilledCells, [['1.22', '1.23'], ['3', '???']], 'numbers are rounded using the precision restriction; any string is valid for a pre-filled cell');
});

question_test(
'Note name used both for question variable and marking note',
Expand Down

0 comments on commit 242dbe5

Please sign in to comment.