Class: OpenapiRuby::Testing::Coverage

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_ruby/testing/coverage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Coverage

Returns a new instance of Coverage.



8
9
10
11
12
# File 'lib/openapi_ruby/testing/coverage.rb', line 8

def initialize(document)
  @document = document
  @covered = Set.new
  @total = extract_all_endpoints
end

Instance Attribute Details

#coveredObject (readonly)

Returns the value of attribute covered.



6
7
8
# File 'lib/openapi_ruby/testing/coverage.rb', line 6

def covered
  @covered
end

#totalObject (readonly)

Returns the value of attribute total.



6
7
8
# File 'lib/openapi_ruby/testing/coverage.rb', line 6

def total
  @total
end

Instance Method Details

#percentageObject



23
24
25
26
27
# File 'lib/openapi_ruby/testing/coverage.rb', line 23

def percentage
  return 100.0 if @total.empty?

  (@covered.size.to_f / @total.size * 100).round(1)
end

#record(method, path, status_code) ⇒ Object



14
15
16
17
# File 'lib/openapi_ruby/testing/coverage.rb', line 14

def record(method, path, status_code)
  key = "#{method.upcase} #{path} #{status_code}"
  @covered.add(key)
end

#reportObject



29
30
31
32
33
34
35
36
37
# File 'lib/openapi_ruby/testing/coverage.rb', line 29

def report
  {
    total: @total.size,
    covered: @covered.size,
    uncovered: uncovered.size,
    percentage: percentage,
    missing: uncovered.to_a.sort
  }
end

#to_json(*_args) ⇒ Object



39
40
41
# File 'lib/openapi_ruby/testing/coverage.rb', line 39

def to_json(*_args)
  JSON.pretty_generate(report)
end

#uncoveredObject



19
20
21
# File 'lib/openapi_ruby/testing/coverage.rb', line 19

def uncovered
  @total - @covered
end