-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtest.rs
312 lines (237 loc) · 7.94 KB
/
gtest.rs
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
use demo_client::{
counter::events::CounterEvents,
dog::events::DogEvents,
ping_pong,
traits::{Counter, DemoFactory, Dog, References},
};
use fixture::Fixture;
use futures::stream::StreamExt;
use sails_rs::{calls::*, events::*};
mod fixture;
#[tokio::test]
async fn counter_add_works() {
// Arrange
let fixture = Fixture::new(fixture::ADMIN_ID);
let demo_factory = fixture.demo_factory();
// Use generated client code for activating Demo program
// using the `new` constructor and the `send_recv` method
let demo_program_id = demo_factory
.new(Some(42), None)
.send_recv(fixture.demo_code_id(), "123")
.await
.unwrap();
let mut counter_client = fixture.counter_client();
// Listen to Counter events
let mut counter_listener = fixture.counter_listener();
let mut counter_events = counter_listener.listen().await.unwrap();
// Act
// Use generated client code for calling Counter service
// using the `send_recv` method
let result = counter_client
.add(10)
.send_recv(demo_program_id)
.await
.unwrap();
// Asert
let event = counter_events.next().await.unwrap();
assert_eq!(result, 52);
assert_eq!((demo_program_id, CounterEvents::Added(10)), event);
}
#[tokio::test]
async fn counter_sub_works() {
// Arrange
let fixture = Fixture::new(fixture::ADMIN_ID);
let demo_factory = fixture.demo_factory();
// Use generated client code for activating Demo program
// using the `new` constructor and the `send`/`recv` pair
// of methods
let activation = demo_factory
.new(Some(42), None)
.send(fixture.demo_code_id(), "123")
.await
.unwrap();
let demo_program_id = activation.recv().await.unwrap();
let mut counter_client = fixture.counter_client();
// Listen to Counter events
let mut counter_listener = fixture.counter_listener();
let mut counter_events = counter_listener.listen().await.unwrap();
// Act
// Use generated client code for calling Counter service
// using the `send`/`recv` pair of methods
let response = counter_client.sub(10).send(demo_program_id).await.unwrap();
let result = response.recv().await.unwrap();
// Assert
let event = counter_events.next().await.unwrap();
assert_eq!(result, 32);
assert_eq!((demo_program_id, CounterEvents::Subtracted(10)), event);
}
#[tokio::test]
async fn ping_pong_works() {
let fixture = Fixture::new(fixture::ADMIN_ID);
let demo_factory = fixture.demo_factory();
// Use generated client code for activating Demo program
// using the `default` constructor and the `send_recv` method
let demo_program_id = demo_factory
.default()
.send_recv(fixture.demo_code_id(), "123")
.await
.unwrap();
let demo_program = fixture.demo_program(demo_program_id);
// Use generated `io` module for encoding/decoding calls and replies
// and send/receive bytes using `gtest` native means
let ping_call_payload = ping_pong::io::Ping::encode_call("ping".into());
let run_result = demo_program.send_bytes(fixture.admin_id(), ping_call_payload);
let reply_log_record = run_result
.log()
.iter()
.find(|entry| entry.reply_to() == Some(run_result.sent_message_id()))
.unwrap();
let ping_reply_payload = reply_log_record.payload();
let ping_reply = ping_pong::io::Ping::decode_reply(ping_reply_payload).unwrap();
assert_eq!(ping_reply, Ok("pong".to_string()));
}
#[tokio::test]
async fn dog_barks() {
// Arrange
let fixture = Fixture::new(fixture::ADMIN_ID);
let demo_factory = fixture.demo_factory();
let demo_program_id = demo_factory
.new(None, Some((1, -1)))
.send_recv(fixture.demo_code_id(), "123")
.await
.unwrap();
let mut dog_client = fixture.dog_client();
let mut dog_listener = fixture.dog_listener();
let mut dog_events = dog_listener.listen().await.unwrap();
// Act
let result = dog_client
.make_sound()
.send_recv(demo_program_id)
.await
.unwrap();
// Assert
let event = dog_events.next().await.unwrap();
assert_eq!(result, "Woof! Woof!");
assert_eq!((demo_program_id, DogEvents::Barked), event);
}
#[tokio::test]
async fn dog_walks() {
// Arrange
let fixture = Fixture::new(fixture::ADMIN_ID);
let demo_factory = fixture.demo_factory();
let demo_program_id = demo_factory
.new(None, Some((1, -1)))
.send_recv(fixture.demo_code_id(), "123")
.await
.unwrap();
let mut dog_client = fixture.dog_client();
let mut dog_listener = fixture.dog_listener();
let mut dog_events = dog_listener.listen().await.unwrap();
// Act
dog_client
.walk(10, 20)
.send_recv(demo_program_id)
.await
.unwrap();
// Assert
let position = dog_client.position().recv(demo_program_id).await.unwrap();
let event = dog_events.next().await.unwrap();
assert_eq!(position, (11, 19));
assert_eq!(
(
demo_program_id,
DogEvents::Walked {
from: (1, -1),
to: (11, 19)
}
),
event
);
}
#[tokio::test]
async fn dog_weights() {
let fixture = Fixture::new(fixture::ADMIN_ID);
let demo_factory = fixture.demo_factory();
let demo_program_id = demo_factory
.new(None, Some((1, -1)))
.send_recv(fixture.demo_code_id(), "123")
.await
.unwrap();
let dog_client = fixture.dog_client();
let avg_weight = dog_client.avg_weight().recv(demo_program_id).await.unwrap();
assert_eq!(avg_weight, 42);
}
#[tokio::test]
async fn references_add() {
let fixture = Fixture::new(fixture::ADMIN_ID);
let demo_factory = fixture.demo_factory();
let demo_program_id = demo_factory
.new(None, Some((1, -1)))
.send_recv(fixture.demo_code_id(), "123")
.await
.unwrap();
let mut client = fixture.references_client();
let value = client.add(42).send_recv(demo_program_id).await.unwrap();
assert_eq!(42, value);
}
#[tokio::test]
async fn references_bytes() {
let fixture = Fixture::new(fixture::ADMIN_ID);
let demo_factory = fixture.demo_factory();
let demo_program_id = demo_factory
.new(None, Some((1, -1)))
.send_recv(fixture.demo_code_id(), "123")
.await
.unwrap();
let mut client = fixture.references_client();
_ = client
.add_byte(42)
.send_recv(demo_program_id)
.await
.unwrap();
_ = client
.add_byte(89)
.send_recv(demo_program_id)
.await
.unwrap();
_ = client
.add_byte(14)
.send_recv(demo_program_id)
.await
.unwrap();
let last = client.last_byte().recv(demo_program_id).await.unwrap();
assert_eq!(Some(14), last);
}
#[tokio::test]
async fn references_guess_num() {
let fixture = Fixture::new(fixture::ADMIN_ID);
let demo_factory = fixture.demo_factory();
let demo_program_id = demo_factory
.new(None, Some((1, -1)))
.send_recv(fixture.demo_code_id(), "123")
.await
.unwrap();
let mut client = fixture.references_client();
let res1 = client
.guess_num(42)
.send_recv(demo_program_id)
.await
.unwrap();
let res2 = client
.guess_num(89)
.send_recv(demo_program_id)
.await
.unwrap();
let res3 = client.message().recv(demo_program_id).await.unwrap();
let res4 = client.set_num(14).send_recv(demo_program_id).await.unwrap();
let res5 = client
.guess_num(14)
.send_recv(demo_program_id)
.await
.unwrap();
assert_eq!(Ok("demo".to_owned()), res1);
assert_eq!(Err("Number is too large".to_owned()), res2);
assert_eq!(Some("demo".to_owned()), res3);
assert_eq!(Ok(()), res4);
assert_eq!(Ok("demo".to_owned()), res5);
}