about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJonathan Creekmore <jonathan@thecreekmores.org>2016-07-23 07:22:58 -0500
committerDoug Goldstein <cardoe@cardoe.com>2016-07-27 10:29:43 -0700
commitbd194a77d57c5487baf1f9bb169e44dd844235e6 (patch)
tree5d1c4320e738646208c59fea96262d530d84efa4
parenteafecbf86855c30d2f6d9c518165edec4cca8248 (diff)
downloadrust-bd194a77d57c5487baf1f9bb169e44dd844235e6.tar.gz
rust-bd194a77d57c5487baf1f9bb169e44dd844235e6.zip
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.
-rw-r--r--src/librustc_back/target/mod.rs30
1 files changed, 28 insertions, 2 deletions
diff --git a/src/librustc_back/target/mod.rs b/src/librustc_back/target/mod.rs
index 4849a76e11d..84cb6c9ba7d 100644
--- a/src/librustc_back/target/mod.rs
+++ b/src/librustc_back/target/mod.rs
@@ -79,6 +79,10 @@ macro_rules! supported_targets {
                     $triple => {
                         let mut t = try!($module::target());
                         t.options.is_builtin = true;
+
+                        // round-trip through the JSON parser to ensure at
+                        // run-time that the parser works correctly
+                        t = try!(Target::from_json(t.to_json()));
                         debug!("Got builtin target: {:?}", t);
                         Ok(t)
                     },
@@ -86,6 +90,28 @@ macro_rules! supported_targets {
                 _ => Err(format!("Unable to find target: {}", target))
             }
         }
+
+        #[cfg(test)]
+        mod test_json_encode_decode {
+            use serialize::json::ToJson;
+            use super::Target;
+            $(use super::$module;)*
+
+            $(
+                #[test]
+                fn $module() {
+                    // Grab the TargetResult struct. If we successfully retrieved
+                    // a Target, then the test JSON encoding/decoding can run for this
+                    // Target on this testing platform (i.e., checking the iOS targets
+                    // only on a Mac test platform).
+                    let _ = $module::target().map(|original| {
+                        let as_json = original.to_json();
+                        let parsed = Target::from_json(as_json).unwrap();
+                        assert_eq!(original, parsed);
+                    });
+                }
+            )*
+        }
     )
 }
 
@@ -148,7 +174,7 @@ supported_targets! {
 /// Everything `rustc` knows about how to compile for a specific target.
 ///
 /// Every field here must be specified, and has no default value.
-#[derive(Clone, Debug)]
+#[derive(PartialEq, Clone, Debug)]
 pub struct Target {
     /// Target triple to pass to LLVM.
     pub llvm_target: String,
@@ -175,7 +201,7 @@ pub struct Target {
 ///
 /// This has an implementation of `Default`, see each field for what the default is. In general,
 /// these try to take "minimal defaults" that don't assume anything about the runtime they run in.
-#[derive(Clone, Debug)]
+#[derive(PartialEq, Clone, Debug)]
 pub struct TargetOptions {
     /// Whether the target is built-in or loaded from a custom target specification.
     pub is_builtin: bool,