Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,11 @@ Renders just the exception details section. Returns `nil` if no exception.
#### `backtrace_html(**options)`
Renders just the backtrace section. Returns `nil` if no backtrace.

#### `status_html(**options)`
Renders just the status section.

#### `render_html(**options)`
Convenience method that renders all three sections in a standard layout.
Convenience method that renders all four sections in a standard layout.

### Class Methods

Expand Down
27 changes: 22 additions & 5 deletions lib/rspec/html_messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,27 @@ def backtrace_html(**options)
render_template("_backtrace")
end

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

# Public boolean methods so users can make their own decisions
def has_output?
# Don't show output for errors before assertions
return false if error_before_assertion?

status == "failed" || has_actual?
failed? || has_actual?
end

def has_failure_message?
status == "failed" && !error_before_assertion? && failure_message.present?
failed? && !error_before_assertion? && failure_message.present?
end

def has_exception_details?
Expand Down Expand Up @@ -142,6 +153,14 @@ def self.diff_css

private

def passed?
status == "passed"
end

def failed?
status == "failed"
end

def default_options
{
force_diffable: FORCE_DIFFABLE_MATCHERS,
Expand Down Expand Up @@ -174,7 +193,7 @@ def failure_message
end

def calculate_failure_message
return nil unless status == "failed"
return nil unless failed?

message = example.dig("exception", "message")
return nil unless message
Expand Down Expand Up @@ -314,8 +333,6 @@ def friendly_error_location
end
end

private
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already a private keyword above this, so I removed this one.


def validate_example!(example)
raise ArgumentError, "Example cannot be nil" if example.nil?
raise ArgumentError, "Example must be a Hash" unless example.is_a?(Hash)
Expand Down
3 changes: 3 additions & 0 deletions lib/rspec/html_messages/templates/_status.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="alert <%= css_class %>" role="alert">
<%= message %>
</div>
8 changes: 7 additions & 1 deletion lib/rspec/html_messages/templates/example.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<div class="rspec-html-messages">
<% if output_content = output_html(**@options) %>
<% if status_content = status_html(**@options) %>
<div>
<%= status_content %>
</div>
<% end %>

<% if output_content = output_html(**@options) %>
<div class="mt-3">
<%= output_content %>
</div>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/html_messages/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Rspec
class HtmlMessages
VERSION = "0.2.0"
VERSION = "0.2.1"
end
end
Loading