string_spec.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. require 'rails_helper'
  2. RSpec.describe String do
  3. describe '#utf8_encode' do
  4. context 'on valid, UTF-8-encoded strings' do
  5. let(:subject) { 'hello' }
  6. it 'returns an identical copy' do
  7. expect(subject.utf8_encode).to eq(subject)
  8. expect(subject.utf8_encode.encoding).to be(subject.encoding)
  9. expect(subject.utf8_encode).not_to be(subject)
  10. end
  11. context 'which are incorrectly set to other, technically valid encodings' do
  12. let(:subject) { 'ö'.force_encoding('tis-620') }
  13. it 'sets input encoding to UTF-8 instead of attempting conversion' do
  14. expect(subject.utf8_encode).to eq(subject.force_encoding('utf-8'))
  15. end
  16. end
  17. end
  18. context 'on strings in other encodings' do
  19. let(:subject) { original_string.encode(input_encoding) }
  20. context 'with no from: option' do
  21. let(:original_string) { 'Tschüss!' }
  22. let(:input_encoding) { Encoding::ISO_8859_2 }
  23. it 'detects the input encoding' do
  24. expect(subject.utf8_encode).to eq(original_string)
  25. end
  26. end
  27. context 'with a valid from: option' do
  28. let(:original_string) { 'Tschüss!' }
  29. let(:input_encoding) { Encoding::ISO_8859_2 }
  30. it 'uses the specified input encoding' do
  31. expect(subject.utf8_encode(from: 'iso-8859-2')).to eq(original_string)
  32. end
  33. it 'uses any valid input encoding, even if not correct' do
  34. expect(subject.utf8_encode(from: 'gb18030')).to eq('Tsch黶s!')
  35. end
  36. end
  37. context 'with an invalid from: option' do
  38. let(:original_string) { '―陈志' }
  39. let(:input_encoding) { Encoding::GB18030 }
  40. it 'does not try it' do
  41. expect { subject.encode('utf-8', 'gb2312') }
  42. .to raise_error(Encoding::InvalidByteSequenceError)
  43. expect { subject.utf8_encode(from: 'gb2312') }
  44. .not_to raise_error(Encoding::InvalidByteSequenceError)
  45. end
  46. it 'uses the detected input encoding instead' do
  47. expect(subject.utf8_encode(from: 'gb2312')).to eq(original_string)
  48. end
  49. end
  50. end
  51. end
  52. end