2024-10-09 19:38:40 -04:00
|
|
|
use kcl_lib::{
|
|
|
|
ast::types::Program,
|
|
|
|
errors::KclError,
|
|
|
|
executor::{ExecutorContext, IdGenerator},
|
|
|
|
};
|
2024-08-05 21:44:49 -05:00
|
|
|
|
|
|
|
macro_rules! gen_test {
|
|
|
|
($file:ident) => {
|
|
|
|
#[tokio::test]
|
|
|
|
async fn $file() {
|
|
|
|
let code = include_str!(concat!("inputs/no_visuals/", stringify!($file), ".kcl"));
|
|
|
|
run(&code).await;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! gen_test_fail {
|
|
|
|
($file:ident, $expected:literal) => {
|
|
|
|
#[tokio::test]
|
|
|
|
async fn $file() {
|
|
|
|
let code = include_str!(concat!("inputs/no_visuals/", stringify!($file), ".kcl"));
|
|
|
|
let actual = run_fail(&code).await;
|
|
|
|
assert_eq!(actual.get_message(), $expected);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn run(code: &str) {
|
2024-10-09 19:38:40 -04:00
|
|
|
let (ctx, program, id_generator) = setup(code).await;
|
2024-08-05 21:44:49 -05:00
|
|
|
|
2024-10-09 19:38:40 -04:00
|
|
|
ctx.run(&program, None, id_generator).await.unwrap();
|
2024-08-05 21:44:49 -05:00
|
|
|
}
|
|
|
|
|
2024-10-09 19:38:40 -04:00
|
|
|
async fn setup(program: &str) -> (ExecutorContext, Program, IdGenerator) {
|
2024-08-05 21:44:49 -05:00
|
|
|
let tokens = kcl_lib::token::lexer(program).unwrap();
|
|
|
|
let parser = kcl_lib::parser::Parser::new(tokens);
|
|
|
|
let program = parser.ast().unwrap();
|
|
|
|
let ctx = kcl_lib::executor::ExecutorContext {
|
|
|
|
engine: std::sync::Arc::new(Box::new(
|
|
|
|
kcl_lib::engine::conn_mock::EngineConnection::new().await.unwrap(),
|
|
|
|
)),
|
|
|
|
fs: std::sync::Arc::new(kcl_lib::fs::FileManager::new()),
|
|
|
|
stdlib: std::sync::Arc::new(kcl_lib::std::StdLib::new()),
|
|
|
|
settings: Default::default(),
|
2024-10-01 12:45:01 -07:00
|
|
|
context_type: kcl_lib::executor::ContextType::Mock,
|
2024-08-05 21:44:49 -05:00
|
|
|
};
|
2024-10-09 19:38:40 -04:00
|
|
|
(ctx, program, IdGenerator::default())
|
2024-08-05 21:44:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn run_fail(code: &str) -> KclError {
|
2024-10-09 19:38:40 -04:00
|
|
|
let (ctx, program, id_generator) = setup(code).await;
|
|
|
|
let Err(e) = ctx.run(&program, None, id_generator).await else {
|
2024-08-05 21:44:49 -05:00
|
|
|
panic!("Expected this KCL program to fail, but it (incorrectly) never threw an error.");
|
|
|
|
};
|
|
|
|
e
|
|
|
|
}
|
|
|
|
|
|
|
|
gen_test!(property_of_object);
|
|
|
|
gen_test!(index_of_array);
|
|
|
|
gen_test_fail!(
|
|
|
|
invalid_index_str,
|
|
|
|
"semantic: Only integers >= 0 can be used as the index of an array, but you're using a string"
|
|
|
|
);
|
|
|
|
gen_test_fail!(
|
|
|
|
invalid_index_negative,
|
|
|
|
"semantic: i's value is not a valid property/index, you can only use a string or int (>= 0) here"
|
|
|
|
);
|
|
|
|
gen_test_fail!(
|
|
|
|
invalid_index_fractional,
|
|
|
|
"semantic: Only strings or ints (>= 0) can be properties/indexes"
|
|
|
|
);
|
|
|
|
gen_test_fail!(
|
|
|
|
invalid_member_object,
|
|
|
|
"semantic: Only arrays and objects can be indexed, but you're trying to index a number"
|
|
|
|
);
|
|
|
|
gen_test_fail!(
|
|
|
|
invalid_member_object_prop,
|
|
|
|
"semantic: Only arrays and objects can be indexed, but you're trying to index a boolean (true/false value)"
|
|
|
|
);
|
|
|
|
gen_test_fail!(
|
|
|
|
non_string_key_of_object,
|
|
|
|
"semantic: Only strings can be used as the property of an object, but you're using a number"
|
|
|
|
);
|
|
|
|
gen_test_fail!(
|
|
|
|
array_index_oob,
|
|
|
|
"undefined value: The array doesn't have any item at index 0"
|
|
|
|
);
|
|
|
|
gen_test_fail!(
|
|
|
|
object_prop_not_found,
|
|
|
|
"undefined value: Property 'age' not found in object"
|
|
|
|
);
|
2024-09-16 15:10:33 -04:00
|
|
|
gen_test_fail!(
|
|
|
|
pipe_substitution_inside_function_called_from_pipeline,
|
|
|
|
"semantic: cannot use % outside a pipe expression"
|
|
|
|
);
|
2024-09-27 15:44:44 -07:00
|
|
|
gen_test!(sketch_in_object);
|
2024-09-30 15:40:50 -05:00
|
|
|
gen_test!(if_else);
|
|
|
|
// gen_test_fail!(
|
|
|
|
// if_else_no_expr,
|
|
|
|
// "syntax: blocks inside an if/else expression must end in an expression"
|
|
|
|
// );
|
2024-09-16 12:43:49 -04:00
|
|
|
gen_test!(add_lots);
|