Skip to content

Commit 5fb5060

Browse files
committed
ctest: Add extraction of relevant types.
1 parent 7d3fb2b commit 5fb5060

File tree

11 files changed

+66
-20
lines changed

11 files changed

+66
-20
lines changed

ctest-next/src/ast/constant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub struct Const {
1414

1515
impl Const {
1616
/// Return the identifier of the constant as a string.
17-
pub fn ident(&self) -> &BoxStr {
17+
pub fn ident(&self) -> &str {
1818
&self.ident
1919
}
2020
}

ctest-next/src/ast/field.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub struct Field {
1212

1313
impl Field {
1414
/// Return the identifier of the field as a string if it exists.
15-
pub fn ident(&self) -> &BoxStr {
15+
pub fn ident(&self) -> &str {
1616
&self.ident
1717
}
1818
}

ctest-next/src/ast/function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub struct Fn {
1818

1919
impl Fn {
2020
/// Return the identifier of the function as a string.
21-
pub fn ident(&self) -> &BoxStr {
21+
pub fn ident(&self) -> &str {
2222
&self.ident
2323
}
2424
}

ctest-next/src/ast/static_variable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct Static {
1717

1818
impl Static {
1919
/// Return the identifier of the static variable as a string.
20-
pub fn ident(&self) -> &BoxStr {
20+
pub fn ident(&self) -> &str {
2121
&self.ident
2222
}
2323
}

ctest-next/src/ast/structure.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub struct Struct {
1212

1313
impl Struct {
1414
/// Return the identifier of the struct as a string.
15-
pub fn ident(&self) -> &BoxStr {
15+
pub fn ident(&self) -> &str {
1616
&self.ident
1717
}
1818
}

ctest-next/src/ast/type_alias.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub struct Type {
1212

1313
impl Type {
1414
/// Return the identifier of the type alias as a string.
15-
pub fn ident(&self) -> &BoxStr {
15+
pub fn ident(&self) -> &str {
1616
&self.ident
1717
}
1818
}

ctest-next/src/ast/union.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub struct Union {
1212

1313
impl Union {
1414
/// Return the identifier of the union as a string.
15-
pub fn ident(&self) -> &BoxStr {
15+
pub fn ident(&self) -> &str {
1616
&self.ident
1717
}
1818
}

ctest-next/src/ffi_items.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{Abi, Const, Field, Fn, Parameter, Static, Struct, Type, Union};
77
/// Represents a collected set of top-level Rust items relevant to FFI generation or analysis.
88
///
99
/// Includes foreign functions/statics, type aliases, structs, unions, and constants.
10-
#[derive(Default)]
10+
#[derive(Default, Clone, Debug)]
1111
pub struct FfiItems {
1212
aliases: Vec<Type>,
1313
structs: Vec<Struct>,
@@ -27,14 +27,12 @@ impl FfiItems {
2727
pub fn contains_struct(&self, ident: &str) -> bool {
2828
self.structs()
2929
.iter()
30-
.any(|structure| structure.ident().as_ref() == ident)
30+
.any(|structure| structure.ident() == ident)
3131
}
3232

3333
/// Return whether the type has parsed a union with the given identifier.
3434
pub fn contains_union(&self, ident: &str) -> bool {
35-
self.unions()
36-
.iter()
37-
.any(|union| union.ident().as_ref() == ident)
35+
self.unions().iter().any(|union| union.ident() == ident)
3836
}
3937

4038
/// Return a list of all type aliases found.

ctest-next/src/generator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{expand, FfiItems, Result};
66

77
/// A builder used to generate a test suite.
88
#[non_exhaustive]
9-
#[derive(Default)]
9+
#[derive(Default, Debug, Clone)]
1010
pub struct TestGenerator {}
1111

1212
impl TestGenerator {

ctest-next/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![warn(missing_docs)]
22
#![warn(unreachable_pub)]
3+
#![warn(missing_debug_implementations)]
34

45
//! # ctest2 - an FFI binding validator
56
//!
@@ -22,4 +23,4 @@ pub type Error = Box<dyn std::error::Error>;
2223
/// A type alias for `std::result::Result` that defaults to our error type.
2324
pub type Result<T, E = Error> = std::result::Result<T, E>;
2425
/// A boxed string for representing identifiers.
25-
pub type BoxStr = Box<str>;
26+
type BoxStr = Box<str>;

ctest-next/tests/extraction.rs

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,57 @@ fn test_extraction_ffi_items() {
1010
let mut ffi_items = FfiItems::new();
1111
ffi_items.visit_file(&ast);
1212

13-
assert_eq!(ffi_items.aliases().len(), 1);
14-
assert_eq!(ffi_items.constants().len(), 1);
15-
assert_eq!(ffi_items.foreign_functions().len(), 1);
16-
assert_eq!(ffi_items.foreign_statics().len(), 1);
17-
assert_eq!(ffi_items.structs().len(), 1);
18-
assert_eq!(ffi_items.unions().len(), 1);
13+
assert_eq!(
14+
ffi_items
15+
.aliases()
16+
.iter()
17+
.map(|a| a.ident())
18+
.collect::<Vec<_>>(),
19+
["Foo"]
20+
);
21+
22+
assert_eq!(
23+
ffi_items
24+
.constants()
25+
.iter()
26+
.map(|a| a.ident())
27+
.collect::<Vec<_>>(),
28+
["bar"]
29+
);
30+
31+
assert_eq!(
32+
ffi_items
33+
.foreign_functions()
34+
.iter()
35+
.map(|a| a.ident())
36+
.collect::<Vec<_>>(),
37+
["malloc"]
38+
);
39+
40+
assert_eq!(
41+
ffi_items
42+
.foreign_statics()
43+
.iter()
44+
.map(|a| a.ident())
45+
.collect::<Vec<_>>(),
46+
["baz"]
47+
);
48+
49+
assert_eq!(
50+
ffi_items
51+
.structs()
52+
.iter()
53+
.map(|a| a.ident())
54+
.collect::<Vec<_>>(),
55+
["Array"]
56+
);
57+
58+
assert_eq!(
59+
ffi_items
60+
.unions()
61+
.iter()
62+
.map(|a| a.ident())
63+
.collect::<Vec<_>>(),
64+
["Word"]
65+
);
1966
}

0 commit comments

Comments
 (0)