summaryrefslogtreecommitdiff
path: root/vendor/windows-implement/src/tests.rs
blob: 3f5c3493aa001f2d7c7795fe7a35a6a2cefbc2bf (plain)
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
//! These tests are just a way to quickly run the `#[implement]` macro and see its output.
//! They don't check the output in any way.
//!
//! This exists because of some difficulties of running `cargo expand` against the `#[implement]`
//! macro. It's also just really convenient. You can see the output by using `--nocapture` and
//! you'll probably want to restrict the output to a single thread:
//!
//! ```text
//! cargo test -p windows-implement --lib -- --nocapture --test-threads=1
//! ```

use std::io::{Read, Write};
use std::process::{Command, Stdio};

use proc_macro2::TokenStream;
use quote::quote;

fn implement(attributes: TokenStream, item_tokens: TokenStream) -> String {
    let out_tokens = crate::implement_core(attributes, item_tokens);
    let tokens_string = out_tokens.to_string();

    let out_string = rustfmt(&tokens_string);
    println!("// output of #[implement] :");
    println!();
    println!("{}", out_string);
    out_string
}

fn rustfmt(input: &str) -> String {
    let mut rustfmt = Command::new("rustfmt");

    rustfmt.stdin(Stdio::piped());
    rustfmt.stdout(Stdio::piped());
    rustfmt.stderr(Stdio::inherit());

    let mut child = match rustfmt.spawn() {
        Ok(c) => c,
        Err(e) => {
            eprintln!("failed to spawn rustfmt: {e:?}");
            return input.to_string();
        }
    };

    let mut stdout = child.stdout.take().unwrap();

    // spawn thread to read stdout
    let stdout_thread = std::thread::spawn(move || {
        let mut buf = String::new();
        stdout.read_to_string(&mut buf).unwrap();
        buf
    });

    // write unformatted into stdin
    let mut stdin = child.stdin.take().unwrap();
    stdin.write_all(input.as_bytes()).unwrap();
    drop(stdin);

    let stdout_string: String = stdout_thread.join().unwrap();

    let exit = child.wait().unwrap();
    if !exit.success() {
        eprintln!("rustfmt terminated with failure status code");
        return input.to_string();
    }

    stdout_string
}

#[test]
fn simple_type() {
    implement(
        quote!(IFoo),
        quote! {
            struct Foo {
                x: u32,
            }
        },
    );
}

#[test]
fn zero_sized_type() {
    implement(
        quote!(IFoo),
        quote! {
            struct Foo;
        },
    );
}

#[test]
fn no_interfaces() {
    implement(
        quote!(),
        quote! {
            struct Foo {}
        },
    );
}

#[test]
fn generic_no_lifetime() {
    implement(
        quote!(IAsyncOperationWithProgress<T, P>, IAsyncInfo),
        quote! {
            struct OperationWithProgress<T, P>(SyncState<IAsyncOperationWithProgress<T, P>>)
            where
                T: RuntimeType + 'static,
                P: RuntimeType + 'static;

        },
    );
}

#[test]
fn generic_with_lifetime() {
    implement(
        quote!(),
        quote! {
            pub struct Foo<'a> {
                pub x: &'a [u8],
            }
        },
    );
}

#[test]
fn tuple_type() {
    implement(
        quote!(IFoo),
        quote! {
            struct Foo(pub i32);
        },
    );
}