Skip to content

Commit 02e3274

Browse files
committed
Fix #86 - Test all variants through deeply nsted recursion
1 parent d140618 commit 02e3274

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

test/shared.cjs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const { parse, stringify } = require(__dirname + '/../cjs');
2+
3+
let arr = ['arr', 1];
4+
let obj = { obj: 2 };
5+
6+
for (let i = 0; i < 64; i++) {
7+
arr = [arr];
8+
obj = { obj: obj };
9+
}
10+
11+
const str = stringify([arr, obj]);
12+
13+
require('fs').writeFileSync(__dirname + '/shared.json', str);
14+
15+
[arr, obj] = parse(str);
16+
17+
for (let i = 0; i < 64; i++) {
18+
arr = arr[0];
19+
obj = obj.obj;
20+
}
21+
22+
console.assert(
23+
arr.length === 2 &&
24+
arr[0] === 'arr' &&
25+
arr[1] === 1 &&
26+
obj.obj === 2
27+
);

test/shared.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[["1","2"],["3"],{"obj":"4"},["5"],{"obj":"6"},["7"],{"obj":"8"},["9"],{"obj":"10"},["11"],{"obj":"12"},["13"],{"obj":"14"},["15"],{"obj":"16"},["17"],{"obj":"18"},["19"],{"obj":"20"},["21"],{"obj":"22"},["23"],{"obj":"24"},["25"],{"obj":"26"},["27"],{"obj":"28"},["29"],{"obj":"30"},["31"],{"obj":"32"},["33"],{"obj":"34"},["35"],{"obj":"36"},["37"],{"obj":"38"},["39"],{"obj":"40"},["41"],{"obj":"42"},["43"],{"obj":"44"},["45"],{"obj":"46"},["47"],{"obj":"48"},["49"],{"obj":"50"},["51"],{"obj":"52"},["53"],{"obj":"54"},["55"],{"obj":"56"},["57"],{"obj":"58"},["59"],{"obj":"60"},["61"],{"obj":"62"},["63"],{"obj":"64"},["65"],{"obj":"66"},["67"],{"obj":"68"},["69"],{"obj":"70"},["71"],{"obj":"72"},["73"],{"obj":"74"},["75"],{"obj":"76"},["77"],{"obj":"78"},["79"],{"obj":"80"},["81"],{"obj":"82"},["83"],{"obj":"84"},["85"],{"obj":"86"},["87"],{"obj":"88"},["89"],{"obj":"90"},["91"],{"obj":"92"},["93"],{"obj":"94"},["95"],{"obj":"96"},["97"],{"obj":"98"},["99"],{"obj":"100"},["101"],{"obj":"102"},["103"],{"obj":"104"},["105"],{"obj":"106"},["107"],{"obj":"108"},["109"],{"obj":"110"},["111"],{"obj":"112"},["113"],{"obj":"114"},["115"],{"obj":"116"},["117"],{"obj":"118"},["119"],{"obj":"120"},["121"],{"obj":"122"},["123"],{"obj":"124"},["125"],{"obj":"126"},["127"],{"obj":"128"},["129"],{"obj":"130"},["131",1],{"obj":2},"arr"]

test/shared.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
error_reporting(E_ALL | E_STRICT);
4+
5+
require_once(__DIR__ . '/../php/flatted.php');
6+
7+
$str = file_get_contents(__DIR__ . '/shared.json');
8+
$json = Flatted::parse($str);
9+
10+
$arr = $json[0];
11+
$obj = $json[1];
12+
13+
for ($i = 0; $i < 64; $i++) {
14+
$arr = $arr[0];
15+
$obj = $obj->obj;
16+
}
17+
18+
assert(count($arr) == 2 && $arr[0] == 'arr' && $arr[1] == 1 && $obj->obj == 2);
19+
20+
assert(Flatted::stringify($json) == $str);
21+
22+
?>

test/shared.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# ⚠️ RUN node test/shared.cjs FIRST
2+
# then python test/shared.py
3+
4+
import os, sys
5+
6+
__dir__ = os.path.dirname(__file__)
7+
sys.path.append(os.path.join(__dir__, '..', 'python'))
8+
9+
from flatted import parse, stringify
10+
11+
with open(os.path.join(__dir__, 'shared.json'), 'r') as f:
12+
str = f.read()
13+
14+
arr, obj = parse(str)
15+
16+
for i in range(64):
17+
arr = arr[0]
18+
obj = obj['obj']
19+
20+
assert len(arr) == 2 and arr[0] == 'arr' and arr[1] == 1 and obj['obj'] == 2
21+
22+
assert stringify(parse(str), separators=(',', ':')) == str

0 commit comments

Comments
 (0)