Skip to content

Commit bd194a7

Browse files
jcreekmorecardoe
authored andcommitted
librustc_back: json tests for builtin targets
Expand the supported_targets!() macro to also generate a set of JSON encode/decode tests to verify that the parser will encode and decode all of the fields needed for all of the builtin targets. Additionally, add PartialEq to Target and TargetOptions in support of the tests.
1 parent eafecbf commit bd194a7

File tree

1 file changed

+28
-2
lines changed
  • src/librustc_back/target

1 file changed

+28
-2
lines changed

src/librustc_back/target/mod.rs

+28-2
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,39 @@ macro_rules! supported_targets {
7979
$triple => {
8080
let mut t = try!($module::target());
8181
t.options.is_builtin = true;
82+
83+
// round-trip through the JSON parser to ensure at
84+
// run-time that the parser works correctly
85+
t = try!(Target::from_json(t.to_json()));
8286
debug!("Got builtin target: {:?}", t);
8387
Ok(t)
8488
},
8589
)+
8690
_ => Err(format!("Unable to find target: {}", target))
8791
}
8892
}
93+
94+
#[cfg(test)]
95+
mod test_json_encode_decode {
96+
use serialize::json::ToJson;
97+
use super::Target;
98+
$(use super::$module;)*
99+
100+
$(
101+
#[test]
102+
fn $module() {
103+
// Grab the TargetResult struct. If we successfully retrieved
104+
// a Target, then the test JSON encoding/decoding can run for this
105+
// Target on this testing platform (i.e., checking the iOS targets
106+
// only on a Mac test platform).
107+
let _ = $module::target().map(|original| {
108+
let as_json = original.to_json();
109+
let parsed = Target::from_json(as_json).unwrap();
110+
assert_eq!(original, parsed);
111+
});
112+
}
113+
)*
114+
}
89115
)
90116
}
91117

@@ -148,7 +174,7 @@ supported_targets! {
148174
/// Everything `rustc` knows about how to compile for a specific target.
149175
///
150176
/// Every field here must be specified, and has no default value.
151-
#[derive(Clone, Debug)]
177+
#[derive(PartialEq, Clone, Debug)]
152178
pub struct Target {
153179
/// Target triple to pass to LLVM.
154180
pub llvm_target: String,
@@ -175,7 +201,7 @@ pub struct Target {
175201
///
176202
/// This has an implementation of `Default`, see each field for what the default is. In general,
177203
/// these try to take "minimal defaults" that don't assume anything about the runtime they run in.
178-
#[derive(Clone, Debug)]
204+
#[derive(PartialEq, Clone, Debug)]
179205
pub struct TargetOptions {
180206
/// Whether the target is built-in or loaded from a custom target specification.
181207
pub is_builtin: bool,

0 commit comments

Comments
 (0)