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
mod generator;
mod parser;
mod syntax;
mod writer;
pub use generator::{
cpp::{fragment::CppFragmentPair, GeneratedCppBlocks},
rust::GeneratedRustBlocks,
};
pub use parser::Parser;
pub use syntax::{parse_qt_file, CxxQtItem};
pub use writer::{cpp::write_cpp, rust::write_rust};
#[cfg(test)]
mod tests {
use super::*;
use clang_format::{clang_format, ClangFormatStyle, CLANG_FORMAT_STYLE};
use generator::{cpp::GeneratedCppBlocks, rust::GeneratedRustBlocks};
use parser::Parser;
use pretty_assertions::assert_str_eq;
use proc_macro2::TokenStream;
use quote::ToTokens;
use std::io::Write;
use writer::{cpp::write_cpp, rust::write_rust};
#[ctor::ctor]
fn init_tests() {
assert!(CLANG_FORMAT_STYLE.set(ClangFormatStyle::Mozilla).is_ok());
}
pub fn assert_tokens_eq<T: ToTokens>(item: &T, tokens: TokenStream) {
assert_str_eq!(item.to_token_stream().to_string(), tokens.to_string());
}
fn format_rs_source(rs_code: &str) -> String {
let mut command = std::process::Command::new("rustfmt");
let mut child = command
.args(["--emit", "stdout"])
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.spawn()
.unwrap();
{
let mut stdin = child.stdin.take().unwrap();
write!(stdin, "{}", rs_code).unwrap();
}
let output = child.wait_with_output().unwrap();
let output = String::from_utf8(output.stdout).unwrap();
output.replace("\n\n", "\n")
}
pub fn tokens_to_syn<T: syn::parse::Parse>(tokens: proc_macro2::TokenStream) -> T {
syn::parse2(tokens.into_token_stream()).unwrap()
}
macro_rules! test_code_generation {
( $file_stem:literal ) => {
let parser = Parser::from(
syn::parse_str(include_str!(concat!("../test_inputs/", $file_stem, ".rs")))
.unwrap(),
)
.unwrap();
let generated_cpp = GeneratedCppBlocks::from(&parser).unwrap();
let cpp = write_cpp(&generated_cpp);
let expected_cpp_header =
clang_format(include_str!(concat!("../test_outputs/", $file_stem, ".h"))).unwrap();
let expected_cpp_source = clang_format(include_str!(concat!(
"../test_outputs/",
$file_stem,
".cpp"
)))
.unwrap();
assert_str_eq!(cpp.header, expected_cpp_header);
assert_str_eq!(cpp.source, expected_cpp_source);
let generated_rust = GeneratedRustBlocks::from(&parser).unwrap();
let rust = format_rs_source(&write_rust(&generated_rust).to_string());
let expected_rust_output =
format_rs_source(include_str!(concat!("../test_outputs/", $file_stem, ".rs")));
assert_str_eq!(rust, expected_rust_output);
};
}
#[test]
fn generates_invokables() {
test_code_generation!("invokables");
}
#[test]
fn generates_passthrough_and_naming() {
test_code_generation!("passthrough_and_naming");
}
#[test]
fn generates_properties() {
test_code_generation!("properties");
}
#[test]
fn generates_signals() {
test_code_generation!("signals");
}
}