Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect extra_failure_lines in RSpec metadata #208

Merged
merged 1 commit into from
Jan 22, 2024
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
23 changes: 22 additions & 1 deletion lib/super_diff/rspec/monkey_patches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,10 @@ def failure_line_groups
}
end
end

@failure_line_groups << {
lines: extra_failure_lines,
already_colorized: true
}
@failure_line_groups
end
end
Expand All @@ -241,6 +244,24 @@ def failure_slash_error_lines
lines
end

# Override to ensure that each item in the array returned is one and
# only one line and doesn't contain any newline characters
def extra_failure_lines
@extra_failure_lines ||=
begin
original_lines = Array(example.metadata[:extra_failure_lines])
normalized_lines =
original_lines.flat_map { |line| line.split("\n") }
unless normalized_lines.empty?
unless normalized_lines.first == ""
normalized_lines.unshift("")
end
normalized_lines.push("") unless normalized_lines.last == ""
end
normalized_lines
end
end

# Exclude this file from being included in backtraces, so that the
# SnippetExtractor prints the right thing
def find_failed_line
Expand Down
51 changes: 51 additions & 0 deletions spec/integration/rspec/magic_metadata_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require "spec_helper"

RSpec.describe "Integration with RSpec's magic metadata", type: :integration do
it "includes extra_failure_lines in failure messages" do
as_both_colored_and_uncolored do |color_enabled|
snippet = <<~TEST.strip
RSpec.describe "test" do
it { expect(true).to be(false) }

after do
RSpec.current_example.metadata[:extra_failure_lines] = "foo\nbar"
end
end
TEST
program =
make_plain_test_program(
snippet,
color_enabled: color_enabled,
preserve_as_whole_file: true
)

expected_output =
build_expected_output(
color_enabled: color_enabled,
test_name: "test is expected to equal false",
snippet: "it { expect(true).to be(false) }",
expectation:
proc do
line do
plain "Expected "
actual "true"
plain " to equal "
expected "false"
plain "."
end
end,
extra_failure_lines:
proc do
indent by: 5 do
line "foo"
line "bar"
end
end
)

expect(program).to produce_output_when_run(expected_output).in_color(
color_enabled
)
end
end
end
11 changes: 9 additions & 2 deletions spec/support/integration/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,17 @@ def build_expected_output(
color_enabled:,
snippet:,
expectation:,
test_name: "test passes",
key_enabled: true,
newline_before_expectation: false,
indentation: 7,
diff: nil
diff: nil,
extra_failure_lines: nil
)
colored(color_enabled: color_enabled) do
line "Failures:\n"

line "1) test passes", indent_by: 2
line "1) #{test_name}", indent_by: 2

line indent_by: 5 do
bold "Failure/Error: "
Expand Down Expand Up @@ -106,6 +108,11 @@ def build_expected_output(
newline
end
end

if extra_failure_lines
newline
evaluate_block(&extra_failure_lines)
end
end
end

Expand Down