1
- use async_std:: io:: Read ;
2
1
use async_std:: fs:: File ;
2
+ use async_std:: io:: { self , Read , Write } ;
3
3
use async_std:: path:: PathBuf ;
4
+ use async_std:: sync:: Arc ;
5
+ use async_std:: task:: { Context , Poll } ;
6
+ use std:: pin:: Pin ;
4
7
5
8
#[ macro_export]
6
9
macro_rules! assert {
7
- ( $actual: expr, $expected : expr, $block: expr) => {
10
+ ( $actual: expr, $expected_file : expr, $block: expr) => {
8
11
task:: block_on( async {
12
+ use async_std:: io:: prelude:: * ;
9
13
$block. await . unwrap( ) ;
10
14
let mut actual = std:: string:: String :: from_utf8( $actual) . unwrap( ) ;
11
- let mut expected = std:: string:: String :: from_utf8( $expected) . unwrap( ) ;
15
+ let mut expected = std:: string:: String :: new( ) ;
16
+ $expected_file. read_to_string( & mut expected) . await . unwrap( ) ;
12
17
match expected. find( "{DATE}" ) {
13
18
Some ( i) => {
14
19
expected. replace_range( i..i + 6 , "" ) ;
@@ -27,9 +32,38 @@ macro_rules! assert {
27
32
} ;
28
33
}
29
34
30
- pub fn read_fixture ( name : & str ) -> Arc < File > {
35
+ pub async fn read_fixture ( name : & str ) -> TestFile {
31
36
let directory: PathBuf = env ! ( "CARGO_MANIFEST_DIR" ) . into ( ) ;
32
37
let path: PathBuf = format ! ( "tests/fixtures/{}.txt" , name) . into ( ) ;
33
- let mut file = File :: open ( directory. join ( path) ) . expect ( "Reading fixture file didn't work" ) ;
34
- Arc :: new ( file)
38
+ let file = File :: open ( directory. join ( path) )
39
+ . await
40
+ . expect ( "Reading fixture file didn't work" ) ;
41
+ TestFile ( Arc :: new ( file) )
42
+ }
43
+
44
+ #[ derive( Clone ) ]
45
+ pub struct TestFile ( Arc < File > ) ;
46
+
47
+ impl Read for TestFile {
48
+ fn poll_read (
49
+ self : Pin < & mut Self > ,
50
+ cx : & mut Context ,
51
+ buf : & mut [ u8 ] ,
52
+ ) -> Poll < io:: Result < usize > > {
53
+ Pin :: new ( & mut & * self . 0 ) . poll_read ( cx, buf)
54
+ }
55
+ }
56
+
57
+ impl Write for TestFile {
58
+ fn poll_write ( self : Pin < & mut Self > , cx : & mut Context , buf : & [ u8 ] ) -> Poll < io:: Result < usize > > {
59
+ Pin :: new ( & mut & * self . 0 ) . poll_write ( cx, buf)
60
+ }
61
+
62
+ fn poll_flush ( self : Pin < & mut Self > , cx : & mut Context ) -> Poll < io:: Result < ( ) > > {
63
+ Pin :: new ( & mut & * self . 0 ) . poll_flush ( cx)
64
+ }
65
+
66
+ fn poll_close ( self : Pin < & mut Self > , cx : & mut Context ) -> Poll < io:: Result < ( ) > > {
67
+ Pin :: new ( & mut & * self . 0 ) . poll_close ( cx)
68
+ }
35
69
}
0 commit comments