Skip to content

Commit

Permalink
Dev: Setting up dummy JS setting before LEMval() in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt authored and LouisGac committed Aug 28, 2017
1 parent 634e917 commit 80ac62d
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions tests/helpers/ExpressionCoreHelperTest.php
Expand Up @@ -279,15 +279,14 @@ public function testCompareNumberPlusString()

/**
* Expression: 3 + 2
* @todo Need LEMval() to work.
*/
public function testCompareNumberPlusNumber()
{
$sgqa = '563168X136X5376';
$expression = '((563168X136X5376.NAOK + 2))';
$value = 3;
$jsonEncodeResult = true;
//$this->compareExpression($sgqa, $value, $expression, $jsonEncodeResult);
$this->compareExpression($sgqa, $value, $expression, $jsonEncodeResult);
}

/**
Expand All @@ -308,8 +307,8 @@ protected function compareExpression($sgqa, $value, $expression, $jsonEncode = t
[
$sgqa => [
'sgqa' => $sgqa,
'type' => 'N'
//'jsName' => 'anything' // This will trigger LEMval()
'type' => 'N',
'jsName' => 'java' . $sgqa // This will trigger LEMval()
]
]
);
Expand All @@ -326,6 +325,8 @@ protected function compareExpression($sgqa, $value, $expression, $jsonEncode = t
$this->assertEmpty($errors);
$js = $em->GetJavaScriptEquivalentOfExpression();

$js = $this->getDummyNodeSetup($sgqa, $value, null, 0) . $js;

$nodeOutput = $this->runNode($js);

$this->assertCount(1, $nodeOutput);
Expand All @@ -351,11 +352,20 @@ protected function runNode($js)
}

/**
* @group node
* JS code to setup environment so LEMval() can run.
* @param string $sgqa
* @param mixed $value
* @param string $alias
* @param int $onlynum
* @return string
*/
public function testNode()
public function getDummyNodeSetup($sgqa, $value, $alias, $onlynum = 0)
{
$code = "
if (is_string($value)) {
$value = "'$value'";
}
list($surveyId, $groupId, $questionid) = explode('X', $sgqa, 3);
return <<<EOT
// Dummy jQuery.
$ = function() {
return {
Expand All @@ -366,46 +376,38 @@ public function testNode()
document = {
getElementById: function(id) {
//console.log(id);
if (id == 'relevance5376' || id == 'relevance' || id == 'relevanceG0') {
if (id == 'relevance$surveyId' || id == 'relevance' || id == 'relevanceG0') {
return {value: 1};
}
return {value: 123};
return {value: $value};
}
}
eval(fs.readFileSync('./scripts/expressions/em_javascript.js', {encoding: 'utf8'}));
LEMradix = ',';
LEMmode = 'survey';
LEMalias2varName = {
'test': 'java563168X136X5376',
'563168X136X5376': 'java563168X136X5376'
'$alias': 'java$sgqa',
'$sgqa': 'java$sgqa'
};
LEMvarNameAttr = {
'java563168X136X5376': {
'jsName':'java563168X136X5376',
'jsName_on':'java563168X136X5376',
'sgqa':'563168X136X5376',
'qid':5376,
'gid':136,
'java$sgqa': {
'jsName':'java$sgqa',
'jsName_on':'java$sgqa',
'sgqa':'$sgqa',
'qid': '$surveyId',
'gid': '$groupId',
'type':'N',
'default':'',
'rowdivid':'',
'onlynum': 1,
'onlynum': $onlynum,
'gseq':0,
'answers': {
'Y':'Ja',
'N':'Nei'
}
},
};
LEMval('563168X136X5376.NAOK') + 1;
";
$command = sprintf(
'node -p "%s"',
$code
);
$output = [];
exec($command, $output);
var_dump($output);
EOT;
}

/**
Expand Down

0 comments on commit 80ac62d

Please sign in to comment.