ComplianceTest.php
4.04 KB
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
134
135
136
<?php
namespace JmesPath\Tests;
use JmesPath\AstRuntime;
use JmesPath\CompilerRuntime;
use JmesPath\SyntaxErrorException;
class ComplianceTest extends \PHPUnit_Framework_TestCase
{
private static $path;
public static function setUpBeforeClass()
{
self::$path = __DIR__ . '/../../compiled';
array_map('unlink', glob(self::$path . '/jmespath_*.php'));
}
public static function tearDownAfterClass()
{
array_map('unlink', glob(self::$path . '/jmespath_*.php'));
}
/**
* @dataProvider complianceProvider
*/
public function testPassesCompliance(
$data,
$expression,
$result,
$error,
$file,
$suite,
$case,
$compiled,
$asAssoc
) {
$evalResult = null;
$failed = false;
$failureMsg = '';
$failure = '';
$compiledStr = '';
try {
if ($compiled) {
$compiledStr = \JmesPath\Env::COMPILE_DIR . '=on ';
$runtime = new CompilerRuntime(self::$path);
} else {
$runtime = new AstRuntime();
}
$evalResult = $runtime($expression, $data);
} catch (\Exception $e) {
$failed = $e instanceof SyntaxErrorException ? 'syntax' : 'runtime';
$failureMsg = sprintf(
'%s (%s line %d)',
$e->getMessage(),
$e->getFile(),
$e->getLine()
);
}
$file = __DIR__ . '/compliance/' . $file . '.json';
$failure .= "\n{$compiledStr}php bin/jp.php --file {$file} --suite {$suite} --case {$case}\n\n"
. "Expected: " . $this->prettyJson($result) . "\n\n";
$failure .= 'Associative? ' . var_export($asAssoc, true) . "\n\n";
if (!$error && $failed) {
$this->fail("Should not have failed\n{$failure}=> {$failed} {$failureMsg}");
} elseif ($error && !$failed) {
$this->fail("Should have failed\n{$failure}");
}
$this->assertEquals(
$this->convertAssoc($result),
$this->convertAssoc($evalResult),
$failure
);
}
public function complianceProvider()
{
$cases = [];
$files = array_map(function ($f) {
return basename($f, '.json');
}, glob(__DIR__ . '/compliance/*.json'));
foreach ($files as $name) {
$contents = file_get_contents(__DIR__ . "/compliance/{$name}.json");
foreach ([true, false] as $asAssoc) {
$json = json_decode($contents, true);
$jsonObj = json_decode($contents);
foreach ($json as $suiteNumber => $suite) {
$given = $asAssoc ? $suite['given'] : $jsonObj[$suiteNumber]->given;
foreach ($suite['cases'] as $caseNumber => $case) {
$caseData = [
$given,
$case['expression'],
isset($case['result']) ? $case['result'] : null,
isset($case['error']) ? $case['error'] : false,
$name,
$suiteNumber,
$caseNumber,
false,
$asAssoc
];
$cases[] = $caseData;
$caseData[7] = true;
$cases[] = $caseData;
}
}
}
}
return $cases;
}
private function convertAssoc($data)
{
if ($data instanceof \stdClass) {
return $this->convertAssoc((array) $data);
} elseif (is_array($data)) {
return array_map([$this, 'convertAssoc'], $data);
} else {
return $data;
}
}
private function prettyJson($json)
{
if (defined('JSON_PRETTY_PRINT')) {
return json_encode($json, JSON_PRETTY_PRINT);
}
return json_encode($json);
}
}