aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJean-Michel Vedrine <vedrine@vedrine.org>2018-01-31 08:46:01 +0100
committerJean-Michel Vedrine <vedrine@vedrine.org>2018-01-31 08:46:01 +0100
commitb5f2981cc1052390be01fb5d44760c9c59011aa0 (patch)
tree3bfbb7873c55fca20fabf596050b9d9dbe1b7421
parent5e12f43961b92ea152d9ede1200faaa4ee1dc5c9 (diff)
Test parser exceptions
-rw-r--r--parser.php2
-rw-r--r--tests/parser_exception_test.php40
2 files changed, 35 insertions, 7 deletions
diff --git a/parser.php b/parser.php
index f53d0c3..b5bbaba 100644
--- a/parser.php
+++ b/parser.php
@@ -21,8 +21,6 @@
defined('MOODLE_INTERNAL') || die();
-// require_once(__DIR__.'/../../../config.php');
-
/**
* Helper function which will compare two strings using their length only.
*
diff --git a/tests/parser_exception_test.php b/tests/parser_exception_test.php
index d73f766..5a0d24e 100644
--- a/tests/parser_exception_test.php
+++ b/tests/parser_exception_test.php
@@ -18,7 +18,7 @@
* Unit tests for the short answer question definition class.
*
* @package qtype_algebra
- * @copyright 2017 Jean-Michel Vedrine
+ * @copyright 2018 Jean-Michel Vedrine
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@@ -32,20 +32,50 @@ require_once($CFG->dirroot . '/question/type/algebra/parser.php');
/**
- * Unit tests for the algebra question parser.
+ * Unit tests for exceptions of the algebra question parser.
*
- * @copyright 2017 Jean-Michel Vedrine
+ * @copyright 2018 Jean-Michel Vedrine
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_algebra_parser_exception_test extends advanced_testcase {
/**
- * Test parsing an invalid exprssion
+ * No close bracket.
*/
- public function test_parser_invalid() {
+ public function test_parser_mismatched_brackets() {
$this->expectException('parser_exception');
$this->expectExceptionMessage('Mismatched brackets: Open bracket without a close bracket found');
$p = new qtype_algebra_parser;
$expr = $p->parse('sin(2x) + cos(');
}
+
+ /**
+ * Wrong number of arguments for function.
+ */
+ public function test_parser_wrong_arguments_number() {
+ $this->expectException('parser_exception');
+ $this->expectExceptionMessage('Syntax Error: Operator '^' requires two arguments');
+ $p = new qtype_algebra_parser;
+ $expr = $p->parse('x^');
+ }
+
+ /**
+ * Plus or minus in an invalid location.
+ */
+ public function test_parser_invalid_minus() {
+ $this->expectException('parser_exception');
+ $this->expectExceptionMessage('Found a + or - in an invalid location');
+ $p = new qtype_algebra_parser;
+ $expr = $p->parse('(-)');
+ }
+
+ /**
+ * Operator missing one argument.
+ */
+ public function test_parser_invalid_minus() {
+ $this->expectException('parser_exception');
+ $this->expectExceptionMessage('Syntax Error: Operator '-' requires two arguments');
+ $p = new qtype_algebra_parser;
+ $expr = $p->parse('x-');
+ }
}