From 9de51941c749803dc0bc39937e4903a291b82062 Mon Sep 17 00:00:00 2001 From: Shrikant Sharat Kandula Date: Sun, 24 Mar 2019 02:58:26 +0000 Subject: [PATCH] Use standard tokenizations for variable definitions. This is so that multi-line variable content will be possible one day. --- doc/roast.txt | 2 +- python3/roast_api.py | 12 +++++------- python3/test_roast.py | 2 +- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/doc/roast.txt b/doc/roast.txt index 924f881..eca30f4 100644 --- a/doc/roast.txt +++ b/doc/roast.txt @@ -159,7 +159,7 @@ A note about escaping braces in the context of interpolation. Braced should be doubled to escape. So, if we are defining a variable with a JSON value, the braces should be doubled so they won't be interpreted as interpolation targets. < - set payload {{"username": "Sherlock", "password": "Moriarty"}} + set payload '{{"username": "Sherlock", "password": "Moriarty"}}' > Here, the variable `payload` would be set to the following text: < diff --git a/python3/roast_api.py b/python3/roast_api.py index ba9b9cb..e8cb514 100644 --- a/python3/roast_api.py +++ b/python3/roast_api.py @@ -31,19 +31,17 @@ def build_request(lines, line_num) -> requests.Request: current_template.append(line) continue - if line.startswith('set '): - name, value = line[4:].strip().split(None, 1) - # Interpolations in variables are applied when the variable is defined. - variables[name] = value.format(**variables) - continue - parts = tokenize(line) if len(parts) < 2: continue head, *rest = parts - if head == 'alias': + if head == 'set': + # Interpolations in variables are applied when the variable is defined. + variables[rest[0]] = ' '.join(rest[1:]).format(**variables) + + elif head == 'alias': # Interpolations in aliases are applied when the alias is used. aliases[rest[0]] = ' '.join(rest[1:]) diff --git a/python3/test_roast.py b/python3/test_roast.py index cb734ec..da5a210 100644 --- a/python3/test_roast.py +++ b/python3/test_roast.py @@ -95,7 +95,7 @@ def test_interpolation_in_headers(): def test_variable_with_json_value(): req = ra.build_request([ - 'set payload {{"username": "Sherlock", "password": "Moriarty"}}', + 'set payload \'{{"username": "Sherlock", "password": "Moriarty"}}\'', 'POST /post <