Spaces:
Running
Running
Download tests.cjs from vitalipavlovsk/json-shape-diff: direct link, hf CLI and curl.
- Browser
- Download file 2.17 kB
-
https://huggingface.co/spaces/vitalipavlovsk/json-shape-diff/resolve/main/tests.cjs
- Command line
-
hf download hf://spaces/vitalipavlovsk/json-shape-diff/tests.cjs
-
curl -L -o tests.cjs https://huggingface.co/spaces/vitalipavlovsk/json-shape-diff/resolve/main/tests.cjs
2.17 kB
| const assert=require('node:assert/strict');const {compare,pointer}=require('./diff.js');let passed=0;function test(name,fn){fn();passed++;console.log('PASS '+name);}const d=(a,b,values=false)=>compare(a,b,values); | |
| test('identical',()=>assert.equal(d({a:1},{a:1}).changes.length,0)); | |
| test('object key order ignored',()=>assert.equal(d({a:1,b:2},{b:2,a:1}).changes.length,0)); | |
| test('null versus missing',()=>assert.deepEqual(d({x:null},{}).changes[0],{operation:'removed',path:'/x',before_type:'null',after_type:'missing'})); | |
| test('type drift',()=>assert.equal(d({x:1},{x:'1'}).changes[0].operation,'type_changed')); | |
| test('boolean value',()=>assert.equal(d(true,false).changes[0].operation,'value_changed')); | |
| test('root path',()=>assert.equal(d(1,{}).changes[0].path,'')); | |
| test('escaped pointer',()=>assert.equal(pointer('', 'a/~b'),'/a~1~0b')); | |
| test('index array semantics',()=>assert.equal(d([1,2],[2,1]).changes.length,2)); | |
| test('array appended container single change',()=>assert.equal(d([],[{a:1,b:2}]).changes.length,1)); | |
| test('values absent by default',()=>assert.ok(!('before' in d('SECRET','other').changes[0]))); | |
| test('values opt in',()=>assert.equal(d({a:null},{a:1},true).changes[0].before,null)); | |
| test('prototype named keys',()=>assert.equal(d(JSON.parse('{"__proto__":1}'),{}).changes[0].operation,'removed')); | |
| test('number and string differ',()=>assert.equal(d(0,'0').changes[0].operation,'type_changed')); | |
| test('deep recursion refused',()=>{let x={};for(let i=0;i<90;i++)x={a:x};assert.throws(()=>d(x,{}),/depth/)}); | |
| test('node cap refused',()=>assert.throws(()=>d(Array(20001).fill(0),[]),/nodes/)); | |
| test('change cap explicit',()=>{const r=d(Array(3100).fill(0),[]);assert.equal(r.changes.length,3000);assert.equal(r.truncated,true)}); | |
| test('exactly cap not truncated',()=>assert.equal(d(Array(3000).fill(0),[]).truncated,false)); | |
| test('non-finite refused',()=>assert.throws(()=>d(Infinity,0),/Non-finite/)); | |
| test('unsafe integer refused',()=>assert.throws(()=>d(9007199254740992,0),/unsafe/)); | |
| test('HTML stays ordinary data',()=>assert.equal(d('<img onerror=x>','x',true).changes[0].before,'<img onerror=x>')); | |
| console.log(passed+' engine tests passed'); | |