Class: RSpec::RfcHelper::MarkdownRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/rfc_helper/markdown_renderer.rb

Overview

Markdown renderer for specs

Constant Summary collapse

STATUS_COLORS =
{
  # Status from Spec class
  failing:     'red',
  pass:        'green',
  has_pending: 'orange',
  unknown:     'gray',

  # Statuses from RSpec::Core::Example
  failed:      'red',
  pending:     'orange',
  passed:      'green',
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(specs) ⇒ MarkdownRenderer

Returns a new instance of MarkdownRenderer.

Parameters:



23
24
25
# File 'lib/rspec/rfc_helper/markdown_renderer.rb', line 23

def initialize(specs)
  @specs = specs
end

Instance Method Details

#detailed_spec_report_block(spec) ⇒ String

Generates a spec report block (header and examples table)

Parameters:

Returns:

  • (String)


95
96
97
98
99
100
101
# File 'lib/rspec/rfc_helper/markdown_renderer.rb', line 95

def detailed_spec_report_block(spec)
  <<~MD.chomp
    #{detailed_spec_report_header(spec)}

    #{detailed_spec_report_examples(spec)}
  MD
end

#detailed_spec_report_examples(spec) ⇒ String

Generates the example table of a spec report

Parameters:

Returns:

  • (String)


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/rspec/rfc_helper/markdown_renderer.rb', line 132

def detailed_spec_report_examples(spec) # rubocop:disable Metrics/MethodLength
  lines = []
  spec.examples.each do |example|
    meta = example.
    type = meta[:type] ? "`#{meta[:type]}`: " : ''
    lines << "| #{status(example.execution_result.status)} | #{type}`#{meta[:location]}` |"
  end

  return "**No related examples**\n" if lines.empty?

  <<~MD.chomp
    **Examples:**

    | Status | Reference |
    |:------:|-----------|
    #{lines.join("\n")}

  MD
end

#detailed_spec_report_header(spec) ⇒ String

Generates header for a spec report

Parameters:

Returns:

  • (String)


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rspec/rfc_helper/markdown_renderer.rb', line 109

def detailed_spec_report_header(spec) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  section = "#{spec.section} - #{@specs.sections[spec.section][:title]}"
  spec_link = if @specs.sections[spec.section][:url]
                "[#{section}](#{@specs.sections[spec.section][:url]})"
              else
                section
              end
  anchor = spec.id ? "<a id=\"#{spec.id}\"></a>" : ''
  <<~MD.chomp
    ## #{anchor}#{status(spec.status, '')} `#{spec.verb.upcase}` #{spec.id}

    **Specification:** #{spec_link}

    > #{paragraph(spec.text)}
  MD
end

#headerString

Generates the report header

Returns:

  • (String)


53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rspec/rfc_helper/markdown_renderer.rb', line 53

def header
  title = 'Compliance status'
  title = "#{title}: #{@specs.name}" if @specs.name
  url = @specs.specs_url ? "[#{@specs.specs_url}](#{@specs.specs_url})" : '~'

  <<~MD.chomp
    # #{title}

    Report generated on #{Time.now}

    Source specification: #{url}
  MD
end

#main_table(lines) ⇒ String

Generates the main table

Parameters:

  • lines (Array<String>)

    Table rows

Returns:

  • (String)


171
172
173
174
175
176
177
# File 'lib/rspec/rfc_helper/markdown_renderer.rb', line 171

def main_table(lines)
  <<~MD.chomp
    | status | id | section | verb | text |
    |-------:|---:|---------|------|------|
    #{lines.join("\n")}
  MD
end

#main_table_row(spec) ⇒ String

Generates a row for the main table

Parameters:

Returns:

  • (String)


158
159
160
161
162
163
# File 'lib/rspec/rfc_helper/markdown_renderer.rb', line 158

def main_table_row(spec)
  section = @specs.sections[spec.section]
  section_link = section[:url] ? "[#{spec.section}](#{section[:url]})" : spec.section
  id_link = spec.id ? "[`#{spec.id}`](##{spec.id})" : ''
  "| #{status(spec.status)} | #{id_link} | #{section_link} | #{spec.verb} | #{paragraph(spec.text)} |"
end

#paragraph(string) ⇒ String

Makes some replacement in a text

Parameters:

  • string (String)

Returns:

  • (String)


84
85
86
87
# File 'lib/rspec/rfc_helper/markdown_renderer.rb', line 84

def paragraph(string)
  string.gsub("\n", ' <br> ')
        .gsub(/\[\[([^\]]*)\]\]/, '<mark>\1</mark>')
end

#renderString

Generates a report in Markdown format.

Returns:

  • (String)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rspec/rfc_helper/markdown_renderer.rb', line 31

def render
  main_table_lines = []
  detailed_spec_report_blocks = []

  @specs.each do |spec|
    main_table_lines << main_table_row(spec)
    detailed_spec_report_blocks << detailed_spec_report_block(spec)
  end

  <<~MD.chomp
    #{header}

    #{main_table(main_table_lines)}

    #{detailed_spec_report_blocks.join("\n")}
  MD
end

#status(value, string = nil) ⇒ String

Generates a colored HTML string to represent the spec status

Parameters:

  • value (String, Symbol)

    Status

  • string (String?) (defaults to: nil)

    Custom text replacement

Returns:

  • (String)


74
75
76
# File 'lib/rspec/rfc_helper/markdown_renderer.rb', line 74

def status(value, string = nil)
  "<span style='color: #{STATUS_COLORS[value]}'>#{string || value}</span>"
end