-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
executable file
·126 lines (87 loc) · 3.27 KB
/
lib.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
#![cfg_attr(not(feature = "std"), no_std, no_main)]
#[ink::contract]
mod identity {
#[ink(storage)]
pub struct Identity {
value: bool,
}
impl Identity {
#[ink(constructor)]
pub fn new(init_value: bool) -> Self {
Self { value: init_value }
}
#[ink(constructor)]
pub fn default() -> Self {
Self::new(Default::default())
}
#[ink(message)]
pub fn flip(&mut self) {
self.value = !self.value;
}
#[ink(message)]
pub fn get(&self) -> bool {
self.value
}
}
#[cfg(test)]
mod tests {
use super::*;
#[ink::test]
fn default_works() {
let identity = Identity::default();
assert_eq!(identity.get(), false);
}
#[ink::test]
fn it_works() {
let mut identity = Identity::new(false);
assert_eq!(identity.get(), false);
identity.flip();
assert_eq!(identity.get(), true);
}
}
#[cfg(all(test, feature = "e2e-tests"))]
mod e2e_tests {
use super::*;
use ink_e2e::build_message;
type E2EResult<T> = std::result::Result<T, Box<dyn std::error::Error>>;
#[ink_e2e::test]
async fn default_works(mut client: ink_e2e::Client<C, E>) -> E2EResult<()> {
let constructor = IdentityRef::default();
let contract_account_id = client
.instantiate("identity", &ink_e2e::alice(), constructor, 0, None)
.await
.expect("instantiate failed")
.account_id;
let get = build_message::<IdentityRef>(contract_account_id.clone())
.call(|identity| identity.get());
let get_result = client.call_dry_run(&ink_e2e::alice(), &get, 0, None).await;
assert!(matches!(get_result.return_value(), false));
Ok(())
}
#[ink_e2e::test]
async fn it_works(mut client: ink_e2e::Client<C, E>) -> E2EResult<()> {
// Given
let constructor = IdentityRef::new(false);
let contract_account_id = client
.instantiate("identity", &ink_e2e::bob(), constructor, 0, None)
.await
.expect("instantiate failed")
.account_id;
let get = build_message::<IdentityRef>(contract_account_id.clone())
.call(|identity| identity.get());
let get_result = client.call_dry_run(&ink_e2e::bob(), &get, 0, None).await;
assert!(matches!(get_result.return_value(), false));
let flip = build_message::<IdentityRef>(contract_account_id.clone())
.call(|identity| identity.flip());
let _flip_result = client
.call(&ink_e2e::bob(), flip, 0, None)
.await
.expect("flip failed");
let get = build_message::<IdentityRef>(contract_account_id.clone())
.call(|identity| identity.get());
let get_result = client.call_dry_run(&ink_e2e::bob(), &get, 0, None).await;
assert!(matches!(get_result.return_value(), true));
Ok(())
}
}
}