PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib_test/test_str.ml

http://github.com/avsm/ocaml-re
OCaml | 164 lines | 125 code | 29 blank | 10 comment | 3 complexity | 3bfc98d13d037d5a107a0c94b55c5f49 MD5 | raw file
Possible License(s): LGPL-2.1
  1. let eq_match ?pos ?len ?(case = true) r s =
  2. expect_equal_app
  3. ~msg:(str_printer s)
  4. ~printer:arr_ofs_printer
  5. (fun () ->
  6. let pos = match pos with None -> 0 | Some p -> p in
  7. let pat = if case then Str.regexp r else Str.regexp_case_fold r in
  8. let s_start = Str.search_forward pat s pos in
  9. (* need a better way to determine group count -
  10. maybe parse the regular expression ? *)
  11. let n_groups =
  12. try
  13. let m = Re.exec ~pos ?len (Re.compile (Re_emacs.re ~case r)) s in
  14. Array.length (Re.get_all_ofs m)
  15. with _ -> 0
  16. in
  17. (* extract all offset information *)
  18. let rec get_all_ofs i acc =
  19. if i >= n_groups then Array.of_list (List.rev acc)
  20. else
  21. let g_begin = try Str.group_beginning i with _ -> -1 in
  22. let g_end = try Str.group_end i with _ -> -1 in
  23. get_all_ofs (i + 1) ((g_begin, g_end) :: acc)
  24. in
  25. get_all_ofs 0 []
  26. ) ()
  27. (fun () ->
  28. Re.get_all_ofs (
  29. Re.exec ?pos ?len (Re_emacs.compile (Re_emacs.re ~case r)) s
  30. )
  31. ) ()
  32. ;;
  33. (* Literal Match *)
  34. expect_pass "str" (fun () ->
  35. eq_match "a" "a";
  36. eq_match "a" "b";
  37. );
  38. (* Basic Operations *)
  39. expect_pass "alt" (fun () ->
  40. eq_match "a\|b" "a";
  41. eq_match "a\|b" "b";
  42. eq_match "a\|b" "c";
  43. );
  44. expect_pass "seq" (fun () ->
  45. eq_match "ab" "ab";
  46. eq_match "ab" "ac";
  47. );
  48. expect_pass "epsilon" (fun () ->
  49. eq_match "" "";
  50. eq_match "" "a";
  51. );
  52. expect_pass "rep" (fun () ->
  53. eq_match "a*" "";
  54. eq_match "a*" "a";
  55. eq_match "a*" "aa";
  56. eq_match "a*" "b";
  57. );
  58. expect_pass "rep1" (fun () ->
  59. eq_match "a+" "a";
  60. eq_match "a+" "aa";
  61. eq_match "a+" "";
  62. eq_match "a+" "b";
  63. );
  64. expect_pass "opt" (fun () ->
  65. eq_match "a?" "";
  66. eq_match "a?" "a";
  67. );
  68. (* String, line, word *)
  69. expect_pass "bol" (fun () ->
  70. eq_match "^a" "ab";
  71. eq_match "^a" "b\na";
  72. eq_match "^a" "ba";
  73. );
  74. expect_pass "eol" (fun () ->
  75. eq_match "a$" "ba";
  76. eq_match "a$" "a\nb";
  77. eq_match "a$" "ba\n";
  78. eq_match "a$" "ab";
  79. );
  80. expect_pass "bow" (fun () ->
  81. eq_match "\<a" "a";
  82. eq_match "\<a" "bb aa";
  83. eq_match "\<a" "ba ba";
  84. );
  85. expect_pass "eow" (fun () ->
  86. eq_match "\>a" "a";
  87. eq_match "\>a" "bb aa";
  88. eq_match "\>a" "ab ab";
  89. );
  90. expect_pass "bos" (fun () ->
  91. eq_match "\`a" "ab";
  92. eq_match "\`a" "b\na";
  93. eq_match "\`a" "ba";
  94. );
  95. expect_pass "eos" (fun () ->
  96. eq_match "a\'" "ba";
  97. eq_match "a\'" "a\nb";
  98. eq_match "a\'" "ba\n";
  99. eq_match "a\'" "ab";
  100. );
  101. expect_pass "start" (fun () ->
  102. eq_match ~pos:1 "\=a" "xab";
  103. eq_match ~pos:1 "\=a" "xb\na";
  104. eq_match ~pos:1 "\=a" "xba";
  105. );
  106. expect_pass "not_boundary" (fun () ->
  107. eq_match "\Bb\B" "abc";
  108. eq_match "\Ba" "abc";
  109. eq_match "c\B" "abc";
  110. );
  111. (* Match semantics *)
  112. expect_pass "match semantics" (fun () ->
  113. eq_match "\(a\|b\)*b" "aabaab";
  114. eq_match "aa\|aaa" "aaaa";
  115. eq_match "aaa\|aa" "aaaa";
  116. );
  117. (* Group (or submatch) *)
  118. expect_pass "group" (fun () ->
  119. eq_match "\(a\)\(a\)?\(b\)" "ab";
  120. );
  121. (* Character set *)
  122. expect_pass "rg" (fun () ->
  123. eq_match "[0-9]+" "0123456789";
  124. eq_match "[0-9]+" "a";
  125. );
  126. expect_pass "compl" (fun () ->
  127. eq_match "[^0-9a-z]+" "A:Z+";
  128. eq_match "[^0-9a-z]+" "0";
  129. eq_match "[^0-9a-z]+" "a";
  130. );
  131. (* Case modifiers *)
  132. expect_pass "no_case" (fun () ->
  133. eq_match ~case:false "abc" "abc";
  134. eq_match ~case:false "abc" "ABC";
  135. );