Commit 9bff4422 by andrew morton Committed by GitHub

Merge pull request #11 from drewish/formatter-output

Show path, method and status code while generating docs
parents b8d7d6a0 d6556e33
require 'rspec/core/formatters/base_text_formatter' RSpec::Support.require_rspec_core "formatters/base_text_formatter"
RSpec::Support.require_rspec_core "formatters/console_codes"
module RSpec module RSpec
module Rails module Rails
module Swagger module Swagger
class Formatter < RSpec::Core::Formatters::BaseTextFormatter class Formatter < RSpec::Core::Formatters::BaseTextFormatter
RSpec::Core::Formatters.register self, :example_finished, :close RSpec::Core::Formatters.register self, :example_group_started,
:example_passed, :example_pending, :example_failed, :example_finished,
:close
def documents def documents
# We don't try to load the docs in `initalize` because when running # We don't try to load the docs in `initalize` because when running
...@@ -13,22 +16,58 @@ module RSpec ...@@ -13,22 +16,58 @@ module RSpec
@documents ||= ::RSpec.configuration.swagger_docs @documents ||= ::RSpec.configuration.swagger_docs
end end
def example_group_started(notification)
output.print *group_output(notification)
end
def example_passed(notification)
output.print RSpec::Core::Formatters::ConsoleCodes.wrap(example_output(notification), :success)
end
def example_pending(notification)
output.print RSpec::Core::Formatters::ConsoleCodes.wrap(example_output(notification), :pending)
end
def example_failed(notification)
output.print RSpec::Core::Formatters::ConsoleCodes.wrap(example_output(notification), :failure)
end
def example_finished(notification) def example_finished(notification)
metadata = notification.example.metadata metadata = notification.example.metadata
return unless metadata[:swagger_object] == :response return unless metadata[:swagger_object] == :response
# metadata.each do |k, v| # Then add everything to the document
# puts "#{k}\t#{v}" if k.to_s.starts_with?("swagger")
# end
document = document_for(metadata[:swagger_document]) document = document_for(metadata[:swagger_document])
path_item = path_item_for(document, metadata[:swagger_path_item]) path_item = path_item_for(document, metadata[:swagger_path_item])
operation = operation_for(path_item, metadata[:swagger_operation]) operation = operation_for(path_item, metadata[:swagger_operation])
response_for(operation, metadata[:swagger_response]) response = response_for(operation, metadata[:swagger_response])
end end
def close(_notification) def close(_notification)
documents.each{|k, v| write_json(k, v)} documents.each{|k, v| write_json(k, v)}
self
end
private
def group_output(notification)
metadata = notification.group.metadata
# This is a little odd because I didn't want to split the logic across
# a start and end method. Instead we just start a new line for each
# path and operation and just let the status codes pile up on the end.
# There's probably a better way that doesn't have the initial newline.
case metadata[:swagger_object]
when :path_item
["\n", metadata[:swagger_path_item][:path]]
when :operation
["\n ", metadata[:swagger_operation][:method].to_s, "\t"]
end
end
def example_output(notification)
" #{notification.example.metadata[:swagger_response][:status_code]}"
end end
def write_json(name, document) def write_json(name, document)
...@@ -91,9 +130,9 @@ module RSpec ...@@ -91,9 +130,9 @@ module RSpec
end end
def prepare_examples(examples) def prepare_examples(examples)
if examples["application/json"].present? if examples['application/json'].kind_of? String
begin begin
examples["application/json"] = JSON.parse(examples["application/json"]) examples['application/json'] = JSON.parse(examples['application/json'])
rescue JSON::ParserError rescue JSON::ParserError
end end
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment