forked from mgattozzi/assay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_tests.rs
184 lines (158 loc) · 4.22 KB
/
integration_tests.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
/*
* Copyright (C) 2021 Michael Gattozzi <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
use assay::{assay, assert_eq_sorted};
use std::{
collections::HashMap,
env, fs,
future::Future,
path::PathBuf,
pin::Pin,
task::{Context, Poll},
};
#[assay]
fn private_1() {
fs::write("test", "This is a test\nprivate 1\n").unwrap();
assert_eq!(
"This is a test\nprivate 1\n",
&fs::read_to_string("test").unwrap()
);
}
#[assay]
fn private_2() {
fs::write("test", "This is a test\nprivate 2\n")?;
assert_eq!("This is a test\nprivate 2\n", &fs::read_to_string("test")?);
}
#[assay(include = ["Cargo.toml", "src/lib.rs"])]
fn include() {
assert!(fs::metadata("src/lib.rs")?.is_file());
assert!(fs::metadata("Cargo.toml")?.is_file());
}
#[assay(should_panic)]
fn hash_map_comparison() {
let map1: HashMap<String, u8> = (0..5).map(|n| (n.to_string(), n)).collect();
let mut map2: HashMap<String, u8> = (0..5).map(|n| (n.to_string(), n)).collect();
// Force the last item to be different
map2.insert("4".to_string(), 5);
assert_eq_sorted!(map1, map2);
}
#[assay]
async fn async_func() {
ReadyOnPoll.await;
}
#[assay(should_panic)]
fn panic_test() {
panic!("Panic! At The Proc-Macro");
}
#[assay(include = ["Cargo.toml"], should_panic)]
fn multiple_attribute_values() {
panic!("Panic! At The Proc-Macro 2: Cargo.toml Boogaloo");
}
#[assay(should_panic, include = ["Cargo.toml"])]
fn multiple_attribute_values_in_different_order() {
panic!("Panic! At The Proc-Macro 3: Attribute Switcharoo");
}
#[assay(
env = [
("GOODBOY", "Bukka"),
("BADDOGS", "false")
]
)]
fn env_vars() {
assert_eq!(env::var("GOODBOY")?, "Bukka");
assert_eq!(env::var("BADDOGS")?, "false");
}
#[assay(
setup = setup_func(5)?,
teardown = teardown_func(),
)]
fn setup_teardown_test_1() {
assert_eq!(fs::read_to_string("setup")?, "Value: 5");
}
#[assay(
setup = setup_func_2(),
teardown = teardown_func(),
)]
fn setup_teardown_test_2() {
assert_eq!(fs::read_to_string("setup")?, "Value: 5");
}
#[assay(
setup = setup_func_2(),
include = ["Cargo.toml", "src/lib.rs"],
env = [
("GOODBOY", "Bukka"),
("BADDOGS", "false")
],
teardown = teardown_func(),
should_panic,
)]
async fn one_test_to_call_it_all() {
ReadyOnPoll.await;
assert_eq!(env::var("GOODBOY")?, "Bukka");
assert_eq!(env::var("BADDOGS")?, "false");
assert_eq!(fs::read_to_string("setup")?, "Value: 5");
assert!(PathBuf::from("Cargo.toml").exists());
assert!(PathBuf::from("src/lib.rs").exists());
// Removing this actually causes the test to fail
panic!();
}
#[assay(
setup = setup_func(5)?,
env = [
("GOODBOY", "Bukka"),
("BADDOGS", "false")
],
teardown = teardown_func(),
include = ["Cargo.toml", "src/lib.rs"],
should_panic,
)]
async fn one_test_to_call_it_all_2() {
ReadyOnPoll.await;
assert_eq!(env::var("GOODBOY")?, "Bukka");
assert_eq!(env::var("BADDOGS")?, "false");
assert_eq!(fs::read_to_string("setup")?, "Value: 5");
assert!(PathBuf::from("Cargo.toml").exists());
assert!(PathBuf::from("src/lib.rs").exists());
// Removing this actually causes the test to fail
panic!();
}
fn setup_func(input: i32) -> Result<(), Box<dyn std::error::Error>> {
fs::write("setup", format!("Value: {}", input))?;
Ok(())
}
fn teardown_func() {
fs::remove_file("setup").unwrap();
assert!(!PathBuf::from("setup").exists());
}
fn setup_func_2() {
fs::write("setup", "Value: 5").unwrap();
}
struct ReadyOnPoll;
impl Future for ReadyOnPoll {
type Output = ();
fn poll(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> {
Poll::Ready(())
}
}
#[assay(ignore)]
fn should_be_ignored() {
panic!("this test should be ignored")
}
#[assay]
#[should_panic]
fn respects_other_attributes() {
panic!("this test is expected to panic");
}
use test_case::test_case;
#[test_case(-2, -4 ; "when both operands are negative")]
#[test_case( 2, 4 ; "when both operands are positive")]
#[test_case( 4, 2 ; "when operands are swapped")]
#[assay]
fn should_work_with_test_case(x: i8, y: i8) {
let actual = (x * y).abs();
assert_eq!(8, actual)
}