Advertisement
Guest User

Untitled

a guest
Aug 6th, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. (*
  2. * Opifex
  3. *
  4. * Copyrights(C) 2012,2013 by Pawel Wieczorek <wieczyk at gmail>
  5. *)
  6.  
  7. open Camlp4
  8. open Camlp4.PreCast
  9. open Syntax
  10.  
  11. (*********************************************************************************************************************
  12. * Helpers
  13. ********************************************************************************************************************)
  14.  
  15. let create_ocaml_list _loc exprs =
  16. let add_to_list expr list_expr = <:expr< $expr$ :: $list_expr$ >> in
  17. List.fold_right add_to_list exprs <:expr<[]>>
  18.  
  19. let create_converted_ocaml_list _loc f exprs =
  20. create_ocaml_list _loc (List.map (f _loc) exprs)
  21.  
  22. let get_filename _loc = Filename.basename (Loc.file_name _loc)
  23.  
  24.  
  25. (*********************************************************************************************************************
  26. * SYNTAX: testgroup
  27. *
  28. * "testgroup" "begin"
  29. * <<ModuleName1>> ...
  30. * "end"
  31. *
  32. * is handled as
  33. * build_testgroup _ [ <<ModuleName1>> ; ... ]
  34. *
  35. ********************************************************************************************************************)
  36.  
  37. let change_module_name_to_test_suite_reference _loc module_name =
  38. <:expr< $lid:module_name$.test_suite >>
  39.  
  40. let build_testgroup _loc list_of_modules =
  41. let test_group_name = get_filename _loc in
  42. let test_group_list_expression = create_converted_ocaml_list
  43. _loc
  44. change_module_name_to_test_suite_reference
  45. list_of_modules
  46. in
  47. <:str_item<
  48. let test_suite = $str:test_group_name$ >::: $test_group_list_expression$
  49. >>
  50.  
  51.  
  52. (*********************************************************************************************************************
  53. *
  54. ********************************************************************************************************************)
  55.  
  56. let create_test_suite _loc identifier test_body =
  57. let test_func_name = "test_" ^ identifier in
  58. let test_case_name = <:expr<$str:identifier$>> in
  59. let test_func_patt = <:patt<$lid:test_func_name$>> in
  60. let test_definition =
  61. <:str_item<
  62. let $test_func_patt$ = $test_case_name$ >:: begin fun () ->
  63. $test_body$
  64. end
  65. >> in
  66.  
  67. (test_func_name, test_definition)
  68.  
  69. let create_test_list _loc identifier test_names =
  70. let test_name_expr n = <:expr<$lid:n$>> in
  71. let test_names_expr = List.map test_name_expr test_names in
  72. <:str_item<
  73. let $lid:identifier$ = $create_ocaml_list _loc test_names_expr$
  74. >>
  75.  
  76. let create_test_list_and_tests _loc identifier test_suites =
  77. let test_names = List.map fst test_suites in
  78. let test_definitions = List.map snd test_suites in
  79.  
  80. let test_list = create_test_list _loc identifier test_names in
  81.  
  82. Ast.stSem_of_list (test_definitions @ [test_list])
  83.  
  84. (*********************************************************************************************************************
  85. * Grammar
  86. ********************************************************************************************************************)
  87.  
  88. EXTEND Gram
  89. GLOBAL: str_item;
  90.  
  91. one_test:
  92. [ [ "test"; identifier = a_ident; "="; test_body = expr -> begin
  93. create_test_suite _loc identifier test_body
  94. end
  95.  
  96. ]];
  97.  
  98. testlist:
  99. [[ "Testlist"; identifier = a_ident; "begin"; tests = LIST1 one_test; "end" -> begin
  100. create_test_list_and_tests _loc identifier tests
  101. end
  102. ]];
  103.  
  104. testgroup:
  105. [[ "testgroup"; "begin"; modules = LIST1 a_UIDENT; "end" -> begin
  106. build_testgroup _loc modules
  107. end
  108. ]];
  109.  
  110. str_item:
  111. [[ testlist = testlist ->
  112. testlist
  113.  
  114. | testgroup = testgroup ->
  115. testgroup
  116. ]];
  117.  
  118. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement