Skip to content

Commit

Permalink
MDL-34538 add float validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Jul 27, 2012
1 parent 55a568f commit 9a97fa5
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions lib/tests/moodlelib_test.php
Expand Up @@ -912,6 +912,56 @@ function test_validate_param() {
} catch (invalid_parameter_exception $ex) {
$this->assertTrue(true);
}
try {
$param = validate_param('1.0', PARAM_FLOAT);
$this->assertSame(1.0, $param);

// Make sure valid floats do not cause exception.
validate_param(1.0, PARAM_FLOAT);
validate_param(10, PARAM_FLOAT);
validate_param('0', PARAM_FLOAT);
validate_param('119813454.545464564564546564545646556564465465456465465465645645465645645645', PARAM_FLOAT);
validate_param('011.1', PARAM_FLOAT);
validate_param('11', PARAM_FLOAT);
validate_param('+.1', PARAM_FLOAT);
validate_param('-.1', PARAM_FLOAT);
validate_param('1e10', PARAM_FLOAT);
validate_param('.1e+10', PARAM_FLOAT);
validate_param('1E-1', PARAM_FLOAT);
$this->assertTrue(true);
} catch (invalid_parameter_exception $ex) {
$this->fail('Valid float notation not accepted');
}
try {
$param = validate_param('1,2', PARAM_FLOAT);
$this->fail('invalid_parameter_exception expected');
} catch (invalid_parameter_exception $ex) {
$this->assertTrue(true);
}
try {
$param = validate_param('', PARAM_FLOAT);
$this->fail('invalid_parameter_exception expected');
} catch (invalid_parameter_exception $ex) {
$this->assertTrue(true);
}
try {
$param = validate_param('.', PARAM_FLOAT);
$this->fail('invalid_parameter_exception expected');
} catch (invalid_parameter_exception $ex) {
$this->assertTrue(true);
}
try {
$param = validate_param('e10', PARAM_FLOAT);
$this->fail('invalid_parameter_exception expected');
} catch (invalid_parameter_exception $ex) {
$this->assertTrue(true);
}
try {
$param = validate_param('abc', PARAM_FLOAT);
$this->fail('invalid_parameter_exception expected');
} catch (invalid_parameter_exception $ex) {
$this->assertTrue(true);
}
}

function test_shorten_text() {
Expand Down

0 comments on commit 9a97fa5

Please sign in to comment.