Skip to content

Commit 1cac5e6

Browse files
authored
Merge pull request #43 from yoshuawuyts/nicer-tests
Clean up test code
2 parents 11c96cc + c261964 commit 1cac5e6

File tree

3 files changed

+101
-99
lines changed

3 files changed

+101
-99
lines changed

tests/common/mod.rs

Lines changed: 89 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,83 +7,118 @@ use async_std::task::{Context, Poll};
77
use std::pin::Pin;
88
use std::sync::Mutex;
99

10-
#[macro_export]
11-
macro_rules! assert {
12-
($actual:expr, $expected_file:expr, $block:expr) => {
13-
task::block_on(async {
14-
use async_std::io::prelude::*;
15-
$block.await.unwrap();
16-
let mut actual = $actual.to_string().await;
17-
let mut expected = std::string::String::new();
18-
$expected_file.read_to_string(&mut expected).await.unwrap();
19-
match expected.find("{DATE}") {
20-
Some(i) => {
21-
expected.replace_range(i..i + 6, "");
22-
match expected.get(i..i + 1) {
23-
Some(byte) => {
24-
let j = actual[i..].find(byte).expect("Byte not found");
25-
actual.replace_range(i..i + j, "");
26-
}
27-
None => expected.replace_range(i.., ""),
28-
}
29-
}
30-
None => {}
31-
}
32-
pretty_assertions::assert_eq!(actual, expected);
33-
})
34-
};
10+
#[derive(Clone)]
11+
pub struct TestCase {
12+
request_fixture: Arc<File>,
13+
response_fixture: Arc<Mutex<File>>,
14+
result: Arc<Mutex<File>>,
3515
}
3616

37-
pub async fn read_fixture(name: &str) -> TestFile {
38-
let directory: PathBuf = env!("CARGO_MANIFEST_DIR").into();
39-
let path: PathBuf = format!("tests/fixtures/{}.txt", name).into();
40-
let file = File::open(directory.join(path))
41-
.await
42-
.expect("Reading fixture file didn't work");
43-
let temp = std::env::temp_dir().join("foo.txt");
44-
let temp = OpenOptions::new()
45-
.read(true)
46-
.write(true)
47-
.open(temp)
48-
.await
49-
.unwrap();
50-
TestFile(Arc::new(file), Arc::new(Mutex::new(temp)))
51-
}
17+
impl TestCase {
18+
pub async fn new(request_file_path: &str, response_file_path: &str) -> TestCase {
19+
let request_fixture = File::open(fixture_path(&request_file_path))
20+
.await
21+
.expect(&format!(
22+
"Could not open request fixture file: {:?}",
23+
&fixture_path(request_file_path)
24+
));
25+
let request_fixture = Arc::new(request_fixture);
5226

53-
#[derive(Clone)]
54-
pub struct TestFile(Arc<File>, Arc<Mutex<File>>);
27+
let response_fixture =
28+
File::open(fixture_path(&response_file_path))
29+
.await
30+
.expect(&format!(
31+
"Could not open response fixture file: {:?}",
32+
&fixture_path(response_file_path)
33+
));
34+
let response_fixture = Arc::new(Mutex::new(response_fixture));
35+
36+
let temp = std::env::temp_dir().join("result.txt");
37+
let temp = OpenOptions::new()
38+
.read(true)
39+
.write(true)
40+
.create(true)
41+
.open(temp)
42+
.await
43+
.expect("Could not read temporary file where response will be written to");
44+
let result = Arc::new(Mutex::new(temp));
45+
46+
TestCase {
47+
request_fixture,
48+
response_fixture,
49+
result,
50+
}
51+
}
5552

56-
impl TestFile {
57-
pub async fn to_string(self) -> String {
53+
pub async fn read_result(&self) -> String {
5854
use async_std::prelude::*;
59-
let mut buf = String::new();
60-
let mut file = self.1.lock().unwrap();
55+
let mut result = String::new();
56+
let mut file = self.result.lock().unwrap();
6157
file.seek(SeekFrom::Start(0)).await.unwrap();
62-
dbg!(file.read_to_string(&mut buf).await.unwrap());
63-
buf
58+
file.read_to_string(&mut result).await.unwrap();
59+
result
6460
}
61+
62+
pub async fn read_expected(&self) -> String {
63+
use async_std::prelude::*;
64+
let mut expected = std::string::String::new();
65+
self.response_fixture
66+
.lock()
67+
.unwrap()
68+
.read_to_string(&mut expected)
69+
.await
70+
.unwrap();
71+
expected
72+
}
73+
74+
pub async fn assert(self) {
75+
let mut actual = self.read_result().await;
76+
let mut expected = self.read_expected().await;
77+
78+
// munge actual and expected so that we don't rely on dates matching exactly
79+
match expected.find("{DATE}") {
80+
Some(i) => {
81+
expected.replace_range(i..i + 6, "");
82+
match expected.get(i..i + 1) {
83+
Some(byte) => {
84+
let j = actual[i..].find(byte).expect("Byte not found");
85+
actual.replace_range(i..i + j, "");
86+
}
87+
None => expected.replace_range(i.., ""),
88+
}
89+
}
90+
None => {}
91+
}
92+
93+
pretty_assertions::assert_eq!(actual, expected);
94+
}
95+
}
96+
97+
fn fixture_path(relative_path: &str) -> PathBuf {
98+
let directory: PathBuf = env!("CARGO_MANIFEST_DIR").into();
99+
directory.join("tests").join(relative_path)
65100
}
66101

67-
impl Read for TestFile {
102+
impl Read for TestCase {
68103
fn poll_read(
69104
self: Pin<&mut Self>,
70105
cx: &mut Context,
71106
buf: &mut [u8],
72107
) -> Poll<io::Result<usize>> {
73-
Pin::new(&mut &*self.0).poll_read(cx, buf)
108+
Pin::new(&mut &*self.request_fixture).poll_read(cx, buf)
74109
}
75110
}
76111

77-
impl Write for TestFile {
112+
impl Write for TestCase {
78113
fn poll_write(self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll<io::Result<usize>> {
79-
Pin::new(&mut &*self.1.lock().unwrap()).poll_write(cx, buf)
114+
Pin::new(&mut &*self.result.lock().unwrap()).poll_write(cx, buf)
80115
}
81116

82117
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
83-
Pin::new(&mut &*self.1.lock().unwrap()).poll_flush(cx)
118+
Pin::new(&mut &*self.result.lock().unwrap()).poll_flush(cx)
84119
}
85120

86121
fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
87-
Pin::new(&mut &*self.1.lock().unwrap()).poll_close(cx)
122+
Pin::new(&mut &*self.result.lock().unwrap()).poll_close(cx)
88123
}
89124
}

tests/server.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
mod common;
22
use async_h1::server;
33
use async_std::task;
4-
use common::read_fixture;
4+
use common::TestCase;
55
use http_types::{Response, StatusCode};
66

77
#[test]
88
fn test_basic_request() {
99
task::block_on(async {
10-
let io = read_fixture("request1").await;
11-
let mut expected = read_fixture("response1").await;
10+
let case = TestCase::new("fixtures/request1.txt", "fixtures/response1.txt").await;
1211
let addr = "http://example.com";
1312

14-
assert!(
15-
io.clone(),
16-
expected,
17-
server::accept(addr, io.clone(), |_req| {
18-
async {
19-
let mut resp = Response::new(StatusCode::Ok);
20-
resp.set_body("");
21-
Ok(resp)
22-
}
23-
})
24-
);
25-
})
13+
server::accept(addr, case.clone(), |_req| async {
14+
let mut resp = Response::new(StatusCode::Ok);
15+
resp.set_body("");
16+
Ok(resp)
17+
})
18+
.await
19+
.unwrap();
20+
21+
case.assert().await;
22+
});
2623
}

tests/test.rs

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)