Skip to content

Commit cf39c12

Browse files
committed
tests simplified
1 parent 868207f commit cf39c12

File tree

1 file changed

+48
-113
lines changed

1 file changed

+48
-113
lines changed

prebindgen/src/codegen/tests/cfg_expr.rs

Lines changed: 48 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::collections::HashSet;
2-
31
use crate::{codegen::cfg_expr::CfgExpr, codegen::CfgExprRules, SourceLocation};
42

53
#[test]
@@ -28,37 +26,30 @@ fn test_target_vendor_os_env_parse() {
2826

2927
#[test]
3028
fn test_target_filters_processing() {
31-
use std::collections::HashMap;
32-
let enabled_features = HashSet::new();
33-
let disabled_features = HashSet::new();
34-
let feature_mappings: HashMap<String, String> = HashMap::new();
3529
let src = SourceLocation::default();
3630

3731
// With no selection, keep predicates as-is
3832
let expr = CfgExpr::TargetOs("macos".into());
33+
assert_eq!(expr.apply_rules(&CfgExprRules::default(), &src), Some(CfgExpr::TargetOs("macos".into())));
34+
35+
// No selection for arch/vendor/env should also keep predicates as-is
3936
assert_eq!(
40-
expr.apply_rules(
41-
&CfgExprRules {
42-
enabled_features: enabled_features.clone(),
43-
disabled_features: disabled_features.clone(),
44-
feature_mappings: feature_mappings.clone(),
45-
..Default::default()
46-
},
47-
&src,
48-
),
49-
Some(CfgExpr::TargetOs("macos".into()))
37+
CfgExpr::TargetArch("x86_64".into()).apply_rules(&CfgExprRules::default(), &src),
38+
Some(CfgExpr::TargetArch("x86_64".into()))
39+
);
40+
assert_eq!(
41+
CfgExpr::TargetVendor("apple".into()).apply_rules(&CfgExprRules::default(), &src),
42+
Some(CfgExpr::TargetVendor("apple".into()))
43+
);
44+
assert_eq!(
45+
CfgExpr::TargetEnv("gnu".into()).apply_rules(&CfgExprRules::default(), &src),
46+
Some(CfgExpr::TargetEnv("gnu".into()))
5047
);
5148

5249
// Select OS = macos: becomes true (None)
5350
assert_eq!(
5451
CfgExpr::TargetOs("macos".into()).apply_rules(
55-
&CfgExprRules {
56-
enabled_features: enabled_features.clone(),
57-
disabled_features: disabled_features.clone(),
58-
feature_mappings: feature_mappings.clone(),
59-
enabled_target_os: Some("macos".into()),
60-
..Default::default()
61-
},
52+
&CfgExprRules { enabled_target_os: Some("macos".into()), ..Default::default() },
6253
&src,
6354
),
6455
None
@@ -67,13 +58,7 @@ fn test_target_filters_processing() {
6758
// Non-matching becomes False
6859
assert_eq!(
6960
CfgExpr::TargetOs("linux".into()).apply_rules(
70-
&CfgExprRules {
71-
enabled_features: enabled_features.clone(),
72-
disabled_features: disabled_features.clone(),
73-
feature_mappings: feature_mappings.clone(),
74-
enabled_target_os: Some("macos".into()),
75-
..Default::default()
76-
},
61+
&CfgExprRules { enabled_target_os: Some("macos".into()), ..Default::default() },
7762
&src,
7863
),
7964
Some(CfgExpr::False)
@@ -82,26 +67,14 @@ fn test_target_filters_processing() {
8267
// Arch selection
8368
assert_eq!(
8469
CfgExpr::TargetArch("x86_64".into()).apply_rules(
85-
&CfgExprRules {
86-
enabled_features: enabled_features.clone(),
87-
disabled_features: disabled_features.clone(),
88-
feature_mappings: feature_mappings.clone(),
89-
enabled_target_arch: Some("x86_64".into()),
90-
..Default::default()
91-
},
70+
&CfgExprRules { enabled_target_arch: Some("x86_64".into()), ..Default::default() },
9271
&src,
9372
),
9473
None
9574
);
9675
assert_eq!(
9776
CfgExpr::TargetArch("aarch64".into()).apply_rules(
98-
&CfgExprRules {
99-
enabled_features: enabled_features.clone(),
100-
disabled_features: disabled_features.clone(),
101-
feature_mappings: feature_mappings.clone(),
102-
enabled_target_arch: Some("x86_64".into()),
103-
..Default::default()
104-
},
77+
&CfgExprRules { enabled_target_arch: Some("x86_64".into()), ..Default::default() },
10578
&src,
10679
),
10780
Some(CfgExpr::False)
@@ -110,30 +83,34 @@ fn test_target_filters_processing() {
11083
// Vendor and Env selection
11184
assert_eq!(
11285
CfgExpr::TargetVendor("apple".into()).apply_rules(
113-
&CfgExprRules {
114-
enabled_features: enabled_features.clone(),
115-
disabled_features: disabled_features.clone(),
116-
feature_mappings: feature_mappings.clone(),
117-
enabled_target_vendor: Some("apple".into()),
118-
..Default::default()
119-
},
86+
&CfgExprRules { enabled_target_vendor: Some("apple".into()), ..Default::default() },
12087
&src,
12188
),
12289
None
12390
);
91+
// Non-matching vendor becomes False
92+
assert_eq!(
93+
CfgExpr::TargetVendor("unknown".into()).apply_rules(
94+
&CfgExprRules { enabled_target_vendor: Some("apple".into()), ..Default::default() },
95+
&src,
96+
),
97+
Some(CfgExpr::False)
98+
);
12499
assert_eq!(
125100
CfgExpr::TargetEnv("gnu".into()).apply_rules(
126-
&CfgExprRules {
127-
enabled_features: enabled_features.clone(),
128-
disabled_features: disabled_features.clone(),
129-
feature_mappings: feature_mappings.clone(),
130-
enabled_target_env: Some("gnu".into()),
131-
..Default::default()
132-
},
101+
&CfgExprRules { enabled_target_env: Some("gnu".into()), ..Default::default() },
133102
&src,
134103
),
135104
None
136105
);
106+
// Non-matching env becomes False
107+
assert_eq!(
108+
CfgExpr::TargetEnv("msvc".into()).apply_rules(
109+
&CfgExprRules { enabled_target_env: Some("gnu".into()), ..Default::default() },
110+
&src,
111+
),
112+
Some(CfgExpr::False)
113+
);
137114
}
138115

139116
#[test]
@@ -175,25 +152,12 @@ fn test_all_expression() {
175152

176153
#[test]
177154
fn test_strict_feature_processing() {
178-
use std::collections::HashMap;
179-
180-
let mut enabled_features = HashSet::new();
181-
enabled_features.insert("feature1".to_string());
182-
183-
let mut disabled_features = HashSet::new();
184-
disabled_features.insert("feature2".to_string());
185-
186-
let mut feature_mappings = HashMap::new();
187-
feature_mappings.insert("old_feature".to_string(), "new_feature".to_string());
188-
189155
// Test enabled feature - should be removed (None = always true)
190156
let expr = CfgExpr::Feature("feature1".to_string());
191157
assert_eq!(
192158
expr.apply_rules(
193159
&CfgExprRules {
194-
enabled_features: enabled_features.clone(),
195-
disabled_features: disabled_features.clone(),
196-
feature_mappings: feature_mappings.clone(),
160+
enabled_features: vec!["feature1".to_string()].into_iter().collect(),
197161
..Default::default()
198162
},
199163
&SourceLocation::default()
@@ -206,9 +170,7 @@ fn test_strict_feature_processing() {
206170
assert_eq!(
207171
expr.apply_rules(
208172
&CfgExprRules {
209-
enabled_features: enabled_features.clone(),
210-
disabled_features: disabled_features.clone(),
211-
feature_mappings: feature_mappings.clone(),
173+
disabled_features: vec!["feature2".to_string()].into_iter().collect(),
212174
..Default::default()
213175
},
214176
&SourceLocation::default()
@@ -221,9 +183,9 @@ fn test_strict_feature_processing() {
221183
assert_eq!(
222184
expr.apply_rules(
223185
&CfgExprRules {
224-
enabled_features: enabled_features.clone(),
225-
disabled_features: disabled_features.clone(),
226-
feature_mappings: feature_mappings.clone(),
186+
feature_mappings: vec![("old_feature".to_string(), "new_feature".to_string())]
187+
.into_iter()
188+
.collect(),
227189
..Default::default()
228190
},
229191
&SourceLocation::default()
@@ -239,9 +201,8 @@ fn test_strict_feature_processing() {
239201
assert_eq!(
240202
expr.apply_rules(
241203
&CfgExprRules {
242-
enabled_features: enabled_features.clone(),
243-
disabled_features: disabled_features.clone(),
244-
feature_mappings: feature_mappings.clone(),
204+
enabled_features: vec!["feature1".to_string()].into_iter().collect(),
205+
disabled_features: vec!["feature2".to_string()].into_iter().collect(),
245206
..Default::default()
246207
},
247208
&SourceLocation::default()
@@ -257,9 +218,8 @@ fn test_strict_feature_processing() {
257218
assert_eq!(
258219
expr.apply_rules(
259220
&CfgExprRules {
260-
enabled_features: enabled_features.clone(),
261-
disabled_features: disabled_features.clone(),
262-
feature_mappings: feature_mappings.clone(),
221+
enabled_features: vec!["feature1".to_string()].into_iter().collect(),
222+
disabled_features: vec!["feature2".to_string()].into_iter().collect(),
263223
..Default::default()
264224
},
265225
&SourceLocation::default()
@@ -272,9 +232,7 @@ fn test_strict_feature_processing() {
272232
assert_eq!(
273233
expr.apply_rules(
274234
&CfgExprRules {
275-
enabled_features: enabled_features.clone(),
276-
disabled_features: disabled_features.clone(),
277-
feature_mappings: feature_mappings.clone(),
235+
disabled_features: vec!["feature2".to_string()].into_iter().collect(),
278236
..Default::default()
279237
},
280238
&SourceLocation::default()
@@ -287,9 +245,7 @@ fn test_strict_feature_processing() {
287245
assert_eq!(
288246
expr.apply_rules(
289247
&CfgExprRules {
290-
enabled_features: enabled_features.clone(),
291-
disabled_features: disabled_features.clone(),
292-
feature_mappings: feature_mappings.clone(),
248+
enabled_features: vec!["feature1".to_string()].into_iter().collect(),
293249
..Default::default()
294250
},
295251
&SourceLocation::default()
@@ -301,46 +257,25 @@ fn test_strict_feature_processing() {
301257
#[test]
302258
#[should_panic(expected = "unmapped feature: unknown")]
303259
fn test_strict_feature_processing_unmapped_panic() {
304-
use std::collections::HashMap;
305-
306-
let enabled_features = HashSet::new();
307-
let disabled_features = HashSet::new();
308-
let feature_mappings = HashMap::new();
309-
310260
// Test unmapped feature - should panic
311261
let expr = CfgExpr::Feature("unknown".to_string());
312262
expr.apply_rules(
313-
&CfgExprRules {
314-
enabled_features,
315-
disabled_features,
316-
feature_mappings,
317-
..Default::default()
318-
},
263+
&CfgExprRules::default(),
319264
&SourceLocation::default(),
320265
);
321266
}
322267

323268
#[test]
324269
#[should_panic(expected = "unmapped feature: unknown")]
325270
fn test_strict_feature_processing_unmapped_in_any_panic() {
326-
use std::collections::HashMap;
327-
328-
let mut enabled_features = HashSet::new();
329-
enabled_features.insert("feature1".to_string());
330-
331-
let disabled_features = HashSet::new();
332-
let feature_mappings = HashMap::new();
333-
334271
// Test unmapped feature in any() - should panic
335272
let expr = CfgExpr::Any(vec![
336273
CfgExpr::Feature("feature1".to_string()),
337274
CfgExpr::Feature("unknown".to_string()),
338275
]);
339276
expr.apply_rules(
340277
&CfgExprRules {
341-
enabled_features,
342-
disabled_features,
343-
feature_mappings,
278+
enabled_features: vec!["feature1".to_string()].into_iter().collect(),
344279
..Default::default()
345280
},
346281
&SourceLocation::default(),

0 commit comments

Comments
 (0)