-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcounter_test.ts
117 lines (101 loc) · 3.33 KB
/
counter_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
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
import {
Account,
Chain,
Clarinet,
Tx,
types,
} from "https://deno.land/x/[email protected]/index.ts";
// https://github.com/hirosystems/clarity-starter/blob/aa4a7e31a73ab8bb23a813d92598d5eba8876d1c/tests/counter_test.ts
Clarinet.test({
name: "ensure <get-counter> send the counter value",
fn(chain: Chain, accounts: Map<string, Account>) {
const { address } = accounts.get("wallet_1")!;
const block = chain.mineBlock([
Tx.contractCall("counter", "get-counter", [], address),
]);
block.receipts[0].result.expectUint(0);
},
});
Clarinet.test({
name: "ensure <increment> adds 1",
fn(chain: Chain, accounts: Map<string, Account>) {
const { address } = accounts.get("wallet_1")!;
const block = chain.mineBlock([
Tx.contractCall("counter", "increment", [], address),
Tx.contractCall("counter", "get-counter", [], address),
]);
block.receipts[0].result.expectOk().expectBool(true);
block.receipts[1].result.expectUint(1);
},
});
Clarinet.test({
name: "ensure <decrement> removes 1",
fn(chain: Chain, accounts: Map<string, Account>) {
const { address } = accounts.get("wallet_1")!;
const block = chain.mineBlock([
Tx.contractCall("counter", "increment", [], address),
Tx.contractCall("counter", "decrement", [], address),
Tx.contractCall("counter", "get-counter", [], address),
]);
block.receipts[1].result.expectOk().expectBool(true);
block.receipts[2].result.expectUint(0);
},
});
Clarinet.test({
name: "ensure <decrement> throws an error if result is lower than 0",
fn(chain: Chain, accounts: Map<string, Account>) {
const { address } = accounts.get("wallet_1")!;
const block = chain.mineBlock([
Tx.contractCall("counter", "decrement", [], address),
]);
block.receipts[0].result.expectErr().expectUint(401);
},
});
Clarinet.test({
name: "ensure <add> adds up the right amout",
fn(chain: Chain, accounts: Map<string, Account>) {
const { address } = accounts.get("wallet_1")!;
const block = chain.mineBlock([
Tx.contractCall("counter", "add", [types.uint(3)], address),
Tx.contractCall("counter", "get-counter", [], address),
]);
block.receipts[0].result.expectOk().expectBool(true);
block.receipts[1].result.expectUint(3);
},
});
Clarinet.test({
name: "ensure <add> throws an error if n is too low",
fn(chain: Chain, accounts: Map<string, Account>) {
const { address } = accounts.get("wallet_1")!;
const block = chain.mineBlock([
Tx.contractCall("counter", "add", [types.uint(1)], address),
]);
block.receipts[0].result.expectErr().expectUint(402);
},
});
import fc from "https://cdn.skypack.dev/[email protected]";
import { CounterCommands } from "./CounterCommands.ts";
// See also:
// https://github.com/dubzzz/fast-check/discussions/3026#discussioncomment-3875818
Clarinet.test({
name: "invariant tests",
fn(chain: Chain, accounts: Map<string, Account>) {
const initialChain = { chain: chain };
const initialModel = {
counter: 0,
};
fc.assert(
fc.property(
CounterCommands(accounts),
(cmds: []) => {
const initialState = () => ({
model: initialModel,
real : initialChain,
});
fc.modelRun(initialState, cmds);
},
),
{ numRuns: 100, verbose: true },
);
},
});