Skip to content

Commit cb0cf3b

Browse files
authored
Merge pull request #141 from Peefy/rust-plugin-unit-tests
test: add kcl rust plugin unit tests
2 parents 24046d6 + 60d6d93 commit cb0cf3b

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ license = "Apache-2.0"
1212
[dependencies]
1313
anyhow = "1"
1414
kclvm-api = { git = "https://github.com/kcl-lang/kcl", version = "0.10.0-rc.1" }
15+
kclvm-evaluator = { git = "https://github.com/kcl-lang/kcl", version = "0.10.0-rc.1" }
16+
kclvm-loader = { git = "https://github.com/kcl-lang/kcl", version = "0.10.0-rc.1" }
17+
kclvm-parser = { git = "https://github.com/kcl-lang/kcl", version = "0.10.0-rc.1" }
18+
kclvm-runtime = { git = "https://github.com/kcl-lang/kcl", version = "0.10.0-rc.1" }

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ use anyhow::Result;
3333

3434
pub type API = KclvmServiceImpl;
3535

36+
#[cfg(test)]
37+
mod tests;
38+
3639
/// Call KCL API with the API name and argument protobuf bytes.
3740
#[inline]
3841
pub fn call<'a>(name: &'a [u8], args: &'a [u8]) -> Result<Vec<u8>> {

src/tests.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use anyhow::{anyhow, Result};
2+
use kclvm_evaluator::Evaluator;
3+
use kclvm_loader::{load_packages, LoadPackageOptions};
4+
use kclvm_parser::LoadProgramOptions;
5+
use kclvm_runtime::{Context, IndexMap, PluginFunction, ValueRef};
6+
use std::{cell::RefCell, rc::Rc, sync::Arc};
7+
8+
fn my_plugin_sum(_: &Context, args: &ValueRef, _: &ValueRef) -> Result<ValueRef> {
9+
let a = args
10+
.arg_i_int(0, Some(0))
11+
.ok_or(anyhow!("expect int value for the first param"))?;
12+
let b = args
13+
.arg_i_int(1, Some(0))
14+
.ok_or(anyhow!("expect int value for the second param"))?;
15+
Ok((a + b).into())
16+
}
17+
18+
fn context_with_plugin() -> Rc<RefCell<Context>> {
19+
let mut plugin_functions: IndexMap<String, PluginFunction> = Default::default();
20+
let func = Arc::new(my_plugin_sum);
21+
plugin_functions.insert("my_plugin.add".to_string(), func);
22+
let mut ctx = Context::new();
23+
ctx.plugin_functions = plugin_functions;
24+
Rc::new(RefCell::new(ctx))
25+
}
26+
27+
#[test]
28+
fn test_exec_with_plugin() -> Result<()> {
29+
let src = r#"
30+
import kcl_plugin.my_plugin
31+
32+
sum = my_plugin.add(1, 1)
33+
"#;
34+
let p = load_packages(&LoadPackageOptions {
35+
paths: vec!["test.k".to_string()],
36+
load_opts: Some(LoadProgramOptions {
37+
load_plugins: true,
38+
k_code_list: vec![src.to_string()],
39+
..Default::default()
40+
}),
41+
load_builtin: false,
42+
..Default::default()
43+
})?;
44+
let evaluator = Evaluator::new_with_runtime_ctx(&p.program, context_with_plugin());
45+
let result = evaluator.run()?;
46+
println!("yaml result {}", result.1);
47+
Ok(())
48+
}

0 commit comments

Comments
 (0)