From 994a8f6a7167c0fe795f4611ebe85bdb488f9dd7 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Fri, 3 Mar 2017 16:49:41 +0000 Subject: [PATCH] generator: Add a function to find an action by name. Fairly simple refactoring. --- generator/actions.ml | 5 +++++ generator/actions.mli | 4 ++++ generator/tests_c_api.ml | 5 +---- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/generator/actions.ml b/generator/actions.ml index ab4840443..fa0e6568a 100644 --- a/generator/actions.ml +++ b/generator/actions.ml @@ -187,3 +187,8 @@ let fish_functions = List.filter is_fish * alphabetically, so this is useful: *) let sort = List.sort action_compare + +(* Find a single action by name, or give an error. *) +let find name = + try List.find (fun { name = n } -> n = name) actions + with Not_found -> failwithf "could not find action named '%s'" name diff --git a/generator/actions.mli b/generator/actions.mli index 9ee37ab3c..0d326b609 100644 --- a/generator/actions.mli +++ b/generator/actions.mli @@ -47,6 +47,10 @@ val sort : Types.action list -> Types.action list (** Sort the functions alphabetically by name (see also {!Utils.action_compare}). *) +val find : string -> Types.action +(** Find an action by name. If it doesn't exist, this fails with an + error. *) + val is_documented : Types.action -> bool (** Returns true if function should be documented, false otherwise. *) diff --git a/generator/tests_c_api.ml b/generator/tests_c_api.ml index 9061d19ab..7f89e0ed6 100644 --- a/generator/tests_c_api.ml +++ b/generator/tests_c_api.ml @@ -380,10 +380,7 @@ and generate_test_command_call ?(expect_error = false) ?(do_return = true) ?test match cmd with [] -> assert false | name :: args -> name, args in (* Look up the function. *) - let f = - try List.find (fun { name = n } -> n = name) actions - with Not_found -> - failwithf "%s: in test, command %s was not found" test_name name in + let f = Actions.find name in (* Look up the arguments and return type. *) let style_ret, style_args, style_optargs = f.style in