Skip to content

Commit

Permalink
Fix formatting of nested response_field scopes
Browse files Browse the repository at this point in the history
Fixes zipmark#256

Signed-off-by: Alex Coles <[email protected]>
  • Loading branch information
myabc committed Jul 2, 2016
1 parent d8570ef commit 0ac72ed
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
17 changes: 11 additions & 6 deletions features/html_documentation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ Feature: Generate HTML documentation from test examples
request = Rack::Request.new(env)
response = Rack::Response.new
response["Content-Type"] = "application/json"
response.write({ "hello" => request.params["target"] }.to_json)
response.write({
"hello" => request.params["target"],
"more_greetings" => { "bonjour" => { "message" => "le monde" } }
}.to_json)
response.finish
end
end
Expand All @@ -31,14 +34,15 @@ Feature: Generate HTML documentation from test examples
parameter :scoped, "This is a scoped variable", :scope => :scope
parameter :sub, "This is scoped", :scope => [:scope, :further]
response_field :hello, "The greeted thing"
response_field :hello, "The greeted thing"
response_field :message, "Translated greeting", scope: [:more_greetings, :bonjour]
example "Greeting your favorite gem" do
do_request :target => "rspec_api_documentation"
expect(response_headers["Content-Type"]).to eq("application/json")
expect(status).to eq(200)
expect(response_body).to eq('{"hello":"rspec_api_documentation"}')
expect(response_body).to eq('{"hello":"rspec_api_documentation","more_greetings":{"bonjour":{"message":"le monde"}}}')
end
end
end
Expand Down Expand Up @@ -75,8 +79,9 @@ Feature: Generate HTML documentation from test examples
When I open the index
And I navigate to "Greeting your favorite gem"
Then I should see the following response fields:
| name | description |
| hello | The greeted thing |
| name | description |
| hello | The greeted thing |
| more_greetings[bonjour][message] | Translated greeting |

Scenario: Example HTML documentation includes the request information
When I open the index
Expand All @@ -99,5 +104,5 @@ Feature: Generate HTML documentation from test examples
| Content-Length | 35 |
And I should see the following response body:
"""
{ "hello": "rspec_api_documentation" }
{ "hello": "rspec_api_documentation", "more_greetings": { "bonjour": { "message": "le monde" } } }
"""
27 changes: 19 additions & 8 deletions lib/rspec_api_documentation/views/markup_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ def filename
def parameters
super.each do |parameter|
if parameter.has_key?(:scope)
scope = Array(parameter[:scope]).each_with_index.map do |scope, index|
if index == 0
scope
else
"[#{scope}]"
end
end.join
parameter[:scope] = scope
parameter[:scope] = format_scope(parameter[:scope])
end
end
end

def response_fields
super.each do |response_field|
if response_field.has_key?(:scope)
response_field[:scope] = format_scope(response_field[:scope])
end
end
end
Expand Down Expand Up @@ -71,6 +72,16 @@ def format_hash(hash = {})
"#{k}: #{v}"
end.join("\n")
end

def format_scope(unformatted_scope)
Array(unformatted_scope).each_with_index.map do |scope, index|
if index == 0
scope
else
"[#{scope}]"
end
end.join
end
end
end
end

0 comments on commit 0ac72ed

Please sign in to comment.