@@ -26,6 +26,8 @@ type VolatileItem = Box<dyn Fn(VolatileItemKind) -> bool>;
26
26
type ArrayArg = Box < dyn Fn ( crate :: Fn , Parameter ) -> bool > ;
27
27
/// A function that determines whether to skip a test, taking in the identifier name.
28
28
type SkipTest = Box < dyn Fn ( & str ) -> bool > ;
29
+ /// A function that determines whether a type alias is a c enum.
30
+ type CEnum = Box < dyn Fn ( & str ) -> bool > ;
29
31
30
32
/// A builder used to generate a test suite.
31
33
#[ derive( Default ) ]
@@ -42,6 +44,7 @@ pub struct TestGenerator {
42
44
pub ( crate ) skips : Vec < Skip > ,
43
45
pub ( crate ) verbose_skip : bool ,
44
46
pub ( crate ) volatile_items : Vec < VolatileItem > ,
47
+ pub ( crate ) c_enums : Vec < CEnum > ,
45
48
pub ( crate ) array_arg : Option < ArrayArg > ,
46
49
pub ( crate ) skip_private : bool ,
47
50
pub ( crate ) skip_roundtrip : Option < SkipTest > ,
@@ -206,6 +209,20 @@ impl TestGenerator {
206
209
self
207
210
}
208
211
212
+ /// Indicate that a type alias is actually a C enum.
213
+ ///
214
+ /// # Examples
215
+ /// ```no_run
216
+ /// use ctest::TestGenerator;
217
+ ///
218
+ /// let mut cfg = TestGenerator::new();
219
+ /// cfg.alias_is_c_enum(|e| e == "pid_type");
220
+ /// ```
221
+ pub fn alias_is_c_enum ( & mut self , f : impl Fn ( & str ) -> bool + ' static ) -> & mut Self {
222
+ self . c_enums . push ( Box :: new ( f) ) ;
223
+ self
224
+ }
225
+
209
226
/// Indicate that a struct field should be marked `volatile`.
210
227
///
211
228
/// # Examples
@@ -516,6 +533,30 @@ impl TestGenerator {
516
533
self
517
534
}
518
535
536
+ /// Configures whether tests for a C enum are generated.
537
+ ///
538
+ /// A C enum consists of a type alias, as well as constants that have the same type. Tests
539
+ /// for both the alias as well as the constants are skipped.
540
+ ///
541
+ /// # Examples
542
+ ///
543
+ /// ```no_run
544
+ /// use ctest::TestGenerator;
545
+ ///
546
+ /// let mut cfg = TestGenerator::new();
547
+ /// cfg.skip_c_enum(|e| e == "pid_type");
548
+ /// ```
549
+ pub fn skip_c_enum ( & mut self , f : impl Fn ( & str ) -> bool + ' static ) -> & mut Self {
550
+ self . skips . push ( Box :: new ( move |item| {
551
+ if let MapInput :: CEnumType ( e) = item {
552
+ f ( e)
553
+ } else {
554
+ false
555
+ }
556
+ } ) ) ;
557
+ self
558
+ }
559
+
519
560
/// Add a flag to the C compiler invocation.
520
561
///
521
562
/// # Examples
@@ -976,6 +1017,7 @@ impl TestGenerator {
976
1017
MapInput :: UnionField ( _, f) => f. ident ( ) . to_string ( ) ,
977
1018
MapInput :: StructType ( ty) => format ! ( "struct {ty}" ) ,
978
1019
MapInput :: UnionType ( ty) => format ! ( "union {ty}" ) ,
1020
+ MapInput :: CEnumType ( ty) => format ! ( "enum {ty}" ) ,
979
1021
MapInput :: StructFieldType ( _, f) => f. ident ( ) . to_string ( ) ,
980
1022
MapInput :: UnionFieldType ( _, f) => f. ident ( ) . to_string ( ) ,
981
1023
MapInput :: Type ( ty) => translate_primitive_type ( ty) ,
0 commit comments