This repository was archived by the owner on May 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMustacheSpecTest.php
133 lines (117 loc) · 4.21 KB
/
MustacheSpecTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
namespace Handlebars\Tests;
use Handlebars\DefaultRegistry;
class MustacheSpecTest extends Common
{
private $data;
private $dataName;
static private $skipSuites = array('delimiters', '~lambdas');
static private $skipTests = array(
'partials - Standalone Indentation - Each line of the partial should be indented before rendering.',
);
public function __construct($name = null, array $data = array(), $dataName = '') {
$this->data = $data;
$this->dataName = $dataName;
parent::__construct($name, $data, $dataName);
}
public function setUp()
{
parent::setUp();
if( !extension_loaded('handlebars') ) {
return $this->markTestSkipped("Integration tests require the handlebars extension");
}
}
/**
* @param SpecTestModel $test
* @dataProvider specProvider
*/
public function testCompiler(SpecTestModel $test)
{
if( in_array($test->name, self::$skipTests) ) {
$this->markTestIncomplete();
}
if( $test->exception ) {
$this->setExpectedException('\\Handlebars\\Exception');
}
$handlebars = $this->handlebarsFactory($test, 'compiler');
$fn = $handlebars->compile($test->template, array('compat' => true));
$actual = $fn($test->getData());
$this->assertEquals($test->expected, $actual);
}
/**
* @param SpecTestModel $test
* @dataProvider specProvider
*/
public function testLegacyVM(SpecTestModel $test)
{
if( in_array($test->name, self::$skipTests) ) {
$this->markTestIncomplete();
}
$handlebars = $this->handlebarsFactory($test, 'vm');
$actual = $handlebars->render($test->template, $test->getData(), array('compat' => true));
$this->assertEquals($test->expected, $actual);
}
/**
* @param SpecTestModel $test
* @dataProvider specProvider
*/
public function testNewVM(SpecTestModel $test)
{
if( in_array($test->name, self::$skipTests) ) {
$this->markTestIncomplete();
}
$handlebars = $this->handlebarsFactory($test, 'cvm');
$actual = $handlebars->render($test->template, $test->getData(), array('compat' => true));
$this->assertEquals($test->expected, $actual);
}
public function specProvider()
{
$dir = getenv('MUSTACHE_SPEC_DIR');
if( !$dir ) {
$dir = __DIR__ . '/../vendor/jbboehr/mustache-spec/specs';
}
$tests = array();
foreach( scandir($dir) as $file ) {
if( $file[0] === '.' || substr($file, -5) !== '.json' ) continue;
$suiteName = substr($file, 0, -strlen('.json'));
if( in_array($suiteName, self::$skipSuites) ) continue;
$filePath = $dir . '/' . $file;
$i = 0;
$testData = json_decode(file_get_contents($filePath), true);
foreach( $testData['tests'] as $test ) {
$i++;
// Convert to handlebars spec format
$test['it'] = $test['desc'];
$test['description'] = $test['name'];
// Patch
$test['suiteName'] = $suiteName;
$test['number'] = $i;
$test['name'] = $name = sprintf('%s - %s - %s', $test['suiteName'], $test['description'], $test['it']);
$tests[$name] = array(new SpecTestModel($test));
}
}
return $tests;
}
protected function getDataSetAsString($includeData = true)
{
$out = ' | ' . $this->dataName;
if( $includeData ) {
$test = $this->data[0];
$out .= "\n";
foreach( $test as $k => $v ) {
if( $k === 'name' ) continue;
if( !is_scalar($v) ) {
$v = json_encode($v);
}
$out .= $k . ': ' . $v . "\n";
}
}
return $out;
}
protected function handlebarsFactory(SpecTestModel $test, $mode = null)
{
$handlebars = new \Handlebars\Handlebars(array('mode' => $mode));
$handlebars->setPartials(new DefaultRegistry($test->getAllPartials()));
return $handlebars;
}
}