ocaml-iteratees / tests_common.ml

Diff from to

tests_common.ml

 ;
 
 
+(* +
+   This is my test for enumeratee that transforms iteratee over
+   utf8 chars (type [It_misc.uchar]) to iteratee over octets (type [char]).
+*)
+
+module U = It_misc.UTF8(IO);
+
+value (dump_utf8_chars : iteratee U.uchar unit) =
+ let pr s = mprintf "dump_utf8_chars: %s\n" s in
+ ie_cont inner
+ where rec inner s =
+  match s with
+  [ EOF opt_err ->
+      match opt_err with
+      [ None -> pr "natural end"
+      | Some e -> pr & sprintf "unnatural end: \"%s\"" & Printexc.to_string e
+      ]
+      >>% fun () ->
+      match opt_err with
+      [ None -> ie_doneM () s
+      | Some e -> IO.return & (throw_err e, s)
+      ]
+  | Chunk c ->
+      pr
+       (sprintf "chunk of %i chars: [%s]"
+        (S.length c)
+        (String.concat "" &
+         List.map (fun c -> sprintf "&#x%X;" (c : U.uchar :> int)) &
+         S.to_list c)
+       )
+      >>% fun () ->
+      ie_contM inner
+  ]
+;
+
+
+value utf8_chars = expl "Мама мыла раму.  Qwert."
+;
+
+exception Myexc;
+
+value test_utf8_enumeratee () =
+(
+  assert ((
+    runA & enum_pure_nchunk utf8_chars 3
+           (joinI & U.utf8_of_char dump_utf8_chars)
+    ) = `Ok ()
+  )
+;
+  let res =
+      runA & (enum_pure_nchunk utf8_chars 3 >>> enum_err Myexc)
+             (joinI & U.utf8_of_char dump_utf8_chars)
+  in
+(*
+  match res with
+  [ `Ok () -> assert False
+  | `Error e -> Printf.printf "exn: %s\n%!" (Printexc.to_string e)
+  ]
+*)
+    assert (res = `Error (Iteratees_err_msg Myexc))
+;
+  ()
+)
+;
+
+
 
 value () =
   ( printf "TESTS BEGIN.\n"
 
   ; tests_driver_full ()
 
+  ; test_utf8_enumeratee ()
+
   ; printf "TESTS END.\n"
   );
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.