Skip to content

Commit

Permalink
more tests, more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippImhof committed Dec 12, 2024
1 parent bd1dcb9 commit 027100a
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 16 deletions.
1 change: 1 addition & 0 deletions lang/en/qtype_formulas.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ functions and operators is given in the documentation.



$string['error_model_answer_no_content'] = 'Invalid answer: the model answer contains no evaluable symbols.';
$string['error_string_for_algebraic_formula'] = 'Invalid answer format: the answer type "algebraic formula" expects one single string wrapped in quotes or a list of strings, each wrapped in quotes.';
$string['error_number_for_numeric_answertypes'] = 'Invalid answer format: this answer type expects one number or a list of numbers.';
$string['error_in_answer'] = 'error in answer #{$a->answerno}: {$a->message}';
Expand Down
5 changes: 5 additions & 0 deletions question.php
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,11 @@ public function get_evaluated_answers(): array {
*/
private static function wrap_algebraic_formulas_in_quotes(array $formulas): array {
foreach ($formulas as &$formula) {
// If the formula is aready wrapped in quotes (e. g. after an earlier call to this
// function), there is nothing to do.
if (preg_match('/^\"[^\"]+\"$/', $formula)) {
continue;
}
$formula = '"' . $formula . '"';
}
// In case we later write to $formula, this would alter the last entry of the $formulas
Expand Down
6 changes: 6 additions & 0 deletions questiontype.php
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,12 @@ public function check_variables_and_expressions(object $data, array $parts): obj
// If (and only if) the answer is algebraic, the answer parser should
// interpret ^ as **.
$answerparser = new answer_parser($part->answer, $knownvars, $isalgebraic);
// If the user enters a comment sign in the model answer, it is not technically empty,
// but it will be parsed as an empty expression. We catch this here and make use of
// the catch block to pass an error message.
if (empty($answerparser->get_statements())) {
throw new Exception(get_string('error_model_answer_no_content', 'qtype_formulas'));
}
$modelanswers = $partevaluator->evaluate($answerparser->get_statements())[0];
} catch (Exception $e) {
// If the answer type is algebraic, the model answer field must contain one string (with quotes)
Expand Down
3 changes: 0 additions & 3 deletions tests/behat/criterion.feature
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ Feature: Test setting the grading criterion in different modes
| correctness[0] | |
And I press "id_submitbutton"
And I wait until the page is ready
# FIXME: with invalid criterion and only one part, might give error "At least one answer is required."
# even though an answer is there
Then I should see "The grading criterion must not be empty."
# Then I should see "The grading criterion must be evaluated to a single number."
And the following fields match these values:
| correctness_simple_mode[0] | |
And the "Simplified mode" "checkbox" should be enabled
Expand Down
13 changes: 0 additions & 13 deletions tests/parser_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,6 @@ public function test_basic_operations() {
//print_r($output);
}

public function test_parse_list() {
// FIXME - TODO --> implement new test for lists
//$input = '[1, 2, 3]';
//$input = '[1, "a", 3]';
//$input = '[1, ["x", "y"], 3]';
//$input = '[[1,2]]';
$input = 'a = [1, ["x", "y"], [3, 4], 5, [[1,2]],6]';
$input = '-123.541e-13; 4';

$parser = new answer_parser($input);
// print_r($parser->statements);
}

/**
* @dataProvider provide_sets
*/
Expand Down
4 changes: 4 additions & 0 deletions tests/questiontype_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ public function provide_single_part_data_for_form_validation(): array {
'subqtext' => [0 => ['text' => '{_0} and {_0}']],
]
],
[
['answer[0]' => get_string('error_model_answer_no_content', 'qtype_formulas')],
['answer' => [0 => '#']],
],
[
['globalunitpenalty' => get_string('error_unitpenalty', 'qtype_formulas')],
['globalunitpenalty' => 2]
Expand Down

0 comments on commit 027100a

Please sign in to comment.