1 use super::AutoCfg;
2 use std::env;
3 
4 impl AutoCfg {
core_std(&self, path: &str) -> String5     fn core_std(&self, path: &str) -> String {
6         let krate = if self.no_std { "core" } else { "std" };
7         format!("{}::{}", krate, path)
8     }
9 
assert_std(&self, probe_result: bool)10     fn assert_std(&self, probe_result: bool) {
11         assert_eq!(!self.no_std, probe_result);
12     }
13 
assert_min(&self, major: usize, minor: usize, probe_result: bool)14     fn assert_min(&self, major: usize, minor: usize, probe_result: bool) {
15         assert_eq!(self.probe_rustc_version(major, minor), probe_result);
16     }
17 
for_test() -> Result<Self, super::error::Error>18     fn for_test() -> Result<Self, super::error::Error> {
19         match env::var_os("TESTS_TARGET_DIR") {
20             Some(d) => Self::with_dir(d),
21             None => Self::with_dir("target"),
22         }
23     }
24 }
25 
26 #[test]
autocfg_version()27 fn autocfg_version() {
28     let ac = AutoCfg::for_test().unwrap();
29     println!("version: {:?}", ac.rustc_version);
30     assert!(ac.probe_rustc_version(1, 0));
31 }
32 
33 #[test]
version_cmp()34 fn version_cmp() {
35     use super::version::Version;
36     let v123 = Version::new(1, 2, 3);
37 
38     assert!(Version::new(1, 0, 0) < v123);
39     assert!(Version::new(1, 2, 2) < v123);
40     assert!(Version::new(1, 2, 3) == v123);
41     assert!(Version::new(1, 2, 4) > v123);
42     assert!(Version::new(1, 10, 0) > v123);
43     assert!(Version::new(2, 0, 0) > v123);
44 }
45 
46 #[test]
probe_add()47 fn probe_add() {
48     let ac = AutoCfg::for_test().unwrap();
49     let add = ac.core_std("ops::Add");
50     let add_rhs = add.clone() + "<i32>";
51     let add_rhs_output = add.clone() + "<i32, Output = i32>";
52     let dyn_add_rhs_output = "dyn ".to_string() + &*add_rhs_output;
53     assert!(ac.probe_path(&add));
54     assert!(ac.probe_trait(&add));
55     assert!(ac.probe_trait(&add_rhs));
56     assert!(ac.probe_trait(&add_rhs_output));
57     ac.assert_min(1, 27, ac.probe_type(&dyn_add_rhs_output));
58 }
59 
60 #[test]
probe_as_ref()61 fn probe_as_ref() {
62     let ac = AutoCfg::for_test().unwrap();
63     let as_ref = ac.core_std("convert::AsRef");
64     let as_ref_str = as_ref.clone() + "<str>";
65     let dyn_as_ref_str = "dyn ".to_string() + &*as_ref_str;
66     assert!(ac.probe_path(&as_ref));
67     assert!(ac.probe_trait(&as_ref_str));
68     assert!(ac.probe_type(&as_ref_str));
69     ac.assert_min(1, 27, ac.probe_type(&dyn_as_ref_str));
70 }
71 
72 #[test]
probe_i128()73 fn probe_i128() {
74     let ac = AutoCfg::for_test().unwrap();
75     let i128_path = ac.core_std("i128");
76     ac.assert_min(1, 26, ac.probe_path(&i128_path));
77     ac.assert_min(1, 26, ac.probe_type("i128"));
78 }
79 
80 #[test]
probe_sum()81 fn probe_sum() {
82     let ac = AutoCfg::for_test().unwrap();
83     let sum = ac.core_std("iter::Sum");
84     let sum_i32 = sum.clone() + "<i32>";
85     let dyn_sum_i32 = "dyn ".to_string() + &*sum_i32;
86     ac.assert_min(1, 12, ac.probe_path(&sum));
87     ac.assert_min(1, 12, ac.probe_trait(&sum));
88     ac.assert_min(1, 12, ac.probe_trait(&sum_i32));
89     ac.assert_min(1, 12, ac.probe_type(&sum_i32));
90     ac.assert_min(1, 27, ac.probe_type(&dyn_sum_i32));
91 }
92 
93 #[test]
probe_std()94 fn probe_std() {
95     let ac = AutoCfg::for_test().unwrap();
96     ac.assert_std(ac.probe_sysroot_crate("std"));
97 }
98 
99 #[test]
probe_alloc()100 fn probe_alloc() {
101     let ac = AutoCfg::for_test().unwrap();
102     ac.assert_min(1, 36, ac.probe_sysroot_crate("alloc"));
103 }
104 
105 #[test]
probe_bad_sysroot_crate()106 fn probe_bad_sysroot_crate() {
107     let ac = AutoCfg::for_test().unwrap();
108     assert!(!ac.probe_sysroot_crate("doesnt_exist"));
109 }
110 
111 #[test]
probe_no_std()112 fn probe_no_std() {
113     let ac = AutoCfg::for_test().unwrap();
114     assert!(ac.probe_type("i32"));
115     assert!(ac.probe_type("[i32]"));
116     ac.assert_std(ac.probe_type("Vec<i32>"));
117 }
118 
119 #[test]
probe_expression()120 fn probe_expression() {
121     let ac = AutoCfg::for_test().unwrap();
122     assert!(ac.probe_expression(r#""test".trim_left()"#));
123     ac.assert_min(1, 30, ac.probe_expression(r#""test".trim_start()"#));
124     ac.assert_std(ac.probe_expression("[1, 2, 3].to_vec()"));
125 }
126 
127 #[test]
probe_constant()128 fn probe_constant() {
129     let ac = AutoCfg::for_test().unwrap();
130     assert!(ac.probe_constant("1 + 2 + 3"));
131     ac.assert_min(1, 33, ac.probe_constant("{ let x = 1 + 2 + 3; x * x }"));
132     ac.assert_min(1, 39, ac.probe_constant(r#""test".len()"#));
133 }
134 
135 #[test]
dir_does_not_contain_target()136 fn dir_does_not_contain_target() {
137     assert!(!super::dir_contains_target(
138         &Some("x86_64-unknown-linux-gnu".into()),
139         &"/project/target/debug/build/project-ea75983148559682/out".into(),
140         None,
141     ));
142 }
143 
144 #[test]
dir_does_contain_target()145 fn dir_does_contain_target() {
146     assert!(super::dir_contains_target(
147         &Some("x86_64-unknown-linux-gnu".into()),
148         &"/project/target/x86_64-unknown-linux-gnu/debug/build/project-0147aca016480b9d/out".into(),
149         None,
150     ));
151 }
152 
153 #[test]
dir_does_not_contain_target_with_custom_target_dir()154 fn dir_does_not_contain_target_with_custom_target_dir() {
155     assert!(!super::dir_contains_target(
156         &Some("x86_64-unknown-linux-gnu".into()),
157         &"/project/custom/debug/build/project-ea75983148559682/out".into(),
158         Some("custom".into()),
159     ));
160 }
161 
162 #[test]
dir_does_contain_target_with_custom_target_dir()163 fn dir_does_contain_target_with_custom_target_dir() {
164     assert!(super::dir_contains_target(
165         &Some("x86_64-unknown-linux-gnu".into()),
166         &"/project/custom/x86_64-unknown-linux-gnu/debug/build/project-0147aca016480b9d/out".into(),
167         Some("custom".into()),
168     ));
169 }
170