Skip to content

Commit

Permalink
Add in test cases to illustrate issue #1279 (parsing expressions like…
Browse files Browse the repository at this point in the history
… (b+1)(d+1) back from Maxima).
  • Loading branch information
sangwinc committed Oct 15, 2024
1 parent b10591f commit 0c24e0f
Showing 4 changed files with 53 additions and 4 deletions.
4 changes: 4 additions & 0 deletions stack/cas/cassession2.class.php
Original file line number Diff line number Diff line change
@@ -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());
}
14 changes: 14 additions & 0 deletions tests/caskeyval_exception_test.php
Original file line number Diff line number Diff line change
@@ -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)');
}
}
15 changes: 11 additions & 4 deletions tests/caskeyval_test.php
Original file line number Diff line number Diff line change
@@ -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 ' .
'<span class="stacksyntaxexample">c:(b+1)-(b+1)<span class="stacksyntaxexamplehighlight">' .
'*</span>(d+1)</span>.'];
$this->assertEquals($expected, $kv->get_errors());
}
}
24 changes: 24 additions & 0 deletions tests/castext_test.php
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 0c24e0f

Please sign in to comment.