File: ounit.patch

package info (click to toggle)
ounit 1.0.3-5
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 184 kB
  • ctags: 139
  • sloc: ml: 607; xml: 190; makefile: 88
file content (469 lines) | stat: -rw-r--r-- 14,714 bytes parent folder | download
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
diff -Nurd -x depend ounit-1.0.2.orig/oUnit.ml ounit-1.0.2/oUnit.ml
--- ounit-1.0.2.orig/oUnit.ml	2008-07-29 22:04:53.000000000 +0200
+++ ounit-1.0.2/oUnit.ml	2008-07-29 23:40:46.000000000 +0200
@@ -15,6 +15,15 @@
 	  tear_down fixture;
 	  raise e
 
+exception Skip of string
+let skip_if b msg =
+  if b then
+    raise (Skip msg)
+
+exception Todo of string
+let todo msg =
+  raise (Todo msg)
+
 let assert_failure msg = 
   failwith ("OUnit: " ^ msg)
 
@@ -66,9 +75,12 @@
 (* Now some handy shorthands *)
 let (@?) = assert_bool
 
+(* The type of test function *)
+type test_fun = unit -> unit
+
 (* The type of tests *)
 type test = 
-    TestCase of (unit -> unit)
+    TestCase of test_fun
   | TestList of test list
   | TestLabel of string * test
 
@@ -77,6 +89,16 @@
 let (>::) s f = TestLabel(s, TestCase(f))  (* infix *)
 let (>:::) s l = TestLabel(s, TestList(l)) (* infix *)
 
+(* Utility function to manipulate test *)
+let rec test_decorate g tst =
+  match tst with
+    | TestCase f -> 
+        TestCase (g f)
+    | TestList tst_lst ->
+        TestList (List.map (test_decorate g) tst_lst)
+    | TestLabel (str, tst) ->
+        TestLabel (str, test_decorate g tst)
+
 (* Return the number of available tests *)
 let rec test_case_count test = 
   match test with 
@@ -132,49 +154,131 @@
   in
     tcps [] test
 
+(* Test filtering with their path *)
+module SetTestPath = Set.Make(String)
+
+let test_filter only test =
+  let set_test =
+    List.fold_left 
+      (fun st str -> SetTestPath.add str st)
+      SetTestPath.empty
+      only
+  in
+  let foldi f acc lst = 
+    List.fold_left
+      (fun (i, acc) e ->
+         let nacc =
+           f i acc e
+         in
+           (i + 1), nacc
+      )
+      acc
+      lst
+  in
+  let rec filter_test path tst =
+    if SetTestPath.mem (string_of_path path) set_test then
+      (
+        Some tst
+      )
+    else
+      (
+        match tst with
+          | TestCase _ ->
+              None
+          | TestList tst_lst ->
+              let (_, ntst_lst) =
+                foldi 
+                  (fun i ntst_lst tst ->
+                     let nntst_lst =
+                       match filter_test ((ListItem i) :: path) tst with
+                         | Some tst ->
+                             tst :: ntst_lst
+                         | None ->
+                             ntst_lst
+                     in
+                       nntst_lst
+                  )
+                  (0, [])
+                  tst_lst
+              in
+                if ntst_lst = [] then
+                  None
+                else
+                  Some (TestList ntst_lst)
+          | TestLabel (lbl, tst) ->
+              let ntst =
+                filter_test 
+                  ((Label lbl) :: path)
+                  tst
+              in
+                match ntst with
+                  | Some tst ->
+                      Some (TestLabel (lbl, tst))
+                  | None ->
+                      None
+      )
+  in
+    filter_test [] test
+
+
 (* The possible test results *)
 type test_result =
     RSuccess of path
   | RFailure of path * string
   | RError of path * string
+  | RSkip of path * string
+  | RTodo of path * string
 
 let is_success = function
-    RSuccess _ -> true
-  | RError _ -> false
-  | RFailure _ -> false
+    RSuccess _  -> true
+  | RFailure _ | RError _  | RSkip _ | RTodo _ -> false
 
 let is_failure = function
     RFailure _ -> true
-  | RError _ -> false
-  | RSuccess _ -> false
+  | RSuccess _ | RError _  | RSkip _ | RTodo _ -> false
 
 let is_error = function 
     RError _ -> true
-  | RFailure _ -> false
-  | RSuccess _ -> false
+  | RSuccess _ | RFailure _ | RSkip _ | RTodo _ -> false
+
+let is_skip = function
+    RSkip _ -> true
+  | RSuccess _ | RFailure _ | RError _  | RTodo _ -> false
+
+let is_todo = function
+    RTodo _ -> true
+  | RSuccess _ | RFailure _ | RError _  | RSkip _ -> false
 
 let result_flavour = function
     RError _ -> "Error"
   | RFailure _ -> "Failure"
   | RSuccess _ -> "Success"
+  | RSkip _ -> "Skip"
+  | RTodo _ -> "Todo"
 
 let result_path = function
-    RSuccess path -> path
-  | RError (path, _) -> path
-  | RFailure (path, _) -> path
+    RSuccess path 
+  | RError (path, _) 
+  | RFailure (path, _) 
+  | RSkip (path, _)
+  | RTodo (path, _) -> path
 
 let result_msg = function
     RSuccess _ -> "Success"
-  | RError (_, msg) -> msg
-  | RFailure (_, msg) -> msg
+  | RError (_, msg) 
+  | RFailure (_, msg) 
+  | RSkip (_, msg)
+  | RTodo (_, msg) -> msg
 
 (* Returns true if the result list contains successes only *)
 let rec was_successful results = 
   match results with 
       [] -> true
-    | RSuccess _::t -> was_successful t
-    | RFailure _::t -> false
-    | RError _::t -> false
+    | RSuccess _::t 
+    | RSkip _::t -> was_successful t
+    | RFailure _::_
+    | RError _::_ 
+    | RTodo _::_ -> false
 
 (* Events which can happen during testing *)
 type test_event =
@@ -190,6 +294,8 @@
       RSuccess path
     with
 	Failure s -> RFailure (path, s)
+      | Skip s -> RSkip (path, s)
+      | Todo s -> RTodo (path, s)
       | s -> RError (path, (Printexc.to_string s))
   in
   let rec run_test path results test = 
@@ -223,12 +329,16 @@
   let separator2 = 
     "----------------------------------------------------------------------" in
   let string_of_result = function
-      RSuccess path ->
+      RSuccess _ ->
 	if verbose then "ok\n" else "."
-    | RFailure (path, _) ->
+    | RFailure (_, _) ->
 	if verbose then "FAIL\n" else "F"
-    | RError (path, _) -> 
+    | RError (_, _) -> 
 	if verbose then "ERROR\n" else "E"
+    | RSkip (_, _) ->
+	if verbose then "SKIP\n" else "S"
+    | RTodo (_, _) ->
+        if verbose then "TODO\n" else "T"
   in
   let report_event = function
       EStart p -> 
@@ -252,6 +362,8 @@
   let running_time, results = time_fun perform_test report_event test in
   let errors = List.filter is_error results in
   let failures = List.filter is_failure results in
+  let skips = List.filter is_skip results in
+  let todos = List.filter is_todo results in
     
     if not verbose then printf "\n";
 
@@ -262,12 +374,19 @@
       (List.length results) running_time;
 
     (* Print final verdict *)
-    if was_successful results then
-      printf "OK\n"
+    if was_successful results then 
+      (
+        if skips = [] then
+          printf "OK"
+        else 
+          printf "OK: Cases: %d Skip: %d\n"
+            (test_case_count test) (List.length skips)
+      )
     else
-      printf "FAILED: Cases: %d Tried: %d Errors: %d Failures: %d\n" 
+      printf "FAILED: Cases: %d Tried: %d Errors: %d Failures: %d Skip:%d Todo:%d\n" 
 	(test_case_count test) (List.length results) 
-	(List.length errors) (List.length failures);
+	(List.length errors) (List.length failures)
+        (List.length skips) (List.length todos);
 
     (* Return the results possibly for further processing *)
     results
@@ -275,14 +394,35 @@
 (* Call this one from you test suites *)
 let run_test_tt_main suite = 
   let verbose = ref false in 
-  let set_verbose _ = verbose := true in 
+  let only_test = ref [] in
     
     Arg.parse
-      [("-verbose", Arg.Unit set_verbose, "Run the test in verbose mode.");]
+      (Arg.align
+         [("-verbose", Arg.Set verbose, " Run the test in verbose mode.");
+          ("-only-test", Arg.String (fun str -> only_test := str :: !only_test),
+           "path Run only the selected test");
+         ]
+      )
       (fun x -> raise (Arg.Bad ("Bad argument : " ^ x)))
-      ("usage: " ^ Sys.argv.(0) ^ " [-verbose]");
+      ("usage: " ^ Sys.argv.(0) ^ " [-verbose] [-only-test path]*");
     
-    let result = run_test_tt ~verbose:!verbose suite in
+    let nsuite = 
+      if !only_test = [] then
+        (
+          suite
+        ) 
+      else
+        (
+          match test_filter !only_test suite with 
+            | Some tst ->
+                tst
+            | None ->
+                failwith ("Filtering test "^
+                          (String.concat ", " !only_test)^
+                          " lead to no test")
+        )
+    in
+    let result = run_test_tt ~verbose:!verbose nsuite in
       if not (was_successful result) then
 	exit 1
       else
diff -Nurd -x depend ounit-1.0.2.orig/oUnit.mli ounit-1.0.2/oUnit.mli
--- ounit-1.0.2.orig/oUnit.mli	2008-07-29 22:04:53.000000000 +0200
+++ ounit-1.0.2/oUnit.mli	2008-07-29 23:48:07.000000000 +0200
@@ -59,6 +59,25 @@
     @raise Failure description *)
 val assert_raises : ?msg:string -> exn -> (unit -> 'a) -> unit
 
+(** {5 Skipping tests } 
+  *
+  * In certain condition test can be written but there is no point running it, because they
+  * are not significant (missing OS features for example). In this case this is not a failure
+  * nor a success. Following function allow you to escape test, just as assertion but without
+  * the same error status.
+  *
+  * A test skipped is counted as success. A test todo is counted as failure.
+  *)
+
+(** [skip cond msg] If [cond] is true, skip the test for the reason explain in [msg].
+  * For example [skip_if (Sys.os_type = "Win32") "Test a doesn't run on windows"].
+  *)
+val skip_if : bool -> string -> unit
+
+(** The associated test is still to be done, for the reason given.
+  *)
+val todo : string -> unit
+
 (** {5 Compare Functions} *)
 
 (** Compare floats up to a given relative error. *)
@@ -76,9 +95,12 @@
 
 (** {5 Constructing Tests} *)
 
+(** The type of test function *)
+type test_fun = unit -> unit
+
 (** The type of tests *)
 type test =
-    TestCase of (unit -> unit)
+    TestCase of test_fun
   | TestList of test list
   | TestLabel of string * test
 
@@ -86,7 +108,7 @@
 val (>:) : string -> test -> test
 
 (** Create a TestLabel for a TestCase *)
-val (>::) : string -> (unit -> unit) -> test
+val (>::) : string -> test_fun -> test
 
 (** Create a TestLabel for a TestList *)
 val (>:::) : string -> test list -> test
@@ -104,6 +126,12 @@
    [TestLabel("test-suite", TestSuite([TestLabel("test2", TestCase((fun _ -> ())))]))]
 *)
 
+(** [test_decorate g tst] Apply [g] to test function contains in [tst] tree. *)
+val test_decorate : (test_fun -> test_fun) -> test -> test
+
+(** [test_filter paths tst] Filter test based on their path string representation. *)
+val test_filter : string list -> test -> test option
+
 (** {5 Retrieve Information from Tests} *)
 
 (** Returns the number of available test cases *)
@@ -130,6 +158,8 @@
     RSuccess of path
   | RFailure of path * string
   | RError of path * string
+  | RSkip of path * string
+  | RTodo of path * string
 
 (** Events which occur during a test run *)   
 type test_event =
@@ -145,5 +175,6 @@
 val run_test_tt : ?verbose:bool -> test -> test_result list
 
 (** Main version of the text based test runner. It reads the supplied command 
-    line arguments to set the verbose level *)
+    line arguments to set the verbose level and limit the number of test to run
+  *)
 val run_test_tt_main : test -> test_result list
diff -Nurd -x depend ounit-1.0.2.orig/test_OUnit.ml ounit-1.0.2/test_OUnit.ml
--- ounit-1.0.2.orig/test_OUnit.ml	2008-07-29 22:04:53.000000000 +0200
+++ ounit-1.0.2/test_OUnit.ml	2008-07-29 23:53:36.000000000 +0200
@@ -90,6 +90,73 @@
   assert_raises (Failure "OUnit: false") 
     (fun _ -> assert_bool "false" false)
 
+let test_case_filter () = 
+  let assert_test_case_count res tst_opt =
+    match tst_opt with 
+      | Some tst ->
+          assert_equal res (OUnit.test_case_count tst)
+      | None ->
+          assert_failure "Unexcpected empty filter result"
+  in
+  assert_equal None (test_filter [] suite_a);
+  assert_equal None (test_filter [] suite_b);
+  assert_equal None (test_filter [] suite_c);
+  assert_equal None (test_filter [] suite_d);
+  assert_test_case_count 1 (test_filter ["suite_a"] suite_a);  
+  assert_test_case_count 1 (test_filter ["suite_a:0"] suite_a);  
+  assert_test_case_count 1 (test_filter ["suite_b:0:label"] suite_b);  
+  assert_test_case_count 1 (test_filter ["suite_c:0"] suite_c);
+  assert_test_case_count 2 (test_filter ["suite_c:0";"suite_c:1:label"] suite_c) 
+
+let assert_equal_test_result =
+  assert_equal 
+    ~printer:(fun tst_results -> 
+                String.concat "; "
+                  (List.map
+                     (function 
+                        | RSuccess path -> 
+                            Printf.sprintf "RSuccess %S" (string_of_path path)
+                        | RFailure (path, str) ->
+                            Printf.sprintf "RFailure(%S, %S)" 
+                              (string_of_path path)
+                              str
+                        | RError (path, str) ->
+                            Printf.sprintf "RError(%S, %S)" 
+                              (string_of_path path)
+                              str
+                        | RSkip (path, str) ->
+                            Printf.sprintf "RSkip(%S, %S)" 
+                              (string_of_path path)
+                              str
+                        | RTodo (path, str) ->
+                            Printf.sprintf "RTodo(%S, %S)" 
+                              (string_of_path path)
+                              str
+                     )
+                     tst_results
+                  ))
+
+let test_case_decorate () = 
+    assert_equal_test_result 
+      [RSuccess [Label "label"; ListItem 1; Label "suite_c"]; 
+       RSuccess [ListItem 0; Label "suite_c"]]
+      (perform_test ignore suite_c);
+    assert_equal_test_result
+      [RFailure([Label "label"; ListItem 1; Label "suite_c"], "OUnit: fail"); 
+       RFailure([ListItem 0; Label "suite_c"], "OUnit: fail")]
+      (perform_test ignore 
+         (test_decorate (fun _ -> (fun () -> assert_failure "fail")) suite_c))
+
+let test_case_skip () = 
+  assert_equal_test_result
+    [RSkip ([Label "skip"], "test")] 
+    (perform_test ignore ("skip" >:: (fun () -> skip_if true "test")))
+
+let test_case_todo () = 
+  assert_equal_test_result
+    [RTodo ([Label "todo"], "test")] 
+    (perform_test ignore ("todo" >:: (fun () -> todo "test")))
+
 (* Construct the test suite *)
 let suite = "OUnit" >::: 
   [ "test_case_count" >:: test_case_count;
@@ -98,6 +165,10 @@
     "test_assert_string" >:: test_assert_string;
     "test_assert_bool" >:: test_assert_bool;
     "test_cmp_float" >:: test_cmp_float;
+    "test_case_filter" >:: test_case_filter;
+    "test_case_decorate" >:: test_case_decorate;
+    "test_case_skip" >:: test_case_skip;
+    "test_case_todo" >:: test_case_todo;
   ]
 
 (* Run the tests in test suite *)