naught 1.1.0 → 2.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.
- checksums.yaml +5 -5
- data/LICENSE.txt +1 -1
- data/lib/naught/basic_object.rb +4 -14
- data/lib/naught/call_location.rb +131 -0
- data/lib/naught/caller_info.rb +128 -0
- data/lib/naught/chain_proxy.rb +51 -0
- data/lib/naught/conversions.rb +108 -34
- data/lib/naught/null_class_builder/command.rb +42 -4
- data/lib/naught/null_class_builder/commands/callstack.rb +89 -0
- data/lib/naught/null_class_builder/commands/define_explicit_conversions.rb +21 -9
- data/lib/naught/null_class_builder/commands/define_implicit_conversions.rb +19 -21
- data/lib/naught/null_class_builder/commands/impersonate.rb +13 -1
- data/lib/naught/null_class_builder/commands/mimic.rb +75 -31
- data/lib/naught/null_class_builder/commands/null_safe_proxy.rb +92 -0
- data/lib/naught/null_class_builder/commands/pebble.rb +21 -17
- data/lib/naught/null_class_builder/commands/predicates_return.rb +42 -24
- data/lib/naught/null_class_builder/commands/singleton.rb +14 -17
- data/lib/naught/null_class_builder/commands/traceable.rb +16 -11
- data/lib/naught/null_class_builder/commands.rb +10 -8
- data/lib/naught/null_class_builder.rb +213 -119
- data/lib/naught/stub_strategy.rb +30 -0
- data/lib/naught/version.rb +3 -1
- data/lib/naught.rb +31 -7
- metadata +34 -66
- data/.gitignore +0 -23
- data/.rspec +0 -2
- data/.rubocop.yml +0 -65
- data/.travis.yml +0 -24
- data/Changelog.md +0 -18
- data/Gemfile +0 -25
- data/Guardfile +0 -15
- data/README.markdown +0 -474
- data/Rakefile +0 -15
- data/naught.gemspec +0 -22
- data/spec/base_object_spec.rb +0 -46
- data/spec/basic_null_object_spec.rb +0 -34
- data/spec/blackhole_spec.rb +0 -14
- data/spec/explicit_conversions_spec.rb +0 -21
- data/spec/functions/actual_spec.rb +0 -22
- data/spec/functions/just_spec.rb +0 -22
- data/spec/functions/maybe_spec.rb +0 -35
- data/spec/functions/null_spec.rb +0 -33
- data/spec/implicit_conversions_spec.rb +0 -28
- data/spec/mimic_spec.rb +0 -123
- data/spec/naught/null_object_builder/command_spec.rb +0 -10
- data/spec/naught/null_object_builder_spec.rb +0 -31
- data/spec/naught_spec.rb +0 -93
- data/spec/pebble_spec.rb +0 -77
- data/spec/predicate_spec.rb +0 -79
- data/spec/singleton_null_object_spec.rb +0 -33
- data/spec/spec_helper.rb +0 -15
- data/spec/support/convertable_null.rb +0 -4
- data/spec/support/jruby.rb +0 -3
- data/spec/support/rubinius.rb +0 -3
- data/spec/support/ruby_18.rb +0 -3
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe 'basic null object' do
|
|
4
|
-
let(:null_class) { Naught.build }
|
|
5
|
-
subject(:null) { null_class.new }
|
|
6
|
-
|
|
7
|
-
it 'responds to arbitrary messages and returns nil' do
|
|
8
|
-
expect(null.info).to be_nil
|
|
9
|
-
expect(null.foobaz).to be_nil
|
|
10
|
-
expect(null.to_s).to be_nil
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
it 'accepts any arguments for any messages' do
|
|
14
|
-
null.foobaz(1, 2, 3)
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
it 'reports that it responds to any message' do
|
|
18
|
-
expect(null).to respond_to(:info)
|
|
19
|
-
expect(null).to respond_to(:foobaz)
|
|
20
|
-
expect(null).to respond_to(:to_s)
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
it 'can be inspected' do
|
|
24
|
-
expect(null.inspect).to eq('<null>')
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
it 'knows its own class' do
|
|
28
|
-
expect(null.class).to eq(null_class)
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
it 'aliases .new to .get' do
|
|
32
|
-
expect(null_class.get.class).to be(null_class)
|
|
33
|
-
end
|
|
34
|
-
end
|
data/spec/blackhole_spec.rb
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe 'black hole null object' do
|
|
4
|
-
subject(:null) { null_class.new }
|
|
5
|
-
let(:null_class) do
|
|
6
|
-
Naught.build(&:black_hole)
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
it 'returns self from arbitray method calls' do
|
|
10
|
-
expect(null.info).to be(null)
|
|
11
|
-
expect(null.foobaz).to be(null)
|
|
12
|
-
expect(null << 'bar').to be(null)
|
|
13
|
-
end
|
|
14
|
-
end
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
require 'spec_helper.rb'
|
|
2
|
-
|
|
3
|
-
describe 'explicitly convertable null object' do
|
|
4
|
-
let(:null_class) do
|
|
5
|
-
Naught.build(&:define_explicit_conversions)
|
|
6
|
-
end
|
|
7
|
-
subject(:null) { null_class.new }
|
|
8
|
-
|
|
9
|
-
it 'defines common explicit conversions to return zero values' do
|
|
10
|
-
expect(null.to_s).to eq('')
|
|
11
|
-
expect(null.to_a).to eq([])
|
|
12
|
-
expect(null.to_i).to eq(0)
|
|
13
|
-
expect(null.to_f).to eq(0.0)
|
|
14
|
-
if RUBY_VERSION >= '2.0'
|
|
15
|
-
expect(null.to_h).to eq({})
|
|
16
|
-
elsif RUBY_VERSION >= '1.9'
|
|
17
|
-
expect(null.to_c).to eq(Complex(0))
|
|
18
|
-
expect(null.to_r).to eq(Rational(0))
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
end
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe 'Actual()' do
|
|
4
|
-
include ConvertableNull::Conversions
|
|
5
|
-
|
|
6
|
-
specify 'given a null object, returns nil' do
|
|
7
|
-
null = ConvertableNull.get
|
|
8
|
-
expect(Actual(null)).to be_nil
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
specify 'given anything else, returns the input unchanged' do
|
|
12
|
-
expect(Actual(false)).to be(false)
|
|
13
|
-
str = 'hello'
|
|
14
|
-
expect(Actual(str)).to be(str)
|
|
15
|
-
expect(Actual(nil)).to be_nil
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
it 'also works with blocks' do
|
|
19
|
-
expect(Actual { ConvertableNull.new }).to be_nil
|
|
20
|
-
expect(Actual { 'foo' }).to eq('foo')
|
|
21
|
-
end
|
|
22
|
-
end
|
data/spec/functions/just_spec.rb
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe 'Just()' do
|
|
4
|
-
include ConvertableNull::Conversions
|
|
5
|
-
|
|
6
|
-
specify 'passes non-nullish values through' do
|
|
7
|
-
expect(Just(false)).to be(false)
|
|
8
|
-
str = 'hello'
|
|
9
|
-
expect(Just(str)).to be(str)
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
specify 'rejects nullish values' do
|
|
13
|
-
expect { Just(nil) }.to raise_error(ArgumentError)
|
|
14
|
-
expect { Just('') }.to raise_error(ArgumentError)
|
|
15
|
-
expect { Just(ConvertableNull.get) }.to raise_error(ArgumentError)
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
it 'also works with blocks' do
|
|
19
|
-
expect { Just { nil }.class }.to raise_error(ArgumentError)
|
|
20
|
-
expect(Just { 'foo' }).to eq('foo')
|
|
21
|
-
end
|
|
22
|
-
end
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe 'Maybe()' do
|
|
4
|
-
include ConvertableNull::Conversions
|
|
5
|
-
|
|
6
|
-
specify 'given nil, returns a null object' do
|
|
7
|
-
expect(Maybe(nil).class).to be(ConvertableNull)
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
specify 'given a null object, returns the same null object' do
|
|
11
|
-
null = ConvertableNull.get
|
|
12
|
-
expect(Maybe(null)).to be(null)
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
specify 'given anything in null_equivalents, returns a null object' do
|
|
16
|
-
expect(Maybe('').class).to be(ConvertableNull)
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
specify 'given anything else, returns the input unchanged' do
|
|
20
|
-
expect(Maybe(false)).to be(false)
|
|
21
|
-
str = 'hello'
|
|
22
|
-
expect(Maybe(str)).to be(str)
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
it 'generates null objects with useful trace info' do
|
|
26
|
-
null, line = Maybe(), __LINE__ # rubocop:disable ParallelAssignment
|
|
27
|
-
expect(null.__file__).to eq(__FILE__)
|
|
28
|
-
expect(null.__line__).to eq(line)
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
it 'also works with blocks' do
|
|
32
|
-
expect(Maybe { nil }.class).to eq(ConvertableNull)
|
|
33
|
-
expect(Maybe { 'foo' }).to eq('foo')
|
|
34
|
-
end
|
|
35
|
-
end
|
data/spec/functions/null_spec.rb
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe 'Null()' do
|
|
4
|
-
include ConvertableNull::Conversions
|
|
5
|
-
|
|
6
|
-
specify 'given no input, returns a null object' do
|
|
7
|
-
expect(Null().class).to be(ConvertableNull)
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
specify 'given nil, returns a null object' do
|
|
11
|
-
expect(Null(nil).class).to be(ConvertableNull)
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
specify 'given a null object, returns the same null object' do
|
|
15
|
-
null = ConvertableNull.get
|
|
16
|
-
expect(Null(null)).to be(null)
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
specify 'given anything in null_equivalents, returns a null object' do
|
|
20
|
-
expect(Null('').class).to be(ConvertableNull)
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
specify 'given anything else, raises an ArgumentError' do
|
|
24
|
-
expect { Null(false) }.to raise_error(ArgumentError)
|
|
25
|
-
expect { Null('hello') }.to raise_error(ArgumentError)
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
it 'generates null objects with useful trace info' do
|
|
29
|
-
null, line = Null(), __LINE__ # rubocop:disable ParallelAssignment
|
|
30
|
-
expect(null.__file__).to eq(__FILE__)
|
|
31
|
-
expect(null.__line__).to eq(line)
|
|
32
|
-
end
|
|
33
|
-
end
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe 'implicitly convertable null object' do
|
|
4
|
-
subject(:null) { null_class.new }
|
|
5
|
-
let(:null_class) do
|
|
6
|
-
Naught.build(&:define_implicit_conversions)
|
|
7
|
-
end
|
|
8
|
-
it 'implicitly splats the same way an empty array does' do
|
|
9
|
-
a, b = null
|
|
10
|
-
expect(a).to be_nil
|
|
11
|
-
expect(b).to be_nil
|
|
12
|
-
end
|
|
13
|
-
it 'is implicitly convertable to String' do
|
|
14
|
-
expect(instance_eval(null)).to be_nil
|
|
15
|
-
end
|
|
16
|
-
it 'implicitly converts to an empty array' do
|
|
17
|
-
expect(null.to_ary).to eq([])
|
|
18
|
-
end
|
|
19
|
-
it 'implicitly converts to an empty hash' do
|
|
20
|
-
expect(null.to_hash).to eq({})
|
|
21
|
-
end
|
|
22
|
-
it 'implicitly converts to zero' do
|
|
23
|
-
expect(null.to_int).to eq(0)
|
|
24
|
-
end
|
|
25
|
-
it 'implicitly converts to an empty string' do
|
|
26
|
-
expect(null.to_str).to eq('')
|
|
27
|
-
end
|
|
28
|
-
end
|
data/spec/mimic_spec.rb
DELETED
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
require 'logger'
|
|
3
|
-
|
|
4
|
-
describe 'null object mimicking a class' do
|
|
5
|
-
class User
|
|
6
|
-
attr_reader :login
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
module Authorizable
|
|
10
|
-
def authorized_for?(_); end
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
class LibraryPatron < User
|
|
14
|
-
include Authorizable
|
|
15
|
-
attr_reader :name
|
|
16
|
-
|
|
17
|
-
def member?; end
|
|
18
|
-
|
|
19
|
-
def notify_of_overdue_books(_); end
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
subject(:null) { mimic_class.new }
|
|
23
|
-
let(:mimic_class) do
|
|
24
|
-
Naught.build do |b|
|
|
25
|
-
b.mimic LibraryPatron
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
it 'responds to all methods defined on the target class' do
|
|
29
|
-
expect(null.member?).to be_nil
|
|
30
|
-
expect(null.name).to be_nil
|
|
31
|
-
expect(null.notify_of_overdue_books(['The Grapes of Wrath'])).to be_nil
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
it 'does not respond to methods not defined on the target class' do
|
|
35
|
-
expect { null.foobar }.to raise_error(NoMethodError)
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
it 'reports which messages it does and does not respond to' do
|
|
39
|
-
expect(null).to respond_to(:member?)
|
|
40
|
-
expect(null).to respond_to(:name)
|
|
41
|
-
expect(null).to respond_to(:notify_of_overdue_books)
|
|
42
|
-
expect(null).not_to respond_to(:foobar)
|
|
43
|
-
end
|
|
44
|
-
it 'has an informative inspect string' do
|
|
45
|
-
expect(null.inspect).to eq('<null:LibraryPatron>')
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
it 'excludes Object methods from being mimicked' do
|
|
49
|
-
expect(null.object_id).not_to be_nil
|
|
50
|
-
expect(null.hash).not_to be_nil
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
it 'includes inherited methods' do
|
|
54
|
-
expect(null.authorized_for?('something')).to be_nil
|
|
55
|
-
expect(null.login).to be_nil
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
describe 'with include_super: false' do
|
|
59
|
-
let(:mimic_class) do
|
|
60
|
-
Naught.build do |b|
|
|
61
|
-
b.mimic LibraryPatron, :include_super => false
|
|
62
|
-
end
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
it 'excludes inherited methods' do
|
|
66
|
-
expect(null).to_not respond_to(:authorized_for?)
|
|
67
|
-
expect(null).to_not respond_to(:login)
|
|
68
|
-
end
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
describe 'with an instance as example' do
|
|
72
|
-
let(:mimic_class) do
|
|
73
|
-
milton = LibraryPatron.new
|
|
74
|
-
def milton.stapler; end
|
|
75
|
-
Naught.build do |b|
|
|
76
|
-
b.mimic :example => milton
|
|
77
|
-
end
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
it 'responds to method defined only on the example instance' do
|
|
81
|
-
expect(null).to respond_to(:stapler)
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
it 'responds to method defined on the class of the instance' do
|
|
85
|
-
expect(null).to respond_to(:member?)
|
|
86
|
-
end
|
|
87
|
-
end
|
|
88
|
-
end
|
|
89
|
-
|
|
90
|
-
describe 'using mimic with black_hole' do
|
|
91
|
-
subject(:null) { mimic_class.new }
|
|
92
|
-
let(:mimic_class) do
|
|
93
|
-
Naught.build do |b|
|
|
94
|
-
b.mimic Logger
|
|
95
|
-
b.black_hole
|
|
96
|
-
end
|
|
97
|
-
end
|
|
98
|
-
|
|
99
|
-
shared_examples_for 'a black hole mimic' do
|
|
100
|
-
it 'returns self from mimicked methods' do
|
|
101
|
-
expect(null.info).to equal(null)
|
|
102
|
-
expect(null.error).to equal(null)
|
|
103
|
-
expect(null << 'test').to equal(null)
|
|
104
|
-
end
|
|
105
|
-
|
|
106
|
-
it 'does not respond to methods not defined on the target class' do
|
|
107
|
-
expect { null.foobar }.to raise_error(NoMethodError)
|
|
108
|
-
end
|
|
109
|
-
end
|
|
110
|
-
|
|
111
|
-
it_should_behave_like 'a black hole mimic'
|
|
112
|
-
|
|
113
|
-
describe '(reverse order)' do
|
|
114
|
-
let(:mimic_class) do
|
|
115
|
-
Naught.build do |b|
|
|
116
|
-
b.black_hole
|
|
117
|
-
b.mimic Logger
|
|
118
|
-
end
|
|
119
|
-
end
|
|
120
|
-
|
|
121
|
-
it_should_behave_like 'a black hole mimic'
|
|
122
|
-
end
|
|
123
|
-
end
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
module Naught
|
|
4
|
-
class NullClassBuilder
|
|
5
|
-
module Commands
|
|
6
|
-
class TestCommand
|
|
7
|
-
end
|
|
8
|
-
end
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
describe NullClassBuilder do
|
|
12
|
-
subject(:builder) { NullClassBuilder.new }
|
|
13
|
-
it 'responds to commands defined in NullObjectBuilder::Commands' do
|
|
14
|
-
expect(builder).to respond_to(:test_command)
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
it 'translates method calls into command invocations including arguments' do
|
|
18
|
-
test_command = double
|
|
19
|
-
expect(NullClassBuilder::Commands::TestCommand).to receive(:new).
|
|
20
|
-
with(builder, 'foo', 42).
|
|
21
|
-
and_return(test_command)
|
|
22
|
-
expect(test_command).to receive(:call).and_return('COMMAND RESULT')
|
|
23
|
-
expect(builder.test_command('foo', 42)).to eq('COMMAND RESULT')
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
it 'handles missing non-command missing methods normally' do
|
|
27
|
-
expect(builder).not_to respond_to(:nonexistant_method)
|
|
28
|
-
expect { builder.nonexistent_method }.to raise_error(NoMethodError)
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
end
|
data/spec/naught_spec.rb
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe 'null object impersonating another type' do
|
|
4
|
-
class Point
|
|
5
|
-
attr_reader :x, :y
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
subject(:null) { impersonation_class.new }
|
|
9
|
-
let(:impersonation_class) do
|
|
10
|
-
Naught.build do |b|
|
|
11
|
-
b.impersonate Point
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
it 'matches the impersonated type' do
|
|
16
|
-
expect(null).to be_a Point
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
it 'responds to methods from the impersonated type' do
|
|
20
|
-
expect(null.x).to be_nil
|
|
21
|
-
expect(null.y).to be_nil
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
it 'does not respond to unknown methods' do
|
|
25
|
-
expect { null.foo }.to raise_error(NoMethodError)
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
describe 'traceable null object' do
|
|
30
|
-
subject(:trace_null) do
|
|
31
|
-
null_object_and_line.first
|
|
32
|
-
end
|
|
33
|
-
let(:null_object_and_line) do
|
|
34
|
-
obj, line = trace_null_class.new, __LINE__ # rubocop:disable ParallelAssignment
|
|
35
|
-
[obj, line]
|
|
36
|
-
end
|
|
37
|
-
let(:instantiation_line) { null_object_and_line.last }
|
|
38
|
-
let(:trace_null_class) do
|
|
39
|
-
Naught.build(&:traceable)
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
it 'remembers the file it was instantiated from' do
|
|
43
|
-
expect(trace_null.__file__).to eq(__FILE__)
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
it 'remembers the line it was instantiated from' do
|
|
47
|
-
expect(trace_null.__line__).to eq(instantiation_line)
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
def make_null
|
|
51
|
-
trace_null_class.get(:caller => caller(1))
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
it 'can accept custom backtrace info' do
|
|
55
|
-
obj, line = make_null, __LINE__ # rubocop:disable ParallelAssignment
|
|
56
|
-
expect(obj.__line__).to eq(line)
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
describe 'customized null object' do
|
|
61
|
-
subject(:custom_null) { custom_null_class.new }
|
|
62
|
-
let(:custom_null_class) do
|
|
63
|
-
Naught.build do |b|
|
|
64
|
-
b.define_explicit_conversions
|
|
65
|
-
def to_path
|
|
66
|
-
'/dev/null'
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
def to_s
|
|
70
|
-
'NOTHING TO SEE HERE'
|
|
71
|
-
end
|
|
72
|
-
end
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
it 'responds to custom-defined methods' do
|
|
76
|
-
expect(custom_null.to_path).to eq('/dev/null')
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
it 'allows generated methods to be overridden' do
|
|
80
|
-
expect(custom_null.to_s).to eq('NOTHING TO SEE HERE')
|
|
81
|
-
end
|
|
82
|
-
end
|
|
83
|
-
TestNull = Naught.build
|
|
84
|
-
|
|
85
|
-
describe 'a named null object class' do
|
|
86
|
-
it 'has named ancestor modules' do
|
|
87
|
-
expect(TestNull.ancestors[0..2].collect(&:name)).to eq([
|
|
88
|
-
'TestNull',
|
|
89
|
-
'TestNull::Customizations',
|
|
90
|
-
'TestNull::GeneratedMethods',
|
|
91
|
-
])
|
|
92
|
-
end
|
|
93
|
-
end
|
data/spec/pebble_spec.rb
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
require 'stringio'
|
|
3
|
-
|
|
4
|
-
describe 'pebble null object' do
|
|
5
|
-
class Caller
|
|
6
|
-
def call_method(thing)
|
|
7
|
-
thing.info
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
def call_method_inside_block(thing)
|
|
11
|
-
2.times.each { thing.info }
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
def call_method_inside_nested_block(thing)
|
|
15
|
-
2.times.each { 2.times.each { thing.info } }
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
subject(:null) { null_class.new }
|
|
20
|
-
let(:null_class) do
|
|
21
|
-
output = test_output # getting local binding
|
|
22
|
-
Naught.build do |b|
|
|
23
|
-
b.pebble output
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
let(:test_output) { StringIO.new }
|
|
28
|
-
|
|
29
|
-
it 'prints the name of the method called' do
|
|
30
|
-
expect(test_output).to receive(:puts).with(/^info\(\)/)
|
|
31
|
-
null.info
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
it 'prints the arguments received' do
|
|
35
|
-
expect(test_output).to receive(:puts).with(/^info\(\'foo\', 5, \:sym\)/)
|
|
36
|
-
null.info('foo', 5, :sym)
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
it 'prints the name of the caller' do
|
|
40
|
-
expect(test_output).to receive(:puts).with(/from call_method$/)
|
|
41
|
-
Caller.new.call_method(null)
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
it 'returns self' do
|
|
45
|
-
expect(null.info).to be(null)
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
context 'when is called from a block' do
|
|
49
|
-
it 'prints the indication of a block',
|
|
50
|
-
:pending => jruby? || rubinius? || ruby_18? do
|
|
51
|
-
expect(test_output).to receive(:puts).twice.
|
|
52
|
-
with(/from block/)
|
|
53
|
-
Caller.new.call_method_inside_block(null)
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
it 'prints the name of the method that has the block' do
|
|
57
|
-
expect(test_output).to receive(:puts).twice.
|
|
58
|
-
with(/call_method_inside_block$/)
|
|
59
|
-
Caller.new.call_method_inside_block(null)
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
context 'when is called from many levels blocks' do
|
|
64
|
-
it 'prints the indication of blocks and its levels',
|
|
65
|
-
:pending => jruby? || rubinius? || ruby_18? do
|
|
66
|
-
expect(test_output).to receive(:puts).exactly(4).times.
|
|
67
|
-
with(/from block \(2 levels\)/)
|
|
68
|
-
Caller.new.call_method_inside_nested_block(null)
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
it 'prints the name of the method that has the block' do
|
|
72
|
-
expect(test_output).to receive(:puts).exactly(4).times.
|
|
73
|
-
with(/call_method_inside_nested_block$/)
|
|
74
|
-
Caller.new.call_method_inside_nested_block(null)
|
|
75
|
-
end
|
|
76
|
-
end
|
|
77
|
-
end
|
data/spec/predicate_spec.rb
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe 'a null object with predicates_return(false)' do
|
|
4
|
-
subject(:null) { null_class.new }
|
|
5
|
-
let(:null_class) do
|
|
6
|
-
Naught.build do |config|
|
|
7
|
-
config.predicates_return false
|
|
8
|
-
end
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
it 'responds to predicate-style methods with false' do
|
|
12
|
-
expect(null.too_much_coffee?).to be(false)
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
it 'responds to other methods with nil' do
|
|
16
|
-
expect(null.foobar).to be(nil)
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
describe '(black hole)' do
|
|
20
|
-
let(:null_class) do
|
|
21
|
-
Naught.build do |config|
|
|
22
|
-
config.black_hole
|
|
23
|
-
config.predicates_return false
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
it 'responds to predicate-style methods with false' do
|
|
28
|
-
expect(null.too_much_coffee?).to be(false)
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
it 'responds to other methods with self' do
|
|
32
|
-
expect(null.foobar).to be(null)
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
describe '(black hole, reverse order config)' do
|
|
37
|
-
let(:null_class) do
|
|
38
|
-
Naught.build do |config|
|
|
39
|
-
config.predicates_return false
|
|
40
|
-
config.black_hole
|
|
41
|
-
end
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
it 'responds to predicate-style methods with false' do
|
|
45
|
-
expect(null.too_much_coffee?).to be(false)
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
it 'responds to other methods with self' do
|
|
49
|
-
expect(null.foobar).to be(null)
|
|
50
|
-
end
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
class Coffee
|
|
54
|
-
attr_reader :origin
|
|
55
|
-
def black?; end
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
describe '(mimic)' do
|
|
59
|
-
let(:null_class) do
|
|
60
|
-
Naught.build do |config|
|
|
61
|
-
config.mimic Coffee
|
|
62
|
-
config.predicates_return false
|
|
63
|
-
end
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
it 'responds to predicate-style methods with false' do
|
|
67
|
-
expect(null.black?).to be(false)
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
it 'responds to other methods with nil' do
|
|
71
|
-
expect(null.origin).to be(nil)
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
it 'does not respond to undefined methods' do
|
|
75
|
-
expect(null).not_to respond_to(:leaf_variety)
|
|
76
|
-
expect { null.leaf_variety }.to raise_error(NoMethodError)
|
|
77
|
-
end
|
|
78
|
-
end
|
|
79
|
-
end
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe 'singleton null object' do
|
|
4
|
-
subject(:null_class) do
|
|
5
|
-
Naught.build(&:singleton)
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
it 'does not respond to .new' do
|
|
9
|
-
expect { null_class.new }.to raise_error(NoMethodError)
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
it 'has only one instance' do
|
|
13
|
-
null1 = null_class.instance
|
|
14
|
-
null2 = null_class.instance
|
|
15
|
-
expect(null1).to be(null2)
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
it 'can be cloned' do
|
|
19
|
-
null = null_class.instance
|
|
20
|
-
expect(null.clone).to be(null)
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
it 'can be duplicated' do
|
|
24
|
-
null = null_class.instance
|
|
25
|
-
expect(null.dup).to be(null)
|
|
26
|
-
end
|
|
27
|
-
it 'aliases .instance to .get' do
|
|
28
|
-
expect(null_class.get).to be null_class.instance
|
|
29
|
-
end
|
|
30
|
-
it 'permits arbitrary arguments to be passed to .get' do
|
|
31
|
-
null_class.get(42, :foo => 'bar')
|
|
32
|
-
end
|
|
33
|
-
end
|
data/spec/spec_helper.rb
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
GEM_ROOT = File.expand_path('../../', __FILE__)
|
|
2
|
-
$LOAD_PATH.unshift File.join(GEM_ROOT, 'lib')
|
|
3
|
-
|
|
4
|
-
require 'simplecov'
|
|
5
|
-
require 'coveralls'
|
|
6
|
-
|
|
7
|
-
SimpleCov.formatters = [SimpleCov::Formatter::HTMLFormatter, Coveralls::SimpleCov::Formatter]
|
|
8
|
-
|
|
9
|
-
SimpleCov.start do
|
|
10
|
-
add_filter '/spec/'
|
|
11
|
-
minimum_coverage(97.7)
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
require 'naught'
|
|
15
|
-
Dir[File.join(GEM_ROOT, 'spec', 'support', '**/*.rb')].each { |f| require f }
|
data/spec/support/jruby.rb
DELETED