Skip to content

Instantly share code, notes, and snippets.

@karmacoma-eth
Last active December 6, 2022 18:14
Show Gist options
  • Save karmacoma-eth/3efdaf6dfae018c0cb80920cfbfb877e to your computer and use it in GitHub Desktop.
Save karmacoma-eth/3efdaf6dfae018c0cb80920cfbfb877e to your computer and use it in GitHub Desktop.
solidity-json-testing
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import {Test} from "forge-std/Test.sol";
import {stdJson} from "forge-std/StdJson.sol";
import {console2} from "forge-std/console2.sol";
contract TestJson is Test {
/// @dev fields need to be sorted alphabetically (see docs of vm.parseJson())
struct ContractURISchema {
string description;
address fee_recipient;
string image;
string name;
uint256 seller_fee_basis_points;
}
/// @dev tests that the returned json object conforms exactly to the schema (no spurious fields)
function testContractURIWellFormed_objectStyle() public {
string memory json = parseDataUri(edition.contractURI());
ContractURISchema memory parsed = abi.decode(vm.parseJson(json), (ContractURISchema));
assertEq(parsed.name, DEFAULT_NAME);
assertEq(parsed.description, DEFAULT_DESCRIPTION);
assertEq(parsed.image, DEFAULT_IMAGE_URL);
assertEq(parsed.seller_fee_basis_points, DEFAULT_ROYALTIES_BPS);
assertEq(parsed.fee_recipient, editionOwner);
}
/// @dev tests only the fields we care about, may contain more
function testContractURIWellFormed_jqStyle() public {
string memory json = parseDataUri(edition.contractURI());
assertEq(stdJson.readString(json, ".name"), DEFAULT_NAME);
assertEq(stdJson.readString(json, ".description"), DEFAULT_DESCRIPTION);
assertEq(stdJson.readString(json, ".image"), DEFAULT_IMAGE_URL);
assertEq(stdJson.readUint(json, ".seller_fee_basis_points"), DEFAULT_ROYALTIES_BPS);
assertEq(stdJson.readAddress(json, ".fee_recipient"), editionOwner);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment