Skip to content

Commit

Permalink
JSON Parser Test Suite (#232)
Browse files Browse the repository at this point in the history
* feat: port tests from jsmnsol lib

The truffle tests were pretty minimal, but good to keep in sync since
this isn't a published package we're importing.
https://github.com/chrisdotn/jsmnSol/tree/master/test

* feat: add jsc JSONTestSuite

Since the existing test suite didn't catch any of the reported problems
with the JSON parsing, adding another larger suite that's used to
compare popular JSON parsers should!

Obviously because this is so large it can be trimmed, but the hope is to
find overlap between the feedback and the test-cases here.

* fix: update to latest era-test-node

This action is broken again, hopefully the latest works!

* fix: provide more debug for json tests

Tests connect to the files

* fix: parse an emoji instead of backslash

Skipping all the json format tests for now,
These are helpful if we want a fully compliant parser, but aren't
helpful in parsing the client data JSON.

* feat: add duplicate key parsing test

It doesn't return an error, because it doesn't actually record key
values

* fix: undo era-test-node version

it looks like this is fixed now

* Update test/JsmnSolLib.ts

Co-authored-by: Nicolas Villanueva <[email protected]>

* Update test/JsmnSolLib.ts

Co-authored-by: Nicolas Villanueva <[email protected]>

* Update test/JsmnSolLib.ts

Co-authored-by: Nicolas Villanueva <[email protected]>

* feat: add json string tests

Covers existing functionality

* fix: re-add removed return result

---------

Co-authored-by: Nicolas Villanueva <[email protected]>
Co-authored-by: Lyova Potyomkin <[email protected]>
  • Loading branch information
3 people authored Jan 8, 2025
1 parent 489e8bb commit 8cef3bc
Show file tree
Hide file tree
Showing 287 changed files with 839 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ deployments-zk
**/*.tsx
**/*.mjs
**/*.cjs

# These are explicitly poorly formatted files to test json parsing
test/json-files/*.json
4 changes: 2 additions & 2 deletions src/libraries/JsmnSolLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ library JsmnSolLib {
}
// parseInt
function parseInt(string memory _a) internal pure returns (int) {
function parseInt(string memory _a) internal pure returns (int256) {
return parseInt(_a, 0);
}
Expand Down Expand Up @@ -342,7 +342,7 @@ library JsmnSolLib {
}
}
function strCompare(string memory _a, string memory _b) internal pure returns (int) {
function strCompare(string memory _a, string memory _b) internal pure returns (int256) {
bytes memory a = bytes(_a);
bytes memory b = bytes(_b);
uint256 minLength = a.length;
Expand Down
35 changes: 35 additions & 0 deletions src/test/JsmnSolLibTest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import { JsmnSolLib } from "../libraries/JsmnSolLib.sol";

import "hardhat/console.sol";

contract JsmnSolLibTest {
function init(uint256 length) external pure returns (JsmnSolLib.Parser memory, JsmnSolLib.Token[] memory) {
return JsmnSolLib.init(length);
}

function parse(
string calldata json,
uint256 length
) external pure returns (uint, JsmnSolLib.Token[] memory tokens, uint) {
return JsmnSolLib.parse(abi.decode(abi.encode(json), (string)), length);
}

function getBytes(string calldata json, uint256 start, uint256 end) external pure returns (string memory) {
return JsmnSolLib.getBytes(string(abi.decode(abi.encode(json), (string))), start, end);
}

function parseIntNoSize(string memory a) external pure returns (int256) {
return JsmnSolLib.parseInt(string(abi.decode(abi.encode(a), (string))));
}

function parseIntSize(string calldata a, uint256 length) external pure returns (int256) {
return JsmnSolLib.parseInt(string(abi.decode(abi.encode(a), (string))), length);
}

function parseBool(string memory b) external pure returns (bool) {
return JsmnSolLib.parseBool(string(abi.decode(abi.encode(b), (string))));
}
}
Loading

0 comments on commit 8cef3bc

Please sign in to comment.