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