Debian Codegraph @

debian/rust-vendor/autocfg/tests/tests.rs

raw
1 extern crate autocfg;
2
3 use autocfg::AutoCfg;
4
5 mod support;
6
7 fn core_std(ac: &AutoCfg, path: &str) -> String {
8 let krate = if ac.no_std() { "core" } else { "std" };
9 format!("{}::{}", krate, path)
10 }
11
12 fn assert_std(ac: &AutoCfg, probe_result: bool) {
13 assert_eq!(!ac.no_std(), probe_result);
14 }
15
16 fn assert_min(ac: &AutoCfg, major: usize, minor: usize, probe_result: bool) {
17 assert_eq!(ac.probe_rustc_version(major, minor), probe_result);
18 }
19
20 fn autocfg_for_test() -> AutoCfg {
21 AutoCfg::with_dir(support::out_dir().as_ref()).unwrap()
22 }
23
24 #[test]
25 fn autocfg_version() {
26 let ac = autocfg_for_test();
27 assert!(ac.probe_rustc_version(1, 0));
28 }
29
30 #[test]
31 fn probe_add() {
32 let ac = autocfg_for_test();
33 let add = core_std(&ac, "ops::Add");
34 let add_rhs = add.clone() + "<i32>";
35 let add_rhs_output = add.clone() + "<i32, Output = i32>";
36 let dyn_add_rhs_output = "dyn ".to_string() + &*add_rhs_output;
37 assert!(ac.probe_path(&add));
38 assert!(ac.probe_trait(&add));
39 assert!(ac.probe_trait(&add_rhs));
40 assert!(ac.probe_trait(&add_rhs_output));
41 assert_min(&ac, 1, 27, ac.probe_type(&dyn_add_rhs_output));
42 }
43
44 #[test]
45 fn probe_as_ref() {
46 let ac = autocfg_for_test();
47 let as_ref = core_std(&ac, "convert::AsRef");
48 let as_ref_str = as_ref.clone() + "<str>";
49 let dyn_as_ref_str = "dyn ".to_string() + &*as_ref_str;
50 assert!(ac.probe_path(&as_ref));
51 assert!(ac.probe_trait(&as_ref_str));
52 assert!(ac.probe_type(&as_ref_str));
53 assert_min(&ac, 1, 27, ac.probe_type(&dyn_as_ref_str));
54 }
55
56 #[test]
57 fn probe_i128() {
58 let ac = autocfg_for_test();
59 let i128_path = core_std(&ac, "i128");
60 assert_min(&ac, 1, 26, ac.probe_path(&i128_path));
61 assert_min(&ac, 1, 26, ac.probe_type("i128"));
62 }
63
64 #[test]
65 fn probe_sum() {
66 let ac = autocfg_for_test();
67 let sum = core_std(&ac, "iter::Sum");
68 let sum_i32 = sum.clone() + "<i32>";
69 let dyn_sum_i32 = "dyn ".to_string() + &*sum_i32;
70 assert_min(&ac, 1, 12, ac.probe_path(&sum));
71 assert_min(&ac, 1, 12, ac.probe_trait(&sum));
72 assert_min(&ac, 1, 12, ac.probe_trait(&sum_i32));
73 assert_min(&ac, 1, 12, ac.probe_type(&sum_i32));
74 assert_min(&ac, 1, 27, ac.probe_type(&dyn_sum_i32));
75 }
76
77 #[test]
78 fn probe_std() {
79 let ac = autocfg_for_test();
80 assert_std(&ac, ac.probe_sysroot_crate("std"));
81 }
82
83 #[test]
84 fn probe_alloc() {
85 let ac = autocfg_for_test();
86 assert_min(&ac, 1, 36, ac.probe_sysroot_crate("alloc"));
87 }
88
89 #[test]
90 fn probe_bad_sysroot_crate() {
91 let ac = autocfg_for_test();
92 assert!(!ac.probe_sysroot_crate("doesnt_exist"));
93 }
94
95 #[test]
96 fn probe_no_std() {
97 let ac = autocfg_for_test();
98 assert!(ac.probe_type("i32"));
99 assert!(ac.probe_type("[i32]"));
100 assert_std(&ac, ac.probe_type("Vec<i32>"));
101 }
102
103 #[test]
104 fn probe_expression() {
105 let ac = autocfg_for_test();
106 assert!(ac.probe_expression(r#""test".trim_left()"#));
107 assert_min(&ac, 1, 30, ac.probe_expression(r#""test".trim_start()"#));
108 assert_std(&ac, ac.probe_expression("[1, 2, 3].to_vec()"));
109 }
110
111 #[test]
112 fn probe_constant() {
113 let ac = autocfg_for_test();
114 assert!(ac.probe_constant("1 + 2 + 3"));
115 assert_min(
116 &ac,
117 1,
118 33,
119 ac.probe_constant("{ let x = 1 + 2 + 3; x * x }"),
120 );
121 assert_min(&ac, 1, 39, ac.probe_constant(r#""test".len()"#));
122 }
123
124 #[test]
125 fn probe_raw() {
126 let ac = autocfg_for_test();
127 let prefix = if ac.no_std() { "#![no_std]\n" } else { "" };
128 let f = |s| format!("{}{}", prefix, s);
129
130 // This attribute **must** be used at the crate level.
131 assert!(ac.probe_raw(&f("#![no_builtins]")).is_ok());
132
133 assert!(ac.probe_raw(&f("#![deny(dead_code)] fn x() {}")).is_err());
134 assert!(ac.probe_raw(&f("#![allow(dead_code)] fn x() {}")).is_ok());
135 assert!(ac
136 .probe_raw(&f("#![deny(dead_code)] pub fn x() {}"))
137 .is_ok());
138 }
139
140 #[test]
141 fn probe_cleanup() {
142 let dir = support::out_dir().join("autocfg_test_probe_cleanup");
143 std::fs::create_dir(&dir).unwrap();
144
145 let ac = AutoCfg::with_dir(&dir).unwrap();
146 assert!(ac.probe_type("i32"));
147
148 // NB: this is not `remove_dir_all`, so it will only work if the directory
149 // is empty -- i.e. the probe should have removed any output files.
150 std::fs::remove_dir(&dir).unwrap();
151 }
152
153 #[test]
154 fn editions() {
155 let mut ac = autocfg_for_test();
156 assert!(ac.edition().is_none());
157 assert!(ac.probe_raw("").is_ok());
158
159 for (edition, minor) in vec![(2015, 27), (2018, 31), (2021, 56), (2024, 85)] {
160 let edition = edition.to_string();
161 ac.set_edition(Some(edition.clone()));
162 assert_eq!(ac.edition(), Some(&*edition));
163 assert_min(&ac, 1, minor, ac.probe_raw("").is_ok());
164 }
165
166 ac.set_edition(Some("invalid".into()));
167 assert_eq!(ac.edition(), Some("invalid"));
168 assert!(ac.probe_raw("").is_err());
169
170 ac.set_edition(None);
171 assert!(ac.edition().is_none());
172 assert!(ac.probe_raw("").is_ok());
173 }
174
175 #[test]
176 fn edition_keyword_try() {
177 let mut ac = autocfg_for_test();
178
179 if ac.probe_rustc_version(1, 27) {
180 ac.set_edition(Some(2015.to_string()));
181 }
182 assert!(ac.probe_expression("{ let try = 0; try }"));
183
184 if ac.probe_rustc_version(1, 31) {
185 ac.set_edition(Some(2018.to_string()));
186 assert!(!ac.probe_expression("{ let try = 0; try }"));
187 assert!(ac.probe_expression("{ let r#try = 0; r#try }"));
188 }
189 }