File: test_partial.ml

package info (click to toggle)
ocaml-re 1.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 848 kB
  • sloc: ml: 8,054; makefile: 18; sh: 11
file content (69 lines) | stat: -rw-r--r-- 2,174 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
open Import

let t re s =
  let re = Re.compile re in
  let res = Re.exec_partial re s in
  Format.printf
    "`%s@."
    (match res with
     | `Partial -> "Partial"
     | `Full -> "Full"
     | `Mismatch -> "Mismatch")
;;

let%expect_test "partial matches" =
  let open Re in
  t (str "hello") "he";
  [%expect {| `Partial |}];
  t (str "hello") "goodbye";
  [%expect {| `Partial |}];
  (* exec_partial 3 should be `Full *)
  t (str "hello") "hello";
  [%expect {| `Partial |}];
  t (whole_string (str "hello")) "hello";
  [%expect {| `Partial |}];
  t (whole_string (str "hello")) "goodbye";
  [%expect {| `Mismatch |}];
  t (str "hello") "";
  [%expect {| `Partial |}];
  t (str "") "hello";
  [%expect {| `Full |}];
  t (whole_string (str "hello")) "";
  [%expect {| `Partial |}]
;;

let t = exec_partial_detailed

let%expect_test "partial detailed" =
  let open Re in
  t (str "hello") "he";
  [%expect {| `Partial 0 |}];
  (* Because of how the matching engine currently works, situations where
     the entirety of the input string cannot be a match like the test below
     actually return the last character as a potential start instead of just
     return `Partial (String.length input). This is still fine however as
     it still respects the mli contract, as no match could start before
     the given position, and is fine in practice as testing an extra
     character on extra input doesn't add much more in terms of workload.
  *)
  t (str "hello") "goodbye";
  [%expect {| `Partial 6 |}];
  t (str "hello") "hello";
  [%expect {| `Full [|0,5,"hello"|] |}];
  t (whole_string (str "hello")) "hello";
  [%expect {| `Full [|0,5,"hello"|] |}];
  t (whole_string (str "hello")) "goodbye";
  [%expect {| `Mismatch |}];
  t (str "hello") "";
  [%expect {| `Partial 0 |}];
  t (str "") "hello";
  [%expect {| `Full [|0,0,""|] |}];
  t (whole_string (str "hello")) "";
  [%expect {| `Partial 0 |}];
  t (str "abc") ".ab.ab";
  [%expect {| `Partial 4 |}];
  t ~pos:1 (seq [ not_boundary; str "b" ]) "ab";
  [%expect {| `Full [|1,2,"b"|] |}];
  t (seq [ group (str "a"); rep any; group (str "b") ]) ".acb.";
  [%expect {| `Full [|1,4,"acb";1,2,"a";3,4,"b"|] |}]
;;