/
common.test.ts
83 lines (74 loc) · 4.11 KB
/
common.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { describe, expect, it } from "vitest";
import { getLeftPaddingBits, isLeftAligned, renderTableId, renderValueTypeToBytes32 } from "./common";
describe("isLeftAligned", () => {
it("returns true when a Solidity type's bytes should be left aligned", () => {
expect(isLeftAligned({ internalTypeId: "bytes32" })).toBe(true);
expect(isLeftAligned({ internalTypeId: "uint256" })).toBe(false);
expect(isLeftAligned({ internalTypeId: "bool" })).toBe(false);
});
});
describe("getLeftPaddingBits", () => {
it("returns number of bits to left pad the bytes representation", () => {
expect(getLeftPaddingBits({ internalTypeId: "bytes32", staticByteLength: 32 })).toBe(0);
expect(getLeftPaddingBits({ internalTypeId: "bytes16", staticByteLength: 16 })).toBe(0);
expect(getLeftPaddingBits({ internalTypeId: "uint32", staticByteLength: 4 })).toBe(224);
expect(getLeftPaddingBits({ internalTypeId: "uint64", staticByteLength: 8 })).toBe(192);
expect(getLeftPaddingBits({ internalTypeId: "uint128", staticByteLength: 16 })).toBe(128);
});
});
describe("renderValueTypeToBytes32", () => {
it("returns Solidity code to cast native type to bytes32", () => {
expect(renderValueTypeToBytes32("someField", { typeUnwrap: "", internalTypeId: "bytes32" })).toMatchInlineSnapshot(
'"someField"'
);
expect(
renderValueTypeToBytes32("someField", { typeUnwrap: "SomeStruct.unwrap", internalTypeId: "bytes32" })
).toMatchInlineSnapshot('"SomeStruct.unwrap(someField)"');
expect(renderValueTypeToBytes32("someField", { typeUnwrap: "", internalTypeId: "bytes16" })).toMatchInlineSnapshot(
'"bytes32(someField)"'
);
expect(
renderValueTypeToBytes32("someField", { typeUnwrap: "SomeStruct.unwrap", internalTypeId: "bytes16" })
).toMatchInlineSnapshot('"bytes32(SomeStruct.unwrap(someField))"');
expect(renderValueTypeToBytes32("someField", { typeUnwrap: "", internalTypeId: "uint8" })).toMatchInlineSnapshot(
'"bytes32(uint256(someField))"'
);
expect(
renderValueTypeToBytes32("someField", { typeUnwrap: "SomeStruct.unwrap", internalTypeId: "uint8" })
).toMatchInlineSnapshot('"bytes32(uint256(SomeStruct.unwrap(someField)))"');
expect(renderValueTypeToBytes32("someField", { typeUnwrap: "", internalTypeId: "bool" })).toMatchInlineSnapshot(
'"_boolToBytes32(someField)"'
);
expect(
renderValueTypeToBytes32("someField", { typeUnwrap: "SomeStruct.unwrap", internalTypeId: "bool" })
).toMatchInlineSnapshot('"_boolToBytes32(SomeStruct.unwrap(someField))"');
expect(renderValueTypeToBytes32("someField", { typeUnwrap: "", internalTypeId: "address" })).toMatchInlineSnapshot(
'"bytes32(uint256(uint160(someField)))"'
);
expect(
renderValueTypeToBytes32("someField", { typeUnwrap: "SomeStruct.unwrap", internalTypeId: "address" })
).toMatchInlineSnapshot('"bytes32(uint256(uint160(SomeStruct.unwrap(someField))))"');
});
});
describe("renderTableId", () => {
it("returns Solidity code to compute table ID", () => {
expect(renderTableId({ namespace: "somewhere", name: "Player", offchainOnly: false, tableIdName: "PlayerTableId" }))
.toMatchInlineSnapshot(`
"
// Hex below is the result of \`WorldResourceIdLib.encode({ namespace: \\"somewhere\\", name: \\"Player\\", typeId: RESOURCE_TABLE });\`
ResourceId constant _tableId = ResourceId.wrap(0x7462736f6d6577686572650000000000506c6179657200000000000000000000);
ResourceId constant PlayerTableId = _tableId;
"
`);
});
it("returns Solidity code to compute offchain table ID", () => {
expect(renderTableId({ namespace: "somewhere", name: "Player", offchainOnly: true, tableIdName: "PlayerTableId" }))
.toMatchInlineSnapshot(`
"
// Hex below is the result of \`WorldResourceIdLib.encode({ namespace: \\"somewhere\\", name: \\"Player\\", typeId: RESOURCE_OFFCHAIN_TABLE });\`
ResourceId constant _tableId = ResourceId.wrap(0x6f74736f6d6577686572650000000000506c6179657200000000000000000000);
ResourceId constant PlayerTableId = _tableId;
"
`);
});
});