Skip to content

Commit

Permalink
test : kclvm-cli c api call
Browse files Browse the repository at this point in the history
  • Loading branch information
NeverRaR committed Jul 29, 2022
1 parent 107a71c commit d998289
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
35 changes: 35 additions & 0 deletions kclvm/src/api_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::*;
use kclvm_api::model::gpyrpc::*;
use kclvm_api::service::util::*;
use std::ffi::CStr;
use std::fs;
use std::path::Path;
const TEST_DATA_PATH: &str = "./src/testdata";

#[test]
fn test_c_api_call_exec_program() {
let serv = kclvm_service_new();
let input_path = Path::new(TEST_DATA_PATH).join("exec-program.json");
let input = fs::read_to_string(&input_path)
.expect(format!("Something went wrong reading {}", input_path.display()).as_str());
let args = unsafe {
CString::from_vec_unchecked(transform_json_to_protobuf::<ExecProgram_Args>(&input))
};
let call = CString::new("KclvmService.ExecProgram").unwrap();
let result = unsafe { CStr::from_ptr(kclvm_service_call(serv, call.as_ptr(), args.as_ptr())) };

let result = parse_message_from_protobuf::<ExecProgram_Result>(result.to_bytes());
let except_result_path = Path::new(TEST_DATA_PATH).join("exec-program.response.json");
let except_result_json = fs::read_to_string(&except_result_path).expect(
format!(
"Something went wrong reading {}",
except_result_path.display()
)
.as_str(),
);
let except_result = parse_message_from_json::<ExecProgram_Result>(&except_result_json);
assert_eq!(result.json_result, except_result.json_result);
assert_eq!(result.yaml_result, except_result.yaml_result);

kclvm_service_delete(serv);
}
10 changes: 10 additions & 0 deletions kclvm/src/testdata/exec-program.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

{
"work_dir" : "./src/testdata",
"k_filename_list":[
"hello.k"
],
"k_code_list":[
"a=1"
]
}
5 changes: 5 additions & 0 deletions kclvm/src/testdata/exec-program.response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"json_result": "{\"a\": 1}",
"yaml_result": "a: 1\n",
"escaped_time": "0.002061128616333008"
}
1 change: 1 addition & 0 deletions kclvm/src/testdata/hello.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = 1

0 comments on commit d998289

Please sign in to comment.