Find and remove dead code and unused APIs in OCaml projects
1(* Tests for module alias detection *)
2
3(* Test the module name pattern *)
4let test_module_name_pattern () =
5 let module_name_re = Re.compile Prune.Module_alias.module_name in
6
7 let test_cases =
8 [
9 ("MyModule", true);
10 ("M", true);
11 ("Module_123", true);
12 ("_Module", true);
13 (* Starts with underscore *)
14 ("Module'", true);
15 (* With apostrophe *)
16 ("myModule", false);
17 (* Starts with lowercase *)
18 ("123Module", false);
19 (* Starts with number *)
20 ]
21 in
22
23 List.iter
24 (fun (input, expected) ->
25 let matches =
26 try
27 let m = Re.exec module_name_re input in
28 (* Check if the whole string was matched *)
29 Re.Group.offset m 0 = (0, String.length input)
30 with Not_found -> false
31 in
32 Alcotest.(check bool) (Fmt.str "module name: %s" input) expected matches)
33 test_cases
34
35(* Test whitespace patterns *)
36let test_whitespace_patterns () =
37 let ws_re = Re.compile Prune.Module_alias.ws in
38 let ws1_re = Re.compile Prune.Module_alias.ws1 in
39
40 (* ws should match zero or more spaces *)
41 Alcotest.(check bool) "ws matches empty" true (Re.execp ws_re "");
42 Alcotest.(check bool) "ws matches space" true (Re.execp ws_re " ");
43 Alcotest.(check bool) "ws matches newline" true (Re.execp ws_re "\n");
44 Alcotest.(check bool) "ws matches multiple" true (Re.execp ws_re " \n\r ");
45
46 (* ws1 should match one or more spaces *)
47 Alcotest.(check bool) "ws1 doesn't match empty" false (Re.execp ws1_re "");
48 Alcotest.(check bool) "ws1 matches space" true (Re.execp ws1_re " ");
49 Alcotest.(check bool) "ws1 matches newline" true (Re.execp ws1_re "\n");
50 Alcotest.(check bool) "ws1 matches multiple" true (Re.execp ws1_re " \n\r ")
51
52(* Test signature mismatch parsing *)
53let test_signature_mismatch_parsing () =
54 let value_required_re =
55 Re.(
56 compile
57 (seq
58 [
59 str "The value ";
60 opt (char '"');
61 group (rep1 (compl [ space; char '"' ]));
62 opt (char '"');
63 str " is required but not provided";
64 ]))
65 in
66
67 let test_cases =
68 [
69 ("The value foo is required but not provided", Some "foo");
70 ("The value \"bar\" is required but not provided", Some "bar");
71 ( "The value \"kpi_comparison\" is required but not provided",
72 Some "kpi_comparison" );
73 ("Something else", None);
74 ]
75 in
76
77 List.iter
78 (fun (input, expected) ->
79 let result =
80 try
81 let groups = Re.exec value_required_re input in
82 Some (Re.Group.get groups 1)
83 with Not_found -> None
84 in
85 Alcotest.(check (option string))
86 (Fmt.str "signature mismatch: %s" input)
87 expected result)
88 test_cases
89
90let suite =
91 ( "module_alias",
92 [
93 Alcotest.test_case "Module name pattern" `Quick test_module_name_pattern;
94 Alcotest.test_case "Whitespace patterns" `Quick test_whitespace_patterns;
95 Alcotest.test_case "Signature mismatch parsing" `Quick
96 test_signature_mismatch_parsing;
97 ] )