Skip to content

Commit c363d8f

Browse files
committed
test(elixir): pin the clause fold inside compile-time if/else variants
A multi-clause function defined in both branches of a module-level if must fold each branch's clauses into one definition and keep the two branches as variants of one identity: one pick node, CALLS to both branch bodies, and exactly the variant spans 5-6 and 8-9. Without the fold the node carries one span per clause (4); without the if/unless branch walk from main there is no pick node at all. Covers the combination the main merge brought together. Signed-off-by: Martin Vogel <[email protected]>
1 parent 0064906 commit c363d8f

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

‎tests/test_conditional_variants.c‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,45 @@ TEST(cv_elixir_compile_time_if) {
402402
PASS();
403403
}
404404

405+
/* A multi-clause function under a compile-time `if`. Each branch's clauses
406+
* fold into one definition spanning them (the clause fold), and the two
407+
* branches stay two variants of one identity: exactly the spans 5-6 and 8-9,
408+
* neither one span per clause (no fold) nor one span over both branches (a
409+
* fold across the branch boundary). */
410+
TEST(cv_elixir_multi_clause_under_if) {
411+
static const cv_file_t files[] = {{"pick.ex", "defmodule Pick do\n" /* 1 */
412+
" def alpha(_), do: :ok\n" /* 2 */
413+
" def beta(_), do: :ok\n" /* 3 */
414+
" if Code.ensure_loaded?(:win32) do\n" /* 4 */
415+
" def pick(0), do: alpha(0)\n" /* 5 */
416+
" def pick(n), do: alpha(n)\n" /* 6 */
417+
" else\n" /* 7 */
418+
" def pick(0), do: beta(0)\n" /* 8 */
419+
" def pick(n), do: beta(n)\n" /* 9 */
420+
" end\n" /* 10 */
421+
" def caller, do: pick(1)\n" /* 11 */
422+
"end\n"}}; /* 12 */
423+
cv_proj_t p;
424+
ASSERT_TRUE(cv_index(&p, files, 1));
425+
cv_result_t r = cv_measure(&p, "pick", "alpha", "beta", "caller");
426+
const char *span_do =
427+
"SELECT COUNT(*) FROM nodes n, json_each(n.properties,'$.variants') v "
428+
"WHERE n.project=?1 AND n.name=?2 AND n.label IN " CV_DEF_LABELS
429+
" AND json_extract(v.value,'$.start_line')=5 AND json_extract(v.value,'$.end_line')=6";
430+
const char *span_else =
431+
"SELECT COUNT(*) FROM nodes n, json_each(n.properties,'$.variants') v "
432+
"WHERE n.project=?1 AND n.name=?2 AND n.label IN " CV_DEF_LABELS
433+
" AND json_extract(v.value,'$.start_line')=8 AND json_extract(v.value,'$.end_line')=9";
434+
int do_spans = cv_count(&p, span_do, "pick", NULL);
435+
int else_spans = cv_count(&p, span_else, "pick", NULL);
436+
cv_cleanup(&p);
437+
ASSERT_TRUE(cv_expect("elixir_multi_clause", r));
438+
ASSERT_EQ(r.variant_spans, 2);
439+
ASSERT_EQ(do_spans, 1);
440+
ASSERT_EQ(else_spans, 1);
441+
PASS();
442+
}
443+
405444
/* Verilog: the identity only. Module instantiation produces no CALLS edge in
406445
* this graph even without any `ifdef (a general Verilog gap, not a variant
407446
* one), so the body and caller edges are not this suite's to assert. */
@@ -798,6 +837,7 @@ SUITE(conditional_variants) {
798837
RUN_TEST(cv_pascal_ifdef);
799838
RUN_TEST(cv_julia_static_if);
800839
RUN_TEST(cv_elixir_compile_time_if);
840+
RUN_TEST(cv_elixir_multi_clause_under_if);
801841
RUN_TEST(cv_verilog_ifdef);
802842
RUN_TEST(cv_c_platform_files);
803843
RUN_TEST(cv_c_platform_directories);

0 commit comments

Comments
 (0)