Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,37 @@ You can also remember JSON inline:
Then the JSON response at "0/first_name" should be %{FIRST_NAME}
```

If the value should be parsed before memorizing, "parsed" modifier can be used.
For example:

Response data:
```json
[
{
"uuid": "ad796a4b",
"url": "/entities/ad796a4b"
}
]
```

```cucumber
Given I keep the JSON at "0/uuid" as parsed "UUID"
```

Now `%{UUID}` will be replaced with `ad796a4b` instead of `"ad796a4b"`, so it can be interpolated into the string:

```cucumber
Then the JSON should be:
"""
[
{
"uuid": "%{UUID}",
"url": "/entities/%{UUID}"
}
]
"""
```

### More

Check out the [specs](https://github.com/collectiveidea/json_spec/blob/master/spec)
Expand Down
20 changes: 20 additions & 0 deletions features/memory.feature
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,26 @@ Feature: Memory
}
"""

Scenario: Parsed value
Given the JSON is:
"""
{
"uuid": "ad796a4b",
"url": "/entities/ad796a4b"
}
"""
And I get the JSON

When I keep the JSON at "uuid" as parsed "UUID"
Then the JSON at "uuid" should be "%{UUID}"
And the JSON should be:
"""
{
"uuid": "%{UUID}",
"url": "/entities/%{UUID}"
}
"""

Scenario: Table format
When I keep the JSON at "string" as "STRING"
And I keep the JSON at "integer" as "INTEGER"
Expand Down
7 changes: 5 additions & 2 deletions lib/json_spec/cucumber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
JsonSpec.forget
end

When /^(?:I )?keep the (?:JSON|json)(?: response)?(?: at "(.*)")? as "(.*)"$/ do |path, key|
JsonSpec.memorize(key, normalize_json(last_json, path))
When /^(?:I )?keep the (?:JSON|json)(?: response)?(?: at "(.*)")? as( parsed)? "(.*)"$/ do |path, parsed, key|
json = normalize_json(last_json, path)
json = parse_json(json) if parsed

JsonSpec.memorize(key, json)
end

Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? be:$/ do |path, negative, json|
Expand Down