Skip to content

Commit

Permalink
Rust: add some tests for the crate graph
Browse files Browse the repository at this point in the history
  • Loading branch information
aibaars committed Feb 27, 2025
1 parent 22caf56 commit ed57708
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 0 deletions.
117 changes: 117 additions & 0 deletions rust/ql/test/extractor-tests/crate_graph/crates.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#-----| Crate([email protected])

#-----| Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])

#-----| Crate([email protected])
#-----| -> Crate([email protected])

#-----| Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])

#-----| Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])

#-----| Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])

#-----| Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])

#-----| Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])

#-----| Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])

#-----| Crate([email protected])
#-----| -> Crate([email protected])

#-----| Crate([email protected])

#-----| Crate([email protected])
#-----| -> Crate([email protected])

#-----| Crate([email protected])
#-----| -> Crate([email protected])

#-----| Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])

#-----| Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])

#-----| Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])

#-----| Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])

#-----| Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])

#-----| Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])

#-----| Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])

#-----| Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])

#-----| Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
#-----| -> Crate([email protected])
11 changes: 11 additions & 0 deletions rust/ql/test/extractor-tests/crate_graph/crates.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @id crate-graph
* @name Crate Graph
* @kind graph
*/

import rust

query predicate nodes(Crate c) { any() }

query predicate edges(Crate c1, Crate c2) { c1.getADependency() = c2 }
3 changes: 3 additions & 0 deletions rust/ql/test/extractor-tests/crate_graph/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println! {"Hello world"}
}
42 changes: 42 additions & 0 deletions rust/ql/test/extractor-tests/crate_graph/module.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::fmt;

pub enum X {
A,
B,
}

pub struct X_List {
x: X,
tail: Option<Box<X_List>>,
}

pub fn length(list: X_List) -> usize {
match list {
X_List { x: _, tail: None } => 1,
X_List {
x: _,
tail: Some(tail),
} => 1 + length(*tail),
}
}
pub trait AsString {
fn as_string(&self) -> &str;
}

impl AsString for X {
fn as_string(&self) -> &str {
match self {
X::A => "a",
X::B => "b",
}
}
}

impl fmt::Display for X {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_string())
}
}

pub const X_A: X = X::A;
pub static X_B: X = X::B;
40 changes: 40 additions & 0 deletions rust/ql/test/extractor-tests/crate_graph/modules.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#-----| fn as_string

#-----| fn length

#-----| fn write

#-----| fn as_string

#-----| fn fmt

#-----| struct X_List

#-----| Static

#-----| mod module
#-----| -> fn length
#-----| -> fn write
#-----| -> struct X_List
#-----| -> Static
#-----| -> trait AsString
#-----| -> Const
#-----| -> impl AsString for ...::X { ... }
#-----| -> impl ...::Display for ...::X { ... }
#-----| -> enum X

#-----| mod crate
#-----| -> mod module

#-----| trait AsString
#-----| -> fn as_string

#-----| Const

#-----| impl AsString for ...::X { ... }
#-----| -> fn as_string

#-----| impl ...::Display for ...::X { ... }
#-----| -> fn fmt

#-----| enum X
17 changes: 17 additions & 0 deletions rust/ql/test/extractor-tests/crate_graph/modules.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @id module-graph
* @name Module and Item Graph
* @kind graph
*/

import rust

query predicate nodes(Item i) {
i.getParentNode*() = any(Crate m | m.getName() = "test" and m.getVersion() = "0.0.1").getModule()
}

query predicate edges(Item container, Item element) {
element = container.(Module).getItemList().getAnItem() or
element = container.(Impl).getAssocItemList().getAnAssocItem() or
element = container.(Trait).getAssocItemList().getAnAssocItem()
}

0 comments on commit ed57708

Please sign in to comment.