Skip to content

Commit

Permalink
tests: increase test case coverage for C API
Browse files Browse the repository at this point in the history
  • Loading branch information
plusvic committed Feb 14, 2024
1 parent 995ae48 commit 9b93c30
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions capi/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use crate::compiler::{
yrx_compiler_add_source, yrx_compiler_build, yrx_compiler_create,
yrx_compiler_define_global_bool, yrx_compiler_define_global_float,
yrx_compiler_define_global_int, yrx_compiler_define_global_str,
yrx_compiler_destroy, yrx_compiler_new_namespace,
};
use crate::{
yrx_scanner_create, yrx_scanner_on_matching_rule, yrx_scanner_scan,
YRX_RULE,
yrx_scanner_create, yrx_scanner_destroy, yrx_scanner_on_matching_rule,
yrx_scanner_scan, YRX_RULE,
};
use std::ffi::{c_void, CString};

Expand All @@ -19,13 +22,40 @@ fn capi() {
let mut compiler = std::ptr::null_mut();
yrx_compiler_create(&mut compiler);

let src =
CString::new(b"rule test {condition: true}".to_vec()).unwrap();
let src = CString::new(
b"rule test {\
condition: \
some_bool and \
some_str == \"some_str\" and \
some_int == 1 and \
some_float == 3.14 \
}"
.to_vec(),
)
.unwrap();

let some_bool = CString::new(b"some_bool".to_vec()).unwrap();
let some_str = CString::new(b"some_str".to_vec()).unwrap();
let some_int = CString::new(b"some_int".to_vec()).unwrap();
let some_float = CString::new(b"some_float".to_vec()).unwrap();

yrx_compiler_define_global_int(compiler, some_int.as_ptr(), 1);
yrx_compiler_define_global_float(compiler, some_float.as_ptr(), 3.14);
yrx_compiler_define_global_bool(compiler, some_bool.as_ptr(), true);
yrx_compiler_define_global_str(
compiler,
some_str.as_ptr(),
some_str.as_ptr(),
);

let namespace = CString::new(b"foo".to_vec()).unwrap();
yrx_compiler_new_namespace(compiler, namespace.as_ptr());
yrx_compiler_add_source(compiler, src.as_ptr());

let rules = yrx_compiler_build(compiler);

yrx_compiler_destroy(compiler);

let mut scanner = std::ptr::null_mut();
yrx_scanner_create(rules, &mut scanner);

Expand All @@ -38,6 +68,7 @@ fn capi() {
);

yrx_scanner_scan(scanner, std::ptr::null(), 0);
yrx_scanner_destroy(scanner);

assert_eq!(matches, 1);
}
Expand Down

0 comments on commit 9b93c30

Please sign in to comment.