Add some tests and refactor test suite
This commit is contained in:
parent
801658b9ea
commit
a810611cee
14 changed files with 86 additions and 17 deletions
|
|
@ -2,10 +2,57 @@ open Common
|
|||
open OUnit
|
||||
open Generate_nim
|
||||
|
||||
module Ast = Ast_cpp
|
||||
module Flag = Flag_parsing_cpp
|
||||
|
||||
(*****************************************************************************)
|
||||
(* Helpers *)
|
||||
(*****************************************************************************)
|
||||
let _gen_nim file =
|
||||
Common.save_excursion Flag.error_recovery false (fun () ->
|
||||
Common.save_excursion Flag.show_parsing_error false (fun () ->
|
||||
Common.save_excursion Flag.verbose_parsing false (fun () ->
|
||||
Generate_nim.generate_nim file ~macro_files:[]
|
||||
)))
|
||||
|
||||
let strip_string s =
|
||||
let rep = Str.global_replace (Str.regexp "[\r\n\t ]$") "" s in
|
||||
Str.global_replace (Str.regexp "^[\r\n\t ]") "" rep
|
||||
|
||||
let get_files glob =
|
||||
let path = Filename.concat Config_pfff.path "/tests/generators/nim/" in
|
||||
sort (Common2.glob (spf "%s/%s" path glob))
|
||||
|
||||
let basename fpath =
|
||||
let fullpath = Filename.concat Config_pfff.path "/tests/generators/nim//" in
|
||||
readable ~root:fullpath fpath
|
||||
|
||||
let get_code_pairs name =
|
||||
let cglob = name ^ "*.c" in
|
||||
let nimglob = name ^ "*.nim" in
|
||||
let cfiles = get_files cglob
|
||||
and nimfiles = get_files nimglob in
|
||||
List.map2 (fun cfile nimfile ->
|
||||
let test_code = _gen_nim cfile in
|
||||
let correct_code = Common.read_file nimfile in
|
||||
((cfile, test_code), (nimfile, correct_code))
|
||||
)
|
||||
cfiles nimfiles
|
||||
|
||||
let gen_pair_test suite_name ((cfile, test_code), (nim_file, correct_code)) =
|
||||
"Test " ^ suite_name ^ ": " ^ (basename cfile) >:: (fun () ->
|
||||
let res = compare (strip_string test_code) (strip_string correct_code) == 0 in
|
||||
assert_bool ("Func is wrong: \n\n" ^ test_code ^ "\n\n" ^ correct_code) res
|
||||
)
|
||||
|
||||
let tests suite_name test_file_name =
|
||||
let file_pairs = get_code_pairs test_file_name in
|
||||
List.map (gen_pair_test suite_name) file_pairs
|
||||
|
||||
|
||||
let unittest =
|
||||
"generating_nim" >::: [
|
||||
"Test function declarations" >:: (fun () ->
|
||||
pr "None"
|
||||
)
|
||||
]
|
||||
"generating_nim" >:::
|
||||
tests "function declarations" "test_func" @
|
||||
tests "enums" "test_enum" @
|
||||
tests "macros" "test_macro" @
|
||||
[]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue