MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at master 73 lines 2.8 kB view raw
1import { test, testDeep, summary } from './helpers.js'; 2 3console.log('JSON Tests\n'); 4 5let obj1 = JSON.parse('{"name":"John","age":30}'); 6test('parse object name', obj1.name, 'John'); 7test('parse object age', obj1.age, 30); 8 9let arr = JSON.parse('[1,2,3,4,5]'); 10test('parse array length', arr.length, 5); 11test('parse array first', arr[0], 1); 12test('parse array last', arr[4], 5); 13 14let nested = JSON.parse('{"user":{"name":"Alice","age":25},"active":true}'); 15test('parse nested name', nested.user.name, 'Alice'); 16test('parse nested age', nested.user.age, 25); 17test('parse nested bool', nested.active, true); 18 19let arrObj = JSON.parse('[{"id":1,"name":"Item1"},{"id":2,"name":"Item2"}]'); 20test('parse array of objects length', arrObj.length, 2); 21test('parse array of objects first name', arrObj[0].name, 'Item1'); 22test('parse array of objects second id', arrObj[1].id, 2); 23 24let types = JSON.parse('{"active":true,"inactive":false,"data":null,"count":123}'); 25test('parse bool true', types.active, true); 26test('parse bool false', types.inactive, false); 27test('parse null', types.data, null); 28test('parse number', types.count, 123); 29 30let nums = JSON.parse('{"int":42,"float":3.14,"negative":-10,"zero":0}'); 31test('parse int', nums.int, 42); 32test('parse float', nums.float, 3.14); 33test('parse negative', nums.negative, -10); 34test('parse zero', nums.zero, 0); 35 36let emptyObj = JSON.parse('{}'); 37let emptyArr = JSON.parse('[]'); 38testDeep('parse empty object', emptyObj, {}); 39test('parse empty array length', emptyArr.length, 0); 40 41let stringified = JSON.stringify([10, 20, 30]); 42test('stringify array', stringified, '[10,20,30]'); 43 44let objStr = JSON.stringify({ name: 'Charlie', age: 35 }); 45test('stringify object contains name', objStr.includes('"name"'), true); 46test('stringify object contains age', objStr.includes('"age"'), true); 47 48let withTypes = JSON.stringify({ active: true, inactive: false, data: null }); 49test('stringify bool true', withTypes.includes('true'), true); 50test('stringify bool false', withTypes.includes('false'), true); 51test('stringify null', withTypes.includes('null'), true); 52 53let original = '{"count":5}'; 54let parsed = JSON.parse(original); 55parsed.count = 10; 56test('modify after parse', parsed.count, 10); 57 58let spacedJson = ' { "key" : "value" } '; 59let spacedParsed = JSON.parse(spacedJson); 60test('parse with whitespace', spacedParsed.key, 'value'); 61 62let api = JSON.parse('{"status":"success","data":{"user":"john","token":"abc123"}}'); 63test('parse api status', api.status, 'success'); 64test('parse api data user', api.data.user, 'john'); 65test('parse api data token', api.data.token, 'abc123'); 66 67let roundtrip = { a: 1, b: 'two', c: true }; 68let rt = JSON.parse(JSON.stringify(roundtrip)); 69test('roundtrip a', rt.a, 1); 70test('roundtrip b', rt.b, 'two'); 71test('roundtrip c', rt.c, true); 72 73summary();