diff --git a/tests/unit/dune b/tests/unit/dune index 4bc4f509..329becc8 100644 --- a/tests/unit/dune +++ b/tests/unit/dune @@ -1,5 +1,5 @@ -(test - (name t_util) +(tests + (names t_util t_buf) (package tiny_httpd) - (libraries tiny_httpd qcheck-core qcheck-core.runner)) + (libraries tiny_httpd qcheck-core qcheck-core.runner test_util)) diff --git a/tests/unit/t_buf.ml b/tests/unit/t_buf.ml new file mode 100644 index 00000000..9ee0f685 --- /dev/null +++ b/tests/unit/t_buf.ml @@ -0,0 +1,27 @@ +open Test_util +open Tiny_httpd_buf + +let spf = Printf.sprintf + +let () = + let b = create ~size:4 () in + add_string b "hello"; + assert_eq ~to_string:(spf "%S") "hello" (contents b); + + add_string b " world"; + assert_eq ~to_string:(spf "%S") "hello world" (contents b); + () + +let buffer_of_string str = + let buf = Buffer.create 32 in + Buffer.add_string buf str; + buf + +let () = + let b = create ~size:4 () in + add_buffer b (buffer_of_string "hello"); + assert_eq ~to_string:(spf "%S") "hello" (contents b); + + add_buffer b (buffer_of_string " world"); + assert_eq ~to_string:(spf "%S") "hello world" (contents b); + () diff --git a/tests/unit/t_util.ml b/tests/unit/t_util.ml index 556c5e6f..3ae913a4 100644 --- a/tests/unit/t_util.ml +++ b/tests/unit/t_util.ml @@ -1,25 +1,6 @@ -module Q = QCheck - -(* test utils *) -let pp_res f = function - | Ok x -> f x - | Error e -> e - -let pp_res_query = Q.Print.(pp_res (list (pair string string))) - -let err_map f = function - | Ok x -> Ok (f x) - | Error e -> Error e - -let sort_l l = List.sort compare l -let eq_sorted a b = err_map sort_l a = err_map sort_l b -let is_ascii_char c = Char.code c < 128 -let assert_eq ?(cmp = ( = )) a b = assert (cmp a b) - +open Test_util open Tiny_httpd_util -let qchecks = ref [] -let add_qcheck f = qchecks := f :: !qchecks let () = assert_eq "hello%20world" (percent_encode "hello world") let () = assert_eq "%23%25^%24%40^%40" (percent_encode "#%^$@^@") @@ -67,4 +48,4 @@ let () = in eq_sorted (Ok l) (parse_query s)) -let () = exit @@ QCheck_base_runner.run_tests ~colors:false !qchecks +let () = run_qcheck_and_exit () diff --git a/tests/unit/util/dune b/tests/unit/util/dune new file mode 100644 index 00000000..fb97b15e --- /dev/null +++ b/tests/unit/util/dune @@ -0,0 +1,5 @@ + +(library + (name test_util) + (modules test_util) + (libraries qcheck-core qcheck-core.runner)) diff --git a/tests/unit/util/test_util.ml b/tests/unit/util/test_util.ml new file mode 100644 index 00000000..ae225b00 --- /dev/null +++ b/tests/unit/util/test_util.ml @@ -0,0 +1,31 @@ +module Q = QCheck + +(* test utils *) +let pp_res f = function + | Ok x -> f x + | Error e -> e + +let pp_res_query = Q.Print.(pp_res (list (pair string string))) + +let err_map f = function + | Ok x -> Ok (f x) + | Error e -> Error e + +let sort_l l = List.sort compare l +let eq_sorted a b = err_map sort_l a = err_map sort_l b +let is_ascii_char c = Char.code c < 128 + +let assert_eq ?to_string ?(cmp = ( = )) a b = + let ok = cmp a b in + if not ok then ( + (match to_string with + | Some f -> Printf.eprintf "failed: %s != %s\n%!" (f a) (f b) + | None -> ()); + failwith "test failed" + ) + +let qchecks = ref [] +let add_qcheck f = qchecks := f :: !qchecks + +let run_qcheck_and_exit () : 'a = + exit @@ QCheck_base_runner.run_tests ~colors:false !qchecks