Skip to content

Commit

Permalink
More coding style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
timhunt committed Feb 14, 2024
1 parent 812094f commit 71f2502
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 109 deletions.
2 changes: 1 addition & 1 deletion tests/behat/preview.feature
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Feature: Preview a Variable numeric set question
| Marked out of | 3 |
| Question variant | 1 |
| Marks | Show mark and max |
And I press "Start again with these options"
And I press "id_saverestart"
Then I should see "What is 2 + 8?"
And the state of "What is 2 + 8?" question is shown as "Tries remaining: 3"
When I set the field "Answer:" to "2"
Expand Down
2 changes: 1 addition & 1 deletion tests/calculator_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function test_evaluate_variables_in_text(string $variable, string $expect
*
* @return array List of data sets (test cases).
*/
public function evaluate_variables_in_text_provider(): array {
public static function evaluate_variables_in_text_provider(): array {

return [
'The argument is treated as an integer and presented as a binary number' => [
Expand Down
4 changes: 2 additions & 2 deletions tests/evalmath_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* @covers \EvalMath
*/
class evalmath_test extends basic_testcase {
public function test_basic_expressions() {
public function test_basic_expressions(): void {
$ev = new EvalMath(true, true);

$this->assertEquals(2, $ev->evaluate('a=2'));
Expand All @@ -46,7 +46,7 @@ public function test_basic_expressions() {
$this->assertEquals(2, $ev->evaluate('a'));

}
public function test_random_expressions() {
public function test_random_expressions(): void {
$ev = new EvalMath(true, true);
$results = [];
for ($i = 0; $i < 500; $i++) {
Expand Down
33 changes: 13 additions & 20 deletions tests/form_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@

namespace qtype_varnumericset;

/**
* Unit tests for the varnumericset question edit form.
*
* @package qtype_varnumericset
* @copyright 2023 The Open University
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();
global $CFG;

Expand All @@ -33,6 +25,7 @@
/**
* Unit tests for the qtype_varnumericset question edit form.
*
* @package qtype_varnumericset
* @copyright 2023 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \qtype_varnumeric_edit_form_base
Expand Down Expand Up @@ -104,7 +97,7 @@ public function test_form_validation(array $fromform, array $expectederrors): vo
*
* @return array List of data sets (test cases).
*/
public function form_validation_testcases(): array {
public static function form_validation_testcases(): array {
return [
'All OK' => [
[
Expand Down Expand Up @@ -145,20 +138,20 @@ public function form_validation_testcases(): array {
],
[
'questiontext' => get_string('errorvalidationformatnumber', 'qtype_varnumericset',
'[[x.e]] - Expression evaluation error: an unexpected error occurred.'),
'[[x.e]] - Expression evaluation error: an unexpected error occurred.'),
'generalfeedback' => get_string('errorvalidationformatnumber', 'qtype_varnumericset',
'[[x.e]] - Expression evaluation error: an unexpected error occurred.'),
'[[x.e]] - Expression evaluation error: an unexpected error occurred.'),
'feedback[0]' => get_string('errorvalidationformatnumber', 'qtype_varnumericset',
"[[x,&nbsp;.e]] - The format specifier must not contain non-breaking space characters."),
"[[x,&nbsp;.e]] - The format specifier must not contain non-breaking space characters."),
'feedback[1]' => get_string('errorvalidationformatnumber', 'qtype_varnumericset',
'[[x,]] - Missing format specifier at end of string.'),
'[[x,]] - Missing format specifier at end of string.'),
'hint[0]' => get_string('errorvalidationformatnumber', 'qtype_varnumericset',
'[[x,i]] - Unknown format specifier &quot;i&quot;.'),
'[[x,i]] - Unknown format specifier &quot;i&quot;.'),
'hint[1]' => get_string('errorvalidationformatnumber', 'qtype_varnumericset',
'[[x,.3\'.09 f]] - Unknown format specifier &quot;&#039;&quot;.'),
'[[x,.3\'.09 f]] - Unknown format specifier &quot;&#039;&quot;.'),
'hint[2]' => get_string('errorvalidationformatnumber', 'qtype_varnumericset',
"<ul>\n<li>[[x,]] - Missing format specifier at end of string.</li>\n" .
"<li>[[x,i]] - Unknown format specifier &quot;i&quot;.</li>\n</ul>"),
"<ul>\n<li>[[x,]] - Missing format specifier at end of string.</li>\n" .
"<li>[[x,i]] - Unknown format specifier &quot;i&quot;.</li>\n</ul>"),
],
],
[
Expand All @@ -172,9 +165,9 @@ public function form_validation_testcases(): array {
'variant1' => [],
],
[
'vartype[0]' => 'At least one of the variables must be a predefined variable.'
]
]
'vartype[0]' => 'At least one of the variables must be a predefined variable.',
],
],
];
}
}
28 changes: 14 additions & 14 deletions tests/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,12 @@ public function get_varnumericset_question_form_data_sci_notation_formatted() {
$form->fraction = ['0' => '1.0', '1' => '0.0', '2' => '0.0'];
$form->feedback = [
'0' => ['format' => FORMAT_HTML, 'text' => 'Well done!'],
'1' => ['format' => FORMAT_HTML, 'text' => 'Sorry, no.']
'1' => ['format' => FORMAT_HTML, 'text' => 'Sorry, no.'],
];
$form->penalty = '0.3333333';
$form->hint = [
['text' => 'Please try again.', 'format' => FORMAT_HTML],
['text' => 'You may use a calculator if necessary.', 'format' => FORMAT_HTML]
['text' => 'You may use a calculator if necessary.', 'format' => FORMAT_HTML],
];

return $form;
Expand All @@ -340,19 +340,19 @@ public function make_varnumericset_question_sci_notation_formatted() {
$vs = $this->make_varnumericset_question_no_accepted_error();

$vs->answers = [1 => new qtype_varnumericset_answer(
'1', // Id.
'12', // Answer.
1.0000000, // Fraction.
'1', // Id.
'12', // Answer.
1.0000000, // Fraction.
'<p>Your answer is correct.</p>', // Feedback.
FORMAT_HTML,// Feedbackformat.
'0', // Sigfigs.
'', // Error.
'0.25', // Syserrorpenalty.
'0', // Checknumerical.
'1', // Checkscinotation.
'0', // Checkpowerof10.
'0', // Checkrounding.
'1', // Checkscinotationformat.
FORMAT_HTML, // Feedbackformat.
'0', // Sigfigs.
'', // Error.
'0.25', // Syserrorpenalty.
'0', // Checknumerical.
'1', // Checkscinotation.
'0', // Checkpowerof10.
'0', // Checkrounding.
'1', // Checkscinotationformat.
)];
$vs->requirescinotation = true;
$vs->usesupeditor = true;
Expand Down
21 changes: 6 additions & 15 deletions tests/number_interpreter_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,22 @@
use qtype_varnumericset_number_interpreter_number_with_optional_decimal_place;
use qtype_varnumericset_number_interpreter_number_with_optional_sci_notation;

/**
* Unit tests for the number interpreter classes .
*
* @package qtype_varnumericset
* @copyright 2012 The Open University
* @author Jamie Pratt [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/


defined('MOODLE_INTERNAL') || die();
global $CFG;

require_once($CFG->dirroot . '/question/type/varnumericset/number_interpreter.php');


/**
* Unit tests for the number interpreter classes .
*
* @package qtype_varnumericset
* @copyright 2012 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @group qtype_varnumericset
* @covers \qtype_varnumericset_number_interpreter_number_with_optional_decimal_place
* @covers \qtype_varnumericset_number_interpreter_number_with_optional_sci_notation
*/
class number_interpreter_test extends basic_testcase {
public function test_interpret_number_with_optional_decimal_place() {
public function test_interpret_number_with_optional_decimal_place(): void {
$num = new qtype_varnumericset_number_interpreter_number_with_optional_decimal_place();

$this->assertFalse($num->match('newt'));
Expand Down Expand Up @@ -91,7 +82,7 @@ public function test_interpret_number_with_optional_decimal_place() {
$this->assertSame('', $num->get_postfix());
}

public function test_interpret_number_with_optional_sci_notation_not_accepting_html_exponent() {
public function test_interpret_number_with_optional_sci_notation_not_accepting_html_exponent(): void {
$num = new qtype_varnumericset_number_interpreter_number_with_optional_sci_notation(false);

$this->assertFalse($num->match('newt'));
Expand Down Expand Up @@ -155,7 +146,7 @@ public function test_interpret_number_with_optional_sci_notation_not_accepting_h
$this->assertSame('m', $num->get_postfix());
}

public function test_interpret_number_with_optional_sci_notation_accepting_html_exponent() {
public function test_interpret_number_with_optional_sci_notation_accepting_html_exponent(): void {
$num = new qtype_varnumericset_number_interpreter_number_with_optional_sci_notation(true);

$this->assertFalse($num->match('newt'));
Expand Down
48 changes: 25 additions & 23 deletions tests/question_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Unit tests for the varnumericset question definition class.
*
* @package qtype_varnumericset
* @copyright 2012 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace qtype_varnumericset;

use advanced_testcase;
use qtype_varnumeric_question_base;
use qtype_varnumericset_answer;
use qtype_varnumericset_question;
use question_attempt_step;
use test_question_maker;

defined('MOODLE_INTERNAL') || die();
global $CFG;
Expand All @@ -33,10 +33,11 @@
/**
* Unit tests for the varnumericset question definition class.
*
* @package qtype_varnumericset
* @copyright 2012 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \qtype_varnumeric_question_base
* @covers \qtype_varnumericset_question
* @covers \qtype_varnumeric_question_base
* @covers \qtype_varnumericset_question
*/
class question_test extends advanced_testcase {

Expand All @@ -45,7 +46,7 @@ class question_test extends advanced_testcase {
*
* @return array of arrays of arguments for test_num_within_allowed_error
*/
public function num_within_allowed_error_cases(): array {
public static function num_within_allowed_error_cases(): array {
return [
['1.23000000000001e4', 1.23e4, '', true],
['1.23000000000002e4', 1.23e4, '', false],
Expand Down Expand Up @@ -83,7 +84,7 @@ public function num_within_allowed_error_cases(): array {
/**
* @dataProvider num_within_allowed_error_cases
*/
public function test_num_within_allowed_error($response, $answer, $allowederror, $shouldmatch) {
public function test_num_within_allowed_error($response, $answer, $allowederror, $shouldmatch): void {
if ($shouldmatch) {
$this->assertTrue(
qtype_varnumeric_question_base::num_within_allowed_error($response, $answer, $allowederror));
Expand All @@ -98,7 +99,7 @@ public function test_num_within_allowed_error($response, $answer, $allowederror,
*
* @return array of arrays of arguments for test_num_within_allowed_error
*/
public function wrong_by_a_factor_of_ten_cases(): array {
public static function wrong_by_a_factor_of_ten_cases(): array {
return [
['1.23e4', 1.23e5, '', 1, true],
['1.23e4', 1.23e6, '', 1, false],
Expand All @@ -112,7 +113,7 @@ public function wrong_by_a_factor_of_ten_cases(): array {
/**
* @dataProvider wrong_by_a_factor_of_ten_cases
*/
public function test_wrong_by_a_factor_of_ten($response, $roundedanswer, $allowederror, $maxfactor, $shouldmatch) {
public function test_wrong_by_a_factor_of_ten($response, $roundedanswer, $allowederror, $maxfactor, $shouldmatch): void {
if ($shouldmatch) {
$this->assertTrue(
qtype_varnumeric_question_base::wrong_by_a_factor_of_ten(
Expand All @@ -124,7 +125,7 @@ public function test_wrong_by_a_factor_of_ten($response, $roundedanswer, $allowe
}
}

public function test_has_number_of_sig_figs() {
public function test_has_number_of_sig_figs(): void {
$this->assertTrue(
qtype_varnumericset_question::has_number_of_sig_figs('1.23e4', 3));

Expand Down Expand Up @@ -169,7 +170,7 @@ public function test_has_number_of_sig_figs() {
qtype_varnumericset_question::has_number_of_sig_figs('15.0', 2));
}

public function test_has_too_many_sig_figs() {
public function test_has_too_many_sig_figs(): void {
$this->assertTrue(qtype_varnumericset_question::has_too_many_sig_figs('1.23456e5', 123456, 2));
$this->assertTrue(qtype_varnumericset_question::has_too_many_sig_figs('1.23456e5', 123456, 3));
$this->assertTrue(qtype_varnumericset_question::has_too_many_sig_figs('1.23456e5', 123456, 4));
Expand Down Expand Up @@ -210,7 +211,7 @@ public function test_has_too_many_sig_figs() {
qtype_varnumericset_question::has_too_many_sig_figs('-1.23456e-12', -1.2346e-12, 4));
}

public function test_rounding_incorrect() {
public function test_rounding_incorrect(): void {
$this->assertTrue(
qtype_varnumericset_question::rounding_incorrect('1.234', 1.2345, 4));
$this->assertTrue(
Expand All @@ -228,7 +229,7 @@ public function test_rounding_incorrect() {
*
* @return array of arrays of arguments for test_num_within_allowed_error
*/
public function round_to_cases(): array {
public static function round_to_cases(): array {
return [
['0.123', 0.12345, 3, false, false],
['0.1235', 0.12345, 4, false, false],
Expand All @@ -254,7 +255,7 @@ public function round_to_cases(): array {
/**
* @dataProvider round_to_cases
*/
public function test_round_to($expected, $number, $sigfigs, $scinotation, $floor) {
public function test_round_to($expected, $number, $sigfigs, $scinotation, $floor): void {
$this->assertSame($expected,
qtype_varnumeric_question_base::round_to($number, $sigfigs, $scinotation, $floor));
}
Expand All @@ -271,7 +272,7 @@ protected function grade(qtype_varnumericset_question $question, string $entered
return $fraction;
}

public function test_compare_response_with_answer() {
public function test_compare_response_with_answer(): void {
/** @var qtype_varnumericset_question $q */
$q = test_question_maker::make_question('varnumericset'); // Does not matter which one.

Expand Down Expand Up @@ -318,7 +319,7 @@ public function test_compare_response_with_answer() {
$this->assertNull($answertoreturn);
}

public function test_compare_num_as_string_with_answer() {
public function test_compare_num_as_string_with_answer(): void {
$q = test_question_maker::make_question('varnumericset'); // Does not matter which one.

$answer = new qtype_varnumericset_answer(12345, // Id.
Expand Down Expand Up @@ -369,6 +370,7 @@ public function test_compare_num_as_string_with_answer() {
$this->assertEquals(1, $penalty);

// Test check scinotation format.
/** @var qtype_varnumericset_question $question */
$question = test_question_maker::make_question('varnumericset', 'sci_notation_formatted');
$answer = new qtype_varnumericset_answer(12345, // Id.
'12', // Answer.
Expand Down Expand Up @@ -416,7 +418,7 @@ public function test_compare_num_as_string_with_answer() {
$this->assertEquals(0.25, $penalty);
}

public function test_compare_num_as_string_with_answer_no_rounding() {
public function test_compare_num_as_string_with_answer_no_rounding(): void {
$q = test_question_maker::make_question('varnumericset'); // Does not matter which one.

$answer = new qtype_varnumericset_answer(12345, // Id.
Expand All @@ -438,7 +440,7 @@ public function test_compare_num_as_string_with_answer_no_rounding() {
$this->assertEquals(0, $penalty);
}

public function test_grade_response() {
public function test_grade_response(): void {
/** @var qtype_varnumericset_question $question */
$question = test_question_maker::make_question('varnumericset', 'no_accepted_error');
$this->assertEquals(1, $this->grade($question, '-4.2'));
Expand Down Expand Up @@ -529,7 +531,7 @@ public function test_grade_response() {
$this->assertEquals(0.75, $this->grade($question, '1.200000 x 10<sup>+ 1</sup>'));
}

public function test_get_question_summary() {
public function test_get_question_summary(): void {
$question = test_question_maker::make_question('varnumericset', 'with_variables');
$question->start_attempt(new question_attempt_step(), 1);
$this->assertEquals('What is 2 + 3?', $question->get_question_summary());
Expand Down
Loading

0 comments on commit 71f2502

Please sign in to comment.