Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit 05611c5

Browse files
committed
Handle RSpec description with japanese char in CP932 encoded files
When the user create a test with CP932 encoding and japanese chars RSpec fail to properly display characters or crash with the error: > Encoding::CompatibilityError: incompatible character encodings: Windows-31J and UTF-8 Fix: - #2570 - https://github.com/rspec/rspec-core/issues/2543 For Ruby 1.8.7 We are following the same behavior as for rspec-support https://github.com/rspec/rspec-support/blob/9940a8656071655b807f772f36101b4d27f1b67d/lib/rspec/support/spec/string_matcher.rb#L8
1 parent 4fba098 commit 05611c5

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

lib/rspec/core/formatters/exception_presenter.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def fully_formatted(failure_number, colorizer=::RSpec::Core::Formatters::Console
8181

8282
def fully_formatted_lines(failure_number, colorizer)
8383
lines = [
84-
description,
84+
encoded_description(description),
8585
detail_formatter.call(example, colorizer),
8686
formatted_message_and_backtrace(colorizer),
8787
extra_detail_formatter.call(failure_number, colorizer),
@@ -244,6 +244,16 @@ def formatted_message_and_backtrace(colorizer)
244244
end
245245
end
246246

247+
def encoded_description(description)
248+
return if description.nil?
249+
250+
if String.method_defined?(:encoding)
251+
encoded_string(description)
252+
else # for 1.8.7
253+
description
254+
end
255+
end
256+
247257
def exception_backtrace
248258
exception.backtrace || []
249259
end

spec/rspec/core/formatters/exception_presenter_spec.rb

+17
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@ module RSpec::Core
9494
EOS
9595
end
9696

97+
if String.method_defined?(:encoding)
98+
it 'allows the caller to add encoded description' do
99+
the_presenter = Formatters::ExceptionPresenter.new(exception, example,
100+
:description => "ジ".encode("CP932"))
101+
102+
expect(the_presenter.fully_formatted(1)).to eq(<<-EOS.gsub(/^ +\|/, ''))
103+
|
104+
| 1) ジ
105+
| Failure/Error: # The failure happened here!#{ encoding_check }
106+
|
107+
| Boom
108+
| Bam
109+
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:#{line_num}
110+
EOS
111+
end
112+
end
113+
97114
it 'allows the caller to omit the description' do
98115
the_presenter = Formatters::ExceptionPresenter.new(exception, example,
99116
:detail_formatter => Proc.new { "Detail!" },

0 commit comments

Comments
 (0)