-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.rs
209 lines (188 loc) · 6.17 KB
/
common.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
#[cfg(test)]
mod tests {
use std::{fmt::Display, i32};
use yerevan::yer;
// Some simple user-defined structs for compuation expressions
struct SimpleBinder {}
impl SimpleBinder {
pub fn bind<T, U>(val: Option<T>, f: &dyn Fn(T) -> Option<U>) -> Option<U> {
match val {
Some(v) => f(v),
None => SimpleBinder::zero(),
}
}
pub fn ret<T>(val: T) -> Option<T> {
Some(val)
}
pub fn zero<T>() -> Option<T> {
None
}
pub fn ret_yield<T>(val: T) -> Option<Vec<T>> {
Some(vec![val])
}
pub fn combine<T>(val1: Option<Vec<T>>, val2: Option<Vec<T>>) -> Option<Vec<T>> {
match (val1, val2) {
(Some(mut unwrapped1), Some(mut unwrapped2)) => {
unwrapped2.append(&mut unwrapped1);
Some(unwrapped2)
}
_ => None,
}
}
pub fn run<T>(val: Option<T>) -> String
where
T: Display,
{
match val {
Some(v) => format!("SOME: {}", v.to_string()),
None => "NONE".to_string(),
}
}
}
struct Incrementer {}
impl Incrementer {
pub fn bind(val: i32, f: &dyn Fn(i32) -> i32) -> i32 {
f(val + 1)
}
pub fn ret(val: i32) -> i32 {
val
}
}
#[test]
fn check_binding_for_simplebinder() {
let value_from_yer_macro = yer!(
SimpleBinder =>
let! unwrapper1 = Some(1);
let! unwrapped2 = Some(2);
ret unwrapped2 + unwrapper1
);
assert_eq!(
value_from_yer_macro,
SimpleBinder::bind(Some(1), &|unwrapped1| {
SimpleBinder::bind(Some(2), &|unwrapped2| {
SimpleBinder::ret(unwrapped2 + unwrapped1)
})
}),
"Testing macro is returning the same value as the same non-yerevanized expression"
);
assert_eq!(
value_from_yer_macro,
Some(3),
"Testing specific tested yer-macro is returning Some(3) from the sum of binded Some(1) and binded Some(2)"
);
}
#[test]
fn check_binding_for_incrementer() {
let value_from_yer_macro = yer!(
Incrementer =>
let! unwrapper1 = 1; // incrementing to 2
let! unwrapped2 = 2; // incrementing to 3
ret unwrapped2 + unwrapper1 // returning 5
);
assert_eq!(
value_from_yer_macro.clone(),
Incrementer::bind(1, &|unwrapped1| {
Incrementer::bind(2, &|unwrapped2| Incrementer::ret(unwrapped2 + unwrapped1))
}),
"Testing macro is returning the same value as the same non-yerevanized expression"
);
assert_eq!(
value_from_yer_macro, 5,
"Testing specific tested yer-macro is returning 5 from the sum of binded 1 and binded 2"
);
}
#[test]
fn check_zero() {
assert_eq!(
yer!(
SimpleBinder =>
do! None::<()>;
ret ()
),
None,
"Testing not successful returning"
);
}
#[test]
fn check_yielding() {
let value_from_yer_macro = yer!(
SimpleBinder =>
let! some_value = Some(1);
yield some_value; // yielding 1
yield 2; // yielding 2
let some_sum = some_value + 2; // result is 3
yield some_sum; // yielding 3
);
assert_eq!(
value_from_yer_macro,
Some(vec![1, 2, 3]),
"Testing yielding values to one-dimensional vec"
);
}
#[test]
fn check_ce_nesting() {
let value_from_yer_macro = yer!(
SimpleBinder =>
let! unwrapped1 = Some(2); // 2
let! unwrapped2 = Some(3); // 3
let one = 1;
Incrementer =>
let! res = one + unwrapped1 + unwrapped2; // incremented by 1 result of 1 + 2 + 3 = 6 + 1 = 7
ret res // Some(7)
);
assert_eq!(
value_from_yer_macro,
SimpleBinder::bind(Some(2), &|unwrapped1| {
SimpleBinder::bind(Some(3), &|unwrapped2| {
let one = 1;
SimpleBinder::ret(Incrementer::bind(one + unwrapped1 + unwrapped2, &|res| {
Incrementer::ret(res)
}))
})
}),
"Testing macro is returning the same value as the same non-yerevanized expression"
);
assert_eq!(
value_from_yer_macro,
Some(7),
"Testing specific tested yer-macro is returning a correct result"
);
}
#[test]
fn check_ce_run() {
let value_from_yer_macro1 = yer!(
run SimpleBinder =>
let! one = Some(1);
Incrementer =>
let! two = one;
ret two
);
let value_from_yer_macro2 = yer!(
run SimpleBinder =>
let! some_none = Option::<i32>::None;
ret <i32> some_none
);
assert_eq!(
value_from_yer_macro1,
SimpleBinder::run(SimpleBinder::bind(Some(1), &|one| {
SimpleBinder::ret(Incrementer::bind(one, &|two| Incrementer::ret(two)))
})),
"1) Testing macro is returning the same value as the same non-yerevanized expression"
);
assert_eq!(
value_from_yer_macro2,
SimpleBinder::run(SimpleBinder::bind(Option::<i32>::None, &|hello| {
SimpleBinder::ret(hello)
})),
"2) Testing macro is returning the same value as the same non-yerevanized expression"
);
assert_eq!(
value_from_yer_macro1, "SOME: 2",
"1) Testing specific tested yer-macro is returning a correct result"
);
assert_eq!(
value_from_yer_macro2, "NONE",
"2) Testing specific tested yer-macro is returning a correct result"
);
}
}