Class: Rspec::HtmlMessages

Inherits:
Object
  • Object
show all
Includes:
DiffFormatter, TemplateRenderer, ValueFormatter
Defined in:
lib/rspec/html_messages.rb,
lib/rspec/html_messages/version.rb,
lib/rspec/html_messages/diff_formatter.rb,
lib/rspec/html_messages/value_formatter.rb,
lib/rspec/html_messages/template_renderer.rb

Overview

Main renderer class for converting enriched JSON examples to HTML

Defined Under Namespace

Modules: DiffFormatter, TemplateRenderer, ValueFormatter

Constant Summary collapse

FORCE_DIFFABLE_MATCHERS =

Matchers that should always show diffs regardless of their diffable flag

["RSpec::Matchers::BuiltIn::ContainExactly"].freeze
FORCE_NOT_DIFFABLE_MATCHERS =

Matchers that should never show diffs regardless of their diffable flag

[
  "RSpec::Matchers::BuiltIn::Include",
  "RSpec::Matchers::BuiltIn::Compound::And",
  "RSpec::Matchers::BuiltIn::Compound::Or"
].freeze
VERSION =
"0.2.2"

Constants included from ValueFormatter

ValueFormatter::AWESOME_PRINT_OPTIONS, ValueFormatter::OJ_LOAD_OPTIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TemplateRenderer

#render_template

Methods included from DiffFormatter

#create_diff, #effective_diffable?

Methods included from ValueFormatter

#deserialize_value, #prettify_for_diff

Constructor Details

#initialize(example) ⇒ HtmlMessages

Returns a new instance of HtmlMessages.



27
28
29
30
# File 'lib/rspec/html_messages.rb', line 27

def initialize(example)
  validate_example!(example)
  @example = example
end

Instance Attribute Details

#exampleObject (readonly)

Returns the value of attribute example.



25
26
27
# File 'lib/rspec/html_messages.rb', line 25

def example
  @example
end

Class Method Details

.diff_cssObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rspec/html_messages.rb', line 117

def self.diff_css
  # Minimal CSS for displaying diffs generated by Diffy
  "    .diff { overflow: auto; }\n    .diff ul {\n      overflow: auto;\n      list-style: none;\n      margin: 0;\n      padding: 0;\n      display: table;\n      width: 100%;\n    }\n    .diff del, .diff ins {\n      display: block;\n      text-decoration: none;\n    }\n    .diff li {\n      padding: 0;\n      display: table-row;\n      margin: 0;\n      height: 1em;\n    }\n    .diff li.ins { background: #dfd; color: #080; }\n    .diff li.del { background: #fee; color: #b00; }\n    .diff li:hover { background: #ffc; }\n    .diff del, .diff ins, .diff span { white-space: pre-wrap; }\n    .diff del strong { font-weight: normal; background: #fcc; }\n    .diff ins strong { font-weight: normal; background: #9f9; }\n    .diff li.diff-comment { display: none; }\n    .diff li.diff-block-info { background: none repeat scroll 0 0 gray; }\n  CSS\nend\n"

Instance Method Details

#backtrace_html(**options) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/rspec/html_messages.rb', line 70

def backtrace_html(**options)
  return nil unless has_backtrace?

  @options = default_options.merge(options)
  @backtrace_max_lines = @options[:backtrace_max_lines]
  @backtrace_silence_gems = @options[:backtrace_silence_gems]

  render_template("_backtrace")
end

#exception_details_html(**options) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/rspec/html_messages.rb', line 62

def exception_details_html(**options)
  return nil unless has_exception_details?

  @options = default_options.merge(options)

  render_template("_exception_details")
end

#failure_message_html(**options) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/rspec/html_messages.rb', line 51

def failure_message_html(**options)
  @options = default_options.merge(options)

  # Don't show failure message for errors - that goes in exception details
  return nil unless has_failure_message?

  @failure_message_text = failure_message

  render_template("_failure_message")
end

#has_backtrace?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/rspec/html_messages.rb', line 107

def has_backtrace?
  exception_backtrace.any?
end

#has_exception_details?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/rspec/html_messages.rb', line 103

def has_exception_details?
  error_before_assertion?
end

#has_failure_message?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/rspec/html_messages.rb', line 99

def has_failure_message?
  failed? && !error_before_assertion? && failure_message.present?
end

#has_output?Boolean

Public boolean methods so users can make their own decisions

Returns:

  • (Boolean)


92
93
94
95
96
97
# File 'lib/rspec/html_messages.rb', line 92

def has_output?
  # Don't show output for errors before assertions
  return false if error_before_assertion?

  failed? || has_actual?
end

#optionsObject



32
33
34
# File 'lib/rspec/html_messages.rb', line 32

def options
  @options ||= default_options
end

#output_html(**options) ⇒ Object

Individual rendering methods - return nil when no content to display



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rspec/html_messages.rb', line 37

def output_html(**options)
  return nil unless has_output?

  @options = default_options.merge(options)
  @force_diffable = @options[:force_diffable]
  @force_not_diffable = @options[:force_not_diffable]

  if should_show_diff?
    render_template("_diff")
  else
    render_template("_actual")
  end
end

#render_html(**options) ⇒ Object

Example of how to use all three together



112
113
114
115
# File 'lib/rspec/html_messages.rb', line 112

def render_html(**options)
  @options = default_options.merge(options)
  render_template("example")
end

#status_html(**options) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/rspec/html_messages.rb', line 80

def status_html(**options)
  if passed?
    css_class = "alert-success"
    message = "This test passed!"
  else
    css_class = "alert-warning"
    message = "The test did not pass."
  end
  render_template("_status", css_class: css_class, message: message)
end