Class: OpenapiFirst::Test::Coverage::HtmlReporter::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_first/test/coverage/html_reporter/context.rb

Overview

Provides the binding and helper methods for the ERB template.

Constant Summary collapse

NO_REQUESTS_WARNING =
'API Coverage did not detect any API requests for the registered ' \
'API descriptions. Make sure to observe your application using OpenapiFirst::Test.'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(coverage_result, verbose) ⇒ Context

Returns a new instance of Context.



17
18
19
20
21
# File 'lib/openapi_first/test/coverage/html_reporter/context.rb', line 17

def initialize(coverage_result, verbose)
  @coverage = coverage_result.coverage
  @plans = coverage_result.plans
  @verbose = verbose
end

Instance Attribute Details

#coverageObject (readonly)

Returns the value of attribute coverage.



15
16
17
# File 'lib/openapi_first/test/coverage/html_reporter/context.rb', line 15

def coverage
  @coverage
end

#plansObject (readonly)

Returns the value of attribute plans.



15
16
17
# File 'lib/openapi_first/test/coverage/html_reporter/context.rb', line 15

def plans
  @plans
end

#verboseObject (readonly)

Returns the value of attribute verbose.



15
16
17
# File 'lib/openapi_first/test/coverage/html_reporter/context.rb', line 15

def verbose
  @verbose
end

Instance Method Details

#any_request_made?(route) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/openapi_first/test/coverage/html_reporter/context.rb', line 39

def any_request_made?(route)
  route.requests.any?(&:requested?)
end

#expand_plan?(plan) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/openapi_first/test/coverage/html_reporter/context.rb', line 29

def expand_plan?(plan)
  verbose || plan.done?
end

#explain_unfinished_request(request) ⇒ Object



71
72
73
74
75
76
# File 'lib/openapi_first/test/coverage/html_reporter/context.rb', line 71

def explain_unfinished_request(request)
  return 'No requests tracked!' unless request.requested?
  return if request.any_valid_request?

  "All requests invalid! (#{request.last_error_message.inspect})"
end

#explain_unfinished_response(response, request_made: false) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/openapi_first/test/coverage/html_reporter/context.rb', line 78

def explain_unfinished_response(response, request_made: false)
  unless response.responded?
    return request_made ? 'No matching response tracked!' : 'No responses tracked!'
  end

  "All responses invalid! (#{response.last_error_message.inspect})" unless response.any_valid_response?
end

#get_bindingObject

Helper for ERB rendering only — exposes this context’s binding so the template can resolve helper methods and instance state.



25
26
27
# File 'lib/openapi_first/test/coverage/html_reporter/context.rb', line 25

def get_binding # rubocop:disable Naming/AccessorMethodName
  binding
end

#h(text) ⇒ Object



67
68
69
# File 'lib/openapi_first/test/coverage/html_reporter/context.rb', line 67

def h(text)
  ERB::Util.html_escape(text)
end

#request_items(route, plan_verbose:) ⇒ Object



54
55
56
57
58
# File 'lib/openapi_first/test/coverage/html_reporter/context.rb', line 54

def request_items(route, plan_verbose:)
  return [] unless any_request_made?(route) && route.requests.any?(&:content_type)

  plan_verbose ? route.requests : route.requests.reject(&:finished?)
end

#response_items(route, plan_verbose:) ⇒ Object



60
61
62
63
64
65
# File 'lib/openapi_first/test/coverage/html_reporter/context.rb', line 60

def response_items(route, plan_verbose:)
  return [] unless plan_verbose || any_request_made?(route)
  return route.responses if plan_verbose || route.responses.any? { |r| !r.finished? }

  []
end

#route_status(route) ⇒ Object



43
44
45
46
47
48
# File 'lib/openapi_first/test/coverage/html_reporter/context.rb', line 43

def route_status(route)
  return :request_problem if route.requests.none?(&:finished?)
  return :responses_problem if any_request_made?(route) && route.responses.any? { |r| !r.finished? }

  :ok
end

#uncovered_responses_count(route) ⇒ Object



50
51
52
# File 'lib/openapi_first/test/coverage/html_reporter/context.rb', line 50

def uncovered_responses_count(route)
  route.responses.count { |r| !r.finished? }
end

#visible_routes(plan) ⇒ Object



33
34
35
36
37
# File 'lib/openapi_first/test/coverage/html_reporter/context.rb', line 33

def visible_routes(plan)
  return plan.routes if expand_plan?(plan)

  plan.routes.reject(&:finished?)
end