summaryrefslogtreecommitdiff
path: root/spec/ruby/core/encoding/converter/putback_spec.rb
blob: c4e0a5da213c9f1aeb36212249278513cfe68874 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# -*- encoding: binary -*-
require_relative '../../../spec_helper'

describe "Encoding::Converter#putback" do
  before :each do
    @ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
    @ret = @ec.primitive_convert(@src="abc\xa1def", @dst="", nil, 10)
  end

  it "returns a String" do
    @ec.putback.should be_an_instance_of(String)
  end

  it "returns a String in the source encoding" do
    @ec.putback.encoding.should == Encoding::EUC_JP
  end

  it "returns the bytes buffered due to an :invalid_byte_sequence error" do
    @ret.should == :invalid_byte_sequence
    @ec.putback.should == 'd'
    @ec.primitive_errinfo.last.should == 'd'
  end

  it "allows conversion to be resumed after an :invalid_byte_sequence" do
    @src = @ec.putback + @src
    @ret = @ec.primitive_convert(@src, @dst, nil, 10)
    @ret.should == :finished
    @dst.should == "abcdef"
    @src.should == ""
  end

  it "returns an empty String when there are no more bytes to put back" do
    @ec.putback
    @ec.putback.should == ""
  end

  it "returns the problematic bytes for UTF-16LE" do
    ec = Encoding::Converter.new("utf-16le", "iso-8859-1")
    src = "\x00\xd8\x61\x00"
    dst = ""
    ec.primitive_convert(src, dst).should == :invalid_byte_sequence
    ec.primitive_errinfo.should == [:invalid_byte_sequence, "UTF-16LE", "UTF-8", "\x00\xD8", "a\x00"]
    ec.putback.should == "a\x00".force_encoding("utf-16le")
    ec.putback.should == ""
  end

  it "accepts an integer argument corresponding to the number of bytes to be put back" do
    ec = Encoding::Converter.new("utf-16le", "iso-8859-1")
    src = "\x00\xd8\x61\x00"
    dst = ""
    ec.primitive_convert(src, dst).should == :invalid_byte_sequence
    ec.primitive_errinfo.should == [:invalid_byte_sequence, "UTF-16LE", "UTF-8", "\x00\xD8", "a\x00"]
    ec.putback(2).should == "a\x00".force_encoding("utf-16le")
    ec.putback.should == ""
  end
end