|
| 1 | +open Base |
| 2 | +open Base_quickcheck |
| 3 | +module Destruct_line = Ocaml_lsp_server.Testing.Destruct_line |
| 4 | + |
| 5 | +let bounded value bound = Int.rem (value land Stdlib.max_int) bound |
| 6 | + |
| 7 | +let whitespace ?(allow_empty = false) selector = |
| 8 | + let length = bounded selector 5 + if allow_empty then 0 else 1 in |
| 9 | + let char = if selector land 1 = 0 then ' ' else '\t' in |
| 10 | + Stdlib.String.make length char |
| 11 | +;; |
| 12 | + |
| 13 | +let identifier selector = Stdlib.String.make (bounded selector 8 + 1) 'x' |
| 14 | +let check label condition = if not condition then failwith label |
| 15 | + |
| 16 | +let check_equal label expected actual = |
| 17 | + if not (Option.equal Int.equal expected actual) |
| 18 | + then |
| 19 | + failwith |
| 20 | + (Printf.sprintf |
| 21 | + "%s: expected %s" |
| 22 | + label |
| 23 | + (Option.value_map expected ~default:"none" ~f:Int.to_string)) |
| 24 | +;; |
| 25 | + |
| 26 | +type context = |
| 27 | + | Line_start |
| 28 | + | Inline |
| 29 | +[@@deriving quickcheck, sexp_of] |
| 30 | + |
| 31 | +module Match_case = struct |
| 32 | + type t = |
| 33 | + { context : context |
| 34 | + ; indentation : int |
| 35 | + ; identifier_length : int |
| 36 | + ; cursor : int |
| 37 | + ; suffix : int |
| 38 | + } |
| 39 | + [@@deriving quickcheck, sexp_of] |
| 40 | +end |
| 41 | + |
| 42 | +let check_match { Match_case.context; indentation; identifier_length; cursor; suffix } = |
| 43 | + let indentation = whitespace ~allow_empty:true indentation in |
| 44 | + let prefix = |
| 45 | + match context with |
| 46 | + | Line_start -> indentation |
| 47 | + | Inline -> indentation ^ "let " ^ identifier identifier_length ^ " = " |
| 48 | + in |
| 49 | + let match_start = String.length prefix in |
| 50 | + let source = prefix ^ "match" ^ whitespace suffix ^ "value" in |
| 51 | + let position = |
| 52 | + match context with |
| 53 | + | Line_start -> 0 |
| 54 | + | Inline -> match_start + bounded cursor (String.length "match" + 1) |
| 55 | + in |
| 56 | + check_equal |
| 57 | + "match location" |
| 58 | + (Some match_start) |
| 59 | + (Destruct_line.find_match source ~position); |
| 60 | + let focused = Destruct_line.mask_prefix source match_start in |
| 61 | + check "focused length" (String.length focused = String.length source); |
| 62 | + check |
| 63 | + "focused suffix" |
| 64 | + (String.equal |
| 65 | + (Base.String.drop_prefix focused match_start) |
| 66 | + (Base.String.drop_prefix source match_start)); |
| 67 | + match context with |
| 68 | + | Line_start -> () |
| 69 | + | Inline -> |
| 70 | + check_equal |
| 71 | + "position outside inline match" |
| 72 | + None |
| 73 | + (Destruct_line.find_match source ~position:0) |
| 74 | +;; |
| 75 | + |
| 76 | +let%test_unit "find_match preserves offsets in line-leading and inline matches" = |
| 77 | + Base_quickcheck.Test.run_exn (module Match_case) ~f:check_match |
| 78 | +;; |
| 79 | + |
| 80 | +type expression = |
| 81 | + | Simple |
| 82 | + | Record_update |
| 83 | + | Nested_match |
| 84 | + | Nested_try |
| 85 | + | String_literal |
| 86 | + | Comment |
| 87 | +[@@deriving quickcheck, sexp_of] |
| 88 | + |
| 89 | +module Existing_case = struct |
| 90 | + type t = |
| 91 | + { indentation : int |
| 92 | + ; identifier_length : int |
| 93 | + ; before_with : int |
| 94 | + ; after_with : int |
| 95 | + ; expression : expression |
| 96 | + } |
| 97 | + [@@deriving quickcheck, sexp_of] |
| 98 | +end |
| 99 | + |
| 100 | +let check_existing_case |
| 101 | + { Existing_case.indentation |
| 102 | + ; identifier_length |
| 103 | + ; before_with |
| 104 | + ; after_with |
| 105 | + ; expression |
| 106 | + } |
| 107 | + = |
| 108 | + let prefix = |
| 109 | + whitespace ~allow_empty:true indentation |
| 110 | + ^ "let " |
| 111 | + ^ identifier identifier_length |
| 112 | + ^ " = " |
| 113 | + in |
| 114 | + let expression = |
| 115 | + match expression with |
| 116 | + | Simple -> "A" |
| 117 | + | Record_update -> "{ r with value = A }.value" |
| 118 | + | Nested_match -> "(match A with | A -> B)" |
| 119 | + | Nested_try -> "(try A with | _ -> A)" |
| 120 | + | String_literal -> "f \"with |\"" |
| 121 | + | Comment -> "f (* with | *)" |
| 122 | + in |
| 123 | + let before_with = whitespace before_with in |
| 124 | + let after_with = whitespace ~allow_empty:true after_with in |
| 125 | + let before_case = prefix ^ "match " ^ expression ^ before_with ^ "with" ^ after_with in |
| 126 | + let source = before_case ^ "| A -> _" in |
| 127 | + let match_start = String.length prefix in |
| 128 | + check_equal |
| 129 | + "case location" |
| 130 | + (Some (String.length before_case)) |
| 131 | + (Destruct_line.find_case source ~match_start) |
| 132 | +;; |
| 133 | + |
| 134 | +let%test_unit "find_case handles prefixes, whitespace, and nested syntax" = |
| 135 | + Base_quickcheck.Test.run_exn (module Existing_case) ~f:check_existing_case |
| 136 | +;; |
| 137 | + |
| 138 | +let%test_unit "keywords embedded in identifiers are ignored" = |
| 139 | + check_equal |
| 140 | + "match identifier" |
| 141 | + None |
| 142 | + (Destruct_line.find_match "let matcher = 0" ~position:4); |
| 143 | + check_equal |
| 144 | + "match identifier with apostrophe" |
| 145 | + None |
| 146 | + (Destruct_line.find_match "let match' = 0" ~position:4); |
| 147 | + check_equal |
| 148 | + "with identifier" |
| 149 | + None |
| 150 | + (Destruct_line.find_case "match value somewith | A -> _" ~match_start:0) |
| 151 | +;; |
| 152 | + |
| 153 | +let%test_unit "find_case crosses comments and line breaks" = |
| 154 | + let before_case = "match A with\n(* existing case *)\n " in |
| 155 | + check_equal |
| 156 | + "case after comment" |
| 157 | + (Some (String.length before_case)) |
| 158 | + (Destruct_line.find_case (before_case ^ "| A -> _") ~match_start:0) |
| 159 | +;; |
| 160 | + |
| 161 | +let%test_unit "matches without cases do not consume a later match" = |
| 162 | + check_equal |
| 163 | + "later match" |
| 164 | + None |
| 165 | + (Destruct_line.find_case "match A with\nlet y = match B with | B -> _" ~match_start:0) |
| 166 | +;; |
| 167 | + |
| 168 | +let%test_unit "record updates without a case are ignored" = |
| 169 | + check_equal |
| 170 | + "record update" |
| 171 | + None |
| 172 | + (Destruct_line.find_case "match { r with value = A } with" ~match_start:0) |
| 173 | +;; |
0 commit comments