Skip to content

Commit

Permalink
test(store): replace testFail with expectRevert (#2137)
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya520 authored Jan 16, 2024
1 parent de2dece commit 07b57c3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
44 changes: 35 additions & 9 deletions packages/store/test/FieldLayout.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Test, console } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { FieldLayout, FieldLayoutLib } from "../src/FieldLayout.sol";
import { FieldLayoutEncodeHelper } from "./FieldLayoutEncodeHelper.sol";
import { MAX_TOTAL_FIELDS, MAX_DYNAMIC_FIELDS } from "../src/constants.sol";

// TODO add tests for all lengths
contract FieldLayoutTest is Test, GasReporter {
Expand Down Expand Up @@ -69,13 +70,14 @@ contract FieldLayoutTest is Test, GasReporter {
fieldLayout[20] = 32;
fieldLayout[21] = 32;
fieldLayout[22] = 32;
FieldLayout encodedFieldLayout = FieldLayoutLib.encode(fieldLayout, 5);
FieldLayout encodedFieldLayout = FieldLayoutLib.encode(fieldLayout, MAX_DYNAMIC_FIELDS);

assertEq(encodedFieldLayout.numStaticFields() + encodedFieldLayout.numDynamicFields(), 28);
}

function testFailEncodeTooLong() public pure {
function testEncodeTooLong() public {
uint256[] memory fieldLayout = new uint256[](17);
uint256 dynamicFields = 12;
fieldLayout[0] = 32;
fieldLayout[1] = 32;
fieldLayout[2] = 32;
Expand All @@ -93,19 +95,34 @@ contract FieldLayoutTest is Test, GasReporter {
fieldLayout[14] = 32;
fieldLayout[15] = 32;
fieldLayout[16] = 32;
FieldLayoutLib.encode(fieldLayout, 12);
vm.expectRevert(
abi.encodeWithSelector(
FieldLayoutLib.FieldLayoutLib_TooManyFields.selector,
fieldLayout.length + dynamicFields,
MAX_TOTAL_FIELDS
)
);
FieldLayoutLib.encode(fieldLayout, dynamicFields);
}

function testEncodeMaxValidDynamic() public {
uint256[] memory fieldLayout = new uint256[](0);
FieldLayout encodedFieldLayout = FieldLayoutLib.encode(fieldLayout, 5);
FieldLayout encodedFieldLayout = FieldLayoutLib.encode(fieldLayout, MAX_DYNAMIC_FIELDS);

assertEq(encodedFieldLayout.numDynamicFields(), 5);
assertEq(encodedFieldLayout.numDynamicFields(), MAX_DYNAMIC_FIELDS);
}

function testFailEncodeTooManyDynamic() public pure {
function testEncodeTooManyDynamic() public {
uint256[] memory fieldLayout = new uint256[](0);
FieldLayoutLib.encode(fieldLayout, 6);
uint256 dynamicFields = 6;
vm.expectRevert(
abi.encodeWithSelector(
FieldLayoutLib.FieldLayoutLib_TooManyDynamicFields.selector,
fieldLayout.length + dynamicFields,
MAX_DYNAMIC_FIELDS
)
);
FieldLayoutLib.encode(fieldLayout, dynamicFields);
}

function testGetStaticFieldLayoutLength() public {
Expand Down Expand Up @@ -173,14 +190,23 @@ contract FieldLayoutTest is Test, GasReporter {
fieldLayout[20] = 32;
fieldLayout[21] = 32;
fieldLayout[22] = 32;
FieldLayout encodedFieldLayout = FieldLayoutLib.encode(fieldLayout, 5);
FieldLayout encodedFieldLayout = FieldLayoutLib.encode(fieldLayout, MAX_DYNAMIC_FIELDS);

startGasReport("validate field layout");
encodedFieldLayout.validate();
endGasReport();
}

function testFailValidate() public pure {
function testValidateInvalidLayout() public {
FieldLayout encodedFieldLayout = FieldLayout.wrap(keccak256("some invalid field layout"));

vm.expectRevert(
abi.encodeWithSelector(
FieldLayoutLib.FieldLayoutLib_TooManyDynamicFields.selector,
encodedFieldLayout.numDynamicFields(),
MAX_DYNAMIC_FIELDS
)
);
FieldLayout.wrap(keccak256("some invalid field layout")).validate();
}

Expand Down
17 changes: 13 additions & 4 deletions packages/store/test/Schema.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ contract SchemaTest is Test, GasReporter {
assertEq(uint8(schema.atIndex(5)), uint8(SchemaType.UINT32_ARRAY));
}

function testFailInvalidSchemaStaticAfterDynamic() public pure {
function testInvalidSchemaStaticAfterDynamic() public {
vm.expectRevert(abi.encodeWithSelector(SchemaLib.SchemaLib_StaticTypeAfterDynamicType.selector));
SchemaEncodeHelper.encode(SchemaType.UINT8, SchemaType.UINT32_ARRAY, SchemaType.UINT16);
}

Expand Down Expand Up @@ -75,7 +76,7 @@ contract SchemaTest is Test, GasReporter {
assertEq(encodedSchema.numStaticFields() + encodedSchema.numDynamicFields(), 28);
}

function testFailEncodeTooLong() public pure {
function testEncodeTooLong() public {
SchemaType[] memory schema = new SchemaType[](29);
schema[0] = SchemaType.UINT256;
schema[1] = SchemaType.UINT256;
Expand Down Expand Up @@ -106,6 +107,7 @@ contract SchemaTest is Test, GasReporter {
schema[26] = SchemaType.UINT32_ARRAY;
schema[27] = SchemaType.UINT32_ARRAY;
schema[28] = SchemaType.UINT32_ARRAY;
vm.expectRevert(abi.encodeWithSelector(SchemaLib.SchemaLib_InvalidLength.selector, schema.length));
SchemaLib.encode(schema);
}

Expand All @@ -121,14 +123,15 @@ contract SchemaTest is Test, GasReporter {
assertEq(encodedSchema.numDynamicFields(), 5);
}

function testFailEncodeTooManyDynamic() public pure {
function testEncodeTooManyDynamic() public {
SchemaType[] memory schema = new SchemaType[](6);
schema[0] = SchemaType.UINT32_ARRAY;
schema[1] = SchemaType.UINT32_ARRAY;
schema[2] = SchemaType.UINT32_ARRAY;
schema[3] = SchemaType.UINT32_ARRAY;
schema[4] = SchemaType.UINT32_ARRAY;
schema[5] = SchemaType.UINT32_ARRAY;
vm.expectRevert(abi.encodeWithSelector(SchemaLib.SchemaLib_InvalidLength.selector, schema.length));
SchemaLib.encode(schema);
}

Expand Down Expand Up @@ -237,7 +240,13 @@ contract SchemaTest is Test, GasReporter {
endGasReport();
}

function testFailValidate() public pure {
function testValidateInvalidSchema() public {
Schema encodedSchema = Schema.wrap(keccak256("some invalid schema"));

vm.expectRevert(
abi.encodeWithSelector(SchemaLib.SchemaLib_InvalidLength.selector, encodedSchema.numDynamicFields())
);

Schema.wrap(keccak256("some invalid schema")).validate({ allowEmpty: false });
}

Expand Down

0 comments on commit 07b57c3

Please sign in to comment.