rubysl-stringio 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
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,60 @@
1
+ require File.expand_path('../fixtures/classes', __FILE__)
2
+
3
+ describe "StringIO#printf" do
4
+ before(:each) do
5
+ @io = StringIO.new('example')
6
+ end
7
+
8
+ it "returns nil" do
9
+ @io.printf("%d %04x", 123, 123).should be_nil
10
+ end
11
+
12
+ it "pads self with \\000 when the current position is after the end" do
13
+ @io.pos = 10
14
+ @io.printf("%d", 123)
15
+ @io.string.should == "example\000\000\000123"
16
+ end
17
+
18
+ it "performs format conversion" do
19
+ @io.printf("%d %04x", 123, 123)
20
+ @io.string.should == "123 007b"
21
+ end
22
+
23
+ it "updates the current position" do
24
+ @io.printf("%d %04x", 123, 123)
25
+ @io.pos.should eql(8)
26
+
27
+ @io.printf("%d %04x", 123, 123)
28
+ @io.pos.should eql(16)
29
+ end
30
+ end
31
+
32
+ describe "StringIO#printf when in append mode" do
33
+ before(:each) do
34
+ @io = StringIO.new("example", "a")
35
+ end
36
+
37
+ it "appends the passed argument to the end of self" do
38
+ @io.printf("%d %04x", 123, 123)
39
+ @io.string.should == "example123 007b"
40
+
41
+ @io.printf("%d %04x", 123, 123)
42
+ @io.string.should == "example123 007b123 007b"
43
+ end
44
+
45
+ it "correctly updates self's position" do
46
+ @io.printf("%d %04x", 123, 123)
47
+ @io.pos.should eql(15)
48
+ end
49
+ end
50
+
51
+ describe "StringIO#printf when self is not writable" do
52
+ it "raises an IOError" do
53
+ io = StringIO.new("test", "r")
54
+ lambda { io.printf("test") }.should raise_error(IOError)
55
+
56
+ io = StringIO.new("test")
57
+ io.close_write
58
+ lambda { io.printf("test") }.should raise_error(IOError)
59
+ end
60
+ end
data/spec/putc_spec.rb ADDED
@@ -0,0 +1,105 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path('../../../spec_helper', __FILE__)
4
+ require File.expand_path('../fixtures/classes', __FILE__)
5
+
6
+ describe "StringIO#putc when passed [String]" do
7
+ before(:each) do
8
+ @io = StringIO.new('example')
9
+ end
10
+
11
+ it "overwrites the character at the current position" do
12
+ @io.putc("t")
13
+ @io.string.should == "txample"
14
+
15
+ @io.pos = 3
16
+ @io.putc("t")
17
+ @io.string.should == "txatple"
18
+ end
19
+
20
+ it "only writes the first character from the passed String" do
21
+ @io.putc("test")
22
+ @io.string.should == "txample"
23
+ end
24
+
25
+ it "returns the passed String" do
26
+ str = "test"
27
+ @io.putc(str).should equal(str)
28
+ end
29
+
30
+ it "correctly updates the current position" do
31
+ @io.putc("t")
32
+ @io.pos.should == 1
33
+
34
+ @io.putc("test")
35
+ @io.pos.should == 2
36
+
37
+ @io.putc("t")
38
+ @io.pos.should == 3
39
+ end
40
+
41
+
42
+ with_feature :encoding do
43
+
44
+ before :each do
45
+ @enc_io = StringIO.new("ëllø")
46
+ end
47
+
48
+ it "writes a byte into the io" do
49
+ @enc_io.putc("t")
50
+ @enc_io.string.should == "t\xABllø"
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+
57
+ describe "StringIO#putc when passed [Object]" do
58
+ before(:each) do
59
+ @io = StringIO.new('example')
60
+ end
61
+
62
+ it "it writes the passed Integer % 256 to self" do
63
+ @io.putc(333) # 333 % 256 == ?M
64
+ @io.string.should == "Mxample"
65
+
66
+ @io.putc(-450) # -450 % 256 == ?>
67
+ @io.string.should == "M>ample"
68
+ end
69
+
70
+ it "pads self with \\000 when the current position is after the end" do
71
+ @io.pos = 10
72
+ @io.putc(?A)
73
+ @io.string.should == "example\000\000\000A"
74
+ end
75
+
76
+ it "tries to convert the passed argument to an Integer using #to_int" do
77
+ obj = mock('to_int')
78
+ obj.should_receive(:to_int).and_return(116)
79
+ @io.putc(obj)
80
+ @io.string.should == "txample"
81
+ end
82
+
83
+ it "raises a TypeError when the passed argument can't be coerced to Integer" do
84
+ lambda { @io.putc(Object.new) }.should raise_error(TypeError)
85
+ end
86
+ end
87
+
88
+ describe "StringIO#putc when in append mode" do
89
+ it "appends to the end of self" do
90
+ io = StringIO.new("test", "a")
91
+ io.putc(?t)
92
+ io.string.should == "testt"
93
+ end
94
+ end
95
+
96
+ describe "StringIO#putc when self is not writable" do
97
+ it "raises an IOError" do
98
+ io = StringIO.new("test", "r")
99
+ lambda { io.putc(?a) }.should raise_error(IOError)
100
+
101
+ io = StringIO.new("test")
102
+ io.close_write
103
+ lambda { io.putc("t") }.should raise_error(IOError)
104
+ end
105
+ end
data/spec/puts_spec.rb ADDED
@@ -0,0 +1,173 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path('../../../spec_helper', __FILE__)
4
+ require File.expand_path('../fixtures/classes', __FILE__)
5
+
6
+ describe "StringIO#puts when passed an Array" do
7
+ before(:each) do
8
+ @io = StringIO.new
9
+ end
10
+
11
+ it "writes each element of the passed Array to self, separated by a newline" do
12
+ @io.puts([1, 2, 3, 4])
13
+ @io.string.should == "1\n2\n3\n4\n"
14
+
15
+ @io.puts([1, 2], [3, 4])
16
+ @io.string.should == "1\n2\n3\n4\n1\n2\n3\n4\n"
17
+ end
18
+
19
+ it "flattens nested Arrays" do
20
+ @io.puts([1, [2, [3, [4]]]])
21
+ @io.string.should == "1\n2\n3\n4\n"
22
+ end
23
+
24
+ it "handles self-recursive arrays correctly" do
25
+ (ary = [5])
26
+ ary << ary
27
+ @io.puts(ary)
28
+ @io.string.should == "5\n[...]\n"
29
+ end
30
+
31
+ it "does not honor the global output record separator $\\" do
32
+ begin
33
+ old_rs, $\ = $\, "test"
34
+ @io.puts([1, 2, 3, 4])
35
+ @io.string.should == "1\n2\n3\n4\n"
36
+ ensure
37
+ $\ = old_rs
38
+ end
39
+ end
40
+
41
+ it "first tries to convert each Array element to an Array using #to_ary" do
42
+ obj = mock("Object")
43
+ obj.should_receive(:to_ary).and_return(["to_ary"])
44
+ @io.puts([obj])
45
+ @io.string.should == "to_ary\n"
46
+ end
47
+
48
+ it "then tries to convert each Array element to a String using #to_s" do
49
+ obj = mock("Object")
50
+ obj.should_receive(:to_s).and_return("to_s")
51
+ @io.puts([obj])
52
+ @io.string.should == "to_s\n"
53
+ end
54
+ end
55
+
56
+ describe "StringIO#puts when passed 1 or more objects" do
57
+ before(:each) do
58
+ @io = StringIO.new
59
+ end
60
+
61
+ it "does not honor the global output record separator $\\" do
62
+ begin
63
+ old_rs, $\ = $\, "test"
64
+ @io.puts(1, 2, 3, 4)
65
+ @io.string.should == "1\n2\n3\n4\n"
66
+ ensure
67
+ $\ = old_rs
68
+ end
69
+ end
70
+
71
+ it "does not put a \\n after each Objects that end in a newline" do
72
+ @io.puts("1\n", "2\n", "3\n")
73
+ @io.string.should == "1\n2\n3\n"
74
+ end
75
+
76
+ it "first tries to convert each Object to an Array using #to_ary" do
77
+ obj = mock("Object")
78
+ obj.should_receive(:to_ary).and_return(["to_ary"])
79
+ @io.puts(obj)
80
+ @io.string.should == "to_ary\n"
81
+ end
82
+
83
+ it "then tries to convert each Object to a String using #to_s" do
84
+ obj = mock("Object")
85
+ obj.should_receive(:to_s).and_return("to_s")
86
+ @io.puts(obj)
87
+ @io.string.should == "to_s\n"
88
+ end
89
+
90
+ it "prints a newline when passed an empty string" do
91
+ @io.puts ''
92
+ @io.string.should == "\n"
93
+ end
94
+
95
+ ruby_version_is ""..."1.9" do
96
+ it "prints a newline when passed nil" do
97
+ @io.puts nil
98
+ @io.string.should == "nil\n"
99
+ end
100
+ end
101
+
102
+ ruby_version_is "1.9" do
103
+ it "prints a newline when passed nil" do
104
+ @io.puts nil
105
+ @io.string.should == "\n"
106
+ end
107
+ end
108
+ end
109
+
110
+ describe "StringIO#puts when passed no arguments" do
111
+ before(:each) do
112
+ @io = StringIO.new
113
+ end
114
+
115
+ it "returns nil" do
116
+ @io.puts.should be_nil
117
+ end
118
+
119
+ it "prints a newline" do
120
+ @io.puts
121
+ @io.string.should == "\n"
122
+ end
123
+
124
+ it "does not honor the global output record separator $\\" do
125
+ begin
126
+ old_rs, $\ = $\, "test"
127
+ @io.puts
128
+ @io.string.should == "\n"
129
+ ensure
130
+ $\ = old_rs
131
+ end
132
+ end
133
+ end
134
+
135
+ describe "StringIO#puts when in append mode" do
136
+ before(:each) do
137
+ @io = StringIO.new("example", "a")
138
+ end
139
+
140
+ it "appends the passed argument to the end of self" do
141
+ @io.puts(", just testing")
142
+ @io.string.should == "example, just testing\n"
143
+
144
+ @io.puts(" and more testing")
145
+ @io.string.should == "example, just testing\n and more testing\n"
146
+ end
147
+
148
+ it "correctly updates self's position" do
149
+ @io.puts(", testing")
150
+ @io.pos.should eql(17)
151
+ end
152
+ end
153
+
154
+ describe "StringIO#puts when self is not writable" do
155
+ it "raises an IOError" do
156
+ io = StringIO.new("test", "r")
157
+ lambda { io.puts }.should raise_error(IOError)
158
+
159
+ io = StringIO.new("test")
160
+ io.close_write
161
+ lambda { io.puts }.should raise_error(IOError)
162
+ end
163
+ end
164
+
165
+ describe "StringIO#puts when passed an encoded string" do
166
+ it "stores the bytes unmodified" do
167
+ io = StringIO.new("")
168
+ io.puts "\x00\x01\x02"
169
+ io.puts "æåø"
170
+
171
+ io.string.should == "\x00\x01\x02\næåø\n"
172
+ end
173
+ end
@@ -0,0 +1,29 @@
1
+ require "stringio"
2
+ require File.expand_path('../shared/read', __FILE__)
3
+ require File.expand_path('../shared/sysread', __FILE__)
4
+
5
+ ruby_version_is "1.9" do
6
+ describe "StringIO#read_nonblock when passed length, buffer" do
7
+ it_behaves_like :stringio_read, :read_nonblock
8
+ end
9
+
10
+ describe "StringIO#read_nonblock when passed length" do
11
+ it_behaves_like :stringio_read_length, :read_nonblock
12
+ end
13
+
14
+ describe "StringIO#read_nonblock when passed no arguments" do
15
+ it_behaves_like :stringio_read_no_arguments, :read_nonblock
16
+ end
17
+
18
+ describe "StringIO#read_nonblock when self is not readable" do
19
+ it_behaves_like :stringio_read_not_readable, :read_nonblock
20
+ end
21
+
22
+ describe "StringIO#read_nonblock when passed nil" do
23
+ it_behaves_like :stringio_read_nil, :read_nonblock
24
+ end
25
+
26
+ describe "StringIO#read_nonblock when passed length" do
27
+ it_behaves_like :stringio_sysread_length, :read_nonblock
28
+ end
29
+ end
data/spec/read_spec.rb ADDED
@@ -0,0 +1,62 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../../../spec_helper', __FILE__)
3
+ require "stringio"
4
+ require File.expand_path('../shared/read', __FILE__)
5
+
6
+ describe "StringIO#read when passed length, buffer" do
7
+ it_behaves_like :stringio_read, :read
8
+ end
9
+
10
+ describe "StringIO#read when passed length" do
11
+ it_behaves_like :stringio_read_length, :read
12
+
13
+ it "returns nil when passed length > 0 and no data remains" do
14
+ @io.send(@method, 8).should == "example"
15
+ @io.send(@method, 2).should be_nil
16
+ end
17
+
18
+ # This was filed as a bug in redmine#156 but since MRI refused to change the
19
+ # 1.8 behavior, it's now considered a version difference by RubySpec since
20
+ # it could have a significant impact on user code.
21
+ ruby_version_is ""..."1.9" do
22
+ it "returns nil when passed 0 and no data remains" do
23
+ @io.send(@method, 8).should == "example"
24
+ @io.send(@method, 0).should be_nil
25
+ end
26
+ end
27
+
28
+ ruby_version_is "1.9" do
29
+ it "returns an empty String when passed 0 and no data remains" do
30
+ @io.send(@method, 8).should == "example"
31
+ @io.send(@method, 0).should == ""
32
+ end
33
+
34
+ it "truncates the buffer when limit > 0 and no data remains" do
35
+ @io.send(@method)
36
+ @io.send(@method, 2, buffer = "abc").should be_nil
37
+ buffer.should == ""
38
+ end
39
+ end
40
+ end
41
+
42
+ describe "StringIO#read when passed no arguments" do
43
+ it_behaves_like :stringio_read_no_arguments, :read
44
+
45
+ it "returns an empty string if at EOF" do
46
+ @io.read.should == "example"
47
+ @io.read.should == ""
48
+ end
49
+ end
50
+
51
+ describe "StringIO#read when passed nil" do
52
+ it_behaves_like :stringio_read_nil, :read
53
+
54
+ it "returns an empty string if at EOF" do
55
+ @io.read(nil).should == "example"
56
+ @io.read(nil).should == ""
57
+ end
58
+ end
59
+
60
+ describe "StringIO#read when self is not readable" do
61
+ it_behaves_like :stringio_read_not_readable, :read
62
+ end
@@ -0,0 +1,21 @@
1
+ require 'stringio'
2
+ require File.expand_path('../shared/readchar', __FILE__)
3
+
4
+ ruby_version_is "1.8.7" do
5
+ describe "StringIO#readbyte" do
6
+ it_behaves_like :stringio_readchar, :readbyte
7
+
8
+ it "reads the next 8-bit byte from self's current position" do
9
+ io = StringIO.new("example")
10
+
11
+ io.send(@method).should == 101
12
+
13
+ io.pos = 4
14
+ io.send(@method).should == 112
15
+ end
16
+ end
17
+
18
+ describe "StringIO#readbyte when self is not readable" do
19
+ it_behaves_like :stringio_readchar_not_readable, :readbyte
20
+ end
21
+ end