diff --git a/stack/cas/cassession2.class.php b/stack/cas/cassession2.class.php index 9bdec031de..5805b89684 100644 --- a/stack/cas/cassession2.class.php +++ b/stack/cas/cassession2.class.php @@ -410,6 +410,10 @@ public function instantiate(): bool { $asts[$key] = $value; } } catch (Exception $e) { + // TODO: issue #1279 would change this exception to add in an error associated + // with the values collected rather than a stack_exception. + // We would then add something like this to allow the process to continue. + // $asts[$key] = maxima_parser_utils::parse('null', 'Root', false); throw new stack_exception('stack_cas_session: tried to parse the value ' . $value . ', but got the following exception ' . $e->getMessage()); } diff --git a/tests/caskeyval_exception_test.php b/tests/caskeyval_exception_test.php index 10500f61f4..fc12f609d9 100644 --- a/tests/caskeyval_exception_test.php +++ b/tests/caskeyval_exception_test.php @@ -65,4 +65,18 @@ public function test_exception_7() { $this->expectException(stack_exception::class); $at1 = new stack_cas_keyval('x=1', 't', false); } + + public function test_stack_compile_unexpected_lambda() { + $this->expectException(stack_exception::class); + // This is related to issue #1279. + $tests = 'a:b+1; c:a-a(d+1);'; + $kv = new stack_cas_keyval($tests); + $this->asserttrue($kv->get_valid()); + $expected = []; + $this->assertEquals($expected, $kv->get_errors()); + $kv->instantiate(); + $s = $kv->get_session(); + $s->instantiate(); + $this->assertEquals($s->get_by_key('c')->get_evaluationform(), 'c:(b+1)-(b+1)(d+1)'); + } } diff --git a/tests/caskeyval_test.php b/tests/caskeyval_test.php index f8a5a1dcbb..1ab8874b92 100644 --- a/tests/caskeyval_test.php +++ b/tests/caskeyval_test.php @@ -335,8 +335,6 @@ public function test_stack_compile() { 'q:matrix([i],[j],[k]);v:dotproduct(p,q);'; $kv = new stack_cas_keyval($tests); $this->assertTrue($kv->get_valid()); - $expected = [ - ]; $compiled = $kv->compile('kv-test'); $expected = '(_EC(errcatch(stack_reset_vars(true)),"kv-test/1:1-1:2"),' . @@ -356,8 +354,6 @@ public function test_stack_compile_preamble_end1() { 'q:matrix([i],[j],[k]);v:dotproduct(p,q);'; $kv = new stack_cas_keyval($tests); $this->assertTrue($kv->get_valid()); - $expected = [ - ]; $compiled = $kv->compile('kv-test'); $expected = '(_EC(errcatch(stack_reset_vars(true)),"kv-test/1:1-1:2"),' . @@ -370,4 +366,15 @@ public function test_stack_compile_preamble_end1() { $expected = '(_EC(errcatch(n1:1),"kv-test/1:24-1:2"),true)'; $this->assertEquals($expected, $compiled['contextvariables']); } + + public function test_stack_compile_unexpected_lambda() { + // This is related to issue #1279. + $tests = 'c:(b+1)-(b+1)(d+1);'; + $kv = new stack_cas_keyval($tests); + $this->assertfalse($kv->get_valid()); + $expected = ['You seem to be missing * characters. Perhaps you meant to type ' . + 'c:(b+1)-(b+1)' . + '*(d+1).']; + $this->assertEquals($expected, $kv->get_errors()); + } } diff --git a/tests/castext_test.php b/tests/castext_test.php index bfbee13d4e..66f49baec8 100644 --- a/tests/castext_test.php +++ b/tests/castext_test.php @@ -2451,4 +2451,28 @@ public function test_make_mult_sgn_stackunits() { $this->assertEquals('\({10\, \frac{m}{s}}\), \({1\cdot s^ {- 1 }}\), \({1\, s^ {- 1 }}\). ' . 'Multiplication unaffected: \({a\cdot b}\).', $at1->get_rendered()); } + + /** + * @covers \qtype_stack\stack_cas_castext2_latex + * @covers \qtype_stack\stack_cas_keyval + */ + public function test_unexpected_lambda() { + $a2 = ['a:b+1', 'c:a-a(d+1)']; + $s2 = []; + foreach ($a2 as $s) { + $cs = stack_ast_container::make_from_teacher_source($s, '', new stack_cas_security(), []); + $this->assertTrue($cs->get_valid()); + $s2[] = $cs; + } + $options = new stack_options(); + $options->set_option('simplify', false); + $cs2 = new stack_cas_session2($s2, $options, 0); + $at1 = castext2_evaluatable::make_from_source('{@c@}', + 'test-case'); + $this->assertTrue($at1->get_valid()); + $cs2->add_statement($at1); + $cs2->instantiate(); + $expected = '\({b+1-\left(b+1\right)(d+1)}\)'; + $this->assertEquals($expected, $at1->get_rendered()); + } }