-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshared.tests.ts
145 lines (124 loc) · 4.56 KB
/
shared.tests.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { initSimnet } from "@hirosystems/clarinet-sdk";
import {
getFunctionsFromContractInterfaces,
getFunctionsListForContract,
getSimnetDeployerContractsInterfaces,
hexaString,
getContractNameFromContractId,
} from "./shared";
import { resolve } from "path";
import fc from "fast-check";
describe("Simnet contracts operations", () => {
it("retrieves the contracts from the simnet", async () => {
// Arrange
const manifestPath = resolve(__dirname, "./example/Clarinet.toml");
const simnet = await initSimnet(manifestPath);
const expectedDeployerContracts = new Map(
Array.from(simnet.getContractsInterfaces()).filter(
([key]) => key.split(".")[0] === simnet.deployer
)
);
// Act
const actualDeployerContracts =
getSimnetDeployerContractsInterfaces(simnet);
// Assert
expect(actualDeployerContracts).toEqual(expectedDeployerContracts);
});
it("retrieves the contract functions from the simnet", async () => {
// Arrange
const manifestPath = resolve(__dirname, "./example/Clarinet.toml");
const simnet = await initSimnet(manifestPath);
const sutContractsInterfaces = getSimnetDeployerContractsInterfaces(simnet);
const sutContractsList = Array.from(sutContractsInterfaces.keys());
const allFunctionsMap = new Map(
Array.from(sutContractsInterfaces, ([contractId, contractInterface]) => [
contractId,
contractInterface.functions,
])
);
const expectedContractFunctionsList = sutContractsList.map(
(contractId) => allFunctionsMap.get(contractId) || []
);
// Act
const actualContractFunctionsList = sutContractsList.map((contractId) =>
getFunctionsListForContract(allFunctionsMap, contractId)
);
// Assert
expect(actualContractFunctionsList).toEqual(expectedContractFunctionsList);
});
it("extracts the functions from the contract interfaces", async () => {
// Arrange
const manifestPath = resolve(__dirname, "./example/Clarinet.toml");
const simnet = await initSimnet(manifestPath);
const sutContractsInterfaces = getSimnetDeployerContractsInterfaces(simnet);
const expectedAllFunctionsMap = new Map(
Array.from(sutContractsInterfaces, ([contractId, contractInterface]) => [
contractId,
contractInterface.functions,
])
);
// Act
const actualAllFunctionsMap = getFunctionsFromContractInterfaces(
sutContractsInterfaces
);
// Assert
expect(actualAllFunctionsMap).toEqual(expectedAllFunctionsMap);
});
});
describe("Fast-check deprecated generators replacement validation", () => {
it("string-ascii Clarity type corresponding generator", () => {
// Arrange
const charSet =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
const size = 100;
const seed = Math.floor(Math.random() * size);
const outdated = fc.stringOf(fc.constantFrom(...charSet), {
maxLength: size,
minLength: 0,
});
const proposed = fc.string({
unit: fc.constantFrom(...charSet),
maxLength: size,
});
// Act
const a: string[] = fc.sample(outdated, { seed: seed });
const b: string[] = fc.sample(proposed, { seed: seed });
// Assert
// Strict, same-order comparison.
expect(JSON.stringify(a)).toEqual(JSON.stringify(b));
});
it("buff Clarity type corresponding generator", () => {
// Arrange
const size = 100;
const seed = Math.floor(Math.random() * size);
const outdated = fc.hexaString({ maxLength: size });
const proposed = hexaString({ maxLength: size });
// Act
const a: string[] = fc.sample(outdated, { seed: seed });
const b: string[] = fc.sample(proposed, { seed: seed });
// Assert
// Strict, same-order comparison.
expect(JSON.stringify(a)).toEqual(JSON.stringify(b));
});
});
describe("Contract identifier parsing", () => {
it("gets correct contract name from contract identifier", () => {
const addressCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
const contractNameCharset =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
fc.assert(
// Arrange
fc.property(
fc.string({ unit: fc.constantFrom(...addressCharset) }),
fc.string({ unit: fc.constantFrom(...contractNameCharset) }),
(address, contractName) => {
const contractId = `${address}.${contractName}`;
// Act
const actual = getContractNameFromContractId(contractId);
// Assert
expect(actual).toBe(contractName);
}
)
);
});
});