Skip to content

Commit

Permalink
move SystemSwitch to utils, add test for getUniqueEntity from interna…
Browse files Browse the repository at this point in the history
…l call
  • Loading branch information
alvrs committed Oct 3, 2023
1 parent c0fadbb commit f9d0d36
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ pragma solidity >=0.8.21;
import { IBaseWorld } from "@latticexyz/world/src/codegen/interfaces/IBaseWorld.sol";

import { IUniqueEntitySystem } from "../../interfaces/IUniqueEntitySystem.sol";
import { UniqueEntitySystem } from "./UniqueEntitySystem.sol";

import { SystemSwitch } from "../utils/SystemSwitch.sol";
import { SystemSwitch } from "../../utils/SystemSwitch.sol";
import { SYSTEM_ID } from "./constants.sol";

/**
Expand All @@ -15,7 +16,7 @@ import { SYSTEM_ID } from "./constants.sol";
* For usage outside of a World, use the overload that takes an explicit store argument.
*/
function getUniqueEntity() returns (bytes32 uniqueEntity) {
return abi.decode(SystemSwitch.call(SYSTEM_ID, new bytes(0)), (bytes32));
return abi.decode(SystemSwitch.call(SYSTEM_ID, abi.encodeCall(UniqueEntitySystem.getUniqueEntity, ())), (bytes32));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/world-modules/test/SystemSwitch.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { CoreModule } from "@latticexyz/world/src/modules/core/CoreModule.sol";
import { ResourceId, WorldResourceIdLib } from "@latticexyz/world/src/WorldResourceId.sol";
import { RESOURCE_SYSTEM } from "@latticexyz/world/src/worldResourceTypes.sol";
import { ROOT_NAMESPACE } from "@latticexyz/world/src/constants.sol";
import { SystemSwitch } from "../src/modules/utils/SystemSwitch.sol";
import { SystemSwitch } from "../src/utils/SystemSwitch.sol";

contract EchoSystem is System {
function msgSender() public view returns (address) {
Expand Down
37 changes: 37 additions & 0 deletions packages/world-modules/test/UniqueEntityModule.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { World } from "@latticexyz/world/src/World.sol";
import { IBaseWorld } from "@latticexyz/world/src/codegen/interfaces/IBaseWorld.sol";
import { IWorldErrors } from "@latticexyz/world/src/IWorldErrors.sol";
import { System } from "@latticexyz/world/src/System.sol";
import { RESOURCE_SYSTEM } from "@latticexyz/world/src/worldResourceTypes.sol";

import { CoreModule } from "@latticexyz/world/src/modules/core/CoreModule.sol";
import { UniqueEntityModule } from "../src/modules/uniqueentity/UniqueEntityModule.sol";
Expand All @@ -17,6 +19,13 @@ import { NAMESPACE, TABLE_NAME } from "../src/modules/uniqueentity/constants.sol
import { ResourceId, WorldResourceIdLib, WorldResourceIdInstance } from "@latticexyz/world/src/WorldResourceId.sol";
import { RESOURCE_TABLE } from "@latticexyz/world/src/worldResourceTypes.sol";

contract UniqueEntityTestSystem is System {
function echoUniqueEntity() public returns (bytes32) {
// Execute `getUniqueEntity` from the context of a World
return getUniqueEntity();
}
}

contract UniqueEntityModuleTest is Test, GasReporter {
using WorldResourceIdInstance for ResourceId;

Expand Down Expand Up @@ -91,4 +100,32 @@ contract UniqueEntityModuleTest is Test, GasReporter {
);
UniqueEntity.set(world, tableId, 123);
}

function testAccessInWorldContext() public {
world.installModule(uniqueEntityModule, new bytes(0));

// Set up a system that calls `getUniqueEntity` from the context of a World
UniqueEntityTestSystem uniqueEntityTestSystem = new UniqueEntityTestSystem();
ResourceId uniqueEntityTestSystemId = WorldResourceIdLib.encode({
typeId: RESOURCE_SYSTEM,
namespace: "somens",
name: "echoUniqueEntity"
});
world.registerSystem(uniqueEntityTestSystemId, uniqueEntityTestSystem, true);

// Execute `getUniqueEntity` from the context of a World
bytes32 uniqueEntity1 = abi.decode(
world.call(uniqueEntityTestSystemId, abi.encodeCall(UniqueEntityTestSystem.echoUniqueEntity, ())),
(bytes32)
);
bytes32 uniqueEntity2 = abi.decode(
world.call(uniqueEntityTestSystemId, abi.encodeCall(UniqueEntityTestSystem.echoUniqueEntity, ())),
(bytes32)
);
bytes32 uniqueEntity3 = getUniqueEntity(world);

// The next entity must be incremented
assertEq(uint256(uniqueEntity2), uint256(uniqueEntity1) + 1);
assertEq(uint256(uniqueEntity3), uint256(uniqueEntity2) + 1);
}
}

0 comments on commit f9d0d36

Please sign in to comment.