-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbuild.rs
215 lines (196 loc) · 6.95 KB
/
build.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#[cfg(feature = "json_tests")]
pub mod json_tests {
use indoc::indoc;
use serde::{Deserialize, Serialize};
use serde_json;
use slugify::slugify;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::PathBuf;
#[derive(Debug, Serialize, Deserialize)]
struct Symbol {
name: String,
value: String,
}
#[derive(Debug, Serialize, Deserialize)]
struct StringColumn {
name: String,
value: String,
}
#[derive(Debug, Serialize, Deserialize)]
struct LongColumn {
name: String,
value: i64,
}
#[derive(Debug, Serialize, Deserialize)]
struct DoubleColumn {
name: String,
value: f64,
}
#[derive(Debug, Serialize, Deserialize)]
struct BooleanColumn {
name: String,
value: bool,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "UPPERCASE")]
enum Column {
String(StringColumn),
Long(LongColumn),
Double(DoubleColumn),
Boolean(BooleanColumn),
}
#[derive(Debug, Serialize, Deserialize)]
struct Expected {
line: Option<String>,
#[serde(rename = "anyLines")]
any_lines: Option<Vec<String>>,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "status", rename_all = "UPPERCASE")]
enum Outcome {
Success(Expected),
Error,
}
#[derive(Debug, Serialize, Deserialize)]
struct TestSpec {
#[serde(rename = "testName")]
test_name: String,
table: String,
symbols: Vec<Symbol>,
columns: Vec<Column>,
result: Outcome,
}
fn parse() -> Vec<TestSpec> {
let mut json_path = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
json_path.push("src");
json_path.push("tests");
json_path.push("interop");
json_path.push("ilp-client-interop-test.json");
let file = std::fs::File::open(json_path).unwrap();
serde_json::from_reader(file).unwrap()
}
pub fn build() -> Result<(), Box<dyn std::error::Error>> {
let specs = parse();
// eprintln!("Parsed JSON: {:#?}", specs);
let mut file_path = PathBuf::from(std::env::var("OUT_DIR")?);
file_path.push("json_tests.rs");
let mut output = BufWriter::new(File::create(file_path)?);
// let mut output = String::new();
writeln!(
output,
"{}",
indoc! {r#"
// This file is auto-generated by build.rs.
use crate::{Result, ingress::{Buffer}};
use crate::tests::{TestResult};
fn matches_any_line(line: &str, expected: &[&str]) -> bool {
for &exp in expected {
if line == exp {
return true;
}
}
eprintln!(
"Could not match:\n {:?}\nTo any of: {:#?}",
line, expected);
false
}
"#}
)?;
for (index, spec) in specs.iter().enumerate() {
writeln!(output, "/// {}", spec.test_name)?;
// for line in serde_json::to_string_pretty(&spec).unwrap().split("\n") {
// writeln!(output, "/// {}", line)?;
// }
writeln!(output, "#[test]")?;
writeln!(
output,
"fn test_{:03}_{}() -> TestResult {{",
index,
slugify!(&spec.test_name, separator = "_")
)?;
writeln!(output, " let mut buffer = Buffer::new();")?;
let (expected, indent) = match &spec.result {
Outcome::Success(line) => (Some(line), ""),
Outcome::Error => (None, " "),
};
if expected.is_none() {
writeln!(output, " || -> Result<()> {{")?;
}
writeln!(output, "{} buffer", indent)?;
writeln!(output, "{} .table({:?})?", indent, spec.table)?;
for symbol in spec.symbols.iter() {
writeln!(
output,
"{} .symbol({:?}, {:?})?",
indent, symbol.name, symbol.value
)?;
}
for column in spec.columns.iter() {
match column {
Column::String(column) => writeln!(
output,
"{} .column_str({:?}, {:?})?",
indent, column.name, column.value
)?,
Column::Long(column) => writeln!(
output,
"{} .column_i64({:?}, {:?})?",
indent, column.name, column.value
)?,
Column::Double(column) => writeln!(
output,
"{} .column_f64({:?}, {:?})?",
indent, column.name, column.value
)?,
Column::Boolean(column) => writeln!(
output,
"{} .column_bool({:?}, {:?})?",
indent, column.name, column.value
)?,
}
}
writeln!(output, "{} .at_now()?;", indent)?;
if let Some(expected) = expected {
if let Some(ref line) = expected.line {
let exp_ln = format!("{}\n", line);
writeln!(output, " let exp = {:?};", exp_ln)?;
writeln!(output, " assert_eq!(buffer.as_str(), exp);")?;
} else {
let any: Vec<String> = expected
.any_lines
.as_ref()
.unwrap()
.iter()
.map(|line| format!("{}\n", line))
.collect();
writeln!(output, " let any = [")?;
for line in any.iter() {
writeln!(output, " {:?},", line)?;
}
writeln!(output, " ];")?;
writeln!(
output,
" assert!(matches_any_line(buffer.as_str(), &any));"
)?;
}
} else {
writeln!(output, " Ok(())")?;
writeln!(output, " }}().unwrap_err();")?;
}
writeln!(output, " Ok(())")?;
writeln!(output, "}}")?;
}
Ok(())
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
#[cfg(feature = "json_tests")]
{
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=Cargo.lock");
println!("cargo:rerun-if-changed=src/test/interop/ilp-client-interop-test.json");
json_tests::build()?;
}
Ok(())
}