diff --git a/src/ppx_inline_test.ml b/src/ppx_inline_test.ml index b4a00db..5918df8 100644 --- a/src/ppx_inline_test.ml +++ b/src/ppx_inline_test.ml @@ -168,38 +168,56 @@ let validate_tag tag = ;; let validate_extension_point_exn ~name_of_ppx_rewriter ~loc ~tags = + let errors = ref [] in Has_tests.set true; - if not (can_use_test_extensions ()) - then - Location.raise_errorf - ~loc - "%s: extension is disabled because the tests would be ignored (the build system \ - didn't pass -inline-test-lib. With jenga or dune, this usually happens when \ - writing tests in files that are part of an executable stanza, but only library \ - stanzas support inline tests)" - name_of_ppx_rewriter; - List.iter tags ~f:(fun tag -> - match validate_tag tag with - | Ok () -> () - | Error hint -> - let hint = - match hint with - | None -> "" - | Some hint -> "\n" ^ hint - in - Location.raise_errorf - ~loc - "%s: %S is not a valid tag for inline tests.%s" + if not (can_use_test_extensions ()) then + let error_msg = + Printf.sprintf + "%s: extension is disabled because the tests would be ignored (the build system \ + didn't pass -inline-test-lib. With jenga or dune, this usually happens when \ + writing tests in files that are part of an executable stanza, but only library \ + stanzas support inline tests)" name_of_ppx_rewriter - tag - hint) + in + errors := error_msg :: !errors + else + List.iter tags ~f:(fun tag -> + match validate_tag tag with + | Ok () -> () + | Error hint -> + let hint = + match hint with + | None -> "" + | Some hint -> "\n" ^ hint + in + let error_msg = + Printf.sprintf "%s: %S is not a valid tag for inline tests.%s" + name_of_ppx_rewriter tag hint + in + errors := error_msg :: !errors + ); + !errors ;; let name_of_ppx_rewriter = "ppx_inline_test" +let throw_exception_list ~loc ~(errs: string list) = + match errs with + | [] -> () (* list is empty, do nothing *) + | _ -> + let ast_builder = Ast_builder.make loc in + List.iter errs ~f:(fun err -> + (* loop through errs and raise an error for each one *) + Ast_builder.Default.( pexp_extension ~loc:loc + (Location.error_extensionf ~loc:loc err + )) + ) +;; + let expand_test ~loc ~path:_ ~name:id ~tags e = let loc = { loc with loc_ghost = true } in - validate_extension_point_exn ~name_of_ppx_rewriter ~loc ~tags; + let errs = validate_extension_point_exn ~name:name_of_ppx_rewriter ~loc ~tags in + throw_exception_list loc errs; apply_to_descr "test" ~loc (Some e) id tags [%expr fun () -> [%e e]] ;;