Skip to content

Commit

Permalink
test(schema-type): add more tests (#1146)
Browse files Browse the repository at this point in the history
  • Loading branch information
dk1a authored Jul 11, 2023
1 parent 73e200c commit c6801b8
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions packages/schema-type/test/solidity/SchemaType.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,55 @@
pragma solidity >=0.8.0;

import { Test } from "forge-std/Test.sol";
import { stdError } from "forge-std/StdError.sol";
import { SchemaType } from "../../src/solidity/SchemaType.sol";

contract SchemaTypeTest is Test {
uint256 internal constant SCHEMA_TYPE_LENGTH = 198;

function testGetStaticByteLength() public {
assertEq(SchemaType.UINT8.getStaticByteLength(), 1);
// TODO add more tests (https://github.com/latticexyz/mud/issues/444)
uint8 schemaType = 0;
for (uint256 length = 1; length <= 32; length++) {
assertEq(SchemaType(schemaType).getStaticByteLength(), length, "uint");
schemaType++;
}
for (uint256 length = 1; length <= 32; length++) {
assertEq(SchemaType(schemaType).getStaticByteLength(), length, "int");
schemaType++;
}
for (uint256 length = 1; length <= 32; length++) {
assertEq(SchemaType(schemaType).getStaticByteLength(), length, "bytes");
schemaType++;
}
assertEq(SchemaType(schemaType).getStaticByteLength(), 1, "bool");
schemaType++;
assertEq(SchemaType(schemaType).getStaticByteLength(), 20, "address");
schemaType++;

// all dynamic types must have static length == 0
for (uint256 i; i < 32 * 3; i++) {
assertEq(SchemaType(schemaType).getStaticByteLength(), 0, "uint, int, bytes arrays");
schemaType++;
}
assertEq(SchemaType(schemaType).getStaticByteLength(), 0, "bool array");
schemaType++;
assertEq(SchemaType(schemaType).getStaticByteLength(), 0, "address array");
schemaType++;
assertEq(SchemaType(schemaType).getStaticByteLength(), 0, "dynamic bytes");
schemaType++;
assertEq(SchemaType(schemaType).getStaticByteLength(), 0, "dynamic string");
schemaType++;

// expected length of the enum
assertEq(schemaType, SCHEMA_TYPE_LENGTH);
}

function testSchemaTypeMaxLength() public {
assertEq(SchemaType(SCHEMA_TYPE_LENGTH - 1).getStaticByteLength(), 0);
}

function testSchemaTypeOverMaxLength() public {
vm.expectRevert(stdError.enumConversionError);
SchemaType(SCHEMA_TYPE_LENGTH).getStaticByteLength();
}
}

0 comments on commit c6801b8

Please sign in to comment.