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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
open OUnit2
let (|>) x f = f x
let printer elems =
"[" ^ (elems
|> List.map (fun x -> "\"" ^ x ^ "\"")
|> String.concat ";")
^ "]"
let test_split_1 _ =
let strings = Stringext.split "test:one:two" ~on:':' in
assert_equal ~printer ["test";"one";"two"] strings
let test_split_bounded_1 _ =
let strings = Stringext.split "testing:foo:bar" ~on:':' ~max:2 in
assert_equal ~printer ["testing";"foo:bar"] strings
let test_split_none _ =
let s = "foo:bar" in
assert_equal ~printer [s] (Stringext.split s ~on:'=')
let split_trim_left1 _ =
let strings = Stringext.split_trim_left
" testing, stuff; \t again" ~on:",;" ~trim:" \t" in
assert_equal ~printer ["testing";"stuff";"again"] strings
let split_trim_left2 _ =
let strings = Stringext.split_trim_left
" testing,stuff;\t again" ~on:",;" ~trim:" \t" in
assert_equal ~printer ["testing";"stuff";"again"] strings
let split_trim_left3 _ =
assert_equal ~printer
["a ";"b ";"c"]
(Stringext.split_trim_left ~on:"," ~trim:" " "a ,b ,c")
let split_trim_left4 _ =
assert_equal ~printer
["vpof "; "hbjeu "; ""; "c"]
(Stringext.split_trim_left ~on:"," ~trim:" " "vpof ,hbjeu , ,c")
let printer s = "'" ^ (String.concat ";" s) ^ "'"
let full_split1 _ =
let strings = Stringext.full_split
"//var/test//ocaml/" ~on:'/' in
assert_equal ~printer
["/";"/";"var";"/";"test";"/";"/";"ocaml";"/"] strings
let full_split2 _ =
let strings = Stringext.full_split "//foobar.com/quux" ~on:'/' in
assert_equal ~printer
["/";"/";"foobar.com";"/";"quux"] strings
let full_split3 _ =
let strings = Stringext.full_split "foobar.com/quux" ~on:'/' in
assert_equal ~printer
["foobar.com";"/";"quux"] strings
let full_split4 _ =
let strings = Stringext.full_split "a/path/fragment" ~on:'/' in
assert_equal ~printer
["a";"/";"path";"/";"fragment"] strings
let (to_list, of_list) = Stringext.(to_list, of_list)
let to_list1 _ = assert_equal ['o';'c';'a';'m';'l'] (to_list "ocaml")
let to_list2 _ = assert_equal [] (to_list "")
let of_list1 _ = assert_equal "" (of_list [])
let of_list2 _ = assert_equal "ocaml" (of_list ['o';'c';'a';'m';'l'])
let s = "testing one two three"
let opt_int = function
| None -> "none"
| Some x -> string_of_int x
let find_from1 _ =
let r = Stringext.find_from s ~pattern:"ocaml" in
assert_equal None r
let find_from2 _ =
let r = Stringext.find_from s ~pattern:"testing" in
assert_equal (Some 0) r
let find_from3 _ =
let r = Stringext.find_from s ~pattern:"one" in
assert_equal (Some 8) r
let find_from4 _ =
let r = Stringext.find_from s ~pattern:"threee" in
assert_equal None r
let find_from5 _ =
let r = Stringext.find_from s ~pattern:" " in
assert_equal (Some 7) r
let find_from6 _ =
let pattern = "three" in
let r = Stringext.find_from s ~pattern in
assert_equal ~printer:opt_int
(Some (String.length s - String.length pattern)) r
let replace_all1 _ =
let s = "the quick brown fox brown." in
let s' = Stringext.replace_all s ~pattern:"brown" ~with_:"blue" in
assert_equal ~printer:(fun x -> x) "the quick blue fox blue." s'
let replace_all2 _ =
let s = "one two three" in
let s' = Stringext.replace_all s ~pattern:" " ~with_:"_" in
assert_equal ~printer:(fun x -> x) "one_two_three" s'
let replace_all_assoc1 _ =
let s = "hello from ocaml" in
let tbl = [("hello", "goodbye"); ("ocaml", "haskell")] in
let s' = Stringext.replace_all_assoc s tbl in
assert_equal ~printer:(fun x -> x) "goodbye from haskell" s'
let replace_all_assoc2 _ =
let s = "one two three" in
let t = [("one", "four"); ("two", "five"); ("three", "six"); (" ", "_")] in
let s' = Stringext.replace_all_assoc s t in
assert_equal ~printer:(fun x -> x) "four_five_six" s'
let replace_all_assoc3 _ =
let s = "one two three" in
let t = [(" ", "_")] in
let s' = Stringext.replace_all_assoc s t in
assert_equal ~printer:(fun x -> x) "one_two_three" s'
let replace_all_assoc4 _ =
let s = "onetwo" in
let t = [("one", "xxxx"); ("two", "yyy")] in
let s' = Stringext.replace_all_assoc s t in
assert_equal ~printer:(fun x -> x) "xxxxyyy" s'
let of_array1 _ =
let s = [| 'a'; 'b'; 'c' |] in
assert_equal "abc" (Stringext.of_array s)
let trim_left_sub1 _ =
let s = "testing" in
assert_equal ~printer:(fun x -> x)
s (Stringext.trim_left_sub s ~pos:0 ~len:(String.length s) ~chars:" ")
let trim_left_sub2 _ =
let s = " , testing" in
assert_equal ~printer:(fun x -> x)
"testing"
(Stringext.trim_left_sub s ~pos:0 ~len:(String.length s) ~chars:" ,")
let trim_left_sub3 _ =
let s = " , testing" in
assert_equal ~printer:(fun x -> x)
"test" (Stringext.trim_left_sub s ~pos:0 ~len:(7) ~chars:" ,")
let trim_left_sub4 _ =
let s = "a a" in
assert_equal ~printer:(fun x -> x)
s (Stringext.trim_left_sub s ~pos:0 ~len:3 ~chars:" ")
let trim_left_sub5 _ =
assert_equal ~printer:(fun x -> x)
"a" (Stringext.trim_left_sub "a" ~pos:0 ~len:1 ~chars:" ")
let trim_left1 _ =
assert_equal ~printer:(fun x -> x) "" (Stringext.trim_left " ")
let trim_left2 _ =
assert_equal ~printer:(fun x -> x) "" (Stringext.trim_left "")
let test_fixtures =
"test various string functions" >:::
[ "test split char 1" >:: test_split_1
; "test split bounded 1" >:: test_split_bounded_1
; "test split none" >:: test_split_none
; "split trim left1" >:: split_trim_left1
; "split trim left2" >:: split_trim_left2
; "split trim left3" >:: split_trim_left3
; "split trim left4" >:: split_trim_left4
; "trim left sub1" >:: trim_left_sub1
; "trim left sub2" >:: trim_left_sub2
; "trim left sub3" >:: trim_left_sub3
; "trim left sub4" >:: trim_left_sub4
; "trim left sub5" >:: trim_left_sub5
; "trim left1" >:: trim_left1
; "trim left2" >:: trim_left2
; "full split1" >:: full_split1
; "full split2" >:: full_split2
; "full split3" >:: full_split3
; "full split4" >:: full_split4
; "to_list1" >:: to_list1
; "to_list2" >:: to_list2
; "of_list1" >:: of_list1
; "of_list2" >:: of_list2
; "find_from1" >:: find_from1
; "find_from2" >:: find_from2
; "find_from3" >:: find_from3
; "find_from4" >:: find_from4
; "find_from5" >:: find_from5
; "find_from6" >:: find_from6
; "replace_all1" >:: replace_all1
; "replace_all2" >:: replace_all2
; "replace_all_assoc1" >:: replace_all_assoc1
; "replace_all_assoc2" >:: replace_all_assoc2
; "replace_all_assoc3" >:: replace_all_assoc3
; "replace_all_assoc4" >:: replace_all_assoc4
; "chop_prefix" >:: (fun _ ->
let ae = assert_equal ~printer:(function
| Some x -> "Some " ^ x
| None -> "None") in
ae (Some "bar") (Stringext.chop_prefix "foobar" ~prefix:"foo");
ae None (Stringext.chop_prefix "foobar" ~prefix:"bar");
ae (Some "foobar") (Stringext.chop_prefix "foobar" ~prefix:"");
ae (Some "") (Stringext.chop_prefix "foobar" ~prefix:"foobar")
)
; "take" >:: (fun _ ->
let ae = assert_equal ~printer:(fun x -> x) in
ae "foo" (Stringext.take "foobar" 3);
ae "bar" (Stringext.take "bar" 5);
ae "" (Stringext.take "" 0);
ae "" (Stringext.take "xxx" 0)
)
; "drop" >:: (fun _ ->
let ae = assert_equal ~printer:(fun x -> x) in
ae "foobar" (Stringext.drop "foobar" 0);
ae "bar" (Stringext.drop "foobar" 3);
ae "" (Stringext.drop "" 5);
ae "" (Stringext.drop "foobar" 99)
)
; "of_array" >:: of_array1 ]
let _ = run_test_tt_main test_fixtures
|