Skip to content

Commit fded0c5

Browse files
committed
Move boilerplate code out of lengthy module
1 parent 830c548 commit fded0c5

File tree

2 files changed

+52
-56
lines changed

2 files changed

+52
-56
lines changed

lib/openapi_first/test.rb

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# frozen_string_literal: true
22

33
require_relative 'test/configuration'
4+
require_relative 'test/registry'
45

56
module OpenapiFirst
67
# Test integration
78
module Test
89
autoload :Coverage, 'openapi_first/test/coverage'
910
autoload :Methods, 'openapi_first/test/methods'
11+
extend Registry
1012

1113
class CoverageError < Error; end
1214

@@ -59,8 +61,7 @@ def self.handle_exit
5961
)
6062
return unless configuration.report_coverage == true
6163

62-
coverage = Coverage.result.coverage
63-
return if coverage >= configuration.minimum_coverage
64+
return if Coverage.result.coverage >= configuration.minimum_coverage
6465

6566
raise OpenapiFirst::Test::CoverageError, 'Not all described requests and responses have been tested.'
6667
end
@@ -69,8 +70,7 @@ def self.handle_exit
6970
# @param formatter A formatter to define the report.
7071
# @output [IO] An output where to puts the report.
7172
def self.report_coverage(formatter: Coverage::TerminalFormatter, **)
72-
coverage_result = Coverage.result
73-
puts formatter.new(**).format(coverage_result)
73+
puts formatter.new(**).format(Coverage.result)
7474
end
7575

7676
# Returns the Rack app wrapped with silent request, response validation
@@ -90,10 +90,12 @@ def self.install
9090

9191
OpenapiFirst.configure do |config|
9292
@after_request_validation = config.after_request_validation do |validated_request, oad|
93-
after_request_validation(validated_request, oad)
93+
Coverage.track_request(validated_request, oad)
9494
end
9595
@after_response_validation = config.after_response_validation do |validated_response, rack_request, oad|
96-
after_response_validation(validated_response, rack_request, oad)
96+
raise validated_response.error.exception if configuration.response_raise_error && validated_response.invalid?
97+
98+
Coverage.track_response(validated_response, rack_request, oad)
9799
end
98100
end
99101
@installed = true
@@ -108,55 +110,5 @@ def self.uninstall
108110
@installed = nil
109111
@exit_handler = nil
110112
end
111-
112-
class NotRegisteredError < StandardError; end
113-
class AlreadyRegisteredError < StandardError; end
114-
115-
@definitions = {}
116-
117-
class << self
118-
attr_reader :definitions
119-
120-
# Register an OpenAPI definition for testing
121-
# @param path_or_definition [String, Definition] Path to the OpenAPI file or a Definition object
122-
# @param as [Symbol] Name to register the API definition as
123-
def register(path_or_definition, as: :default)
124-
if definitions.key?(as) && as == :default
125-
raise(
126-
AlreadyRegisteredError,
127-
"#{definitions[as].filepath.inspect} is already registered " \
128-
"as ':default' so you cannot register #{path_or_definition.inspect} without " \
129-
'giving it a custom name. Please call register with a custom key like: ' \
130-
"OpenapiFirst::Test.register(#{path_or_definition.inspect}, as: :my_other_api)"
131-
)
132-
end
133-
134-
definition = OpenapiFirst.load(path_or_definition)
135-
definitions[as] = definition
136-
definition
137-
end
138-
139-
def [](api)
140-
definitions.fetch(api) do
141-
option = api == :default ? '' : ", as: #{api.inspect}"
142-
raise(NotRegisteredError,
143-
"API description '#{api.inspect}' not found." \
144-
"Please call OpenapiFirst::Test.register('myopenapi.yaml'#{option}) " \
145-
'once before running tests.')
146-
end
147-
end
148-
149-
private
150-
151-
def after_request_validation(validated_request, oad)
152-
Coverage.track_request(validated_request, oad)
153-
end
154-
155-
def after_response_validation(validated_response, request, oad)
156-
raise validated_response.error.exception if configuration.response_raise_error && validated_response.invalid?
157-
158-
Coverage.track_response(validated_response, request, oad)
159-
end
160-
end
161113
end
162114
end

lib/openapi_first/test/registry.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# frozen_string_literal: true
2+
3+
module OpenapiFirst
4+
module Test
5+
class NotRegisteredError < Error; end
6+
class AlreadyRegisteredError < Error; end
7+
8+
# @visibility private
9+
module Registry
10+
def definitions
11+
@definitions ||= {}
12+
end
13+
14+
# Register an OpenAPI definition for testing
15+
# @param path_or_definition [String, Definition] Path to the OpenAPI file or a Definition object
16+
# @param as [Symbol] Name to register the API definition as
17+
def register(path_or_definition, as: :default)
18+
if definitions.key?(as) && as == :default
19+
raise(
20+
AlreadyRegisteredError,
21+
"#{definitions[as].filepath.inspect} is already registered " \
22+
"as ':default' so you cannot register #{path_or_definition.inspect} without " \
23+
'giving it a custom name. Please call register with a custom key like: ' \
24+
"#{name}.register(#{path_or_definition.inspect}, as: :my_other_api)"
25+
)
26+
end
27+
28+
definition = OpenapiFirst.load(path_or_definition)
29+
definitions[as] = definition
30+
definition
31+
end
32+
33+
def [](api)
34+
definitions.fetch(api) do
35+
option = api == :default ? '' : ", as: #{api.inspect}"
36+
raise(NotRegisteredError,
37+
"API description '#{api.inspect}' not found." \
38+
"Please call #{name}.register('myopenapi.yaml'#{option}) " \
39+
'once before running tests.')
40+
end
41+
end
42+
end
43+
end
44+
end

0 commit comments

Comments
 (0)