Skip to main content

gobject_ast/
lib.rs

1pub mod model;
2mod parser;
3
4pub use parser::Parser;
5
6#[cfg(test)]
7mod tests {
8    use std::path::Path;
9
10    use super::Parser;
11
12    #[test]
13    fn test_includes_simple() {
14        let mut parser = Parser::new().unwrap();
15        let project = parser
16            .parse_file(Path::new("tests/fixtures/includes_simple.c"))
17            .unwrap();
18
19        assert_eq!(project.files.len(), 1);
20        let file = project.files.values().next().unwrap();
21
22        let include_data: Vec<(&str, bool, _)> = file.iter_all_includes().collect();
23
24        // Should parse all 7 includes
25        assert_eq!(
26            include_data.len(),
27            7,
28            "Expected 7 includes, got {}",
29            include_data.len()
30        );
31
32        assert_eq!(include_data[0].0, "config.h");
33        assert!(!include_data[0].1, "config.h should not be system");
34
35        assert_eq!(include_data[1].0, "math.h");
36        assert!(include_data[1].1, "math.h should be system");
37
38        assert_eq!(include_data[2].0, "stdio.h");
39        assert!(include_data[2].1, "stdio.h should be system");
40
41        assert_eq!(include_data[3].0, "glib.h");
42        assert!(include_data[3].1, "glib.h should be system");
43
44        assert_eq!(include_data[4].0, "gtk/gtk.h");
45        assert!(include_data[4].1, "gtk/gtk.h should be system");
46
47        assert_eq!(include_data[5].0, "foo.h");
48        assert!(!include_data[5].1, "foo.h should not be system");
49
50        assert_eq!(include_data[6].0, "bar/baz.h");
51        assert!(!include_data[6].1, "bar/baz.h should not be system");
52    }
53
54    #[test]
55    fn test_includes_with_conditionals() {
56        let mut parser = Parser::new().unwrap();
57        let project = parser
58            .parse_file(Path::new("tests/fixtures/includes_with_conditionals.c"))
59            .unwrap();
60
61        assert_eq!(project.files.len(), 1);
62        let file = project.files.values().next().unwrap();
63
64        let include_data: Vec<(&str, bool, _)> = file.iter_all_includes().collect();
65
66        // Should parse ALL includes, including those inside #ifdef blocks
67        // config.h, math.h, gobject/gvaluecollector.h, pango/pangocairo.h (in ifdef),
68        // cogl/cogl.h, clutter-actor-private.h, clutter-actor-pango.h (in ifdef),
69        // clutter-pango-private.h (in ifdef), clutter-action.h = 9 total
70        assert_eq!(
71            include_data.len(),
72            9,
73            "Expected 9 includes (including those in #ifdef), got {}",
74            include_data.len()
75        );
76
77        // Verify we got the includes from inside #ifdef blocks
78        let paths: Vec<&str> = include_data.iter().map(|(path, ..)| *path).collect();
79        assert!(
80            paths.contains(&"pango/pangocairo.h"),
81            "Missing include from first #ifdef"
82        );
83        assert!(
84            paths.contains(&"clutter/pango/clutter-actor-pango.h"),
85            "Missing include from second #ifdef"
86        );
87        assert!(
88            paths.contains(&"clutter/pango/clutter-pango-private.h"),
89            "Missing include from second #ifdef"
90        );
91    }
92}