Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions agdb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ serde = { version = "1", features = ["derive"], optional = true }

[dev-dependencies]
serde_json = { version = "1" }
tokio = { version = "1", features = ["macros", "rt"] }
2 changes: 1 addition & 1 deletion agdb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub use agdb_derive::{DbElement, DbSerialize, DbType, DbTypeMarker, DbValue};
#[cfg(feature = "api")]
pub mod type_def;
#[cfg(feature = "api")]
pub use agdb_derive::{TypeDef, TypeDefImpl, fn_def, impl_def, trait_def};
pub use agdb_derive::{TypeDef, TypeDefImpl, fn_def, impl_def, test_def, trait_def};

pub use db::Db;
pub use db::DbAny;
Expand Down
4 changes: 4 additions & 0 deletions agdb/src/type_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub trait TypeDefinition {
match Self::type_def() {
Type::Enum(e) => e.generics.iter().map(|g| g.name).collect(),
Type::Function(f) => f.generics.iter().map(|g| g.name).collect(),
Type::Test(f) => f.generics.iter().map(|g| g.name).collect(),
Type::Struct(s) => s.generics.iter().map(|g| g.name).collect(),
Type::Trait(t) => t.generics.iter().map(|g| g.name).collect(),
Type::Impl(i) => i.generics.iter().map(|g| g.name).collect(),
Expand Down Expand Up @@ -51,6 +52,7 @@ pub trait ImplDefinition: TypeDefinition {
pub enum Type {
Enum(Enum),
Function(Function),
Test(Function),
Generic(Generic),
Impl(Impl),
Literal(Literal),
Expand All @@ -71,6 +73,7 @@ impl Type {
match self {
Type::Enum(e) => e.name,
Type::Function(_) => "fn",
Type::Test(_) => "test",
Type::Generic(g) => g.name,
Type::Impl(i) => i.name,
Type::Literal(l) => l.name(),
Expand Down Expand Up @@ -379,6 +382,7 @@ mod tests {

assert_eq!(def.name, "Type");
assert!(def.variants.iter().any(|v| v.name == "Function"));
assert!(def.variants.iter().any(|v| v.name == "Test"));
assert!(def.variants.iter().any(|v| v.name == "Trait"));
}

Expand Down
66 changes: 66 additions & 0 deletions agdb/src/type_def/expression_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ mod tests {
"tuple_expr" => __tuple_expr_type_def(),
"format_expr" => __format_expr_type_def(),
"vec_macro_expr" => __vec_macro_expr_type_def(),
"assert_macros_expr" => __assert_macros_expr_type_def(),
"match_expr" => __match_expr_type_def(),
"struct_expr" => __struct_expr_type_def(),
"implicit_return" => __implicit_return_type_def(),
Expand Down Expand Up @@ -1247,6 +1248,71 @@ mod tests {
}
}

#[agdb::fn_def]
#[allow(unused)]
fn assert_macros_expr() {
let x = 42;
assert!(x > 0);
assert_eq!(x, 42);
let _matched = matches!(Some(x), Some(42));
}

#[test]
fn test_assert_macros() {
let body = get_body("assert_macros_expr");
assert_eq!(body.len(), 4);

assert!(
matches!(
&body[1],
Expression::Call {
function: Expression::Path {
ident: "assert",
..
},
..
}
),
"Got: {:?}",
body[1]
);

assert!(
matches!(
&body[2],
Expression::Call {
function: Expression::Path {
ident: "assert_eq",
..
},
..
}
),
"Got: {:?}",
body[2]
);

match &body[3] {
Expression::Let {
value: Some(Expression::Call { function, .. }),
..
} => {
assert!(
matches!(
function,
Expression::Path {
ident: "matches",
..
}
),
"Got: {:?}",
function
);
}
_ => panic!("Got: {:?}", body[3]),
}
}

#[agdb::fn_def]
#[allow(unused)]
fn match_expr() -> i32 {
Expand Down
85 changes: 85 additions & 0 deletions agdb/src/type_def/function_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,95 @@ pub struct Function {

#[cfg(test)]
mod tests {
use crate::type_def::Expression;
use crate::type_def::GenericKind;
use crate::type_def::Literal;
use crate::type_def::Type;

#[agdb::test_def]
#[test]
fn plain_test_function() {
let value = 1;
assert!(value == 1);
}

#[agdb::test_def]
#[tokio::test]
async fn tokio_async_test_function() {
let value = 1;
assert_eq!(value, 1);
assert!(matches!(Some(value), Some(1)));
}

#[test]
fn plain_test_type_def() {
let Type::Test(def) = __plain_test_function_type_def() else {
panic!("Expected a test type definition");
};

assert_eq!(def.name, "plain_test_function");
assert!(!def.async_fn);
assert_eq!(def.body.len(), 2);
assert!(
matches!(
&def.body[1],
Expression::Call {
function: Expression::Path {
ident: "assert",
..
},
..
}
),
"Got: {:?}",
def.body[1]
);
}

#[test]
fn tokio_test_type_def() {
let Type::Test(def) = __tokio_async_test_function_type_def() else {
panic!("Expected a test type definition");
};

assert_eq!(def.name, "tokio_async_test_function");
assert!(def.async_fn);
assert_eq!(def.body.len(), 3);

assert!(
matches!(
&def.body[1],
Expression::Call {
function: Expression::Path {
ident: "assert_eq",
..
},
..
}
),
"Got: {:?}",
def.body[1]
);

let Expression::Call { args, .. } = &def.body[2] else {
panic!("Expected assert! call in body");
};
assert!(
matches!(
&args[0],
Expression::Call {
function: Expression::Path {
ident: "matches",
..
},
..
}
),
"Got: {:?}",
args[0]
);
}

#[test]
fn empty_function() {
#[agdb::fn_def]
Expand Down
97 changes: 0 additions & 97 deletions agdb_derive/src/api_def.rs

This file was deleted.

Loading