Skip to content

Commit 41686fb

Browse files
committed
test : kclvm-cli c api call
1 parent 91d8349 commit 41686fb

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

kclvm/src/api_test.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use crate::*;
2+
use kclvm_api::model::gpyrpc::*;
3+
use kclvm_api::service::util::*;
4+
use std::ffi::CStr;
5+
use std::fs;
6+
use std::path::Path;
7+
const TEST_DATA_PATH: &str = "./src/testdata";
8+
9+
#[test]
10+
fn test_c_api_call_exec_program() {
11+
let serv = kclvm_service_new();
12+
let input_path = Path::new(TEST_DATA_PATH).join("exec-program.json");
13+
let input = fs::read_to_string(&input_path)
14+
.expect(format!("Something went wrong reading {}", input_path.display()).as_str());
15+
let args = unsafe {
16+
CString::from_vec_unchecked(transform_json_to_protobuf::<ExecProgram_Args>(&input))
17+
};
18+
let call = CString::new("KclvmService.ExecProgram").unwrap();
19+
let result = unsafe { CStr::from_ptr(kclvm_service_call(serv, call.as_ptr(), args.as_ptr())) };
20+
21+
let result = parse_message_from_protobuf::<ExecProgram_Result>(result.to_bytes());
22+
let except_result_path = Path::new(TEST_DATA_PATH).join("exec-program.response.json");
23+
let except_result_json = fs::read_to_string(&except_result_path).expect(
24+
format!(
25+
"Something went wrong reading {}",
26+
except_result_path.display()
27+
)
28+
.as_str(),
29+
);
30+
let except_result = parse_message_from_json::<ExecProgram_Result>(&except_result_json);
31+
assert_eq!(result.json_result, except_result.json_result);
32+
assert_eq!(result.yaml_result, except_result.yaml_result);
33+
34+
kclvm_service_delete(serv);
35+
}

kclvm/src/testdata/exec-program.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
{
3+
"work_dir" : "./src/testdata",
4+
"k_filename_list":[
5+
"hello.k"
6+
],
7+
"k_code_list":[
8+
"a=1"
9+
]
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"json_result": "{\"a\": 1}",
3+
"yaml_result": "a: 1\n",
4+
"escaped_time": "0.002061128616333008"
5+
}

kclvm/src/testdata/hello.k

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a = 1

0 commit comments

Comments
 (0)