Skip to content

Commit 8bd8ebf

Browse files
committed
Improve test.
1 parent c7595b3 commit 8bd8ebf

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

test_ppx_regexp.ml

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,57 @@
1414
* along with this library. If not, see <http://www.gnu.org/licenses/>.
1515
*)
1616

17-
let f =
17+
let test1 =
1818
(function%pcre
1919
| {|^(?<k>.*): *(?<v>.+)?$|} -> `Attr (k, v)
2020
| {|^# (?<comment>.+)$|} -> `Comment comment
21+
| {|^((?<last>[@%]){2}){0,2}$|} -> `Even_sigils last
22+
| {|^[@%]|} -> `Odd_sigils
2123
| _ -> `Unknown)
2224

2325
let () =
24-
assert (f "x: 1" = `Attr ("x", Some "1"));
25-
assert (f "# Kommentar" = `Comment "Kommentar")
26+
assert (test1 "x: 1" = `Attr ("x", Some "1"));
27+
assert (test1 "# Kommentar" = `Comment "Kommentar");
28+
assert (test1 "" = `Even_sigils None);
29+
assert (test1 "%%%@" = `Even_sigils (Some "@"));
30+
assert (test1 "%%@" = `Odd_sigils)
2631

32+
let last_elt s =
33+
let n = String.length s in
34+
assert (s.[n - 1] = ';');
35+
let i = try String.rindex_from s (n - 2) ';' + 1 with Not_found -> 0 in
36+
String.sub s i (n - i - 1)
37+
38+
let rec test2 s =
39+
(match%pcre s with
40+
| {|^<>$|} -> assert (s = "<>")
41+
| {|^<(?<x>[^<>]+)>$|} -> assert (s = "<" ^ x ^ ">")
42+
| {|^<(?<x>[^<>]+)><(?<y>[^<>]+)>$|} -> assert (s = "<" ^ x ^ "><" ^ y ^ ">")
43+
| {|^((?<elt>[^;<>]);)+$|} -> assert (elt = last_elt s)
44+
| {|^[^{}]*\{(?<s'>.*)\}|} -> test2 s'
45+
| _ -> assert false)
46+
47+
let () =
48+
test2 "<>";
49+
test2 "<a>";
50+
test2 "<ab>";
51+
test2 "<a><b>";
52+
test2 "<ab><cde>";
53+
test2 "a;";
54+
test2 "a;b;c;d;";
55+
test2 "<a;b>";
56+
test2 "Xx{--{a;b;c;}--}yY."
57+
58+
(* It should work in a functor, and Re_pcre.regxp should be lifted to the
59+
* top-level. *)
2760
module F (M : Map.OrderedType) = struct
2861
let f x =
2962
(match%pcre x with
3063
| {|#(?<space>\s)?(?<comment>.*)|} -> Some (space <> None, comment)
3164
| _ -> None)
3265
end
3366

67+
(* It should work as a top-level eval. *)
3468
let r = ref false
3569
;;(match%pcre "" with
3670
| "$^" -> r := true

0 commit comments

Comments
 (0)