Skip to content

Commit 242dbe5

Browse files
committed
add a unit test for rounding numbers in matrix entry pre-filled cells
see #1133
1 parent 1c97720 commit 242dbe5

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

tests/numbas-runtime.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32886,6 +32886,13 @@ MatrixEntryPart.prototype = /** @lends Numbas.parts.MatrixEntryPart.prototype */
3288632886
['minColumns','maxColumns','minRows','maxRows'].map(eval_setting);
3288732887
}
3288832888

32889+
settings.tolerance = Math.max(settings.tolerance,0.00000000001);
32890+
if(settings.precisionType != 'none') {
32891+
settings.allowFractions = false;
32892+
}
32893+
32894+
this.getCorrectAnswer(scope);
32895+
3288932896
var prefilled_fractions = settings.allowFractions && settings.correctAnswerFractions;
3289032897
if(settings.prefilledCellsString) {
3289132898
var prefilledCells = jme.castToType(scope.evaluate(jme.subvars(settings.prefilledCellsString+'',scope)), 'list');
@@ -32916,7 +32923,7 @@ MatrixEntryPart.prototype = /** @lends Numbas.parts.MatrixEntryPart.prototype */
3291632923
return frac.toString();
3291732924
} else {
3291832925
cell = jme.castToType(cell,'number');
32919-
return math.niceRealNumber(cell.value,scope);
32926+
return math.niceRealNumber(cell.value, {precisionType: settings.precisionType, precision: settings.precision, style: settings.correctAnswerStyle});
3292032927
}
3292132928
}
3292232929
p.error('part.matrix.invalid type in prefilled',{type: cell.type});
@@ -32925,10 +32932,6 @@ MatrixEntryPart.prototype = /** @lends Numbas.parts.MatrixEntryPart.prototype */
3292532932
}
3292632933
}
3292732934

32928-
settings.tolerance = Math.max(settings.tolerance,0.00000000001);
32929-
if(settings.precisionType!='none') {
32930-
settings.allowFractions = false;
32931-
}
3293232935
this.studentAnswer = [];
3293332936
for(var i=0;i<this.settings.numRows;i++) {
3293432937
var row = [];
@@ -32937,7 +32940,6 @@ MatrixEntryPart.prototype = /** @lends Numbas.parts.MatrixEntryPart.prototype */
3293732940
}
3293832941
this.studentAnswer.push(row);
3293932942
}
32940-
this.getCorrectAnswer(scope);
3294132943
if(!settings.allowResize && (settings.correctAnswer.rows!=settings.numRows || settings.correctAnswer.columns != settings.numColumns)) {
3294232944
var correctSize = settings.correctAnswer.rows+'×'+settings.correctAnswer.columns;
3294332945
var answerSize = settings.numRows+'×'+settings.numColumns;

tests/parts/part-tests.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,10 @@ Numbas.queueScript('part_tests',['qunit','json','jme','localisation','parts/numb
572572
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');
573573
});
574574

575+
QUnit.test('Pre-filled cells', async function(assert) {
576+
var p = createPartFromJSON({type:'matrix',correctAnswer:'matrix([1.222,1.227],[3,4])', prefilledCells: '[[1+2/9,1.227], ["3", "???"]]', precisionType: 'dp', precision: 2});
577+
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');
578+
});
575579

576580
question_test(
577581
'Note name used both for question variable and marking note',

0 commit comments

Comments
 (0)