Skip to content

Commit

Permalink
Add test of ts_import (#405)
Browse files Browse the repository at this point in the history
* Organize existing test code.

* add test of ts_import

* fix
  • Loading branch information
kxc-wraikny authored Jan 10, 2024
1 parent 7e34648 commit 5c02007
Showing 1 changed file with 53 additions and 39 deletions.
92 changes: 53 additions & 39 deletions src/lib_gen_ts/unit_test/lib_gen_ts_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ module Code = struct
let type_B = "B" in
let type_C = "C" in
let mod_A = "A" in
let libs = "libs" in

let expression_cases = [
test_case'
Expand Down Expand Up @@ -273,21 +274,51 @@ module Code = struct
])
|> rope_of_ts_expression
|> Rope.to_string);
] in

let type_desc_cases = [
test_case'
"rope_of_ts_expression"
("await " ^ var_x)
(`await_expression (`identifier var_x)
|> rope_of_ts_expression
|> Rope.to_string);
|> rope_of_ts_expression
|> Rope.to_string);
test_case'
"rope_of_ts_expression"
"( \"abc\" ) as const"
(`const_assertion (`literal_expression (`string_literal "abc"))
|> rope_of_ts_expression
|> Rope.to_string);
|> rope_of_ts_expression
|> Rope.to_string);
test_case'
"rope_of_ts_expression"
("((" ^ var_x ^ " : " ^ number ^ ", " ^ var_y ^ "? : " ^ number ^ ") => {" ^
"return " ^ var_x ^ " " ^ plus ^ " " ^ var_y
^ "})" ^ "(" ^ var_a ^ ", " ^ var_b ^ ")")
(`call_expression {
tsce_expression = `arrow_function {
tsaf_parameters = [
{ tsp_name = var_x;
tsp_optional = false;
tsp_type_desc = `type_reference number; };
{ tsp_name = var_y;
tsp_optional = true;
tsp_type_desc = `type_reference number; };
];
tsaf_body = [
`return_statement
(`binary_expression {
tsbe_left = `identifier var_x;
tsbe_operator_token = plus;
tsbe_right = `identifier var_y;
})
];
};
tsce_arguments = [
`identifier var_a;
`identifier var_b;
];
}
|> rope_of_ts_expression
|> Rope.to_string);
] in
let type_desc_cases = [
test_case'
"rope_of_ts_type_desc"
number
Expand Down Expand Up @@ -456,6 +487,20 @@ module Code = struct
|> Rope.to_string);
] in
let statement_case = [
test_case'
"rope_of_ts_statement"
("import { "^var_x^", "^var_y^" as "^var_a^" } from \""^libs^"\";")
(`import {
tsi_from = libs;
tsi_import_items = [
{ tsii_name = var_x;
tsii_alias = None };
{ tsii_name = var_y;
tsii_alias = Some var_a };
];
}
|> rope_of_ts_statement
|> Rope.to_string);
test_case'
"rope_of_ts_statement"
("type " ^ type_A ^ " = { " ^ var_x ^ ": " ^ number ^ ", " ^ var_y ^ ": " ^ string ^ ", " ^ var_z ^ "?: " ^ string ^ " }")
Expand Down Expand Up @@ -554,7 +599,7 @@ module Code = struct
|> rope_of_ts_statement
|> Rope.to_string);
test_case'
"rope_of_ts_statment"
"rope_of_ts_statement"
("namespace " ^ mod_A ^ "{ " ^ "type " ^ type_A ^ " = " ^ type_B ^ " }")
(`module_declaration {
tsm_modifiers = [];
Expand Down Expand Up @@ -599,37 +644,6 @@ module Code = struct
})
|> rope_of_ts_statement
|> Rope.to_string);
test_case'
"rope_of_ts_expression"
("((" ^ var_x ^ " : " ^ number ^ ", " ^ var_y ^ "? : " ^ number ^ ") => {" ^
"return " ^ var_x ^ " " ^ plus ^ " " ^ var_y
^ "})" ^ "(" ^ var_a ^ ", " ^ var_b ^ ")")
(`call_expression {
tsce_expression = `arrow_function {
tsaf_parameters = [
{ tsp_name = var_x;
tsp_optional = false;
tsp_type_desc = `type_reference number; };
{ tsp_name = var_y;
tsp_optional = true;
tsp_type_desc = `type_reference number; };
];
tsaf_body = [
`return_statement
(`binary_expression {
tsbe_left = `identifier var_x;
tsbe_operator_token = plus;
tsbe_right = `identifier var_y;
})
];
};
tsce_arguments = [
`identifier var_a;
`identifier var_b;
];
}
|> rope_of_ts_expression
|> Rope.to_string);
] in
[("ex_expression", expression_cases);
("ex_type_desc", type_desc_cases);
Expand Down

0 comments on commit 5c02007

Please sign in to comment.