Skip to content

Commit

Permalink
add tests for schema encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Feb 28, 2024
1 parent da595e4 commit 39fa542
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
15 changes: 15 additions & 0 deletions packages/store/test/Callbacks.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Test } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { Callbacks } from "./codegen/tables/Callbacks.sol";
import { StoreMock } from "./StoreMock.sol";
import { Schema, SchemaLib, SchemaType } from "../src/Schema.sol";

contract CallbacksTest is Test, GasReporter, StoreMock {
function testSetAndGet() public {
Expand Down Expand Up @@ -34,4 +35,18 @@ contract CallbacksTest is Test, GasReporter, StoreMock {
assertEq(returnedCallbacks.length, 2);
assertEq(returnedCallbacks[1], callbacks[0]);
}

function testKeySchemaEncoding() public {
SchemaType[] memory _keySchema = new SchemaType[](1);
_keySchema[0] = SchemaType.BYTES32;

assertEq(Schema.unwrap(SchemaLib.encode(_keySchema)), Schema.unwrap(Callbacks._keySchema));
}

function testValueSchemaEncoding() public {
SchemaType[] memory _valueSchema = new SchemaType[](1);
_valueSchema[0] = SchemaType.BYTES24_ARRAY;

assertEq(Schema.unwrap(SchemaLib.encode(_valueSchema)), Schema.unwrap(Callbacks._valueSchema));
}
}
21 changes: 20 additions & 1 deletion packages/store/test/KeyEncoding.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Test } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { StoreCore } from "../src/StoreCore.sol";
import { FieldLayout } from "../src/FieldLayout.sol";
import { Schema } from "../src/Schema.sol";
import { Schema, SchemaLib, SchemaType } from "../src/Schema.sol";

import { StoreMock } from "./StoreMock.sol";
import { KeyEncoding } from "./codegen/index.sol";
Expand Down Expand Up @@ -87,4 +87,23 @@ contract KeyEncodingTest is Test, GasReporter, StoreMock {
assertEq(keyTuple[4], bytes32(abi.encode(true)));
assertEq(keyTuple[5], bytes32(abi.encode(ExampleEnum.Third)));
}

function testKeySchemaEncoding() public {
SchemaType[] memory _keySchema = new SchemaType[](6);
_keySchema[0] = SchemaType.UINT256;
_keySchema[1] = SchemaType.INT32;
_keySchema[2] = SchemaType.BYTES16;
_keySchema[3] = SchemaType.ADDRESS;
_keySchema[4] = SchemaType.BOOL;
_keySchema[5] = SchemaType.UINT8;

assertEq(Schema.unwrap(SchemaLib.encode(_keySchema)), Schema.unwrap(KeyEncoding._keySchema));
}

function testValueSchemaEncoding() public {
SchemaType[] memory _valueSchema = new SchemaType[](1);
_valueSchema[0] = SchemaType.BOOL;

assertEq(Schema.unwrap(SchemaLib.encode(_valueSchema)), Schema.unwrap(KeyEncoding._valueSchema));
}
}
19 changes: 18 additions & 1 deletion packages/store/test/Mixed.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { StoreCore } from "../src/StoreCore.sol";
import { StoreMock } from "../test/StoreMock.sol";
import { FieldLayout } from "../src/FieldLayout.sol";
import { Schema } from "../src/Schema.sol";
import { Schema, SchemaLib, SchemaType } from "../src/Schema.sol";
import { PackedCounter } from "../src/PackedCounter.sol";

import { Mixed, MixedData } from "./codegen/index.sol";
Expand Down Expand Up @@ -130,4 +130,21 @@ contract MixedTest is Test, GasReporter, StoreMock {
assertEq(encodedLengths.unwrap(), hex"000000000000000000000000000000000000000b000000000800000000000013");
assertEq(dynamicData, hex"0000000300000004736f6d6520737472696e67");
}

function testKeySchemaEncoding() public {
SchemaType[] memory _keySchema = new SchemaType[](1);
_keySchema[0] = SchemaType.BYTES32;

assertEq(Schema.unwrap(SchemaLib.encode(_keySchema)), Schema.unwrap(Mixed._keySchema));
}

function testValueSchemaEncoding() public {
SchemaType[] memory _valueSchema = new SchemaType[](4);
_valueSchema[0] = SchemaType.UINT32;
_valueSchema[1] = SchemaType.UINT128;
_valueSchema[2] = SchemaType.UINT32_ARRAY;
_valueSchema[3] = SchemaType.STRING;

assertEq(Schema.unwrap(SchemaLib.encode(_valueSchema)), Schema.unwrap(Mixed._valueSchema));
}
}
17 changes: 16 additions & 1 deletion packages/store/test/Vector2.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { StoreCore } from "../src/StoreCore.sol";
import { StoreMock } from "../test/StoreMock.sol";
import { FieldLayout } from "../src/FieldLayout.sol";
import { Schema } from "../src/Schema.sol";
import { Schema, SchemaLib, SchemaType } from "../src/Schema.sol";

import { Vector2, Vector2Data } from "./codegen/index.sol";

Expand Down Expand Up @@ -46,4 +46,19 @@ contract Vector2Test is Test, GasReporter, StoreMock {
assertEq(vector.x, 1);
assertEq(vector.y, 2);
}

function testKeySchemaEncoding() public {
SchemaType[] memory _keySchema = new SchemaType[](1);
_keySchema[0] = SchemaType.BYTES32;

assertEq(Schema.unwrap(SchemaLib.encode(_keySchema)), Schema.unwrap(Vector2._keySchema));
}

function testValueSchemaEncoding() public {
SchemaType[] memory _valueSchema = new SchemaType[](2);
_valueSchema[0] = SchemaType.UINT32;
_valueSchema[1] = SchemaType.UINT32;

assertEq(Schema.unwrap(SchemaLib.encode(_valueSchema)), Schema.unwrap(Vector2._valueSchema));
}
}

0 comments on commit 39fa542

Please sign in to comment.