#[cfg(feature = "function")]
use facet::{facet_fn, fn_shape};
#[cfg(feature = "function")]
#[test]
fn simple_function_with_basic_types() {
#[facet_fn]
fn add(x: i32, y: i32) -> i32 {
x + y
}
assert_eq!(add(2, 3), 5);
let shape = fn_shape!(add);
assert_eq!(shape.name, "add");
assert_eq!(shape.param_count, 2);
assert_eq!(shape.param_names, &["x", "y"]);
}
#[cfg(feature = "function")]
#[test]
fn function_with_string_parameter() {
#[facet_fn]
fn greet(name: String) -> String {
format!("Hello, {}!", name)
}
assert_eq!(greet("World".to_string()), "Hello, World!");
let shape = fn_shape!(greet);
assert_eq!(shape.name, "greet");
assert_eq!(shape.param_count, 1);
assert_eq!(shape.param_names, &["name"]);
}
#[cfg(feature = "function")]
#[test]
fn function_with_no_parameters() {
#[facet_fn]
fn no_params() -> &'static str {
"No parameters here!"
}
assert_eq!(no_params(), "No parameters here!");
let shape = fn_shape!(no_params);
assert_eq!(shape.name, "no_params");
assert_eq!(shape.param_count, 0);
assert!(shape.param_names.is_empty());
}
#[cfg(feature = "function")]
#[test]
fn function_with_no_return_type() {
#[facet_fn]
fn side_effect_only(x: i32) {
println!("Side effect: {}", x);
}
side_effect_only(42);
let shape = fn_shape!(side_effect_only);
assert_eq!(shape.name, "side_effect_only");
assert_eq!(shape.param_count, 1);
assert_eq!(shape.param_names, &["x"]);
}
#[cfg(feature = "function")]
#[test]
fn generic_function_with_simple_bounds() {
#[facet_fn]
fn generic_add<T: core::ops::Add<Output = T>>(x: T, y: T) -> T {
x + y
}
assert_eq!(generic_add::<i32>(4, 5), 9);
assert_eq!(generic_add::<i64>(10, 20), 30);
assert_eq!(generic_add::<usize>(7, 8), 15);
let shape_i32 = fn_shape!(generic_add<i32>);
assert_eq!(shape_i32.name, "generic_add");
assert_eq!(shape_i32.param_count, 2);
assert_eq!(shape_i32.param_names, &["x", "y"]);
let shape_i64 = fn_shape!(generic_add<i64>);
assert_eq!(shape_i64.name, "generic_add");
assert_eq!(shape_i64.param_count, 2);
assert_eq!(shape_i64.param_names, &["x", "y"]);
let shape_usize = fn_shape!(generic_add<usize>);
assert_eq!(shape_usize.name, "generic_add");
assert_eq!(shape_usize.param_count, 2);
assert_eq!(shape_usize.param_names, &["x", "y"]);
}
#[cfg(feature = "function")]
#[test]
fn function_shape_debug_output() {
#[facet_fn]
fn debug_test(a: u32, b: String) -> bool {
true
}
let shape = fn_shape!(debug_test);
let debug_output = format!("{shape:?}");
assert!(debug_output.contains("debug_test"));
assert!(debug_output.contains("param_count: 2"));
assert!(debug_output.contains("param_names"));
}
#[cfg(feature = "function")]
#[test]
fn function_shape_clone() {
#[facet_fn]
fn clone_test() -> i32 {
42
}
let shape1 = fn_shape!(clone_test);
let shape2 = shape1.clone();
assert_eq!(shape1.name, shape2.name);
assert_eq!(shape1.param_count, shape2.param_count);
assert_eq!(shape1.param_names, shape2.param_names);
}
#[cfg(all(feature = "function", feature = "doc"))]
#[test]
fn function_with_single_doc_comment() {
#[facet_fn]
fn greet(name: String) -> String {
format!("Hello, {}!", name)
}
assert_eq!(greet("World".to_string()), "Hello, World!");
let shape = fn_shape!(greet);
assert_eq!(shape.name, "greet");
assert_eq!(shape.param_count, 1);
assert_eq!(shape.param_names, &["name"]);
assert_eq!(shape.documentation.len(), 1);
assert_eq!(shape.documentation[0], " Single line documentation");
}
#[cfg(all(feature = "function", feature = "doc"))]
#[test]
fn function_with_multiple_doc_comments() {
#[facet_fn]
fn add(x: i32, y: i32) -> i32 {
x + y
}
assert_eq!(add(2, 3), 5);
let shape = fn_shape!(add);
assert_eq!(shape.name, "add");
assert_eq!(shape.param_count, 2);
assert_eq!(shape.param_names, &["x", "y"]);
assert_eq!(shape.documentation.len(), 2);
assert_eq!(shape.documentation[0], " This is a test function");
assert_eq!(shape.documentation[1], " that does addition of two numbers");
}
#[cfg(all(feature = "function", feature = "doc"))]
#[test]
fn function_with_doc_comments_and_quotes() {
#[facet_fn]
fn greet_suspicious(name: String) -> String {
format!("Hello, {}...?", name)
}
assert_eq!(greet_suspicious("Alice".to_string()), "Hello, Alice...?");
let shape = fn_shape!(greet_suspicious);
assert_eq!(shape.name, "greet_suspicious");
assert_eq!(shape.param_count, 1);
assert_eq!(shape.param_names, &["name"]);
assert_eq!(shape.documentation.len(), 1);
assert_eq!(
shape.documentation[0],
" Hello \"world\", if that is your real name"
);
}
#[cfg(feature = "function")]
#[test]
fn function_without_doc_comments() {
#[facet_fn]
fn no_docs(x: i32) -> i32 {
x * 2
}
assert_eq!(no_docs(5), 10);
let shape = fn_shape!(no_docs);
assert_eq!(shape.name, "no_docs");
assert_eq!(shape.param_count, 1);
assert_eq!(shape.param_names, &["x"]);
assert!(shape.documentation.is_empty());
}