File: putback_spec.rb

package info (click to toggle)
ruby2.7 2.7.4-1%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 112,576 kB
  • sloc: ruby: 849,454; ansic: 697,834; yacc: 45,100; xml: 25,367; pascal: 10,051; javascript: 6,575; sh: 3,848; makefile: 759; cpp: 713; asm: 333; python: 295; lisp: 97; sed: 94; perl: 62; awk: 36
file content (47 lines) | stat: -rw-r--r-- 1,543 bytes parent folder | download | duplicates (2)
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
# -*- 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 "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(1).should == "\x00".force_encoding("utf-16le")
    ec.putback.should == "a".force_encoding("utf-16le")
    ec.putback.should == ""
  end
end