Skip to content

Commit 98d3e02

Browse files
committed
PHPC-941: Update BSON corpus tests for canonical and relaxed JSON
Generated from mongodb/specifications@f019083 Expected test failures are now defined in the generator script and have been updated for libbson changes.
1 parent 0d30de6 commit 98d3e02

File tree

694 files changed

+771
-827
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

694 files changed

+771
-827
lines changed

scripts/convert-bson-corpus-tests.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
require_once __DIR__ . '/../tests/utils/tools.php';
44

5+
$expectedFailures = [
6+
'Double type: 1.23456789012345677E+18' => 'Variation in double\'s string representation (SPEC-850)',
7+
'Double type: -1.23456789012345677E+18' => 'Variation in double\'s string representation (SPEC-850)',
8+
'Int64 type: -1' => 'PHP encodes integers as 32-bit if range allows',
9+
'Int64 type: 0' => 'PHP encodes integers as 32-bit if range allows',
10+
'Int64 type: 1' => 'PHP encodes integers as 32-bit if range allows',
11+
'Javascript Code with Scope: bad scope doc (field has bad string length)' => 'Depends on PHPC-889',
12+
'Javascript Code with Scope: Unicode and embedded null in code string, empty scope' => 'Embedded null in code string is not supported in libbson (CDRIVER-1879)',
13+
'Multiple types within the same document: All BSON types' => 'PHP encodes integers as 32-bit if range allows',
14+
'Top-level document validity: Bad $date (number, not string or hash)' => 'Legacy extended JSON $date syntax uses numbers (CDRIVER-2223)',
15+
];
16+
517
$outputPath = realpath(__DIR__ . '/../tests') . '/bson-corpus/';
618

719
if ( ! is_dir($outputPath) && ! mkdir($outputPath, 0755, true)) {
@@ -35,7 +47,7 @@
3547
foreach ($test['valid'] as $i => $case) {
3648
$outputFile = sprintf('%s-valid-%03d.phpt', pathinfo($inputFile, PATHINFO_FILENAME), $i + 1);
3749
try {
38-
$output = renderPhpt(getParamsForValid($test, $case));
50+
$output = renderPhpt(getParamsForValid($test, $case), $expectedFailures);
3951
} catch (Exception $e) {
4052
printf("Error processing valid[%d] in %s: %s\n", $i, $inputFile, $e->getMessage());
4153
continue;
@@ -52,7 +64,7 @@
5264
foreach ($test['decodeErrors'] as $i => $case) {
5365
$outputFile = sprintf('%s-decodeError-%03d.phpt', pathinfo($inputFile, PATHINFO_FILENAME), $i + 1);
5466
try {
55-
$output = renderPhpt(getParamsForDecodeError($test, $case));
67+
$output = renderPhpt(getParamsForDecodeError($test, $case), $expectedFailures);
5668
} catch (Exception $e) {
5769
printf("Error processing decodeErrors[%d] in %s: %s\n", $i, $inputFile, $e->getMessage());
5870
continue;
@@ -69,7 +81,7 @@
6981
foreach ($test['parseErrors'] as $i => $case) {
7082
$outputFile = sprintf('%s-parseError-%03d.phpt', pathinfo($inputFile, PATHINFO_FILENAME), $i + 1);
7183
try {
72-
$output = renderPhpt(getParamsForParseError($test, $case));
84+
$output = renderPhpt(getParamsForParseError($test, $case), $expectedFailures);
7385
} catch (Exception $e) {
7486
printf("Error processing parseErrors[%d] in %s: %s\n", $i, $inputFile, $e->getMessage());
7587
continue;
@@ -139,12 +151,12 @@ function getParamsForValid(array $test, array $case)
139151
$expect .= $expectedCanonicalBson . "\n";
140152

141153
$code .= "\n// Canonical BSON -> Canonical extJSON \n";
142-
$code .= 'echo json_canonicalize(toExtendedJSON($canonicalBson)), "\n";' . "\n";;
154+
$code .= 'echo json_canonicalize(toCanonicalJSON($canonicalBson)), "\n";' . "\n";;
143155
$expect .= $expectedCanonicalExtJson . "\n";
144156

145157
if (isset($relaxedExtJson)) {
146158
$code .= "\n// Canonical BSON -> Relaxed extJSON \n";
147-
$code .= 'echo json_canonicalize(toJSON($canonicalBson)), "\n";' . "\n";;
159+
$code .= 'echo json_canonicalize(toRelaxedJSON($canonicalBson)), "\n";' . "\n";;
148160
$expect .= $expectedRelaxedExtJson . "\n";
149161
}
150162

@@ -160,12 +172,12 @@ function getParamsForValid(array $test, array $case)
160172
$expect .= $expectedCanonicalBson . "\n";
161173

162174
$code .= "\n// Degenerate BSON -> Canonical extJSON \n";
163-
$code .= 'echo json_canonicalize(toExtendedJSON($degenerateBson)), "\n";' . "\n";;
175+
$code .= 'echo json_canonicalize(toCanonicalJSON($degenerateBson)), "\n";' . "\n";;
164176
$expect .= $expectedCanonicalExtJson . "\n";
165177

166178
if (isset($relaxedExtJson)) {
167179
$code .= "\n// Degenerate BSON -> Relaxed extJSON \n";
168-
$code .= 'echo json_canonicalize(toJSON($degenerateBson)), "\n";' . "\n";;
180+
$code .= 'echo json_canonicalize(toRelaxedJSON($degenerateBson)), "\n";' . "\n";;
169181
$expect .= $expectedRelaxedExtJson . "\n";
170182
}
171183
}
@@ -178,7 +190,7 @@ function getParamsForValid(array $test, array $case)
178190

179191
if (isset($relaxedExtJson)) {
180192
$code .= "\n// Relaxed extJSON -> BSON -> Relaxed extJSON \n";
181-
$code .= 'echo json_canonicalize(toJSON(fromJSON($relaxedExtJson))), "\n";' . "\n";
193+
$code .= 'echo json_canonicalize(toRelaxedJSON(fromJSON($relaxedExtJson))), "\n";' . "\n";
182194
$expect .= $expectedRelaxedExtJson . "\n";
183195
}
184196

@@ -256,12 +268,16 @@ function getParamsForParseError(array $test, array $case)
256268
];
257269
}
258270

259-
function renderPhpt(array $params)
271+
function renderPhpt(array $params, array $expectedFailures)
260272
{
273+
$params['%XFAIL%'] = isset($expectedFailures[$params['%NAME%']])
274+
? "--XFAIL--\n" . $expectedFailures[$params['%NAME%']] . "\n"
275+
: '';
276+
261277
$template = <<< 'TEMPLATE'
262278
--TEST--
263279
%NAME%
264-
--DESCRIPTION--
280+
%XFAIL%--DESCRIPTION--
265281
Generated by scripts/convert-bson-corpus-tests.php
266282
267283
DO NOT EDIT THIS FILE

tests/bson-corpus/array-valid-001.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ $canonicalExtJson = '{"a" : []}';
1616
echo bin2hex(fromPHP(toPHP($canonicalBson))), "\n";
1717

1818
// Canonical BSON -> Canonical extJSON
19-
echo json_canonicalize(toExtendedJSON($canonicalBson)), "\n";
19+
echo json_canonicalize(toCanonicalJSON($canonicalBson)), "\n";
2020

2121
// Canonical extJSON -> Canonical BSON
2222
echo bin2hex(fromJSON($canonicalExtJson)), "\n";

tests/bson-corpus/array-valid-002.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ $canonicalExtJson = '{"a" : [{"$numberInt": "10"}]}';
1616
echo bin2hex(fromPHP(toPHP($canonicalBson))), "\n";
1717

1818
// Canonical BSON -> Canonical extJSON
19-
echo json_canonicalize(toExtendedJSON($canonicalBson)), "\n";
19+
echo json_canonicalize(toCanonicalJSON($canonicalBson)), "\n";
2020

2121
// Canonical extJSON -> Canonical BSON
2222
echo bin2hex(fromJSON($canonicalExtJson)), "\n";

tests/bson-corpus/array-valid-003.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ $canonicalExtJson = '{"a" : [{"$numberInt": "10"}]}';
1717
echo bin2hex(fromPHP(toPHP($canonicalBson))), "\n";
1818

1919
// Canonical BSON -> Canonical extJSON
20-
echo json_canonicalize(toExtendedJSON($canonicalBson)), "\n";
20+
echo json_canonicalize(toCanonicalJSON($canonicalBson)), "\n";
2121

2222
// Canonical extJSON -> Canonical BSON
2323
echo bin2hex(fromJSON($canonicalExtJson)), "\n";
@@ -26,7 +26,7 @@ echo bin2hex(fromJSON($canonicalExtJson)), "\n";
2626
echo bin2hex(fromPHP(toPHP($degenerateBson))), "\n";
2727

2828
// Degenerate BSON -> Canonical extJSON
29-
echo json_canonicalize(toExtendedJSON($degenerateBson)), "\n";
29+
echo json_canonicalize(toCanonicalJSON($degenerateBson)), "\n";
3030

3131
?>
3232
===DONE===

tests/bson-corpus/array-valid-004.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ $canonicalExtJson = '{"a" : [{"$numberInt": "10"}]}';
1717
echo bin2hex(fromPHP(toPHP($canonicalBson))), "\n";
1818

1919
// Canonical BSON -> Canonical extJSON
20-
echo json_canonicalize(toExtendedJSON($canonicalBson)), "\n";
20+
echo json_canonicalize(toCanonicalJSON($canonicalBson)), "\n";
2121

2222
// Canonical extJSON -> Canonical BSON
2323
echo bin2hex(fromJSON($canonicalExtJson)), "\n";
@@ -26,7 +26,7 @@ echo bin2hex(fromJSON($canonicalExtJson)), "\n";
2626
echo bin2hex(fromPHP(toPHP($degenerateBson))), "\n";
2727

2828
// Degenerate BSON -> Canonical extJSON
29-
echo json_canonicalize(toExtendedJSON($degenerateBson)), "\n";
29+
echo json_canonicalize(toCanonicalJSON($degenerateBson)), "\n";
3030

3131
?>
3232
===DONE===

tests/bson-corpus/binary-valid-001.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ $canonicalExtJson = '{"x" : { "$binary" : {"base64" : "", "subType" : "00"}}}';
1616
echo bin2hex(fromPHP(toPHP($canonicalBson))), "\n";
1717

1818
// Canonical BSON -> Canonical extJSON
19-
echo json_canonicalize(toExtendedJSON($canonicalBson)), "\n";
19+
echo json_canonicalize(toCanonicalJSON($canonicalBson)), "\n";
2020

2121
// Canonical extJSON -> Canonical BSON
2222
echo bin2hex(fromJSON($canonicalExtJson)), "\n";

tests/bson-corpus/binary-valid-002.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ $degenerateExtJson = '{"x" : { "$binary" : {"subType" : "00", "base64" : ""}}}';
1717
echo bin2hex(fromPHP(toPHP($canonicalBson))), "\n";
1818

1919
// Canonical BSON -> Canonical extJSON
20-
echo json_canonicalize(toExtendedJSON($canonicalBson)), "\n";
20+
echo json_canonicalize(toCanonicalJSON($canonicalBson)), "\n";
2121

2222
// Canonical extJSON -> Canonical BSON
2323
echo bin2hex(fromJSON($canonicalExtJson)), "\n";

tests/bson-corpus/binary-valid-003.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ $canonicalExtJson = '{"x" : { "$binary" : {"base64" : "//8=", "subType" : "00"}}
1616
echo bin2hex(fromPHP(toPHP($canonicalBson))), "\n";
1717

1818
// Canonical BSON -> Canonical extJSON
19-
echo json_canonicalize(toExtendedJSON($canonicalBson)), "\n";
19+
echo json_canonicalize(toCanonicalJSON($canonicalBson)), "\n";
2020

2121
// Canonical extJSON -> Canonical BSON
2222
echo bin2hex(fromJSON($canonicalExtJson)), "\n";

tests/bson-corpus/binary-valid-004.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ $canonicalExtJson = '{"x" : { "$binary" : {"base64" : "//8=", "subType" : "01"}}
1616
echo bin2hex(fromPHP(toPHP($canonicalBson))), "\n";
1717

1818
// Canonical BSON -> Canonical extJSON
19-
echo json_canonicalize(toExtendedJSON($canonicalBson)), "\n";
19+
echo json_canonicalize(toCanonicalJSON($canonicalBson)), "\n";
2020

2121
// Canonical extJSON -> Canonical BSON
2222
echo bin2hex(fromJSON($canonicalExtJson)), "\n";

tests/bson-corpus/binary-valid-005.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ $canonicalExtJson = '{"x" : { "$binary" : {"base64" : "//8=", "subType" : "02"}}
1616
echo bin2hex(fromPHP(toPHP($canonicalBson))), "\n";
1717

1818
// Canonical BSON -> Canonical extJSON
19-
echo json_canonicalize(toExtendedJSON($canonicalBson)), "\n";
19+
echo json_canonicalize(toCanonicalJSON($canonicalBson)), "\n";
2020

2121
// Canonical extJSON -> Canonical BSON
2222
echo bin2hex(fromJSON($canonicalExtJson)), "\n";

0 commit comments

Comments
 (0)