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
|
require_relative '../../spec_helper'
describe :io_internal_encoding, shared: true do
describe "when Encoding.default_internal is not set" do
before :each do
Encoding.default_internal = nil
end
it "returns nil if the internal encoding is not set" do
@io = new_io @name, @object
@io.internal_encoding.should be_nil
end
it "returns nil if Encoding.default_internal is changed after the instance is created" do
@io = new_io @name, @object
Encoding.default_internal = Encoding::IBM437
@io.internal_encoding.should be_nil
end
it "returns the value set when the instance was created" do
@io = new_io @name, "#{@object}:utf-8:euc-jp"
Encoding.default_internal = Encoding::IBM437
@io.internal_encoding.should equal(Encoding::EUC_JP)
end
it "returns the value set by #set_encoding" do
@io = new_io @name, @object
@io.set_encoding(Encoding::US_ASCII, Encoding::IBM437)
@io.internal_encoding.should equal(Encoding::IBM437)
end
end
describe "when Encoding.default_internal == Encoding.default_external" do
before :each do
Encoding.default_external = Encoding::IBM866
Encoding.default_internal = Encoding::IBM866
end
it "returns nil" do
@io = new_io @name, @object
@io.internal_encoding.should be_nil
end
it "returns nil regardless of Encoding.default_internal changes" do
@io = new_io @name, @object
Encoding.default_internal = Encoding::IBM437
@io.internal_encoding.should be_nil
end
end
describe "when Encoding.default_internal != Encoding.default_external" do
before :each do
Encoding.default_external = Encoding::IBM437
Encoding.default_internal = Encoding::IBM866
end
it "returns the value of Encoding.default_internal when the instance was created if the internal encoding is not set" do
@io = new_io @name, @object
@io.internal_encoding.should equal(Encoding::IBM866)
end
it "does not change when Encoding.default_internal is changed" do
@io = new_io @name, @object
Encoding.default_internal = Encoding::IBM437
@io.internal_encoding.should equal(Encoding::IBM866)
end
it "returns the internal encoding set when the instance was created" do
@io = new_io @name, "#{@object}:utf-8:euc-jp"
@io.internal_encoding.should equal(Encoding::EUC_JP)
end
it "does not change when set and Encoding.default_internal is changed" do
@io = new_io @name, "#{@object}:utf-8:euc-jp"
Encoding.default_internal = Encoding::IBM437
@io.internal_encoding.should equal(Encoding::EUC_JP)
end
it "returns the value set by #set_encoding" do
@io = new_io @name, @object
@io.set_encoding(Encoding::US_ASCII, Encoding::IBM437)
@io.internal_encoding.should equal(Encoding::IBM437)
end
it "returns nil when Encoding.default_external is ASCII-8BIT and the internal encoding is not set" do
Encoding.default_external = Encoding::ASCII_8BIT
@io = new_io @name, @object
@io.internal_encoding.should be_nil
end
it "returns nil when the external encoding is ASCII-8BIT and the internal encoding is not set" do
@io = new_io @name, "#{@object}:ascii-8bit"
@io.internal_encoding.should be_nil
end
end
end
describe "IO#internal_encoding" do
before :each do
@external = Encoding.default_external
@internal = Encoding.default_internal
@name = tmp("io_internal_encoding")
touch(@name)
end
after :each do
@io.close if @io
rm_r @name
Encoding.default_external = @external
Encoding.default_internal = @internal
end
describe "with 'r' mode" do
it_behaves_like :io_internal_encoding, nil, "r"
end
describe "with 'r+' mode" do
it_behaves_like :io_internal_encoding, nil, "r+"
end
describe "with 'w' mode" do
it_behaves_like :io_internal_encoding, nil, "w"
end
describe "with 'w+' mode" do
it_behaves_like :io_internal_encoding, nil, "w+"
end
describe "with 'a' mode" do
it_behaves_like :io_internal_encoding, nil, "a"
end
describe "with 'a+' mode" do
it_behaves_like :io_internal_encoding, nil, "a+"
end
end
|