rubysl-stringio 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.travis.yml +7 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE +25 -0
  6. data/README.md +29 -0
  7. data/Rakefile +1 -0
  8. data/lib/rubysl/stringio.rb +2 -0
  9. data/lib/rubysl/stringio/stringio.rb +611 -0
  10. data/lib/rubysl/stringio/version.rb +5 -0
  11. data/lib/stringio.rb +1 -0
  12. data/rubysl-stringio.gemspec +24 -0
  13. data/spec/append_spec.rb +83 -0
  14. data/spec/binmode_spec.rb +8 -0
  15. data/spec/bytes_spec.rb +12 -0
  16. data/spec/chars_spec.rb +12 -0
  17. data/spec/close_read_spec.rb +30 -0
  18. data/spec/close_spec.rb +22 -0
  19. data/spec/close_write_spec.rb +30 -0
  20. data/spec/closed_read_spec.rb +11 -0
  21. data/spec/closed_spec.rb +15 -0
  22. data/spec/closed_write_spec.rb +11 -0
  23. data/spec/codepoints_spec.rb +11 -0
  24. data/spec/each_byte_spec.rb +10 -0
  25. data/spec/each_char_spec.rb +12 -0
  26. data/spec/each_codepoint_spec.rb +12 -0
  27. data/spec/each_line_spec.rb +14 -0
  28. data/spec/each_spec.rb +14 -0
  29. data/spec/eof_spec.rb +10 -0
  30. data/spec/external_encoding_spec.rb +12 -0
  31. data/spec/fcntl_spec.rb +7 -0
  32. data/spec/fileno_spec.rb +8 -0
  33. data/spec/fixtures/classes.rb +15 -0
  34. data/spec/flush_spec.rb +8 -0
  35. data/spec/fsync_spec.rb +8 -0
  36. data/spec/getbyte_spec.rb +25 -0
  37. data/spec/getc_spec.rb +31 -0
  38. data/spec/gets_spec.rb +245 -0
  39. data/spec/initialize_copy_spec.rb +95 -0
  40. data/spec/initialize_spec.rb +193 -0
  41. data/spec/internal_encoding_spec.rb +12 -0
  42. data/spec/isatty_spec.rb +6 -0
  43. data/spec/length_spec.rb +6 -0
  44. data/spec/lineno_spec.rb +29 -0
  45. data/spec/lines_spec.rb +16 -0
  46. data/spec/open_spec.rb +215 -0
  47. data/spec/path_spec.rb +15 -0
  48. data/spec/pid_spec.rb +7 -0
  49. data/spec/pos_spec.rb +27 -0
  50. data/spec/print_spec.rb +113 -0
  51. data/spec/printf_spec.rb +60 -0
  52. data/spec/putc_spec.rb +105 -0
  53. data/spec/puts_spec.rb +173 -0
  54. data/spec/read_nonblock_spec.rb +29 -0
  55. data/spec/read_spec.rb +62 -0
  56. data/spec/readbyte_spec.rb +21 -0
  57. data/spec/readchar_spec.rb +19 -0
  58. data/spec/readline_spec.rb +127 -0
  59. data/spec/readlines_spec.rb +124 -0
  60. data/spec/readpartial_spec.rb +29 -0
  61. data/spec/reopen_spec.rb +303 -0
  62. data/spec/rewind_spec.rb +23 -0
  63. data/spec/seek_spec.rb +75 -0
  64. data/spec/set_encoding_spec.rb +34 -0
  65. data/spec/shared/codepoints.rb +45 -0
  66. data/spec/shared/each.rb +162 -0
  67. data/spec/shared/each_byte.rb +60 -0
  68. data/spec/shared/each_char.rb +50 -0
  69. data/spec/shared/eof.rb +24 -0
  70. data/spec/shared/getc.rb +43 -0
  71. data/spec/shared/isatty.rb +5 -0
  72. data/spec/shared/length.rb +12 -0
  73. data/spec/shared/read.rb +186 -0
  74. data/spec/shared/readchar.rb +29 -0
  75. data/spec/shared/sysread.rb +27 -0
  76. data/spec/shared/tell.rb +12 -0
  77. data/spec/shared/write.rb +134 -0
  78. data/spec/size_spec.rb +6 -0
  79. data/spec/string_spec.rb +49 -0
  80. data/spec/stringio_spec.rb +8 -0
  81. data/spec/sync_spec.rb +18 -0
  82. data/spec/sysread_spec.rb +27 -0
  83. data/spec/syswrite_spec.rb +18 -0
  84. data/spec/tell_spec.rb +6 -0
  85. data/spec/truncate_spec.rb +69 -0
  86. data/spec/tty_spec.rb +6 -0
  87. data/spec/ungetbyte_spec.rb +88 -0
  88. data/spec/ungetc_spec.rb +98 -0
  89. data/spec/write_nonblock_spec.rb +20 -0
  90. data/spec/write_spec.rb +18 -0
  91. metadata +267 -0
@@ -0,0 +1,12 @@
1
+ require 'stringio'
2
+ require File.expand_path('../../../spec_helper', __FILE__)
3
+
4
+ ruby_version_is "1.9.2" do
5
+ describe "StringIO#internal_encoding" do
6
+ it "returns nil" do
7
+ io = StringIO.new
8
+ io.set_encoding Encoding::UTF_8
9
+ io.internal_encoding.should == nil
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,6 @@
1
+ require File.expand_path('../fixtures/classes', __FILE__)
2
+ require File.expand_path('../shared/isatty', __FILE__)
3
+
4
+ describe "StringIO#tty?" do
5
+ it_behaves_like :stringio_isatty, :isatty
6
+ end
@@ -0,0 +1,6 @@
1
+ require File.expand_path('../fixtures/classes', __FILE__)
2
+ require File.expand_path('../shared/length', __FILE__)
3
+
4
+ describe "StringIO#length" do
5
+ it_behaves_like :stringio_length, :length
6
+ end
@@ -0,0 +1,29 @@
1
+ require "stringio"
2
+
3
+ describe "StringIO#lineno" do
4
+ before(:each) do
5
+ @io = StringIO.new("this\nis\nan\nexample")
6
+ end
7
+
8
+ it "returns the number of lines read" do
9
+ @io.gets
10
+ @io.gets
11
+ @io.gets
12
+ @io.lineno.should eql(3)
13
+ end
14
+ end
15
+
16
+ describe "StringIO#lineno=" do
17
+ before(:each) do
18
+ @io = StringIO.new("this\nis\nan\nexample")
19
+ end
20
+
21
+ it "sets the current line number, but has no impact on the position" do
22
+ @io.lineno = 3
23
+ @io.pos.should eql(0)
24
+
25
+ @io.gets.should == "this\n"
26
+ @io.lineno.should eql(4)
27
+ @io.pos.should eql(5)
28
+ end
29
+ end
@@ -0,0 +1,16 @@
1
+ require 'stringio'
2
+ require File.expand_path('../shared/each', __FILE__)
3
+
4
+ ruby_version_is "1.8.7" do
5
+ describe "StringIO#lines when passed a separator" do
6
+ it_behaves_like :stringio_each_separator, :lines
7
+ end
8
+
9
+ describe "StringIO#lines when passed no arguments" do
10
+ it_behaves_like :stringio_each_no_arguments, :lines
11
+ end
12
+
13
+ describe "StringIO#lines when self is not readable" do
14
+ it_behaves_like :stringio_each_not_readable, :lines
15
+ end
16
+ end
data/spec/open_spec.rb ADDED
@@ -0,0 +1,215 @@
1
+ require 'stringio'
2
+
3
+ describe "StringIO.open when passed [Object, mode]" do
4
+ it "uses the passed Object as the StringIO backend" do
5
+ io = StringIO.open(str = "example", "r")
6
+ io.string.should equal(str)
7
+ end
8
+
9
+ it "returns the blocks return value when yielding" do
10
+ ret = StringIO.open("example", "r") { :test }
11
+ ret.should equal(:test)
12
+ end
13
+
14
+ it "yields self to the passed block" do
15
+ io = nil
16
+ StringIO.open("example", "r") { |strio| io = strio }
17
+ io.should be_kind_of(StringIO)
18
+ end
19
+
20
+ it "closes self after yielding" do
21
+ io = nil
22
+ StringIO.open("example", "r") { |strio| io = strio }
23
+ io.closed?.should be_true
24
+ end
25
+
26
+ it "even closes self when an exception is raised while yielding" do
27
+ io = nil
28
+ begin
29
+ StringIO.open("example", "r") do |strio|
30
+ io = strio
31
+ raise "Error"
32
+ end
33
+ rescue
34
+ end
35
+ io.closed?.should be_true
36
+ end
37
+
38
+ it "sets self's string to nil after yielding" do
39
+ io = nil
40
+ StringIO.open("example", "r") { |strio| io = strio }
41
+ io.string.should be_nil
42
+ end
43
+
44
+ it "even sets self's string to nil when an exception is raised while yielding" do
45
+ io = nil
46
+ begin
47
+ StringIO.open("example", "r") do |strio|
48
+ io = strio
49
+ raise "Error"
50
+ end
51
+ rescue
52
+ end
53
+ io.string.should be_nil
54
+ end
55
+
56
+ it "sets the mode based on the passed mode" do
57
+ io = StringIO.open("example", "r")
58
+ io.closed_read?.should be_false
59
+ io.closed_write?.should be_true
60
+
61
+ io = StringIO.open("example", "rb")
62
+ io.closed_read?.should be_false
63
+ io.closed_write?.should be_true
64
+
65
+ io = StringIO.open("example", "r+")
66
+ io.closed_read?.should be_false
67
+ io.closed_write?.should be_false
68
+
69
+ io = StringIO.open("example", "rb+")
70
+ io.closed_read?.should be_false
71
+ io.closed_write?.should be_false
72
+
73
+ io = StringIO.open("example", "w")
74
+ io.closed_read?.should be_true
75
+ io.closed_write?.should be_false
76
+
77
+ io = StringIO.open("example", "wb")
78
+ io.closed_read?.should be_true
79
+ io.closed_write?.should be_false
80
+
81
+ io = StringIO.open("example", "w+")
82
+ io.closed_read?.should be_false
83
+ io.closed_write?.should be_false
84
+
85
+ io = StringIO.open("example", "wb+")
86
+ io.closed_read?.should be_false
87
+ io.closed_write?.should be_false
88
+
89
+ io = StringIO.open("example", "a")
90
+ io.closed_read?.should be_true
91
+ io.closed_write?.should be_false
92
+
93
+ io = StringIO.open("example", "ab")
94
+ io.closed_read?.should be_true
95
+ io.closed_write?.should be_false
96
+
97
+ io = StringIO.open("example", "a+")
98
+ io.closed_read?.should be_false
99
+ io.closed_write?.should be_false
100
+
101
+ io = StringIO.open("example", "ab+")
102
+ io.closed_read?.should be_false
103
+ io.closed_write?.should be_false
104
+ end
105
+
106
+ it "allows passing the mode as an Integer" do
107
+ io = StringIO.open("example", IO::RDONLY)
108
+ io.closed_read?.should be_false
109
+ io.closed_write?.should be_true
110
+
111
+ io = StringIO.open("example", IO::RDWR)
112
+ io.closed_read?.should be_false
113
+ io.closed_write?.should be_false
114
+
115
+ io = StringIO.open("example", IO::WRONLY)
116
+ io.closed_read?.should be_true
117
+ io.closed_write?.should be_false
118
+
119
+ io = StringIO.open("example", IO::WRONLY | IO::TRUNC)
120
+ io.closed_read?.should be_true
121
+ io.closed_write?.should be_false
122
+
123
+ io = StringIO.open("example", IO::RDWR | IO::TRUNC)
124
+ io.closed_read?.should be_false
125
+ io.closed_write?.should be_false
126
+
127
+ io = StringIO.open("example", IO::WRONLY | IO::APPEND)
128
+ io.closed_read?.should be_true
129
+ io.closed_write?.should be_false
130
+
131
+ io = StringIO.open("example", IO::RDWR | IO::APPEND)
132
+ io.closed_read?.should be_false
133
+ io.closed_write?.should be_false
134
+ end
135
+
136
+ ruby_version_is "" ... "1.9" do
137
+ it "raises a TypeError when passed a frozen String in truncate mode as StringIO backend" do
138
+ lambda { StringIO.open("example".freeze, IO::TRUNC) }.should raise_error(TypeError)
139
+ end
140
+ end
141
+
142
+ ruby_version_is "1.9" do
143
+ it "raises a RuntimeError when passed a frozen String in truncate mode as StringIO backend" do
144
+ lambda { StringIO.open("example".freeze, IO::TRUNC) }.should raise_error(RuntimeError)
145
+ end
146
+ end
147
+
148
+ it "tries to convert the passed mode to a String using #to_str" do
149
+ obj = mock('to_str')
150
+ obj.should_receive(:to_str).and_return("r")
151
+ io = StringIO.open("example", obj)
152
+
153
+ io.closed_read?.should be_false
154
+ io.closed_write?.should be_true
155
+ end
156
+
157
+ it "raises an Errno::EACCES error when passed a frozen string with a write-mode" do
158
+ (str = "example").freeze
159
+ lambda { io = StringIO.open(str, "r+") }.should raise_error(Errno::EACCES)
160
+ lambda { io = StringIO.open(str, "w") }.should raise_error(Errno::EACCES)
161
+ lambda { io = StringIO.open(str, "a") }.should raise_error(Errno::EACCES)
162
+ end
163
+ end
164
+
165
+ describe "StringIO.open when passed [Object]" do
166
+ it "uses the passed Object as the StringIO backend" do
167
+ io = StringIO.open(str = "example")
168
+ io.string.should equal(str)
169
+ end
170
+
171
+ it "yields self to the passed block" do
172
+ io = nil
173
+ ret = StringIO.open("example") { |strio| io = strio }
174
+ io.should equal(ret)
175
+ end
176
+
177
+ it "sets the mode to read-write" do
178
+ io = StringIO.open("example")
179
+ io.closed_read?.should be_false
180
+ io.closed_write?.should be_false
181
+ end
182
+
183
+ it "tries to convert the passed Object to a String using #to_str" do
184
+ obj = mock('to_str')
185
+ obj.should_receive(:to_str).and_return("example")
186
+ io = StringIO.open(obj)
187
+ io.string.should == "example"
188
+ end
189
+
190
+ it "automatically sets the mode to read-only when passed a frozen string" do
191
+ (str = "example").freeze
192
+ io = StringIO.open(str)
193
+ io.closed_read?.should be_false
194
+ io.closed_write?.should be_true
195
+ end
196
+ end
197
+
198
+ describe "StringIO.open when passed no arguments" do
199
+ it "yields self to the passed block" do
200
+ io = nil
201
+ ret = StringIO.open { |strio| io = strio }
202
+ io.should equal(ret)
203
+ end
204
+
205
+ it "sets the mode to read-write" do
206
+ io = StringIO.open
207
+ io.closed_read?.should be_false
208
+ io.closed_write?.should be_false
209
+ end
210
+
211
+ it "uses an empty String as the StringIO backend" do
212
+ StringIO.open.string.should == ""
213
+ end
214
+ end
215
+
data/spec/path_spec.rb ADDED
@@ -0,0 +1,15 @@
1
+ require File.expand_path('../fixtures/classes', __FILE__)
2
+
3
+ describe "StringIO#path" do
4
+ ruby_version_is "" ... "1.9" do
5
+ it "returns nil" do
6
+ StringIO.new("path").path.should be_nil
7
+ end
8
+ end
9
+
10
+ ruby_version_is "1.9" do
11
+ it "is not defined" do
12
+ lambda { StringIO.new("path").path }.should raise_error(NoMethodError)
13
+ end
14
+ end
15
+ end
data/spec/pid_spec.rb ADDED
@@ -0,0 +1,7 @@
1
+ require File.expand_path('../fixtures/classes', __FILE__)
2
+
3
+ describe "StringIO#pid" do
4
+ it "returns nil" do
5
+ StringIO.new("pid").pid.should be_nil
6
+ end
7
+ end
data/spec/pos_spec.rb ADDED
@@ -0,0 +1,27 @@
1
+ require File.expand_path('../fixtures/classes', __FILE__)
2
+ require File.expand_path('../shared/tell', __FILE__)
3
+
4
+ describe "StringIO#pos" do
5
+ it_behaves_like :stringio_tell, :pos
6
+ end
7
+
8
+ describe "StringIO#pos=" do
9
+ before(:each) do
10
+ @io = StringIOSpecs.build
11
+ end
12
+
13
+ it "updates the current byte offset" do
14
+ @io.pos = 26
15
+ @io.read(1).should == "r"
16
+ end
17
+
18
+ it "raises an EINVAL if given a negative argument" do
19
+ lambda { @io.pos = -10 }.should raise_error(Errno::EINVAL)
20
+ end
21
+
22
+ it "updates the current byte offset after reaching EOF" do
23
+ @io.read
24
+ @io.pos = 26
25
+ @io.read(1).should == "r"
26
+ end
27
+ end
@@ -0,0 +1,113 @@
1
+ require File.expand_path('../fixtures/classes', __FILE__)
2
+
3
+ describe "StringIO#print" do
4
+ before(:each) do
5
+ @io = StringIO.new('example')
6
+ end
7
+
8
+ ruby_version_is "" ... "1.9" do
9
+ it "prints $_ when passed no arguments" do
10
+ $_ = nil
11
+ @io.print
12
+ @io.string.should == "nilmple"
13
+
14
+ $_ = "blah"
15
+ @io.print
16
+ @io.string.should == "nilblah"
17
+ end
18
+ end
19
+
20
+ ruby_version_is "1.9" do
21
+ it "prints $_ when passed no arguments" do
22
+ $_ = nil
23
+ @io.print
24
+ @io.string.should == "example"
25
+
26
+ $_ = "blah"
27
+ @io.print
28
+ @io.string.should == "blahple"
29
+ end
30
+ end
31
+
32
+ it "prints the passed arguments to self" do
33
+ @io.print(5, 6, 7, 8)
34
+ @io.string.should == "5678ple"
35
+ end
36
+
37
+ it "tries to convert the passed Object to a String using #to_s" do
38
+ obj = mock("to_s")
39
+ obj.should_receive(:to_s).and_return("to_s")
40
+ @io.print(obj)
41
+ @io.string.should == "to_sple"
42
+ end
43
+
44
+ it "returns nil" do
45
+ @io.print(1, 2, 3).should be_nil
46
+ end
47
+
48
+ it "pads self with \\000 when the current position is after the end" do
49
+ @io.pos = 10
50
+ @io.print(1, 2, 3)
51
+ @io.string.should == "example\000\000\000123"
52
+ end
53
+
54
+ it "honors the output record separator global" do
55
+ old_rs, $\ = $\, 'x'
56
+
57
+ begin
58
+ @io.print(5, 6, 7, 8)
59
+ @io.string.should == '5678xle'
60
+ ensure
61
+ $\ = old_rs
62
+ end
63
+ end
64
+
65
+ it "updates the current position" do
66
+ @io.print(1, 2, 3)
67
+ @io.pos.should eql(3)
68
+
69
+ @io.print(1, 2, 3)
70
+ @io.pos.should eql(6)
71
+ end
72
+
73
+ it "correctly updates the current position when honoring the output record separator global" do
74
+ old_rs, $\ = $\, 'x'
75
+
76
+ begin
77
+ @io.print(5, 6, 7, 8)
78
+ @io.pos.should eql(5)
79
+ ensure
80
+ $\ = old_rs
81
+ end
82
+ end
83
+ end
84
+
85
+ describe "StringIO#print when in append mode" do
86
+ before(:each) do
87
+ @io = StringIO.new("example", "a")
88
+ end
89
+
90
+ it "appends the passed argument to the end of self" do
91
+ @io.print(", just testing")
92
+ @io.string.should == "example, just testing"
93
+
94
+ @io.print(" and more testing")
95
+ @io.string.should == "example, just testing and more testing"
96
+ end
97
+
98
+ it "correctly updates self's position" do
99
+ @io.print(", testing")
100
+ @io.pos.should eql(16)
101
+ end
102
+ end
103
+
104
+ describe "StringIO#print when self is not writable" do
105
+ it "raises an IOError" do
106
+ io = StringIO.new("test", "r")
107
+ lambda { io.print("test") }.should raise_error(IOError)
108
+
109
+ io = StringIO.new("test")
110
+ io.close_write
111
+ lambda { io.print("test") }.should raise_error(IOError)
112
+ end
113
+ end