summaryrefslogtreecommitdiff
path: root/spec/ruby/core/io/readchar_spec.rb
blob: a66773851ad77d161f0b1be3b58b26a23f97597a (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe :io_readchar_internal_encoding, shared: true do
  it "returns a transcoded String" do
    @io.readchar.should == "あ"
  end

  it "sets the String encoding to the internal encoding" do
    @io.readchar.encoding.should equal(Encoding::UTF_8)
  end
end

describe "IO#readchar" do
  before :each do
    @io = IOSpecs.io_fixture "lines.txt"
  end

  after :each do
    @io.close unless @io.closed?
  end

  it "returns the next string from the stream" do
    @io.readchar.should == 'V'
    @io.readchar.should == 'o'
    @io.readchar.should == 'i'
    # read the rest of line
    @io.readline.should == "ci la ligne une.\n"
    @io.readchar.should == 'Q'
  end

  it "raises an EOFError when invoked at the end of the stream" do
    @io.read
    -> { @io.readchar }.should raise_error(EOFError)
  end

  it "raises IOError on closed stream" do
    -> { IOSpecs.closed_io.readchar }.should raise_error(IOError)
  end
end

describe "IO#readchar with internal encoding" do
  after :each do
    @io.close if @io
  end

  describe "not specified" do
    before :each do
      @io = IOSpecs.io_fixture "read_euc_jp.txt", "r:euc-jp"
    end

    it "does not transcode the String" do
      @io.readchar.should == ("あ").encode(Encoding::EUC_JP)
    end

    it "sets the String encoding to the external encoding" do
      @io.readchar.encoding.should equal(Encoding::EUC_JP)
    end
  end

  describe "specified by open mode" do
    before :each do
      @io = IOSpecs.io_fixture "read_euc_jp.txt", "r:euc-jp:utf-8"
    end

    it_behaves_like :io_readchar_internal_encoding, nil
  end

  describe "specified by mode: option" do
    before :each do
      @io = IOSpecs.io_fixture "read_euc_jp.txt", mode: "r:euc-jp:utf-8"
    end

    it_behaves_like :io_readchar_internal_encoding, nil
  end

  describe "specified by internal_encoding: option" do
    before :each do
      options = { mode: "r",
                  internal_encoding: "utf-8",
                  external_encoding: "euc-jp" }
      @io = IOSpecs.io_fixture "read_euc_jp.txt", options
    end

    it_behaves_like :io_readchar_internal_encoding, nil
  end

  describe "specified by encoding: option" do
    before :each do
      options = { mode: "r", encoding: "euc-jp:utf-8" }
      @io = IOSpecs.io_fixture "read_euc_jp.txt", options
    end

    it_behaves_like :io_readchar_internal_encoding, nil
  end
end

describe "IO#readchar" do
  before :each do
    @io = IOSpecs.io_fixture "empty.txt"
  end

  after :each do
    @io.close unless @io.closed?
  end

  it "raises EOFError on empty stream" do
    -> { @io.readchar }.should raise_error(EOFError)
  end
end