From ea1000b51bbe18ff1bc24a162889de377336c71f Mon Sep 17 00:00:00 2001 From: Egor Lynko Date: Fri, 23 Sep 2016 18:56:29 +0300 Subject: [PATCH] Allow to memorize parsed values instead of raw values --- README.md | 31 +++++++++++++++++++++++++++++++ features/memory.feature | 20 ++++++++++++++++++++ lib/json_spec/cucumber.rb | 7 +++++-- 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5a09c63..b8090f0 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/features/memory.feature b/features/memory.feature index 22f1eab..9fc81a4 100644 --- a/features/memory.feature +++ b/features/memory.feature @@ -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" diff --git a/lib/json_spec/cucumber.rb b/lib/json_spec/cucumber.rb index f4b66a9..0e55161 100644 --- a/lib/json_spec/cucumber.rb +++ b/lib/json_spec/cucumber.rb @@ -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|