From f577980eac06dc3bccdc7389e3a09410c734e65d Mon Sep 17 00:00:00 2001 From: TheBurchLog <5104941+TheBurchLog@users.noreply.github.com> Date: Wed, 29 May 2024 16:15:25 +0000 Subject: [PATCH] Expanding Lark Support --- brewtils/choices.py | 7 ++- test/decorators_test.py | 96 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 2 deletions(-) diff --git a/brewtils/choices.py b/brewtils/choices.py index de297692..2cff8b4b 100644 --- a/brewtils/choices.py +++ b/brewtils/choices.py @@ -20,7 +20,6 @@ GrammarError = ParseError LexError = ParseError - choices_grammar = r""" func: CNAME [func_args] url: ADDRESS [url_args] @@ -30,12 +29,16 @@ url_args: "?" arg_pair ("&" arg_pair)* arg_pair: CNAME "=" ref - ?ref: "${" CNAME "}" + ?ref: var_ref + | ESCAPED_STRING + + var_ref: "${" CNAME "}" ADDRESS: /^http[^\?]*/ %import common.CNAME %import common.WS + %import common.ESCAPED_STRING %ignore WS """ diff --git a/test/decorators_test.py b/test/decorators_test.py index be417781..1db4b7cf 100644 --- a/test/decorators_test.py +++ b/test/decorators_test.py @@ -761,6 +761,102 @@ def test_bad_partial_call(self, basic_param): def func(foo): return foo +class TestDynamicParameters(object): + + def test_command_static_choices(self): + + STATIC_CHOICES = ["a", "b", "c"] + @parameter(key="key", type="String", choices=STATIC_CHOICES) + def func1(key): + return key + + cmd1 = _parse_method(func1) + + assert(cmd1.parameters[0].choices.value == STATIC_CHOICES) + assert(cmd1.parameters[0].choices.type == 'static') + + def test_command_url_choices(self): + + CHOICES_URL = "http://example.com/api/choices.json" + + @parameter(key="key", type="String", choices={"type": "url", "value": CHOICES_URL},) + def func1(key): + return key + + cmd1 = _parse_method(func1) + + assert(cmd1.parameters[0].choices.value == CHOICES_URL) + assert(cmd1.parameters[0].choices.type == 'url') + + def test_command_command_choices(self): + + @parameter(key="key", type="String", choices={"type": "command", "value": "get_choices"}) + def func1(key): + return key + + cmd1 = _parse_method(func1) + + assert(cmd1.parameters[0].choices.value == "get_choices") + assert(cmd1.parameters[0].choices.type == 'command') + + def test_command_reference_input(self): + + STATIC_CHOICES = ["a", "b", "c"] + + @parameter( + key="index", type="String", choices=STATIC_CHOICES, default="a" + ) + @parameter( + key="key", + type="String", + optional=False, + choices={ + "type": "command", + "display": "select", + "strict": True, + "value": "get_choices_with_argument(key=${index})", + }, + ) + def func1(index, key): + return {index: key} + + cmd1 = _parse_method(func1) + + assert(cmd1.parameters[0].choices.value == STATIC_CHOICES) + assert(cmd1.parameters[0].choices.type == 'static') + + assert(cmd1.parameters[1].choices.value == "get_choices_with_argument(key=${index})") + assert(cmd1.parameters[1].choices.type == 'command') + + def test_command_reference_and_static_inputs(self): + + STATIC_CHOICES = ["a", "b", "c"] + + @parameter( + key="index", type="String", choices=STATIC_CHOICES, default="a" + ) + @parameter( + key="key", + type="String", + optional=False, + choices={ + "type": "command", + "display": "select", + "strict": True, + "value": 'get_choices_with_argument(key=${index}, value="foo")', + }, + ) + def func1(index, key): + return {index: key} + + cmd1 = _parse_method(func1) + + assert(cmd1.parameters[0].choices.value == STATIC_CHOICES) + assert(cmd1.parameters[0].choices.type == 'static') + + assert(cmd1.parameters[1].choices.value == 'get_choices_with_argument(key=${index}, value="foo")') + assert(cmd1.parameters[1].choices.type == 'command') + class TestParseMethod(object): """Test the various ways of marking a method as a Command"""