UNPKG

functionalscript

Version:

FunctionalScript is a purely functional subset of JavaScript

329 lines (328 loc) 11.2 kB
import * as parser from "./module.f.js"; import * as tokenizer from "../tokenizer/module.f.js"; import * as list from "../../types/list/module.f.js"; const { toArray } = list; import * as json from "../module.f.js"; import * as o from "../../types/object/module.f.js"; const { sort } = o; import * as encoding from "../../text/utf16/module.f.js"; const tokenizeString = s => toArray(tokenizer.tokenize(encoding.stringToList(s))); const stringify = json.stringify(sort); export default { valid: [ () => { const tokenList = tokenizeString('null'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["ok",null]') { throw result; } }, () => { const tokenList = tokenizeString('true'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["ok",true]') { throw result; } }, () => { const tokenList = tokenizeString('false'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["ok",false]') { throw result; } }, () => { const tokenList = tokenizeString('0.1'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["ok",0.1]') { throw result; } }, () => { const tokenList = tokenizeString('1.1e+2'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["ok",110]') { throw result; } }, () => { const tokenList = tokenizeString('"abc"'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["ok","abc"]') { throw result; } }, () => { const tokenList = tokenizeString('[]'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["ok",[]]') { throw result; } }, () => { const tokenList = tokenizeString('[1]'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["ok",[1]]') { throw result; } }, () => { const tokenList = tokenizeString('[[]]'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["ok",[[]]]') { throw result; } }, () => { const tokenList = tokenizeString('[0,[1,[2,[]]],3]'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["ok",[0,[1,[2,[]]],3]]') { throw result; } }, () => { const tokenList = tokenizeString('{}'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["ok",{}]') { throw result; } }, () => { const tokenList = tokenizeString('[{}]'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["ok",[{}]]') { throw result; } }, () => { const tokenList = tokenizeString('{"a":true,"b":false,"c":null}'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["ok",{"a":true,"b":false,"c":null}]') { throw result; } }, () => { const tokenList = tokenizeString('{"a":{"b":{"c":["d"]}}}'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["ok",{"a":{"b":{"c":["d"]}}}]') { throw result; } }, () => { const tokenList = tokenizeString('[1,]'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["ok",[1]]') { throw result; } }, () => { const tokenList = tokenizeString('{"a":1,}'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["ok",{"a":1}]') { throw result; } } ], invalid: [ () => { const tokenList = tokenizeString(''); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["error","unexpected end"]') { throw result; } }, () => { const tokenList = tokenizeString('"123'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["error","unexpected token"]') { throw result; } }, () => { const tokenList = tokenizeString('[,]'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["error","unexpected token"]') { throw result; } }, () => { const tokenList = tokenizeString('[1 2]'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["error","unexpected token"]') { throw result; } }, () => { const tokenList = tokenizeString('[1,,2]'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["error","unexpected token"]') { throw result; } }, () => { const tokenList = tokenizeString('[]]'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["error","unexpected token"]') { throw result; } }, () => { const tokenList = tokenizeString('["a"'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["error","unexpected end"]') { throw result; } }, () => { const tokenList = tokenizeString('[,1]'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["error","unexpected token"]') { throw result; } }, () => { const tokenList = tokenizeString('[:]'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["error","unexpected token"]') { throw result; } }, () => { const tokenList = tokenizeString(']'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["error","unexpected token"]') { throw result; } }, () => { const tokenList = tokenizeString('{,}'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["error","unexpected token"]') { throw result; } }, () => { const tokenList = tokenizeString('{1:2}'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["error","unexpected token"]') { throw result; } }, () => { const tokenList = tokenizeString('{"1"2}'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["error","unexpected token"]') { throw result; } }, () => { const tokenList = tokenizeString('{"1"::2}'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["error","unexpected token"]') { throw result; } }, () => { const tokenList = tokenizeString('{"1":2,,"3":4'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["error","unexpected token"]') { throw result; } }, () => { const tokenList = tokenizeString('{}}'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["error","unexpected token"]') { throw result; } }, () => { const tokenList = tokenizeString('{"1":2'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["error","unexpected end"]') { throw result; } }, () => { const tokenList = tokenizeString('{,"1":2}'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["error","unexpected token"]') { throw result; } }, () => { const tokenList = tokenizeString('}'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["error","unexpected token"]') { throw result; } }, () => { const tokenList = tokenizeString('[{]}'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["error","unexpected token"]') { throw result; } }, () => { const tokenList = tokenizeString('{[}]'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["error","unexpected token"]') { throw result; } }, () => { const tokenList = tokenizeString('10-5'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["error","unexpected token"]') { throw result; } }, () => { const tokenList = tokenizeString('undefined'); const obj = parser.parse(tokenList); const result = stringify(obj); if (result !== '["error","unexpected token"]') { throw result; } }, ] };