Skip to content

Commit 52281d0

Browse files
committed
variable definition updated
1 parent 18a0d74 commit 52281d0

1 file changed

Lines changed: 104 additions & 3 deletions

File tree

docs/references/variable-reference.md

Lines changed: 104 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ title: Variable specification reference
1111

1212
Variables are ways to hold value or computed values, like in programming language.
1313

14-
### Definition
14+
### Variables
1515

1616
Variables can be defined in two ways:
1717

1818
1. Variable specification file. Files with `version: default:variable:x.x.x` [**WIP** - not available yet]
19-
2. `variables` section on a supported specification document. See below.
19+
2. `variables` section on a supported specification document.
2020
```yaml
2121
version: default:http:0.7.2
2222

@@ -25,8 +25,109 @@ Variables can be defined in two ways:
2525
var_2: 'value 2'
2626
...
2727
```
28+
3. Variable can be also passed from a command-line invoke. e.g:
29+
30+
Say a http spec. file looks following
31+
32+
```yaml
33+
version: default:http:0.7.2
34+
35+
variables:
36+
var1: 22
37+
38+
request:
39+
url: https://someurl.com?var-one={$var1}
40+
...
41+
```
42+
and in the command-line you pass as following
43+
44+
```bash
45+
chk http tests/xkcd-joke.chk var1=23
46+
```
47+
48+
then CHKware will replace `{$var1}` with `23`, before making request
49+
```yaml
50+
...
51+
# url: https://someurl.com?var-one={$var1}
52+
# to
53+
url: https://someurl.com?var-one=23
54+
```
55+
56+
However in the command-line you if pass no variable. e.g.
57+
58+
```bash
59+
chk http tests/xkcd-joke.chk
60+
```
61+
62+
then CHKware will replace `{$var1}` with `22` as it's the default value, before making request
63+
```yaml
64+
...
65+
# url: https://someurl.com?var-one={$var1}
66+
# to
67+
url: https://someurl.com?var-one=22
68+
```
69+
70+
### Exposable
71+
72+
There are some special hidden local variable those are available at runtime of the execution. Those are exposable variables.
73+
74+
There is one special type of variable block that can be use in a http / testcase specification file to expose these exposable to callee environment. This exposeable section cab be defined with `expose` node.
75+
76+
For example:
77+
78+
- `$_response` is a local variable that is available in both http and testcase specifications. This special local variable named `_response` get added after the response received successfully.
79+
80+
These nodes are available under `$_response` are `version`, `code`, `reason`, `headers`, `body`.
81+
82+
Therefore, to access `code` you're supposed to use `$_response.code`.
83+
84+
- `$_response.code` holds response status code. e.g. 200, 400, 401, etc
85+
- `$_response.headers` holds response headers.
86+
- `$_response.body` holds response body.
87+
- `$_response.reason` holds response reason. e.g. 'Created', 'Moved Permanently', etc [more here](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
88+
- `$_response.version` holds response HTTP version. e.g. 'HTTP/1.1'
89+
90+
- `$_assertion_results` is a local variable that is available in testcase specifications. This special local variable named `_assertion_results` get added to local variable stack after all the assertion is resolved. `_assertion_results` holds a list of objects.
91+
92+
Each objects those `$_assertion_results` can hold have following nodes: `name`, `name_run`, `actual_original`, `is_success`, `message`, `assert_fn`
93+
94+
Therefore, to access `actual_original` you're supposed to use `$_assertion_results.1.actual_original`.
95+
96+
- `$_assertion_results.name` stores name of the assertion
97+
- `$_assertion_results.name_run` stores name of the specific name and run, this uniquely identifies and assertion
98+
- `$_assertion_results.actual_original` original variable that was supposed to be asserted
99+
- `$_assertion_results.is_success` stores the boolean result of the
100+
- `$_assertion_results.message` stores the error message if the assertion fails
101+
- `$_assertion_results.assert_fn` stores the assertion function used when assertion fails
102+
103+
#### `expose` node
104+
105+
Special block that can used to expose data from callee environment to caller environment. We can write any local variable data to be exposed from this section. `expose` node expects an array to be written. It also can be left as null.
106+
107+
For example to expose response data after the request have been executed and got response successfully. eg:
108+
```yaml
109+
expose:
110+
- $_response.body
111+
- $_response.code
112+
```
113+
114+
or after a testcase execution is done
115+
116+
```yaml
117+
expose: [
118+
$_response.body,
119+
$_response.code,
120+
$_assertion_results.2.name_run,
121+
$_assertion_results,
122+
$_response
123+
]
124+
125+
# or
126+
127+
expose: [$_assertion_results.2.name_run, $_assertion_results.2.is_success]
128+
```
28129

29130
### Supported specification document
30131

31132
- [Http specification document examples](/examples/http-examples#variable-examples) | [More example](https://github.com/chkware/cli/tree/main/tests/resources/storage/sample_config/pass_cases/variables)
32-
- [Testcase specification document examples](/examples/testcase-examples) | [More example](https://github.com/chkware/cli/tree/main/tests/resources/storage/sample_config/pass_cases/UserCreate)
133+
- [Testcase specification document examples](/examples/testcase-examples) | [More example](https://github.com/chkware/cli/tree/main/tests/resources/storage/sample_config/pass_cases/testcases)

0 commit comments

Comments
 (0)