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
3 changes: 2 additions & 1 deletion demo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,8 @@ def initialize
"Custom Messages" => ["custom failure message", "custom message with block"],
"Complex Objects" => ["struct comparison", "custom class object comparison", "test with metadata", "custom object with many instance variables"],
"Fuzzy Matchers" => ["a_string_matching", "a_hash_including", "a_collection_containing_exactly", "an_instance_of", "include with hash conditions"],
"Yield Matchers" => ["yield_control", "yield_with_args"]
"Yield Matchers" => ["yield_control", "yield_with_args"],
"Output Matchers" => ["output to stdout", "output to stderr"]
}

categories.each do |category, descriptions|
Expand Down
11 changes: 10 additions & 1 deletion lib/rspec/enriched_json/expectation_helper_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "oj"
require "rspec/expectations"
require "rspec/support/differ"
require "stringio"

module RSpec
module EnrichedJson
Expand Down Expand Up @@ -46,7 +47,15 @@ def serialize_value(value)
if value.is_a?(Regexp)
return Oj.dump(value.inspect, mode: :compat)
elsif value.is_a?(Proc)
return value.call
original_stdout = $stdout
$stdout = StringIO.new

value.call

output = $stdout.string
$stdout = original_stdout

return output
end

Oj.dump(value, OJ_OPTIONS)
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/enriched_json/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module RSpec
module EnrichedJson
VERSION = "0.8.2"
VERSION = "0.8.3"
end
end
9 changes: 0 additions & 9 deletions spec/oj_serialization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,6 @@
expect(result).to eq("/http:\\/\\/example\\.com/")
end
end

describe "serialization of Proc objects" do
it "calls a simple Proc" do
helloworld = proc { "Hello, world!" }
result = serializer.serialize_value(helloworld)
expect(result).to eq("Hello, world!")
end
end

describe "Fallback behavior for errors" do
it "uses fallback format when Oj.dump fails" do
# Create an object that we'll mock to fail
Expand Down
70 changes: 70 additions & 0 deletions spec/stdout_matcher_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe "Stdout matcher value capture" do
let(:formatter) { RSpec::EnrichedJson::Formatters::EnrichedJsonFormatter.new(StringIO.new) }

# Use the same Oj options for loading that we use for dumping
let(:oj_load_options) do
{
mode: :object,
symbol_keys: true,
auto_define: false,
create_additions: false
}
end

it "captures actual when spec passes" do
test_file = "spec/support/stdout_passing_spec.rb"
File.write(test_file, <<~RUBY)
RSpec.describe "Test" do
it "checks output" do
expect { puts "Hello" }.to output("Hello\\n").to_stdout
end
end
RUBY

output = run_rspec(test_file)
json = Oj.load(output)
example = json["examples"].first

expect(example["details"]["expected"]).to eq("\"Hello\\n\"")
expect(example["details"]["actual"]).to eq("Hello\n")
ensure
File.delete(test_file) if File.exist?(test_file)
end

it "captures actual when spec fails" do
test_file = "spec/support/stdout_passing_spec.rb"
File.write(test_file, <<~RUBY)
RSpec.describe "Test" do
it "checks output" do
expect { puts "Hello" }.to output("Bye\\n").to_stdout
end
end
RUBY

output = run_rspec(test_file)
json = Oj.load(output)
example = json["examples"].first

expect(example["details"]["expected"]).to eq("\"Bye\\n\"")
expect(example["details"]["actual"]).to eq("\"Hello\\n\"")
ensure
File.delete(test_file) if File.exist?(test_file)
end

private

def run_rspec(test_file)
output = nil
Dir.mktmpdir do |dir|
output_file = File.join(dir, "output.json")
cmd = "bundle exec rspec #{test_file} --format RSpec::EnrichedJson::Formatters::EnrichedJsonFormatter --out #{output_file} 2>&1"
system(cmd, out: File::NULL)
output = File.read(output_file)
end
output
end
end
Loading