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
data/spec/size_spec.rb ADDED
@@ -0,0 +1,6 @@
1
+ require File.expand_path('../fixtures/classes', __FILE__)
2
+ require File.expand_path('../shared/length', __FILE__)
3
+
4
+ describe "StringIO#size" do
5
+ it_behaves_like :stringio_length, :size
6
+ end
@@ -0,0 +1,49 @@
1
+ require File.expand_path('../fixtures/classes', __FILE__)
2
+
3
+ describe "StringIO#string" do
4
+ it "returns the underlying string" do
5
+ io = StringIO.new(str = "hello")
6
+ io.string.should equal(str)
7
+ end
8
+ end
9
+
10
+ describe "StringIO#string=" do
11
+ before(:each) do
12
+ @io = StringIO.new("example\nstring")
13
+ end
14
+
15
+ it "returns the passed String" do
16
+ str = "test"
17
+ (@io.string = str).should equal(str)
18
+ end
19
+
20
+ it "changes the underlying string" do
21
+ str = "hello"
22
+ @io.string = str
23
+ @io.string.should equal(str)
24
+ end
25
+
26
+ it "resets the position" do
27
+ @io.pos = 1
28
+ @io.string = "other"
29
+ @io.pos.should eql(0)
30
+ end
31
+
32
+ it "resets the line number" do
33
+ @io.lineno = 1
34
+ @io.string = "other"
35
+ @io.lineno.should eql(0)
36
+ end
37
+
38
+ it "tries to convert the passed Object to a String using #to_str" do
39
+ obj = mock("to_str")
40
+ obj.should_receive(:to_str).and_return("to_str")
41
+
42
+ @io.string = obj
43
+ @io.string.should == "to_str"
44
+ end
45
+
46
+ it "raises a TypeError when the passed Object can't be converted to an Integer" do
47
+ lambda { @io.seek(Object.new) }.should raise_error(TypeError)
48
+ end
49
+ end
@@ -0,0 +1,8 @@
1
+ require "stringio"
2
+
3
+ describe "StringIO" do
4
+ it "includes the Enumerable module" do
5
+ StringIO.should include(Enumerable)
6
+ end
7
+ end
8
+
data/spec/sync_spec.rb ADDED
@@ -0,0 +1,18 @@
1
+ require File.expand_path('../fixtures/classes', __FILE__)
2
+
3
+ describe "StringIO#sync" do
4
+ it "returns true" do
5
+ StringIO.new('').sync.should be_true
6
+ end
7
+ end
8
+
9
+ describe "StringIO#sync=" do
10
+ before(:each) do
11
+ @io = StringIO.new('')
12
+ end
13
+
14
+ it "does not change 'sync' status" do
15
+ @io.sync = false
16
+ @io.sync.should be_true
17
+ end
18
+ end
@@ -0,0 +1,27 @@
1
+ require "stringio"
2
+ require File.expand_path('../shared/read', __FILE__)
3
+ require File.expand_path('../shared/sysread', __FILE__)
4
+
5
+ describe "StringIO#sysread when passed length, buffer" do
6
+ it_behaves_like :stringio_read, :sysread
7
+ end
8
+
9
+ describe "StringIO#sysread when passed length" do
10
+ it_behaves_like :stringio_read_length, :sysread
11
+ end
12
+
13
+ describe "StringIO#sysread when passed no arguments" do
14
+ it_behaves_like :stringio_read_no_arguments, :sysread
15
+ end
16
+
17
+ describe "StringIO#sysread when self is not readable" do
18
+ it_behaves_like :stringio_read_not_readable, :sysread
19
+ end
20
+
21
+ describe "StringIO#sysread when passed nil" do
22
+ it_behaves_like :stringio_read_nil, :sysread
23
+ end
24
+
25
+ describe "StringIO#sysread when passed length" do
26
+ it_behaves_like :stringio_sysread_length, :sysread
27
+ end
@@ -0,0 +1,18 @@
1
+ require File.expand_path('../fixtures/classes', __FILE__)
2
+ require File.expand_path('../shared/write', __FILE__)
3
+
4
+ describe "StringIO#syswrite when passed [Object]" do
5
+ it_behaves_like :stringio_write, :syswrite
6
+ end
7
+
8
+ describe "StringIO#syswrite when passed [String]" do
9
+ it_behaves_like :stringio_write_string, :syswrite
10
+ end
11
+
12
+ describe "StringIO#syswrite when self is not writable" do
13
+ it_behaves_like :stringio_write_not_writable, :syswrite
14
+ end
15
+
16
+ describe "StringIO#syswrite when in append mode" do
17
+ it_behaves_like :stringio_write_append, :syswrite
18
+ end
data/spec/tell_spec.rb ADDED
@@ -0,0 +1,6 @@
1
+ require File.expand_path('../fixtures/classes', __FILE__)
2
+ require File.expand_path('../shared/tell', __FILE__)
3
+
4
+ describe "StringIO#tell" do
5
+ it_behaves_like :stringio_tell, :tell
6
+ end
@@ -0,0 +1,69 @@
1
+ require "stringio"
2
+
3
+ describe "StringIO#truncate when passed [length]" do
4
+ before(:each) do
5
+ @io = StringIO.new('123456789')
6
+ end
7
+
8
+ # TODO: Report to Ruby-Core: The RDoc says it always returns 0
9
+ it "returns the passed length" do
10
+ @io.truncate(4).should eql(4)
11
+ @io.truncate(10).should eql(10)
12
+ end
13
+
14
+ it "truncated the underlying string down to the passed length" do
15
+ @io.truncate(4)
16
+ @io.string.should == "1234"
17
+ end
18
+
19
+ it "does not create a copy of the underlying string" do
20
+ io = StringIO.new(str = "123456789")
21
+ io.truncate(4)
22
+ io.string.should equal(str)
23
+ end
24
+
25
+ it "does not change the position" do
26
+ @io.pos = 7
27
+ @io.truncate(4)
28
+ @io.pos.should eql(7)
29
+ end
30
+
31
+ it "can grow a string to a larger size, padding it with \\000" do
32
+ @io.truncate(12)
33
+ @io.string.should == "123456789\000\000\000"
34
+ end
35
+
36
+ it "raises an Errno::EINVAL when the passed length is negative" do
37
+ lambda { @io.truncate(-1) }.should raise_error(Errno::EINVAL)
38
+ lambda { @io.truncate(-10) }.should raise_error(Errno::EINVAL)
39
+ end
40
+
41
+ it "tries to convert the passed length to an Integer using #to_int" do
42
+ obj = mock("to_int")
43
+ obj.should_receive(:to_int).and_return(4)
44
+
45
+ @io.truncate(obj)
46
+ @io.string.should == "1234"
47
+ end
48
+
49
+ it "returns the passed length Object, NOT the result of #to_int" do
50
+ obj = mock("to_int")
51
+ obj.should_receive(:to_int).and_return(4)
52
+ @io.truncate(obj).should equal(obj)
53
+ end
54
+
55
+ it "raises a TypeError when the passed length can't be converted to an Integer" do
56
+ lambda { @io.truncate(Object.new) }.should raise_error(TypeError)
57
+ end
58
+ end
59
+
60
+ describe "StringIO#truncate when self is not writable" do
61
+ it "raises an IOError" do
62
+ io = StringIO.new("test", "r")
63
+ lambda { io.truncate(2) }.should raise_error(IOError)
64
+
65
+ io = StringIO.new("test")
66
+ io.close_write
67
+ lambda { io.truncate(2) }.should raise_error(IOError)
68
+ end
69
+ end
data/spec/tty_spec.rb ADDED
@@ -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, :tty?
6
+ end
@@ -0,0 +1,88 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../../../spec_helper', __FILE__)
3
+ require File.expand_path('../fixtures/classes', __FILE__)
4
+
5
+ ruby_version_is "1.9" do
6
+ describe "StringIO#ungetbyte" do
7
+ before :each do
8
+ @io = StringIO.new("abcdef")
9
+ end
10
+
11
+ it "returns nil" do
12
+ @io.ungetbyte(65).should be_nil
13
+ end
14
+
15
+ it "returns nil and does not modify data if passed nil" do
16
+ @io.read(2).should == "ab"
17
+ @io.ungetbyte(nil).should be_nil
18
+ @io.rewind
19
+ @io.read.should == "abcdef"
20
+ end
21
+
22
+ it "prepends the byte to the data before data is read" do
23
+ @io.ungetbyte(65)
24
+ @io.read(2).should == "Aa"
25
+ end
26
+
27
+ it "preserves the prepended bytes when #rewind is called" do
28
+ @io.ungetbyte(65)
29
+ @io.ungetbyte(66)
30
+ @io.rewind
31
+ @io.read.should == "BAabcdef"
32
+ end
33
+
34
+ it "prepends byte to the data at the current position" do
35
+ @io.read(3).should == "abc"
36
+ @io.ungetbyte(65)
37
+ @io.read(2).should == "Ad"
38
+ end
39
+
40
+ it "overwrites bytes in the data" do
41
+ @io.read(3).should == "abc"
42
+ @io.ungetbyte(66)
43
+ @io.ungetbyte(65)
44
+ @io.rewind
45
+ @io.read.should == "aABdef"
46
+ end
47
+
48
+ it "prepends a string to data before data is read" do
49
+ @io.ungetbyte("ghi")
50
+ @io.read.should == "ghiabcdef"
51
+ end
52
+
53
+ it "prepends a string at the current position" do
54
+ @io.read(2).should == "ab"
55
+ @io.ungetbyte("dceb")
56
+ @io.read.should == "dcebcdef"
57
+ end
58
+
59
+ it "calls #to_str to convert an object to a String" do
60
+ bytes = mock("stringio ungetbyte")
61
+ bytes.should_receive(:to_str).and_return("xyz")
62
+
63
+ @io.read(3).should == "abc"
64
+ @io.ungetbyte(bytes).should be_nil
65
+ @io.rewind
66
+ @io.read.should == "xyzdef"
67
+ end
68
+
69
+ it "raises an IOError when the mode is not readable" do
70
+ lambda { StringIO.new("", "w").ungetbyte(42) }.should raise_error(IOError)
71
+ end
72
+
73
+ it "raises an IOError when read is closed" do
74
+ @io.read
75
+ @io.close_read
76
+ lambda { @io.ungetbyte(42) }.should raise_error(IOError)
77
+ end
78
+
79
+ with_feature :encoding do
80
+ it "does not change the encoding of the data" do
81
+ @io.ungetbyte(0xff)
82
+ result = @io.read
83
+ result.should == "\xffabcdef"
84
+ result.encoding.should == Encoding::UTF_8
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,98 @@
1
+ require File.expand_path('../fixtures/classes', __FILE__)
2
+
3
+ describe "StringIO#ungetc when passed [char]" do
4
+ before(:each) do
5
+ @io = StringIO.new('1234')
6
+ end
7
+
8
+ it "writes the passed char before the current position" do
9
+ @io.pos = 1
10
+ @io.ungetc(?A)
11
+ @io.string.should == 'A234'
12
+ end
13
+
14
+ it "returns nil" do
15
+ @io.pos = 1
16
+ @io.ungetc(?A).should be_nil
17
+ end
18
+
19
+ it "decreases the current position by one" do
20
+ @io.pos = 2
21
+ @io.ungetc(?A)
22
+ @io.pos.should eql(1)
23
+ end
24
+
25
+ it "pads with \\000 when the current position is after the end" do
26
+ @io.pos = 15
27
+ @io.ungetc(?A)
28
+ @io.string.should == "1234\000\000\000\000\000\000\000\000\000\000A"
29
+ end
30
+
31
+ ruby_version_is "1.8.6" .. "1.8.6.367" do
32
+ it "does nothing when at the beginning of self" do
33
+ @io.ungetc(65)
34
+ @io.string.should == '1234'
35
+ end
36
+ end
37
+
38
+ ruby_version_is "" ... "1.9" do
39
+ it "tries to convert the passed length to an Integer using #to_int" do
40
+ obj = mock("to_int")
41
+ obj.should_receive(:to_int).and_return(?A)
42
+
43
+ @io.pos = 1
44
+ @io.ungetc(obj)
45
+ @io.string.should == "A234"
46
+ end
47
+
48
+ it "raises a TypeError when the passed length can't be converted to an Integer" do
49
+ lambda { @io.ungetc(Object.new) }.should raise_error(TypeError)
50
+ lambda { @io.ungetc("A") }.should raise_error(TypeError)
51
+ end
52
+ end
53
+
54
+ ruby_version_is "1.9" do
55
+ it "tries to convert the passed argument to an String using #to_str" do
56
+ obj = mock("to_str")
57
+ obj.should_receive(:to_str).and_return(?A)
58
+
59
+ @io.pos = 1
60
+ @io.ungetc(obj)
61
+ @io.string.should == "A234"
62
+ end
63
+
64
+ it "raises a TypeError when the passed length can't be converted to an Integer or String" do
65
+ lambda { @io.ungetc(Object.new) }.should raise_error(TypeError)
66
+ end
67
+ end
68
+ end
69
+
70
+ describe "StringIO#ungetc when self is not readable" do
71
+ it "raises an IOError" do
72
+ io = StringIO.new("test", "w")
73
+ io.pos = 1
74
+ lambda { io.ungetc(?A) }.should raise_error(IOError)
75
+
76
+ io = StringIO.new("test")
77
+ io.pos = 1
78
+ io.close_read
79
+ lambda { io.ungetc(?A) }.should raise_error(IOError)
80
+ end
81
+ end
82
+
83
+ # Note: This is incorrect.
84
+ #
85
+ # describe "StringIO#ungetc when self is not writable" do
86
+ # ruby_bug "#", "1.8.7.17" do
87
+ # it "raises an IOError" do
88
+ # io = StringIO.new("test", "r")
89
+ # io.pos = 1
90
+ # lambda { io.ungetc(?A) }.should raise_error(IOError)
91
+ #
92
+ # io = StringIO.new("test")
93
+ # io.pos = 1
94
+ # io.close_write
95
+ # lambda { io.ungetc(?A) }.should raise_error(IOError)
96
+ # end
97
+ # end
98
+ # end
@@ -0,0 +1,20 @@
1
+ require File.expand_path('../fixtures/classes', __FILE__)
2
+ require File.expand_path('../shared/write', __FILE__)
3
+
4
+ ruby_version_is "1.9" do
5
+ describe "StringIO#write_nonblock when passed [Object]" do
6
+ it_behaves_like :stringio_write, :write_nonblock
7
+ end
8
+
9
+ describe "StringIO#write_nonblock when passed [String]" do
10
+ it_behaves_like :stringio_write_string, :write_nonblock
11
+ end
12
+
13
+ describe "StringIO#write_nonblock when self is not writable" do
14
+ it_behaves_like :stringio_write_not_writable, :write_nonblock
15
+ end
16
+
17
+ describe "StringIO#write_nonblock when in append mode" do
18
+ it_behaves_like :stringio_write_append, :write_nonblock
19
+ end
20
+ end
@@ -0,0 +1,18 @@
1
+ require File.expand_path('../fixtures/classes', __FILE__)
2
+ require File.expand_path('../shared/write', __FILE__)
3
+
4
+ describe "StringIO#write when passed [Object]" do
5
+ it_behaves_like :stringio_write, :write
6
+ end
7
+
8
+ describe "StringIO#write when passed [String]" do
9
+ it_behaves_like :stringio_write_string, :write
10
+ end
11
+
12
+ describe "StringIO#write when self is not writable" do
13
+ it_behaves_like :stringio_write_not_writable, :write
14
+ end
15
+
16
+ describe "StringIO#write when in append mode" do
17
+ it_behaves_like :stringio_write_append, :write
18
+ end