test: tests for Buf

This commit is contained in:
Simon Cruanes 2023-06-03 20:54:18 -04:00
parent a32297ac6c
commit 009a8d6d3b
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
5 changed files with 68 additions and 24 deletions

View file

@ -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))

27
tests/unit/t_buf.ml Normal file
View file

@ -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);
()

View file

@ -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 ()

5
tests/unit/util/dune Normal file
View file

@ -0,0 +1,5 @@
(library
(name test_util)
(modules test_util)
(libraries qcheck-core qcheck-core.runner))

View file

@ -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