Skip to content

Commit 8fd74c3

Browse files
authored
fix: prefixed all the internal variables with _ to prevent collisions with user defined variables (#27)
1 parent 7d34c17 commit 8fd74c3

File tree

21 files changed

+165
-98
lines changed

21 files changed

+165
-98
lines changed

mockfaster/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>tzatziki-parent</artifactId>
77
<groupId>com.decathlon.tzatziki</groupId>
8-
<version>1.0.9</version>
8+
<version>1.0.10</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

@@ -26,7 +26,7 @@
2626
<dependency>
2727
<groupId>com.decathlon.tzatziki</groupId>
2828
<artifactId>tzatziki-common</artifactId>
29-
<version>1.0.9</version>
29+
<version>1.0.10</version>
3030
</dependency>
3131
<dependency>
3232
<groupId>org.mock-server</groupId>

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>com.decathlon.tzatziki</groupId>
66
<artifactId>tzatziki-parent</artifactId>
77
<packaging>pom</packaging>
8-
<version>1.0.9</version>
8+
<version>1.0.10</version>
99
<modules>
1010
<module>tzatziki-common</module>
1111
<module>tzatziki-core</module>

tzatziki-common/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>tzatziki-parent</artifactId>
77
<groupId>com.decathlon.tzatziki</groupId>
8-
<version>1.0.9</version>
8+
<version>1.0.10</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111
<artifactId>tzatziki-common</artifactId>

tzatziki-core/README.md

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -411,10 +411,10 @@ This can be handy for not duplicating scenarios just for one additional step.
411411
The same way, any step can be delayed asynchronously by prefixing it with `after <amount>ms`, for example:
412412
```gherkin
413413
Given that after 100ms user is a User:
414-
"""
415-
id: 1
416-
name: bob
417-
"""
414+
"""
415+
id: 1
416+
name: bob
417+
"""
418418
```
419419

420420
This can be useful to test the resilience of your code.
@@ -424,19 +424,19 @@ This can be useful to test the resilience of your code.
424424
You can test that something becomes true within a given time by prefixing your step with `within <amount>ms`, for example:
425425
```gherkin
426426
Then within 100ms user is equal to:
427-
"""
428-
id: 1
429-
name: bob
430-
"""
427+
"""
428+
id: 1
429+
name: bob
430+
"""
431431
```
432432

433433
This will wait until the assertion is true! If you want to verify that the assertion is true for the entire period, you can prefix your step with `during <amount>ms`:
434434
```gherkin
435435
Then during 100ms user is equal to:
436-
"""
437-
id: 1
438-
name: bob
439-
"""
436+
"""
437+
id: 1
438+
name: bob
439+
"""
440440
```
441441

442442
### Chain multiple guards
@@ -456,6 +456,56 @@ Examples:
456456

457457
Moreover, you can chain any number of guards, the only rule is to append guards with a space between them.
458458

459+
### Internal variables in the context
460+
461+
Some internal variables are in the context so that you can access them in a simple way.
462+
463+
By default, if you use a `Scenario Template`, the values from your examples' table are not available in the `Background` of your feature.
464+
In Tzatziki, the ObjectSteps will actually extract them, and export them as the variable `_examples` so that the following will work:
465+
466+
```gherkin
467+
Background:
468+
Given that we call "{{_examples.url}}"
469+
470+
Scenario Template:
471+
When we ...
472+
Then we receive ...
473+
...
474+
475+
Example:
476+
| url |
477+
| http://backend1/endpoint |
478+
| http://backend2/endpoint |
479+
```
480+
481+
Following the same logic, you can access the Scenario object itself as `_scenario`:
482+
483+
```gherkin
484+
@someTag
485+
Scenario: we can access the tags in a scenario
486+
* _scenario.sourceTagNames[0] == "@someTag"
487+
```
488+
489+
write and read an Environment variable at runtime using `_env`:
490+
491+
```gherkin
492+
Scenario: we can access the ENVs from the test
493+
# see com.decathlon.tzatziki.utils.Env to see how we can set an environment variable at runtime
494+
Given that _env.TEST = "something"
495+
Then _env.TEST is equal to "something"
496+
```
497+
498+
or the system properties using `_properties`:
499+
500+
```gherkin
501+
Scenario: we can access the system properties from the test
502+
Given that _properties.test = "something"
503+
Then _properties.test is equal to "something"
504+
```
505+
506+
Generally, Tzatziki internal variables will be prefixed with `_` so that they don't collide with the variables of your tests!
507+
508+
459509
## More examples
460510

461511
For more examples you can have a look at the tests:

tzatziki-core/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.decathlon.tzatziki</groupId>
77
<artifactId>tzatziki-parent</artifactId>
8-
<version>1.0.9</version>
8+
<version>1.0.10</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111
<artifactId>tzatziki-core</artifactId>
@@ -14,7 +14,7 @@
1414
<dependency>
1515
<groupId>com.decathlon.tzatziki</groupId>
1616
<artifactId>tzatziki-common</artifactId>
17-
<version>1.0.9</version>
17+
<version>1.0.10</version>
1818
</dependency>
1919
<dependency>
2020
<groupId>org.projectlombok</groupId>

tzatziki-core/src/main/java/com/decathlon/tzatziki/steps/ObjectSteps.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import lombok.extern.slf4j.Slf4j;
2525
import org.apache.commons.io.IOUtils;
2626
import org.apache.commons.lang3.StringUtils;
27-
import org.assertj.core.api.Assertions;
2827
import org.jetbrains.annotations.NotNull;
2928
import org.junit.Assert;
3029

@@ -150,8 +149,8 @@ public class ObjectSteps {
150149
@Before(order = 1)
151150
public void before(Scenario scenario) {
152151
Time.setToNow();
153-
add("scenario", scenario);
154-
add("env", Proxy.newProxyInstance(Map.class.getClassLoader(), new Class[]{Map.class}, (proxy, method, args) -> {
152+
add("_scenario", scenario);
153+
add("_env", Proxy.newProxyInstance(Map.class.getClassLoader(), new Class[]{Map.class}, (proxy, method, args) -> {
155154
String name = String.valueOf(args[0]);
156155
return switch (method.getName()) {
157156
case "get" -> System.getenv(name);
@@ -160,7 +159,7 @@ public void before(Scenario scenario) {
160159
default -> invoke(new LinkedHashMap<>(), method, args);
161160
};
162161
}));
163-
add("properties", Proxy.newProxyInstance(Map.class.getClassLoader(), new Class[]{Map.class}, (proxy, method, args) -> {
162+
add("_properties", Proxy.newProxyInstance(Map.class.getClassLoader(), new Class[]{Map.class}, (proxy, method, args) -> {
164163
String key = String.valueOf(args[0]);
165164
return switch (method.getName()) {
166165
case "get" -> System.getProperty(key);
@@ -169,8 +168,8 @@ public void before(Scenario scenario) {
169168
default -> invoke(new LinkedHashMap<>(), method, args);
170169
};
171170
}));
171+
add("_examples", getExamples(scenario));
172172
add("randomUUID", (Supplier<UUID>) UUID::randomUUID);
173-
add("examples", getExamples(scenario));
174173
}
175174

176175
private Map<String, String> getExamples(Scenario scenario) {

tzatziki-core/src/test/resources/com/decathlon/tzatziki/steps/objects.feature

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,14 @@ Feature: to interact with objects in the context
230230
- "3039606203C7F24000053624"
231231
"""
232232

233-
Scenario Template: we can access the tags in a scenario
234-
* scenario.sourceTagNames[0] == "@<firstTag>"
235-
* scenario.sourceTagNames[1][5-] == "<arg>"
233+
@someTag
234+
Scenario: we can access the tags in a scenario
235+
* _scenario.sourceTagNames[0] == "@someTag"
236+
237+
238+
Scenario Template: we can access the tags in a scenario template
239+
* _scenario.sourceTagNames[0] == "@<firstTag>"
240+
* _scenario.sourceTagNames[1][5-] == "<arg>"
236241

237242
@test1 @arg=value1
238243
Examples:
@@ -335,12 +340,12 @@ Feature: to interact with objects in the context
335340

336341
Scenario: we can access the ENVs from the test
337342
# see com.decathlon.tzatziki.utils.Env to see how we can set an environment variable at runtime
338-
Given that env.TEST = "something"
339-
Then env.TEST is equal to "something"
343+
Given that _env.TEST = "something"
344+
Then _env.TEST is equal to "something"
340345

341346
Scenario: we can access the system properties from the test
342-
Given that properties.test = "something"
343-
Then properties.test is equal to "something"
347+
Given that _properties.test = "something"
348+
Then _properties.test is equal to "something"
344349

345350
Scenario: we can test that a value is one of a list of values
346351
Given that object is a Map:
@@ -465,9 +470,9 @@ Feature: to interact with objects in the context
465470
some value
466471
"""
467472
Examples:
468-
| param | placeholder |
469-
| {{value}} | {{{[examples.param]}}} |
470-
| {{value}} | {{value}} |
473+
| param | placeholder |
474+
| {{value}} | {{{[_examples.param]}}} |
475+
| {{value}} | {{value}} |
471476

472477
Scenario: we can set an attribute on a map
473478
Given that bob is a Map:

tzatziki-core/src/test/resources/com/decathlon/tzatziki/steps/scenario-with-background.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Feature: a feature with a background that we template from the examples in the s
33
Background:
44
Given that map is a Map:
55
"""yml
6-
property: {{{[examples.testValue]}}}
6+
property: {{{[_examples.testValue]}}}
77
"""
88
And if map.property == 4 => map.property is equal to 4
99

@@ -25,4 +25,4 @@ Feature: a feature with a background that we template from the examples in the s
2525
| 4 |
2626

2727
Scenario: another scenario that doesn't have examples
28-
Then map.property is equal to "examples.testValue"
28+
Then map.property is equal to "_examples.testValue"

tzatziki-http/README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,18 @@ Given that getting on "http://backend/v1/resource/item/(\d+)" will return:
153153
item_id: $1
154154
"""
155155
156-
# using the request passed in the context
156+
# using the request passed in the context and saved as _request
157157
Given that getting on "http://backend/v1/resource/item/(\d+)" will return:
158158
"""
159-
item_id: {{request.pathParameterList.0.values.0}}
159+
item_id: {{_request.pathParameterList.0.values.0}}
160160
"""
161161
```
162162

163163
Split the path/query params to build a list dynamically:
164164
```gherkin
165165
Given that getting on "http://backend/v1/resource/items/(.*)" will return a List:
166166
"""
167-
{{#split request.pathParameterList.0.values.0.value [,]}}
167+
{{#split _request.pathParameterList.0.values.0.value [,]}}
168168
- item_id: {{this}}
169169
{{/split}}
170170
"""
@@ -181,7 +181,7 @@ Or even to use the posted body as an input:
181181
```gherkin
182182
Given that posting on "http://backend/v1/resource/items" will return a List:
183183
"""
184-
{{#foreach request.body}}
184+
{{#foreach _request.body}}
185185
- id: {{this.id}}
186186
name: nameOf{{this.id}}
187187
{{/foreach}}
@@ -206,10 +206,19 @@ Then we receive:
206206
#### URL remapping
207207

208208
Each mocked host will be dynamically remapped on the local mockserver.
209-
This means that `http://backend/users` will actually be `http://localhost:{{mockserver.port}}/http/backend/users`
209+
This means that `http://backend/users` will actually be `http://localhost:<MockFaster.localPort()>/http/backend/users`
210210

211211
Once you have created the mock, your calls will also be remapped, so that you can call `http://backend/users` and not the remapped url.
212212

213+
When you call a relative url like `/endpoint`, rest-assured will automatically prefix it with `http://localhost:8080`.
214+
If you wish to target another host or port, you can override it programmatically with:
215+
216+
```java
217+
httpSteps.setRelativeUrlRewriter(path -> "http://<host>:<port>%s".formatted(path));
218+
```
219+
220+
Sometimes it can also be a bit annoying to repeat the targetted host in the tests.
221+
213222
#### Assert interactions
214223

215224
You can assert that a defined mock has been interacted with, the same way you would do it with mockserver.

tzatziki-http/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.decathlon.tzatziki</groupId>
77
<artifactId>tzatziki-parent</artifactId>
8-
<version>1.0.9</version>
8+
<version>1.0.10</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111
<artifactId>tzatziki-http</artifactId>
@@ -24,12 +24,12 @@
2424
<dependency>
2525
<groupId>com.decathlon.tzatziki</groupId>
2626
<artifactId>tzatziki-core</artifactId>
27-
<version>1.0.9</version>
27+
<version>1.0.10</version>
2828
</dependency>
2929
<dependency>
3030
<groupId>com.decathlon.tzatziki</groupId>
3131
<artifactId>tzatziki-logback</artifactId>
32-
<version>1.0.9</version>
32+
<version>1.0.10</version>
3333
</dependency>
3434
<dependency>
3535
<groupId>io.rest-assured</groupId>
@@ -38,7 +38,7 @@
3838
<dependency>
3939
<groupId>com.decathlon.tzatziki</groupId>
4040
<artifactId>mockfaster</artifactId>
41-
<version>1.0.9</version>
41+
<version>1.0.10</version>
4242
</dependency>
4343
<dependency>
4444
<groupId>io.swagger</groupId>

0 commit comments

Comments
 (0)