Skip to content
This repository was archived by the owner on May 5, 2019. It is now read-only.

Commit 13562b9

Browse files
committed
Use model class for spec tests
1 parent a4b6a64 commit 13562b9

File tree

5 files changed

+250
-239
lines changed

5 files changed

+250
-239
lines changed

Diff for: tests/Common.php

-64
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,8 @@
22

33
namespace Handlebars\Tests;
44

5-
use Handlebars\CompileContext;
6-
use Handlebars\Exception;
7-
use Handlebars\Handlebars;
8-
use Handlebars\Opcode;
9-
use Handlebars\PhpCompiler;
10-
use Handlebars\SafeString;
11-
use Handlebars\VM;
125
use PHPUnit_Framework_TestCase;
136

147
class Common extends PHPUnit_Framework_TestCase
158
{
16-
public function convertOpcode(array $opcode)
17-
{
18-
return new Opcode($opcode['opcode'], $opcode['args']);
19-
}
20-
21-
public function convertContext(array $context)
22-
{
23-
$opcodes = array();
24-
foreach( $context['opcodes'] as $opcode ) {
25-
$opcodes[] = $this->convertOpcode($opcode);
26-
}
27-
28-
$children = array();
29-
foreach( $context['children'] as $k => $v ) {
30-
$children[$k] = $this->convertContext($v);
31-
}
32-
33-
$decorators = null;
34-
if( isset($context['decorators']) ) {
35-
foreach ($context['decorators'] as $k => $v) {
36-
$decorators[$k] = $this->convertContext($v);
37-
}
38-
}
39-
40-
$blockParams = isset($context['blockParams']) ? $context['blockParams'] : null;
41-
42-
$obj = new CompileContext($opcodes, $children, $blockParams);
43-
$obj->decorators = $decorators;
44-
45-
foreach( array('useDepths', 'usePartial', 'useDecorators', 'isSimple', 'options', 'compileOptions') as $k ) {
46-
if( !empty($context[$k]) ) {
47-
$obj->$k = $context[$k];
48-
}
49-
}
50-
51-
return $obj;
52-
}
53-
54-
public function convertCode($data)
55-
{
56-
if( is_array($data) ) {
57-
foreach( $data as $k => $v ) {
58-
if( !is_array($v) ) {
59-
continue;
60-
}
61-
if( !empty($v['!code']) ) {
62-
$data[$k] = eval('use Handlebars\SafeString; use Handlebars\Utils; return ' . $v['php'] . ';');
63-
} else if( !empty($v['!sparsearray']) ) {
64-
unset($v['!sparsearray']);
65-
$data[$k] = $v;
66-
} else {
67-
$data[$k] = $this->convertCode($data[$k]);
68-
}
69-
}
70-
}
71-
return $data;
72-
}
739
}

Diff for: tests/HandlebarsExportTest.php

+34-76
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
namespace Handlebars\Tests;
44

5+
use Handlebars\Handlebars;
56
use Handlebars\DefaultRegistry;
67
use Handlebars\Compiler\PhpCompiler;
8+
use Handlebars\Compiler\Runtime as CompilerRuntime;
9+
use Handlebars\VM\Runtime as VMRuntime;
710

811
class HandlebarsExportTest extends Common
912
{
@@ -29,22 +32,22 @@ public function __construct($name = null, array $data = array(), $dataName = '')
2932
}
3033

3134
/**
35+
* @param SpecTestModel $test
3236
* @dataProvider specProvider
3337
*/
34-
public function testCompiler($test)
38+
public function testCompiler(SpecTestModel $test)
3539
{
36-
if( in_array($test['name'], self::$skipTests) ) {
40+
if( in_array($test->name, self::$skipTests) ) {
3741
$this->markTestIncomplete();
3842
}
39-
$test = $this->prepareTestData($test);
4043

41-
if( !empty($test['exception']) ) {
44+
if( $test->exception ) {
4245
$this->setExpectedException('\\Handlebars\\Exception');
4346
}
4447

4548
$handlebars = $this->handlebarsFactory($test, 'compiler');
4649
$compiler = new PhpCompiler();
47-
$templateSpecStr = $compiler->compile($this->convertContext($test['opcodes']), $test['compileOptions']);
50+
$templateSpecStr = $compiler->compile($test->getOpcodes(), $test->compileOptions);
4851

4952
if( false ) {
5053
$file = tempnam(sys_get_temp_dir(), 'HandlebarsTestsCache');
@@ -57,37 +60,35 @@ public function testCompiler($test)
5760
};
5861
}
5962

60-
$fn = new \Handlebars\Compiler\Runtime($handlebars, $templateSpec);
61-
$actual = $fn($test['data'], $test['options']);
62-
$this->assertEquals($test['expected'], $actual);
63+
$fn = new CompilerRuntime($handlebars, $templateSpec);
64+
$actual = $fn($test->getData(), $test->getOptions());
65+
$this->assertEquals($test->expected, $actual);
6366
}
6467

6568
/**
69+
* @param SpecTestModel $test
6670
* @dataProvider specProvider
6771
*/
68-
public function testLegacyVM($test)
72+
public function testLegacyVM(SpecTestModel $test)
6973
{
70-
if( in_array($test['name'], self::$skipTests) || in_array($test['name'], self::$skipLegacyVMTests) ) {
74+
if( in_array($test->name, self::$skipTests) || in_array($test->name, self::$skipLegacyVMTests) ) {
7175
$this->markTestIncomplete();
7276
}
73-
if( $test['description'] === 'decorators' ||
74-
!empty($test['decorators']) ||
75-
$test['description'] === 'inline partials' ) {
77+
if( $test->description === 'decorators' ||
78+
!empty($test->decorators) ||
79+
$test->description === 'inline partials' ) {
7680
$this->markTestIncomplete("The VM does not support decorators in export mode - requires custom compiler option");
7781
}
78-
$test = $this->prepareTestData($test);
7982

80-
if( !empty($test['exception']) ) {
83+
if( $test->exception ) {
8184
$this->setExpectedException('\\Handlebars\\Exception');
8285
}
8386

8487
$handlebars = $this->handlebarsFactory($test, 'vm');
8588

86-
$allOptions = array_merge($test['compileOptions'], $test['options']);
87-
88-
$vm = new \Handlebars\VM\Runtime($handlebars, $this->convertContext($test['opcodes']));
89-
$actual = $vm($test['data'], $allOptions);
90-
$this->assertEquals($test['expected'], $actual);
89+
$vm = new VMRuntime($handlebars, $test->getOpcodes());
90+
$actual = $vm($test->getData(), $test->getAllOptions());
91+
$this->assertEquals($test->expected, $actual);
9192
}
9293

9394
// Note: New VM doesn't have a way of specifying opcodes (yet)
@@ -113,7 +114,7 @@ public function specProvider($testName)
113114
$test['suiteName'] = $suiteName;
114115
$test['number'] = $i;
115116
$test['name'] = $name = sprintf('%s - %s - %s', $test['suiteName'], $test['description'], $test['it']);
116-
$tests[$name] = array($test);
117+
$tests[$name] = array(new SpecTestModel($test));
117118
}
118119
}
119120
return $tests;
@@ -136,74 +137,31 @@ protected function getDataSetAsString($includeData = true)
136137
return $out;
137138
}
138139

139-
protected function handlebarsFactory($test, $mode = null)
140+
protected function handlebarsFactory(SpecTestModel $test, $mode = null)
140141
{
141-
$globalHelpers = (array) $this->convertCode($test['globalHelpers']);
142-
$globalDecorators = (array) $this->convertCode($test['globalDecorators']);
143-
$helpers = (array) $this->convertCode($test['helpers']);
144-
$decorators = (array) $this->convertCode($test['decorators']);
145-
146-
$pr = new DefaultRegistry();
147-
$handlebars = \Handlebars\Handlebars::factory(array(
142+
$partialRegistry = new DefaultRegistry();
143+
$handlebars = Handlebars::factory(array(
148144
'mode' => $mode,
149-
'helpers' => new DefaultRegistry(array_merge($globalHelpers, $helpers)),
150-
'partials' => $pr,
151-
'decorators' => new DefaultRegistry(array_merge($globalDecorators, $decorators)),
145+
'helpers' => new DefaultRegistry($test->getAllHelpers()),
146+
'partials' => $partialRegistry,
147+
'decorators' => new DefaultRegistry($test->getAllDecorators()),
152148
));
153149
if( $mode === 'compiler' ) {
154150
$compiler = new PhpCompiler();
155-
foreach( $test['globalPartialOpcodes'] as $name => $partialOpcode ) {
156-
$pr[$name] = new \Handlebars\Compiler\Runtime(
157-
$handlebars,
158-
eval('return ' . $compiler->compile($this->convertContext($partialOpcode), $test['compileOptions']) . ';')
159-
);
160-
}
161-
foreach( $test['partialOpcodes'] as $name => $partialOpcode ) {
162-
$pr[$name] = new \Handlebars\Compiler\Runtime(
151+
foreach( $test->getAllPartialOpcodes() as $name => $partialOpcodes ) {
152+
$partialRegistry[$name] = new CompilerRuntime(
163153
$handlebars,
164-
eval('return ' . $compiler->compile($this->convertContext($partialOpcode), $test['compileOptions']) . ';')
154+
eval('return ' . $compiler->compile($partialOpcodes, $test->compileOptions) . ';')
165155
);
166156
}
167157
} else if( $mode === 'vm' ) {
168-
foreach( $test['globalPartialOpcodes'] as $name => $partialOpcode ) {
169-
$pr[$name] = new \Handlebars\VM\Runtime($handlebars, $this->convertContext($partialOpcode));
170-
}
171-
foreach( $test['partialOpcodes'] as $name => $partialOpcode ) {
172-
$pr[$name] = new \Handlebars\VM\Runtime($handlebars, $this->convertContext($partialOpcode));
158+
foreach( $test->getAllPartialOpcodes() as $name => $partialOpcodes ) {
159+
$partialRegistry[$name] = new VMRuntime($handlebars, $partialOpcodes);
173160
}
174161
} else {
175162
throw new \Exception('Unknown mode: ' . $mode);
176163
}
177-
$handlebars->setPartials($pr);
164+
$handlebars->setPartials($partialRegistry);
178165
return $handlebars;
179166
}
180-
181-
protected function prepareTestData($test)
182-
{
183-
$test = array_merge(array(
184-
'data' => null,
185-
'helpers' => array(),
186-
'partials' => array(),
187-
'decorators' => array(),
188-
'globalHelpers' => array(),
189-
'globalPartials' => array(),
190-
'globalDecorators' => array(),
191-
'exception' => false,
192-
'message' => null,
193-
'compileOptions' => array(),
194-
'options' => array(),
195-
'opcodes' => array(),
196-
'partialOpcodes' => array(),
197-
'globalPartialOpcodes' => array(),
198-
), $test);
199-
$test['compileOptions']['data'] = true; // @todo fix
200-
$test['data'] = $this->convertCode($test['data']);
201-
$test['helpers'] = (array) $this->convertCode($test['helpers']);
202-
$test['partials'] = (array) $this->convertCode($test['partials']);
203-
$test['decorators'] = (array) $this->convertCode($test['decorators']);
204-
if( isset($test['options']['data']) ) {
205-
$test['options']['data'] = $this->convertCode($test['options']['data']);
206-
}
207-
return $test;
208-
}
209167
}

0 commit comments

Comments
 (0)