1
- require 'rspec/core/formatters/base_text_formatter'
1
+ RSpec ::Support . require_rspec_core "formatters/base_text_formatter"
2
+ RSpec ::Support . require_rspec_core "formatters/console_codes"
2
3
3
4
module RSpec
4
5
module Rails
5
6
module Swagger
6
7
class Formatter < RSpec ::Core ::Formatters ::BaseTextFormatter
7
- RSpec ::Core ::Formatters . register self , :example_finished , :close
8
+ RSpec ::Core ::Formatters . register self , :example_group_started ,
9
+ :example_passed , :example_pending , :example_failed , :example_finished ,
10
+ :close
8
11
9
12
def documents
10
13
# We don't try to load the docs in `initalize` because when running
@@ -13,22 +16,58 @@ def documents
13
16
@documents ||= ::RSpec . configuration . swagger_docs
14
17
end
15
18
19
+ def example_group_started ( notification )
20
+ output . print *group_output ( notification )
21
+ end
22
+
23
+ def example_passed ( notification )
24
+ output . print RSpec ::Core ::Formatters ::ConsoleCodes . wrap ( example_output ( notification ) , :success )
25
+ end
26
+
27
+ def example_pending ( notification )
28
+ output . print RSpec ::Core ::Formatters ::ConsoleCodes . wrap ( example_output ( notification ) , :pending )
29
+ end
30
+
31
+ def example_failed ( notification )
32
+ output . print RSpec ::Core ::Formatters ::ConsoleCodes . wrap ( example_output ( notification ) , :failure )
33
+ end
34
+
16
35
def example_finished ( notification )
17
- metadata = notification . example . metadata
36
+ metadata = notification . example . metadata
18
37
return unless metadata [ :swagger_object ] == :response
19
38
20
- # metadata.each do |k, v|
21
- # puts "#{k}\t#{v}" if k.to_s.starts_with?("swagger")
22
- # end
23
-
39
+ # Then add everything to the document
24
40
document = document_for ( metadata [ :swagger_document ] )
25
41
path_item = path_item_for ( document , metadata [ :swagger_path_item ] )
26
42
operation = operation_for ( path_item , metadata [ :swagger_operation ] )
27
- response_for ( operation , metadata [ :swagger_response ] )
43
+ response = response_for ( operation , metadata [ :swagger_response ] )
28
44
end
29
45
30
46
def close ( _notification )
31
47
documents . each { |k , v | write_json ( k , v ) }
48
+
49
+ self
50
+ end
51
+
52
+ private
53
+
54
+ def group_output ( notification )
55
+ metadata = notification . group . metadata
56
+
57
+ # This is a little odd because I didn't want to split the logic across
58
+ # a start and end method. Instead we just start a new line for each
59
+ # path and operation and just let the status codes pile up on the end.
60
+ # There's probably a better way that doesn't have the initial newline.
61
+ case metadata [ :swagger_object ]
62
+ when :path_item
63
+ [ "\n " , metadata [ :swagger_path_item ] [ :path ] ]
64
+ when :operation
65
+ [ "\n " , metadata [ :swagger_operation ] [ :method ] . to_s , "\t " ]
66
+ end
67
+ end
68
+
69
+ def example_output ( notification )
70
+ " #{ notification . example . metadata [ :swagger_response ] [ :status_code ] } "
32
71
end
33
72
34
73
def write_json ( name , document )
@@ -91,9 +130,9 @@ def prepare_parameters(params)
91
130
end
92
131
93
132
def prepare_examples ( examples )
94
- if examples [ " application/json" ] . present?
133
+ if examples [ ' application/json' ] . kind_of? String
95
134
begin
96
- examples [ " application/json" ] = JSON . parse ( examples [ " application/json" ] )
135
+ examples [ ' application/json' ] = JSON . parse ( examples [ ' application/json' ] )
97
136
rescue JSON ::ParserError
98
137
end
99
138
end
0 commit comments