Skip to content

Commit

Permalink
Stronger type juggling in setOutcomeValue rule processor.
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Jul 28, 2016
1 parent 28d4b4d commit d8495ac
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
5 changes: 5 additions & 0 deletions qtism/runtime/rules/SetOutcomeValueProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ public function process() {
if ($val !== null && $var->getCardinality() === Cardinality::SINGLE) {
$baseType = $var->getBaseType();

// If we are trying to put a container in a scalar, let's make some changes...
if (($val->getCardinality() === Cardinality::MULTIPLE || $val->getCardinality() === Cardinality::ORDERED) && count($val) > 0) {
$val = $val[0];
}

if ($baseType === BaseType::INTEGER && $val instanceof QtiFloat) {
$val = new QtiInteger(intval($val->getValue()));
}
Expand Down
54 changes: 51 additions & 3 deletions test/qtism/runtime/rules/SetOutcomeValueProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,12 @@ public function testSetOutcomeValueWrongJugglingMultipleOne() {
$processor->process();
}

public function testSetOutcomeValueWrongJugglingMultipleTwo() {
public function testSetOutcomeValueJugglingMultiple() {
$rule = $this->createComponentFromXml('
<setOutcomeValue identifier="SCORE">
<multiple>
<baseValue baseType="float">1337.1337</baseValue>
<baseValue baseType="float">7777.7777</baseValue>
</multiple>
</setOutcomeValue>
');
Expand All @@ -110,10 +111,57 @@ public function testSetOutcomeValueWrongJugglingMultipleTwo() {
$score = new OutcomeVariable('SCORE', Cardinality::SINGLE, BaseType::INTEGER);
$state = new State(array($score));
$processor->setState($state);

$this->setExpectedException('qtism\\runtime\\rules\\RuleProcessingException');
$processor->process();

// In this case, juggling will put the first entry of the multiple container
// in the target single cardinality variable. The float value is then changed into an integer value.
$processor->process();
$this->assertEquals(1337, $state['SCORE']->getValue());
}

public function testSetOutcomeValueJugglingOrdered() {
$rule = $this->createComponentFromXml('
<setOutcomeValue identifier="SCORE">
<ordered>
<baseValue baseType="float">1337.1337</baseValue>
<baseValue baseType="float">7777.7777</baseValue>
</ordered>
</setOutcomeValue>
');

$processor = new SetOutcomeValueProcessor($rule);
$score = new OutcomeVariable('SCORE', Cardinality::SINGLE, BaseType::INTEGER);
$state = new State(array($score));
$processor->setState($state);

// In this case, juggling will put the first entry of the multiple container
// in the target single cardinality variable. The float value is then changed into an integer value.
$processor->process();
$this->assertEquals(1337, $state['SCORE']->getValue());
}

public function testSetOutcomeValueWrongJugglingMultipleBecauseWrongBaseType() {
$rule = $this->createComponentFromXml('
<setOutcomeValue identifier="SCORE">
<multiple>
<baseValue baseType="string">hello</baseValue>
<baseValue baseType="string">world</baseValue>
</multiple>
</setOutcomeValue>
');

$processor = new SetOutcomeValueProcessor($rule);
$score = new OutcomeVariable('SCORE', Cardinality::SINGLE, BaseType::INTEGER);
$state = new State(array($score));
$processor->setState($state);

$this->setExpectedException(
'qtism\\runtime\\rules\\RuleProcessingException',
'Unable to set value hello to variable \'SCORE\' (cardinality = single, baseType = integer).'
);

$processor->process();
}

public function testSetOutcomeValueModerate() {
$rule = $this->createComponentFromXml('
Expand Down

0 comments on commit d8495ac

Please sign in to comment.