devnull 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", ">= 2.3.0"
10
+ gem "bundler", ">= 1.0.0"
11
+ gem "jeweler", ">= 1.5.2"
12
+ gem "rcov", ">= 0"
13
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 H.Hiro (Maraigue)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,47 @@
1
+ = devnull
2
+
3
+ DevNull behaves a null file, and works like an IO object. For example:
4
+
5
+ dn = DevNull.new
6
+ dn.puts "foo" # => nil (do nothing)
7
+ dn.gets # => nil
8
+ dn.read # => ""
9
+
10
+ The library may be a good solution if you would like to switch whether an input/output file is needed. For example:
11
+
12
+ def some_process(arg, logfile = nil)
13
+ # You may set an IO object as 'logfile', and logs are written to the file.
14
+
15
+ result = process1(arg)
16
+ logfile.puts result if logfile
17
+
18
+ result = process2(arg)
19
+ logfile.puts result if logfile
20
+
21
+ result = process3(arg)
22
+ logfile.puts result if logfile
23
+ end
24
+
25
+ can be rewritten as follows:
26
+
27
+ def some_process(arg, logfile = DevNull.new)
28
+ logfile.puts process1(arg)
29
+ logfile.puts process2(arg)
30
+ logfile.puts process3(arg)
31
+ end
32
+
33
+ == Contributing to devnull
34
+
35
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
36
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
37
+ * Fork the project
38
+ * Start a feature/bugfix branch
39
+ * Commit and push until you are happy with your contribution
40
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
41
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
42
+
43
+ == Copyright
44
+
45
+ Copyright (c) 2011 maraigue. See LICENSE.txt for
46
+ further details.
47
+
@@ -0,0 +1,80 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "devnull"
16
+ gem.homepage = "http://github.com/maraigue/devnull"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Ruby implementation of null file (like /dev/null on Un*x, NUL on Windows)}
19
+ gem.description = <<DESC
20
+ DevNull behaves a null file, and works like an IO object. For example:
21
+
22
+ dn = DevNull.new
23
+ dn.puts "foo" # => nil (do nothing)
24
+ dn.gets # => nil
25
+ dn.read # => ""
26
+
27
+ The library may be a good solution if you would like to switch whether an input/output file is needed. For example:
28
+
29
+ def some_process(arg, logfile = nil)
30
+ # You may set an IO object as 'logfile', and logs are written to the file.
31
+
32
+ result = process1(arg)
33
+ logfile.puts result if logfile
34
+
35
+ result = process2(arg)
36
+ logfile.puts result if logfile
37
+
38
+ result = process3(arg)
39
+ logfile.puts result if logfile
40
+ end
41
+
42
+ can be rewritten as follows:
43
+
44
+ def some_process(arg, logfile = DevNull.new)
45
+ logfile.puts process1(arg)
46
+ logfile.puts process2(arg)
47
+ logfile.puts process3(arg)
48
+ end
49
+ DESC
50
+ gem.email = "main@hhiro.net"
51
+ gem.authors = ["H.Hiro (Maraigue)"]
52
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
53
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
54
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
55
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
56
+ end
57
+ Jeweler::RubygemsDotOrgTasks.new
58
+
59
+ require 'rspec/core'
60
+ require 'rspec/core/rake_task'
61
+ RSpec::Core::RakeTask.new(:spec) do |spec|
62
+ spec.pattern = FileList['spec/**/*_spec.rb']
63
+ end
64
+
65
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
66
+ spec.pattern = 'spec/**/*_spec.rb'
67
+ spec.rcov = true
68
+ end
69
+
70
+ task :default => :spec
71
+
72
+ require 'rake/rdoctask'
73
+ Rake::RDocTask.new do |rdoc|
74
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
75
+
76
+ rdoc.rdoc_dir = 'rdoc'
77
+ rdoc.title = "devnull #{version}"
78
+ rdoc.rdoc_files.include('README*')
79
+ rdoc.rdoc_files.include('lib/**/*.rb')
80
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,92 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{devnull}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["H.Hiro (Maraigue)"]
12
+ s.date = %q{2011-03-28}
13
+ s.description = %q{DevNull behaves a null file, and works like an IO object. For example:
14
+
15
+ dn = DevNull.new
16
+ dn.puts "foo" # => nil (do nothing)
17
+ dn.gets # => nil
18
+ dn.read # => ""
19
+
20
+ The library may be a good solution if you would like to switch whether an input/output file is needed. For example:
21
+
22
+ def some_process(arg, logfile = nil)
23
+ # You may set an IO object as 'logfile', and logs are written to the file.
24
+
25
+ result = process1(arg)
26
+ logfile.puts result if logfile
27
+
28
+ result = process2(arg)
29
+ logfile.puts result if logfile
30
+
31
+ result = process3(arg)
32
+ logfile.puts result if logfile
33
+ end
34
+
35
+ can be rewritten as follows:
36
+
37
+ def some_process(arg, logfile = DevNull.new)
38
+ logfile.puts process1(arg)
39
+ logfile.puts process2(arg)
40
+ logfile.puts process3(arg)
41
+ end
42
+ }
43
+ s.email = %q{main@hhiro.net}
44
+ s.extra_rdoc_files = [
45
+ "LICENSE.txt",
46
+ "README.rdoc"
47
+ ]
48
+ s.files = [
49
+ ".document",
50
+ ".rspec",
51
+ "Gemfile",
52
+ "LICENSE.txt",
53
+ "README.rdoc",
54
+ "Rakefile",
55
+ "VERSION",
56
+ "devnull.gemspec",
57
+ "lib/devnull.rb",
58
+ "spec/devnull_spec.rb",
59
+ "spec/spec_helper.rb"
60
+ ]
61
+ s.homepage = %q{http://github.com/maraigue/devnull}
62
+ s.licenses = ["MIT"]
63
+ s.require_paths = ["lib"]
64
+ s.rubygems_version = %q{1.6.2}
65
+ s.summary = %q{Ruby implementation of null file (like /dev/null on Un*x, NUL on Windows)}
66
+ s.test_files = [
67
+ "spec/devnull_spec.rb",
68
+ "spec/spec_helper.rb"
69
+ ]
70
+
71
+ if s.respond_to? :specification_version then
72
+ s.specification_version = 3
73
+
74
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
75
+ s.add_development_dependency(%q<rspec>, [">= 2.3.0"])
76
+ s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
77
+ s.add_development_dependency(%q<jeweler>, [">= 1.5.2"])
78
+ s.add_development_dependency(%q<rcov>, [">= 0"])
79
+ else
80
+ s.add_dependency(%q<rspec>, [">= 2.3.0"])
81
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
82
+ s.add_dependency(%q<jeweler>, [">= 1.5.2"])
83
+ s.add_dependency(%q<rcov>, [">= 0"])
84
+ end
85
+ else
86
+ s.add_dependency(%q<rspec>, [">= 2.3.0"])
87
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
88
+ s.add_dependency(%q<jeweler>, [">= 1.5.2"])
89
+ s.add_dependency(%q<rcov>, [">= 0"])
90
+ end
91
+ end
92
+
@@ -0,0 +1,141 @@
1
+ # Ruby implementation of null file (like /dev/null on Un*x, NUL on Windows)
2
+ # (C) 2010- H.Hiro(Maraigue) main@hhiro.net
3
+ #
4
+ # DevNull behaves a null file, and works like an IO object. For example:
5
+ #
6
+ # <pre>dn = DevNull.new
7
+ # dn.puts "foo" # => nil (do nothing)
8
+ # dn.gets # => nil
9
+ # dn.read # => ""</pre>
10
+ #
11
+ # The library may be a good solution if you would like to switch whether an input/output file is needed. For example:
12
+ #
13
+ # <pre>def some_process(arg, logfile = nil)
14
+ # # You may set an IO object as 'logfile', and logs are written to the file.
15
+ #
16
+ # result = process1(arg)
17
+ # logfile.puts result if logfile
18
+ #
19
+ # result = process2(arg)
20
+ # logfile.puts result if logfile
21
+ #
22
+ # result = process3(arg)
23
+ # logfile.puts result if logfile
24
+ # end</pre>
25
+ #
26
+ # can be rewritten as follows:
27
+ #
28
+ # <pre>def some_process(arg, logfile = DevNull.new)
29
+ # logfile.puts process1(arg)
30
+ # logfile.puts process2(arg)
31
+ # logfile.puts process3(arg)
32
+ # end</pre>
33
+
34
+ require "enumerator"
35
+
36
+ class DevNull
37
+ def initialize
38
+ # do nothing
39
+ end
40
+
41
+ # --
42
+ # methods treated as being not implemented
43
+ # ++
44
+ def fileno; raise NotImplementedError; end
45
+ alias :to_i :fileno
46
+ def fsync; raise NotImplementedError; end
47
+ def fdatasync; raise NotImplementedError; end
48
+ def stat; raise NotImplementedError; end
49
+ def fcntl(arg1, arg2); raise NotImplementedError; end
50
+ def ioctl(arg1, arg2); raise NotImplementedError; end
51
+
52
+ # --
53
+ # methods that do nothing
54
+ # ++
55
+ def close; end
56
+ def close_read; end
57
+ def close_write; end
58
+ def print(*args); end
59
+ def printf(arg1, *other_args); end
60
+ def puts(*args); end
61
+ def syswrite(*args); end
62
+ def ungetbyte(arg); end
63
+ def ungetc(arg); end
64
+
65
+ # --
66
+ # methods that do nothing and returns something
67
+ # ++
68
+ def getc; nil; end
69
+ def getbyte; nil; end
70
+ def gets(arg1=nil, arg2=nil); nil; end
71
+ def path; nil; end
72
+ def pid; nil; end
73
+ def external_encoding; nil; end
74
+ def internal_encoding; nil; end
75
+
76
+ def to_io; self; end
77
+ def <<(obj); self; end
78
+ def binmode; self; end
79
+ def flush; self; end
80
+ def reopen(arg1, arg2=nil); self; end
81
+ def set_encoding(arg1, arg2=nil, arg3=nil); self; end
82
+
83
+ def autoclose?; true; end
84
+ def binmode?; true; end
85
+ def closed?; true; end
86
+ def eof; true; end
87
+ def eof?; true; end
88
+ def sync; true; end
89
+
90
+ def close_on_exec?; false; end
91
+ def closed_read?; false; end
92
+ def closed_write?; false; end
93
+ def isatty; false; end
94
+ def tty?; false; end
95
+
96
+ def lineno; 0; end
97
+ def pos; 0; end
98
+ def tell; 0; end
99
+ def rewind; 0; end
100
+ def seek(arg1, arg2=nil); 0; end
101
+ def sysseek(arg1, arg2=nil); 0; end
102
+
103
+ def readlines(arg1=nil, arg2=nil); []; end
104
+
105
+ def putc(arg); arg; end
106
+ def autoclose=(arg); arg; end
107
+ def close_on_exec=(arg); arg; end
108
+ def lineno=(arg); arg; end
109
+ def pos=(arg); arg; end
110
+ def sync=(arg); arg; end
111
+ def truncate(arg); arg; end
112
+ def write(arg); arg.to_s.length; end
113
+ def syswrite(arg); arg.to_s.length; end
114
+ def write_nonblock(arg); arg.to_s.length; end
115
+
116
+ def each(*args); (block_given? ? self : [].to_enum); end
117
+ alias :each_line :each
118
+ alias :lines :each
119
+ alias :each_byte :each
120
+ alias :bytes :each
121
+ alias :each_char :each
122
+ alias :chars :each
123
+ alias :each_codepoint :each
124
+ alias :codepoints :each
125
+
126
+ def sysread(arg1, arg2=nil); raise EOFError; end
127
+ def readpartial(arg1, arg2=nil); raise EOFError; end
128
+ def read_nonblock(arg1, arg2=nil); raise EOFError; end
129
+ def readchar; raise EOFError; end
130
+ def readbyte; raise EOFError; end
131
+ def readline(arg1=nil, arg2=nil); raise EOFError; end
132
+
133
+ def read(len = 0, outbuf = nil)
134
+ if outbuf != nil
135
+ outbuf.replace("")
136
+ outbuf
137
+ else
138
+ (len.to_i == 0 ? "" : nil)
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,287 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ require "devnull"
4
+
5
+ describe DevNull do
6
+ # GENERAL NOTE:
7
+ #
8
+ # These test codes are based on the document:
9
+ # http://www.ruby-doc.org/core/classes/IO.html .
10
+ # However, the contents in the URL may be changed.
11
+ # If needed, add and/or rewrite the test code.
12
+
13
+ # NOTE 1:
14
+ #
15
+ # This devnull library is not to support any internal statuses
16
+ # stored in IO instances (e.g. instance variables).
17
+ # For example,
18
+ #
19
+ # f = open("/dev/null")
20
+ # f.lineno # => 0
21
+ # f.lineno = 10
22
+ # f.lineno # => 10
23
+ #
24
+ # but
25
+ #
26
+ # g = DevNull.new
27
+ # g.lineno # => 0
28
+ # g.lineno = 10
29
+ # g.lineno # => 0
30
+ #
31
+ # Such internal-status-related methods is defined,
32
+ # but should always return statuses according to
33
+ # its *initial* statuses.
34
+
35
+ # NOTE 2:
36
+ #
37
+ # Some of the instance methods for IO related to low-level I/O is
38
+ # not implemented. In such case, the method returns NotImplementedError.
39
+ # (e.g. DevNull#fileno)
40
+
41
+ before(:each) do
42
+ @null = DevNull.new
43
+ end
44
+
45
+ # ---------- specs for Enumerators:
46
+ # Those methods should return an Enumerator that contains nothing.
47
+ %w[
48
+ each
49
+ bytes each_byte
50
+ chars each_char
51
+ codepoints each_codepoint
52
+ ].each do |method_name|
53
+ it "should return an Enumerator for Devnull##{method_name}" do
54
+ # First, check whether the method returns an Enumerator.
55
+ enum = eval("@null.#{method_name}")
56
+ enum.should be_instance_of(Enumerable::Enumerator)
57
+ # Then, check whether the Enumerator is empty.
58
+ lambda{ enum.each{ raise RuntimeError } }.should_not raise_error
59
+ lambda{ eval("@null.#{method_name} { raise RuntimeError }") }.should_not raise_error
60
+ end
61
+ end
62
+
63
+ # ---------- specs for called without any argument and returning self
64
+ %w[
65
+ flush to_io
66
+ ].each do |method_name|
67
+ it "should return self for Devnull##{method_name}" do
68
+ eval("@null.#{method_name}").should == @null
69
+ end
70
+ end
71
+
72
+ # ---------- specs for called without any argument and returning nil
73
+ %w[
74
+ close close_read close_write external_encoding internal_encoding
75
+ getbyte getc pid
76
+ ].each do |method_name|
77
+ it "should return nil for Devnull##{method_name}" do
78
+ eval("@null.#{method_name}").should == nil
79
+ end
80
+ end
81
+
82
+ # ---------- specs for called with 1 argument and returning nil
83
+ %w[
84
+ ungetbyte ungetc
85
+ ].each do |method_name|
86
+ it "should return nil for Devnull##{method_name} with 1 arguments" do
87
+ lambda{ eval("@null.#{method_name}") }.should raise_error(ArgumentError)
88
+ eval("@null.#{method_name}(1)").should == nil
89
+ lambda{ eval("@null.#{method_name}(1, 2)") }.should raise_error(ArgumentError)
90
+ end
91
+ end
92
+
93
+ # ---------- specs for called with any number of arguments and returning nil
94
+ %w[
95
+ print puts
96
+ ].each do |method_name|
97
+ it "should return nil for Devnull##{method_name} with any number of arguments" do
98
+ eval("@null.#{method_name}").should == nil
99
+ eval("@null.#{method_name}(1)").should == nil
100
+ eval("@null.#{method_name}(1, 2, 3)").should == nil
101
+ end
102
+ end
103
+
104
+ # ---------- specs for called without any argument and returning true
105
+ %w[
106
+ autoclose? binmode? closed? eof eof? sync
107
+ ].each do |method_name|
108
+ it "should return true for Devnull##{method_name}" do
109
+ eval("@null.#{method_name}").should be_true
110
+ end
111
+ end
112
+
113
+ # ---------- specs for called without any argument and returning false
114
+ %w[
115
+ close_on_exec? isatty tty?
116
+ ].each do |method_name|
117
+ it "should return false for Devnull##{method_name}" do
118
+ eval("@null.#{method_name}").should be_false
119
+ end
120
+ end
121
+
122
+ # ---------- specs for called without any argument and returning 0
123
+ %w[
124
+ lineno pos tell rewind
125
+ ].each do |method_name|
126
+ it "should return 0 for Devnull##{method_name}" do
127
+ eval("@null.#{method_name}").should == 0
128
+ end
129
+ end
130
+
131
+ # ---------- specs for called with 1 or 2 arguments and returning 0
132
+ %w[
133
+ seek sysseek
134
+ ].each do |method_name|
135
+ it "should return 0 for Devnull##{method_name}" do
136
+ lambda{ eval("@null.#{method_name}") }.should raise_error(ArgumentError)
137
+ eval("@null.#{method_name}(1)").should == 0
138
+ eval("@null.#{method_name}(1, 2)").should == 0
139
+ lambda{ eval("@null.#{method_name}(1, 2, 3)") }.should raise_error(ArgumentError)
140
+ end
141
+ end
142
+
143
+ # ---------- specs for called with a string argument and returning its string length
144
+ %w[
145
+ syswrite write write_nonblock
146
+ ].each do |method_name|
147
+ it "should return the argument string's length for Devnull##{method_name}(string)" do
148
+ eval("@null.#{method_name}('hoge')").should == 4
149
+ eval("@null.#{method_name}('abcdefg')").should == 7
150
+ end
151
+ end
152
+
153
+ # ---------- specs for assignment-type methods that do nothing
154
+ %w[
155
+ autoclose= close_on_exec= lineno= pos= sync=
156
+ ].each do |method_name|
157
+ it "should do nothing when called Devnull##{method_name}" do
158
+ lambda{ eval("@null.#{method_name}(1)") }.should_not raise_error
159
+ end
160
+ end
161
+
162
+ # ---------- specs for methods called with no argument and always failing with EOFError
163
+ %w[
164
+ readbyte readchar
165
+ ].each do |method_name|
166
+ it "should raise EOFError when called Devnull##{method_name}" do
167
+ lambda{ eval("@null.#{method_name}") }.should raise_error(EOFError)
168
+ lambda{ eval("@null.#{method_name}(1)") }.should raise_error(ArgumentError)
169
+ end
170
+ end
171
+
172
+ # ---------- specs for methods called with 1 or 2 arguments and always failing with EOFError
173
+ %w[
174
+ read_nonblock readpartial sysread
175
+ ].each do |method_name|
176
+ it "should raise EOFError when called Devnull##{method_name}" do
177
+ lambda{ eval("@null.#{method_name}") }.should raise_error(ArgumentError)
178
+ lambda{ eval("@null.#{method_name}(1)") }.should raise_error(EOFError)
179
+ lambda{ eval("@null.#{method_name}(1, 2)") }.should raise_error(EOFError)
180
+ lambda{ eval("@null.#{method_name}(1, 2, 3)") }.should raise_error(ArgumentError)
181
+ end
182
+ end
183
+
184
+ # ---------- specs for not implemented methods
185
+ it "should raise NotImplementedError for Devnull#fcntl when called with 2 arguments" do
186
+ lambda{ @null.fcntl(0) }.should raise_error(ArgumentError)
187
+ lambda{ @null.fcntl(0, 0) }.should raise_error(NotImplementedError)
188
+ lambda{ @null.fcntl(0, 0, 0) }.should raise_error(ArgumentError)
189
+ end
190
+
191
+ it "should raise NotImplementedError for Devnull\#\{fsync,fdatasync\}" do
192
+ lambda{ @null.fsync }.should raise_error(NotImplementedError)
193
+ lambda{ @null.fdatasync }.should raise_error(NotImplementedError)
194
+ end
195
+
196
+ it "should raise NotImplementedError for Devnull\#\{fileno,to_i\}" do
197
+ lambda{ @null.fileno }.should raise_error(NotImplementedError)
198
+ lambda{ @null.to_i }.should raise_error(NotImplementedError)
199
+ end
200
+
201
+ it "should raise NotImplementedError for Devnull#ioctl when called with 2 arguments" do
202
+ lambda{ @null.ioctl(0) }.should raise_error(ArgumentError)
203
+ lambda{ @null.ioctl(0, 0) }.should raise_error(NotImplementedError)
204
+ lambda{ @null.ioctl(0, 0, 0) }.should raise_error(ArgumentError)
205
+ end
206
+
207
+ it "should raise NotImplementedError for Devnull#stat" do
208
+ lambda{ @null.stat }.should raise_error(NotImplementedError)
209
+ end
210
+
211
+ # ---------- others
212
+ it "should return self for Devnull#<<" do
213
+ (@null << "test").should == @null
214
+ end
215
+
216
+ it "should do nothing for Devnull#binmode" do
217
+ lambda{ @null.binmode }.should_not raise_error
218
+ end
219
+
220
+ it "should return nil for Devnull#printf when called with 1 or more arguments" do
221
+ lambda{ @null.printf }.should raise_error(ArgumentError)
222
+ @null.printf(0).should be_nil
223
+ @null.printf(0, 0).should be_nil
224
+ @null.printf(0, 0, 0, 0).should be_nil
225
+ end
226
+
227
+ it "should return the argument for Devnull#putc when called with 1 arguments" do
228
+ lambda{ @null.putc }.should raise_error(ArgumentError)
229
+ @null.putc(0).should == 0
230
+ @null.putc("hoge").should == "hoge"
231
+ lambda{ @null.putc(0, 0) }.should raise_error(ArgumentError)
232
+ end
233
+
234
+ it "should return an empty string for Devnull#read() or Devnull#read(0)" do
235
+ @null.read.should == ""
236
+ @null.read(0).should == ""
237
+
238
+ buffer = "hoge"
239
+ @null.read(0, buffer).should be_equal(buffer)
240
+ buffer.should == ""
241
+ end
242
+
243
+ it "should return nil for Devnull#read(positive_integer)" do
244
+ @null.read(1).should be_nil
245
+ @null.read(10).should be_nil
246
+
247
+ buffer = "hoge"
248
+ @null.read(10, buffer).should be_equal(buffer)
249
+ buffer.should == ""
250
+ end
251
+
252
+ it "should return nil for Devnull#gets(0 to 2 arguments)" do
253
+ @null.gets().should == nil
254
+ @null.gets(0).should == nil
255
+ @null.gets(0, 0).should == nil
256
+ lambda{ @null.gets(0, 0, 0) }.should raise_error(ArgumentError)
257
+ end
258
+
259
+ it "should raise EOFError when called Devnull#readline" do
260
+ lambda{ @null.readline }.should raise_error(EOFError)
261
+ lambda{ @null.readline(0) }.should raise_error(EOFError)
262
+ lambda{ @null.readline(0, 0) }.should raise_error(EOFError)
263
+ lambda{ @null.readline(0, 0, 0) }.should raise_error(ArgumentError)
264
+ end
265
+
266
+ it "should return empty array for Devnull#readlines(0 to 2 arguments)" do
267
+ @null.readlines().should == []
268
+ @null.readlines(0).should == []
269
+ @null.readlines(0, 0).should == []
270
+ lambda{ @null.readlines(0, 0, 0) }.should raise_error(ArgumentError)
271
+ end
272
+
273
+ it "should return self for Devnull#reopen(1 or 2 arguments)" do
274
+ lambda{ @null.reopen() }.should raise_error(ArgumentError)
275
+ @null.reopen(0).should == @null
276
+ @null.reopen(0, 0).should == @null
277
+ lambda{ @null.reopen(0, 0, 0) }.should raise_error(ArgumentError)
278
+ end
279
+
280
+ it "should return self for Devnull#set_encoding(1 to 3 arguments)" do
281
+ lambda{ @null.set_encoding() }.should raise_error(ArgumentError)
282
+ @null.set_encoding(0).should == @null
283
+ @null.set_encoding(0, 0).should == @null
284
+ @null.set_encoding(0, 0, 0).should == @null
285
+ lambda{ @null.set_encoding(0, 0, 0, 0) }.should raise_error(ArgumentError)
286
+ end
287
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'devnull'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: devnull
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - H.Hiro (Maraigue)
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-28 00:00:00 +09:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ version_requirements: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 2
31
+ - 3
32
+ - 0
33
+ version: 2.3.0
34
+ prerelease: false
35
+ type: :development
36
+ requirement: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: bundler
39
+ version_requirements: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 23
45
+ segments:
46
+ - 1
47
+ - 0
48
+ - 0
49
+ version: 1.0.0
50
+ prerelease: false
51
+ type: :development
52
+ requirement: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: jeweler
55
+ version_requirements: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 7
61
+ segments:
62
+ - 1
63
+ - 5
64
+ - 2
65
+ version: 1.5.2
66
+ prerelease: false
67
+ type: :development
68
+ requirement: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: rcov
71
+ version_requirements: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ prerelease: false
81
+ type: :development
82
+ requirement: *id004
83
+ description: |
84
+ DevNull behaves a null file, and works like an IO object. For example:
85
+
86
+ dn = DevNull.new
87
+ dn.puts "foo" # => nil (do nothing)
88
+ dn.gets # => nil
89
+ dn.read # => ""
90
+
91
+ The library may be a good solution if you would like to switch whether an input/output file is needed. For example:
92
+
93
+ def some_process(arg, logfile = nil)
94
+ # You may set an IO object as 'logfile', and logs are written to the file.
95
+
96
+ result = process1(arg)
97
+ logfile.puts result if logfile
98
+
99
+ result = process2(arg)
100
+ logfile.puts result if logfile
101
+
102
+ result = process3(arg)
103
+ logfile.puts result if logfile
104
+ end
105
+
106
+ can be rewritten as follows:
107
+
108
+ def some_process(arg, logfile = DevNull.new)
109
+ logfile.puts process1(arg)
110
+ logfile.puts process2(arg)
111
+ logfile.puts process3(arg)
112
+ end
113
+
114
+ email: main@hhiro.net
115
+ executables: []
116
+
117
+ extensions: []
118
+
119
+ extra_rdoc_files:
120
+ - LICENSE.txt
121
+ - README.rdoc
122
+ files:
123
+ - .document
124
+ - .rspec
125
+ - Gemfile
126
+ - LICENSE.txt
127
+ - README.rdoc
128
+ - Rakefile
129
+ - VERSION
130
+ - devnull.gemspec
131
+ - lib/devnull.rb
132
+ - spec/devnull_spec.rb
133
+ - spec/spec_helper.rb
134
+ has_rdoc: true
135
+ homepage: http://github.com/maraigue/devnull
136
+ licenses:
137
+ - MIT
138
+ post_install_message:
139
+ rdoc_options: []
140
+
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ none: false
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ hash: 3
149
+ segments:
150
+ - 0
151
+ version: "0"
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ hash: 3
158
+ segments:
159
+ - 0
160
+ version: "0"
161
+ requirements: []
162
+
163
+ rubyforge_project:
164
+ rubygems_version: 1.6.2
165
+ signing_key:
166
+ specification_version: 3
167
+ summary: Ruby implementation of null file (like /dev/null on Un*x, NUL on Windows)
168
+ test_files:
169
+ - spec/devnull_spec.rb
170
+ - spec/spec_helper.rb