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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
/// Demystify Property-Based Testing
///
/// Arnaud Bailly
///
/// @abailly.bsky.social - @abailly@piaille.fr - @dr_c0d3 (X)
///
/// https://www.linkedin.com/in/arnaudbailly/
///
/// https://abailly.github.io
///
/// An experiment and an experience
///
/// https://app.radicle.xyz/nodes/seed.hydra.bzh/rad:z28SSH7hyCTu387guFhtPXYxcwtUs/tree/pbt/README.md
///
/// 1. Introduction
///
/// 2. Real world use cases
///
/// 3. Developing a mini-quickcheck from a "concrete" use case
///
import {Option, fromNullable, matchW, toNullable} from 'fp-ts/lib/Option.js';
// keep it simple
export type Account = string;
/// A basic "Adapter" to a banking system
///
/// NOTE: we make the functions Promise-aware in order to support
/// async/await easily.
export interface Bank {
/// Retrieve the balance of given account.
///
/// @param account The account to retrieve balance of
/// @return: nothing if account is unknown, its balance otherwise
balance: (account: Account) => Promise<Option<number>>;
/// Deposit amount into account
///
/// This function increases the balance of the given account by the
/// specified amount. It's a `void` function hence implicitly
/// stateful: It's expected to change some internal state of the
/// implementing class.
///
/// @param account The account to deposit into
/// @param amt The amount to deposit
deposit: (account: Account, amt: number) => Promise<void>;
/// Withdraw amount from given `account`.
///
/// This function decreases the balance of the given account by the
/// specified amount.
///
/// @param account The account to deposit into
/// @param amt The amount to withdraw
withdraw: (account: Account, amt: number) => Promise<void>;
}
/// All possible operations
export type Operation = GetBalance | Withdraw | Deposit;
export type GetBalance = {op: 'GetBalance'; account: Account};
export type Withdraw = {op: 'Withdraw'; account: Account; amount: number};
export type Deposit = {op: 'Deposit'; account: Account; amount: number};
//
//
//
//
//
//
/// A model interpreting bank operations on a simple in-memory map.
/// Interpreting a sequence of operations on the model will return an
/// updated state (map of accounts) and a sequence of observed outputs.
export function model(
accounts: Map<Account, number>,
): (ops: Operation[]) => [Map<Account, number>, (number | null)[]] {
return (ops: Operation[]) => {
const current = new Map(accounts);
const outputs: (number | null)[] = [];
for (const op of ops) {
switch (op.op) {
case 'GetBalance': {
outputs.push(current.get(op.account) || null);
break;
}
case 'Deposit': {
const bal = current.get(op.account);
if (bal) {
current.set(op.account, bal + op.amount);
}
break;
}
case 'Withdraw': {
const bal = current.get(op.account);
if (bal) {
current.set(op.account, bal - op.amount);
}
break;
}
}
}
return [current, outputs];
};
}
/// A predicate asserting that some `Bank` implementation matches
/// our model.
export const bankImplementationMatchesModel =
(
initialAccounts: Map<Account, number>, // Some initial dataset to work with
) =>
(
bank: Bank, // The actual bank implementation we want to test
) =>
async (
ops: Operation[], // an (arbitrary) sequence of operations
): Promise<boolean> => {
// run operations on bank implementation
const observedOutputs: (number | null)[] = [];
for (const op of ops) {
switch (op.op) {
case 'Deposit':
await bank.deposit(op.account, op.amount);
break;
case 'Withdraw':
await bank.withdraw(op.account, op.amount);
break;
case 'GetBalance':
observedOutputs.push(await bank.balance(op.account).then(toNullable));
break;
}
}
// evaluate operations on model
const [endBalances, outputs] = model(initialAccounts)(ops);
// compare outputs
for (const [expected, actual] of outputs.map((v, i) => [
v,
observedOutputs[i],
])) {
if (expected !== actual) {
console.log(`Output mismatch: expected ${expected}, got ${actual}`);
return false;
}
}
// compare end balances
for (const [account, expected] of endBalances.entries()) {
const actual = await bank.balance(account).then(toNullable);
if (actual !== expected) {
console.log(
`Final balance mismatch for account ${account}: expected ${expected}, got ${actual}`,
);
return false;
}
}
return true;
};
//
//
//
//
//
//
//
/// A "simulation" of Bank interface for integration testing purpose.
/// This
export class SimpleBank implements Bank {
private accounts: Map<Account, number>;
constructor(accounts: Map<Account, number>) {
// clone the content of the map
this.accounts = new Map(accounts);
}
balance(account: Account): Promise<Option<number>> {
return Promise.resolve(fromNullable(this.accounts.get(account)));
}
deposit(account: Account, amt: number): Promise<void> {
const current = fromNullable(this.accounts.get(account));
matchW(
() => {
throw new Error(`Unknown account ${account}`);
},
(bal: number) => {
this.accounts.set(account, bal + amt);
},
)(current);
return Promise.resolve();
}
withdraw(account: Account, amt: number): Promise<void> {
const current = fromNullable(this.accounts.get(account));
matchW(
() => {
throw new Error(`Unknown account ${account}`);
},
(bal: number) => {
this.accounts.set(account, bal - amt);
},
)(current);
return Promise.resolve();
}
}
|