Add some tests and refactor test suite

This commit is contained in:
Joey Yakimowich-Payne 2018-05-27 17:57:24 +09:00
commit a810611cee
14 changed files with 86 additions and 17 deletions

View file

@ -8,7 +8,11 @@ generate_nim.cmx : ../../h_program-lang/parse_info.cmx \
../../lang_cpp/parsing/ast_cpp.cmx generate_nim.cmi
generate_nim.cmi : ../../commons/common.cmi
unit_generate_nim.cmo : ../../commons/oUnit.cmi generate_nim.cmi \
../../commons/common.cmi unit_generate_nim.cmi
../../lang_cpp/parsing/flag_parsing_cpp.cmo ../../globals/config_pfff.cmo \
../../commons/common2.cmi ../../commons/common.cmi \
../../lang_cpp/parsing/ast_cpp.cmo unit_generate_nim.cmi
unit_generate_nim.cmx : ../../commons/oUnit.cmx generate_nim.cmx \
../../commons/common.cmx unit_generate_nim.cmi
../../lang_cpp/parsing/flag_parsing_cpp.cmx ../../globals/config_pfff.cmx \
../../commons/common2.cmx ../../commons/common.cmx \
../../lang_cpp/parsing/ast_cpp.cmx unit_generate_nim.cmi
unit_generate_nim.cmi : ../../commons/oUnit.cmi

View file

@ -273,18 +273,22 @@ and process_info token =
process_token token
and process_expression (expr, toks) =
process_exprbis expr
process_exprbis toks expr
and process_exprbis =
and process_exprbis toks =
function
| Id ((name, info)) ->
let (_, _, ident) = name in
process_ident ident
| C const -> process_constant const
| C const ->
let res = match toks with
| [] -> ""
| main_tok :: _ -> process_token main_tok in
res
| Call ((expr, args)) ->
let name = process_expression expr
and args = process_paren (process_comma_list process_argument) args
in name ^ args
and args = process_paren (process_comma_list process_argument) args in
name ^ args
| CondExpr ((v1, v2, v3)) ->
(*let v1 = vof_expression v1
and v2 = Ocaml.vof_option vof_expression v2
@ -573,8 +577,7 @@ and process_init =
and process_block_declaration =
function
| DeclList ((decl, semi_col)) ->
let v1 = process_comma_list process_onedecl decl
in "DECLLIST " ^ v1
process_comma_list process_onedecl decl
| MacroDecl ((v1, v2, v3, v4)) ->
let v1 = process_list process_token v1
and v2 = process_simple_ident v2
@ -756,7 +759,7 @@ and process_define _tok ident kind value =
match kind with
| DefineVar ->
let (idname, _ ) = ident in
"const " ^ idname ^ " = " ^ process_define_val value ^ "\n"
"const " ^ idname ^ " = " ^ process_define_val value
| DefineFunc func ->
""
(*let (idname, _) = ident
@ -864,7 +867,7 @@ and process_toplevel = function
let iter_ast ast =
List.map process_toplevel ast
let generate_nim cfile macro_files =
let generate_nim cfile ?(macro_files = []) =
Parse_cpp.init_defs cfile;
List.iter Parse_cpp.add_defs macro_files;
let ast = Parse_cpp.parse_program cfile in
@ -873,7 +876,7 @@ let generate_nim cfile macro_files =
let test_gen_nim file =
let macro_list = [!Flag.macros_h] in
let nim_str = generate_nim file macro_list in
let nim_str = generate_nim file ~macro_files:macro_list in
pr nim_str
let actions () = [

View file

@ -1,4 +1,7 @@
val test_gen_nim :
Common.filename -> unit
val generate_nim :
Common.filename -> ?macro_files:Common.filename list -> string
val actions : unit -> Common.cmdline_actions

View file

@ -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" @
[]

View file

@ -0,0 +1 @@
enum color { RED = 1, GREEN, BLUE};

View file

@ -0,0 +1 @@
type color = enum RED = 1, GREEN, BLUE

View file

@ -0,0 +1 @@
int sub(int x, int y);

View file

@ -0,0 +1 @@
proc sub(x: cint, y: cint): cint

View file

@ -0,0 +1,2 @@
#define SIMPLE 42
#define FUNC foo(SIMPLE)

View file

@ -0,0 +1,2 @@
const SIMPLE = 42
const FUNC = foo(SIMPLE)

View file

@ -0,0 +1 @@
#define SIMPLE 42

View file

@ -0,0 +1 @@
const SIMPLE = 42

View file

@ -0,0 +1 @@
#define FUNC foo(42)

View file

@ -0,0 +1 @@
const FUNC = foo(42)