From e80f2844f9afd42fe6397d87ceab86df844c09e7 Mon Sep 17 00:00:00 2001 From: Ben Purinton Date: Wed, 16 Jul 2025 14:03:40 -0700 Subject: [PATCH 1/3] Fix serialization error when \n new lines are present --- lib/rspec/enriched_json/expectation_helper_wrapper.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/rspec/enriched_json/expectation_helper_wrapper.rb b/lib/rspec/enriched_json/expectation_helper_wrapper.rb index e84930c..74ba8b5 100644 --- a/lib/rspec/enriched_json/expectation_helper_wrapper.rb +++ b/lib/rspec/enriched_json/expectation_helper_wrapper.rb @@ -85,7 +85,15 @@ def truncate_string(str) def unescape_string_double_quotes(str) if str.start_with?('"') && str.end_with?('"') - return str.undump + begin + # Only undump if it's a valid dumped string + # Check if the string is properly escaped by attempting undump + str.undump + rescue RuntimeError => e + # If undump fails, just return the original string + # This handles cases where the string has quotes but isn't a valid dump format + str + end else str end From 4ba1e49d0da25c33611ae9dc57ab23b43074ae2e Mon Sep 17 00:00:00 2001 From: Ben Purinton Date: Fri, 18 Jul 2025 07:40:17 -0700 Subject: [PATCH 2/3] add demo and test --- demo.rb | 7 ++++++- spec/edge_cases_spec.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/demo.rb b/demo.rb index a587045..a534661 100755 --- a/demo.rb +++ b/demo.rb @@ -178,6 +178,11 @@ expect("\"Hello, ---world\"").to match("Hello, world") end + it "strings with newlines" do + output = "\"l appears 1 times\"\n\"o appears 2 times\"\n\"o appears 2 times\"\n\"p appears 1 times\"" + expect(output).to match("\"l appears 1 times\"\n\"o appears 2 times\"\n\"e appears 1 times\"\n\"p appears 1 times\"") + end + # Change Matchers it "change" do x = 5 @@ -396,7 +401,7 @@ def initialize "Truthiness Matchers" => ["be_truthy", "be_falsey / be_falsy", "be_nil"], "Predicate Matchers" => ["be_empty", "have_key"], "Collection Matchers" => ["include", "include with multiple items", "include with hash", "start_with", "end_with", "match (regex)", "match (regex) with custom message", "contain_exactly", "match_array", "all"], - "String Matchers" => ["match with string", "unescaping quotes in actual"], + "String Matchers" => ["match with string", "unescaping quotes in actual", "strings with newlines"], "Change Matchers" => ["change", "change by", "change by_at_least", "change by_at_most"], "Output Matchers" => ["output to stdout", "output to stderr"], "Exception Matchers" => ["raise_error", "raise_error with message", "raise_error when none raised", "unexpected exception (outside expect block)"], diff --git a/spec/edge_cases_spec.rb b/spec/edge_cases_spec.rb index 20545a1..31cf2a4 100644 --- a/spec/edge_cases_spec.rb +++ b/spec/edge_cases_spec.rb @@ -76,4 +76,31 @@ def to_s expect(e.details[:expected]["to_s"]).to eq("age") end end + + context "handling strings with newlines" do + it "handles strings with embedded newlines and quotes" do + # This was causing "invalid dumped string" errors before the fix + output = "\"l appears 1 times\"\n\"o appears 2 times\"\n\"o appears 2 times\"\n\"p appears 1 times\"" + expected = "\"l appears 1 times\"\n\"o appears 2 times\"\n\"e appears 1 times\"\n\"p appears 1 times\"" + + expect(output).to eq(expected) + rescue RSpec::EnrichedJson::EnrichedExpectationNotMetError => e + # Verify the strings are properly serialized without undump errors + expect(e.details[:actual]).to be_a(String) + expect(e.details[:expected]).to be_a(String) + + # The actual should contain the newlines (actual newlines, not escaped) + expect(e.details[:actual]).to include("\n") + expect(e.details[:actual]).to include("l appears 1 times") + expect(e.details[:actual]).to include("o appears 2 times") + + # The expected should also be properly serialized + expect(e.details[:expected]).to include("\n") + expect(e.details[:expected]).to include("e appears 1 times") + + # Should not have serialization errors + expect(e.details[:actual]).not_to be_a(Hash) + expect(e.details[:actual]).not_to include("serialization_error") + end + end end From a41d6bd8e971de99fad4d329441bb5aa957059e5 Mon Sep 17 00:00:00 2001 From: Ben Purinton Date: Fri, 18 Jul 2025 07:44:09 -0700 Subject: [PATCH 3/3] bump version --- lib/rspec/enriched_json/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rspec/enriched_json/version.rb b/lib/rspec/enriched_json/version.rb index 6feecd2..898f875 100644 --- a/lib/rspec/enriched_json/version.rb +++ b/lib/rspec/enriched_json/version.rb @@ -2,6 +2,6 @@ module RSpec module EnrichedJson - VERSION = "0.6.1" + VERSION = "0.6.2" end end