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
|
(* approx: proxy server for Debian archive files
Copyright (C) 2015 Eric C. Cooper <ecc@cmu.edu>
Released under the GNU General Public License *)
open OUnit2
open List
open Printf
open Testlib
let bad_line = "one two three"
let create_bad ctx =
let file, chan = bracket_tmpfile ctx in
output_string chan (bad_line ^ "\n");
close_out chan;
file
let blank_lines =
["";
" ";
"\t";
" \t # comment"]
let create_blank ctx =
let file, chan = bracket_tmpfile ctx in
let print_line (l) =
output_string chan (l ^ "\n")
in
iter print_line blank_lines;
close_out chan;
file
let test_bindings =
["$debug", "true";
"$interval", "120";
"$user", "approx"]
let create_good ctx =
let file, chan = bracket_tmpfile ctx in
let print_binding (k, v) =
output_string chan "\n";
output_string chan ("# binding " ^ k ^ " = " ^ v ^ "\n");
output_string chan (k ^ " " ^ v ^ "\n")
in
iter print_binding test_bindings;
close_out chan;
file
let cleanup () _ = Config_file.reset ()
let read_good ctx =
bracket
(fun ctx ->
Config_file.reset ();
Config_file.read (create_good ctx))
cleanup ctx
let suite = [
"read_tests" >:::
["(read \"good\")" >::
(fun ctx ->
let file = bracket create_good tear_down ctx in
assert_equal () (Config_file.read file));
"(read \"blank\")" >::
(fun ctx ->
let file = bracket create_blank tear_down ctx in
assert_equal () (Config_file.read file));
"(read \"bad\")" >::
(fun ctx ->
let file = bracket create_bad tear_down ctx in
assert_raises (Failure ("malformed line in " ^ file ^ ": " ^ bad_line))
(fun () -> Config_file.read file))];
"get_tests" >:::
map (fun (key, default, res) ->
sprintf "(get %s %s)" (p_str key) (p_opt p_str default) >::
(fun ctx ->
read_good ctx;
assert_equal ~printer: p_str res (Config_file.get key ?default)))
["$user", None, "approx";
"$syslog", Some "daemon", "daemon"];
"get_bool_tests" >:::
map (fun (key, default, res) ->
sprintf "(get_bool %s %s)" (p_str key) (p_opt p_bool default) >::
(fun ctx ->
read_good ctx;
assert_equal ~printer: p_bool res (Config_file.get_bool key ?default)))
["$debug", None, true;
"$verbose", Some false, false];
"get_int_tests" >:::
map (fun (key, default, res) ->
sprintf "(get_int %s %s)" (p_str key) (p_opt p_int default) >::
(fun ctx ->
read_good ctx;
assert_equal ~printer: p_int res (Config_file.get_int key ?default)))
["$interval", None, 120;
"$percent", Some 50, 50];
"fold_test" >::
(fun ctx ->
read_good ctx;
let collect_binding key value acc = (key, value) :: acc in
assert_equal ~printer: (p_list p_str2) test_bindings
(Config_file.fold collect_binding []));
]
|