summaryrefslogtreecommitdiff
path: root/spec/ruby/core/io/set_encoding_spec.rb
blob: b749331ee0f66e8f1983c14e29c4c09691fee03e (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
require_relative '../../spec_helper'

with_feature :encoding do
  describe :io_set_encoding_write, shared: true do
    it "sets the encodings to nil" do
      @io = new_io @name, "#{@object}:ibm437:ibm866"
      @io.set_encoding nil, nil

      @io.external_encoding.should be_nil
      @io.internal_encoding.should be_nil
    end

    it "prevents the encodings from changing when Encoding defaults are changed" do
      @io = new_io @name, "#{@object}:utf-8:us-ascii"
      @io.set_encoding nil, nil

      Encoding.default_external = Encoding::IBM437
      Encoding.default_internal = Encoding::IBM866

      @io.external_encoding.should be_nil
      @io.internal_encoding.should be_nil
    end

    it "sets the encodings to the current Encoding defaults" do
      @io = new_io @name, @object

      Encoding.default_external = Encoding::IBM437
      Encoding.default_internal = Encoding::IBM866

      @io.set_encoding nil, nil

      @io.external_encoding.should == Encoding::IBM437
      @io.internal_encoding.should == Encoding::IBM866
    end
  end

  describe "IO#set_encoding when passed nil, nil" do
    before :each do
      @external = Encoding.default_external
      @internal = Encoding.default_internal

      Encoding.default_external = Encoding::UTF_8
      Encoding.default_internal = nil

      @name = tmp('io_set_encoding.txt')
      touch(@name)
    end

    after :each do
      Encoding.default_external = @external
      Encoding.default_internal = @internal

      @io.close if @io and not @io.closed?
      rm_r @name
    end

    describe "with 'r' mode" do
      it "sets the encodings to the current Encoding defaults" do
        @io = new_io @name, "r"

        Encoding.default_external = Encoding::IBM437
        Encoding.default_internal = Encoding::IBM866

        @io.set_encoding nil, nil
        @io.external_encoding.should equal(Encoding::IBM437)
        @io.internal_encoding.should equal(Encoding::IBM866)
      end

      it "prevents the #internal_encoding from changing when Encoding.default_internal is changed" do
        @io = new_io @name, "r"
        @io.set_encoding nil, nil

        Encoding.default_internal = Encoding::IBM437

        @io.internal_encoding.should be_nil
      end

      it "allows the #external_encoding to change when Encoding.default_external is changed" do
        @io = new_io @name, "r"
        @io.set_encoding nil, nil

        Encoding.default_external = Encoding::IBM437

        @io.external_encoding.should equal(Encoding::IBM437)
      end
    end

    describe "with 'rb' mode" do
      it "returns Encoding.default_external" do
        @io = new_io @name, "rb"
        @io.external_encoding.should equal(Encoding::ASCII_8BIT)

        @io.set_encoding nil, nil
        @io.external_encoding.should equal(Encoding.default_external)
      end
    end

    describe "with 'r+' mode" do
      it_behaves_like :io_set_encoding_write, nil, "r+"
    end

    describe "with 'w' mode" do
      it_behaves_like :io_set_encoding_write, nil, "w"
    end

    describe "with 'w+' mode" do
      it_behaves_like :io_set_encoding_write, nil, "w+"
    end

    describe "with 'a' mode" do
      it_behaves_like :io_set_encoding_write, nil, "a"
    end

    describe "with 'a+' mode" do
      it_behaves_like :io_set_encoding_write, nil, "a+"
    end
  end

  describe "IO#set_encoding" do
    before :each do
      @name = tmp('io_set_encoding.txt')
      touch(@name)
      @io = new_io @name
    end

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

    it "returns self" do
      @io.set_encoding(Encoding::UTF_8).should equal(@io)
    end

    it "sets the external encoding when passed an Encoding argument" do
      @io.set_encoding(Encoding::UTF_8)
      @io.external_encoding.should == Encoding::UTF_8
      @io.internal_encoding.should be_nil
    end

    it "sets the external and internal encoding when passed two Encoding arguments" do
      @io.set_encoding(Encoding::UTF_8, Encoding::UTF_16BE)
      @io.external_encoding.should == Encoding::UTF_8
      @io.internal_encoding.should == Encoding::UTF_16BE
    end

    it "sets the external encoding when passed the name of an Encoding" do
      @io.set_encoding("utf-8")
      @io.external_encoding.should == Encoding::UTF_8
      @io.internal_encoding.should be_nil
    end

    it "ignores the internal encoding if the same as external when passed Encoding objects" do
      @io.set_encoding(Encoding::UTF_8, Encoding::UTF_8)
      @io.external_encoding.should == Encoding::UTF_8
      @io.internal_encoding.should be_nil
    end

    it "ignores the internal encoding if the same as external when passed encoding names separanted by ':'" do
      @io.set_encoding("utf-8:utf-8")
      @io.external_encoding.should == Encoding::UTF_8
      @io.internal_encoding.should be_nil
    end

    it "sets the external and internal encoding when passed the names of Encodings separated by ':'" do
      @io.set_encoding("utf-8:utf-16be")
      @io.external_encoding.should == Encoding::UTF_8
      @io.internal_encoding.should == Encoding::UTF_16BE
    end

    it "sets the external and internal encoding when passed two String arguments" do
      @io.set_encoding("utf-8", "utf-16be")
      @io.external_encoding.should == Encoding::UTF_8
      @io.internal_encoding.should == Encoding::UTF_16BE
    end

    it "calls #to_str to convert an abject to a String" do
      obj = mock("io_set_encoding")
      obj.should_receive(:to_str).and_return("utf-8:utf-16be")
      @io.set_encoding(obj)
      @io.external_encoding.should == Encoding::UTF_8
      @io.internal_encoding.should == Encoding::UTF_16BE
    end

    it "calls #to_str to convert the second argument to a String" do
      obj = mock("io_set_encoding")
      obj.should_receive(:to_str).at_least(1).times.and_return("utf-16be")
      @io.set_encoding(Encoding::UTF_8, obj)
      @io.external_encoding.should == Encoding::UTF_8
      @io.internal_encoding.should == Encoding::UTF_16BE
    end
  end
end