Skip to content
This repository was archived by the owner on Mar 15, 2022. It is now read-only.

Commit 17ed4a8

Browse files
lucasallanpedroandrade
authored andcommitted
Add and configure RSpec.
1 parent 0c83003 commit 17ed4a8

File tree

9 files changed

+250
-26
lines changed

9 files changed

+250
-26
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ end
99

1010
group :testing do
1111
gem 'test-unit', '~> 3.0.9'
12-
#gem 'rspec', '~> 3.0.0'
12+
gem 'rspec', '~> 3.1.0'
1313
#gem 'simplecov', '~> 0.8.2', :require => false
1414
gem 'coveralls', '~> 0.7.3', :require => false
1515
end

Rakefile

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ require 'rake/testtask'
33
require 'rubygems/package_task'
44
require File.expand_path('../lib/ref', __FILE__)
55

6-
desc 'Default: run unit tests.'
7-
task :default => :test
6+
begin
7+
require 'rspec'
8+
require 'rspec/core/rake_task'
89

9-
desc 'RVM likes to call it tests'
10-
task :tests => :test
10+
RSpec::Core::RakeTask.new(:spec) do |t|
11+
t.rspec_opts = '--color --backtrace --format documentation'
12+
end
1113

12-
Rake::TestTask.new do |t|
13-
t.libs << 'test'
14-
t.pattern = 'test/**/*_test.rb'
15-
t.warning = true
16-
t.verbose = true
14+
task :default => :spec
15+
rescue LoadError
16+
puts 'Error loading Rspec rake tasks, probably building the gem...'
1717
end
1818

1919

20+
spec = eval(File.read(File.expand_path('../ref.gemspec', __FILE__)))
21+
2022
GEM_NAME = 'ref'
2123
EXTENSION_NAME = 'extension'
2224
JAVA_EXT_NAME = 'ref_ext'

spec/ref/mock_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require 'spec_helper'
2+
3+
describe Ref::Mock do
4+
5+
context 'gc with argument' do
6+
specify do
7+
Ref::Mock.use do
8+
obj_1 = Object.new
9+
obj_2 = Object.new
10+
11+
ref_1 = Ref::WeakReference.new(obj_1)
12+
ref_2 = Ref::WeakReference.new(obj_2)
13+
14+
Ref::Mock.gc(obj_1)
15+
16+
expect(ref_1.object).to be_nil
17+
expect(ref_2.object).to equal(obj_2)
18+
end
19+
end
20+
end
21+
22+
context 'gc with no argument' do
23+
specify do
24+
Ref::Mock.use do
25+
obj_1 = Object.new
26+
obj_2 = Object.new
27+
28+
ref_1 = Ref::WeakReference.new(obj_1)
29+
ref_2 = Ref::WeakReference.new(obj_2)
30+
31+
Ref::Mock.gc
32+
33+
expect(ref_1.object).to be_nil
34+
expect(ref_2.object).to be_nil
35+
end
36+
end
37+
end
38+
39+
end

spec/ref/reference_queue_spec.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
require 'spec_helper'
2+
3+
describe Ref::ReferenceQueue do
4+
5+
let(:queue) { Ref::ReferenceQueue.new }
6+
let(:obj_1) { Object.new }
7+
let(:obj_2) { Object.new }
8+
let(:ref_1) { Ref::WeakReference.new(obj_1) }
9+
let(:ref_2) { Ref::WeakReference.new(obj_2) }
10+
11+
describe '#push' do
12+
context 'when the queue is empty' do
13+
it { expect(queue.size).to be(0) }
14+
it { expect(queue).to be_empty }
15+
end
16+
17+
context 'when the queue is not empty' do
18+
before do
19+
queue.push(ref_1)
20+
queue.push(ref_2)
21+
end
22+
23+
it { expect(queue.size).to be(2) }
24+
it { expect(queue).to_not be_empty }
25+
end
26+
end
27+
28+
describe '#shift' do
29+
context 'when the queue is not empty' do
30+
before do
31+
queue.push(ref_1)
32+
queue.push(ref_2)
33+
end
34+
it { expect(queue.shift).to be(ref_1) }
35+
end
36+
37+
context 'when the queue is empty' do
38+
it { expect(queue.shift).to be_nil }
39+
end
40+
end
41+
42+
describe '#pop' do
43+
context 'when the queue is not empty' do
44+
before do
45+
queue.push(ref_1)
46+
queue.push(ref_2)
47+
end
48+
49+
it { expect(queue.pop).to be(ref_2) }
50+
end
51+
context 'when the queue is empty' do
52+
it { expect(queue.pop).to be_nil }
53+
end
54+
end
55+
56+
describe 'references are added immediately if the_object has been collected' do
57+
specify do
58+
Ref::Mock.use do
59+
ref = Ref::WeakReference.new(obj_1)
60+
Ref::Mock.gc(obj_1)
61+
queue.monitor(ref)
62+
63+
expect(ref).to equal(queue.shift)
64+
end
65+
end
66+
end
67+
end

spec/ref/soft_value_map_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require 'spec_helper'
2+
3+
describe Ref::SoftValueMap do
4+
it_behaves_like 'a reference key map' do
5+
let(:map_class) { Ref::SoftValueMap }
6+
let(:reference_class) { Ref::SoftReference }
7+
end
8+
end

spec/ref/strong_reference_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'spec_helper'
2+
3+
describe Ref::StrongReference do
4+
let(:obj) { Object.new }
5+
let(:ref) { Ref::StrongReference.new(obj) }
6+
7+
context '#object' do
8+
it { expect(obj).to equal(ref.object) }
9+
it { expect(obj.object_id).to equal(ref.referenced_object_id) }
10+
end
11+
12+
context '#inspect' do
13+
it { expect(ref.inspect).to_not be_empty }
14+
end
15+
end
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
require 'spec_helper'
2+
3+
shared_examples 'a reference key map' do
4+
5+
let(:value_1) { 'value 1' }
6+
let(:value_2) { 'value 2' }
7+
let(:value_3) { 'value 3' }
8+
let(:key_1) { Object.new }
9+
let(:key_2) { Object.new }
10+
let(:hash) { map_class.new }
11+
12+
describe 'keeps entries with strong references' do
13+
specify do
14+
Ref::Mock.use do
15+
hash[key_1] = value_1
16+
hash[key_2] = value_2
17+
expect(hash[key_1] ).to eq(value_1)
18+
expect(hash[key_2] ).to eq(value_2)
19+
end
20+
end
21+
end
22+
23+
describe 'removes entries that have been garbage collected' do
24+
specify do
25+
Ref::Mock.use do
26+
hash['key 1'] = value_1
27+
hash['key 2'] = value_2
28+
expect(hash['key 1']).to eq(value_1)
29+
expect(hash['key 2']).to eq(value_2)
30+
Ref::Mock.gc(value_2)
31+
expect(value_1).to eq(value_1)
32+
expect(hash['key 2']).to be_nil
33+
end
34+
end
35+
end
36+
37+
describe 'can clear the map' do
38+
specify do
39+
hash['key 1'] = value_1
40+
hash['key 2'] = value_2
41+
hash.clear
42+
expect(hash['key 1']).to be_nil
43+
expect(hash['key 2']).to be_nil
44+
end
45+
end
46+
47+
describe 'can delete entries' do
48+
specify do
49+
hash['key 1'] = value_1
50+
hash['key 2'] = value_2
51+
expect(hash.delete('key 3')).to be_nil
52+
expect(hash.delete('key 1')).to eq(value_1)
53+
expect(hash['key 1']).to be_nil
54+
end
55+
end
56+
57+
describe 'can merge in another hash' do
58+
specify do
59+
hash['key 1'] = value_1
60+
hash['key 2'] = value_2
61+
hash.merge!("key 3" => value_3)
62+
expect(hash['key 1']).to be_nil
63+
64+
end
65+
end
66+
end

spec/spec_helper.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require 'simplecov'
2+
require 'coveralls'
3+
4+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5+
SimpleCov::Formatter::HTMLFormatter,
6+
Coveralls::SimpleCov::Formatter
7+
]
8+
9+
SimpleCov.start do
10+
project_name 'ref'
11+
add_filter '/coverage/'
12+
add_filter '/doc/'
13+
add_filter '/pkg/'
14+
add_filter '/spec/'
15+
add_filter '/tasks/'
16+
end
17+
18+
require 'ref'
19+
require 'ref/mock'
20+
21+
# import all the support files
22+
Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |f| require File.expand_path(f) }
23+
Dir[File.join(File.dirname(__FILE__), 'shared/**/*.rb')].each { |f| require File.expand_path(f) }
24+
25+
RSpec.configure do |config|
26+
config.order = 'random'
27+
end

test/mock.rb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ def use
2828
end
2929
end
3030
end
31-
31+
3232
# Start using mock references.
3333
def setup
3434
raise "Ref::Mock already setup" if object_space
35-
35+
3636
@object_space = {}
37-
37+
3838
class << ObjectSpace
3939
unless method_defined?(:define_finalizer_with_mock_reference)
4040
def define_finalizer_with_mock_reference(obj, finalizer)
@@ -45,11 +45,11 @@ def define_finalizer_with_mock_reference(obj, finalizer)
4545
end
4646
end
4747
end
48-
48+
4949
alias_method :define_finalizer_without_mock_reference, :define_finalizer
5050
alias_method :define_finalizer, :define_finalizer_with_mock_reference
5151
end
52-
52+
5353
class << WeakReference
5454
unless method_defined?(:new_with_mock_reference)
5555
def new_with_mock_reference(obj)
@@ -60,11 +60,11 @@ def new_with_mock_reference(obj)
6060
end
6161
end
6262
end
63-
63+
6464
alias_method :new_without_mock_reference, :new
6565
alias_method :new, :new_with_mock_reference
6666
end
67-
67+
6868
class << SoftReference
6969
unless method_defined?(:new_with_mock_reference)
7070
def new_with_mock_reference(obj)
@@ -75,25 +75,25 @@ def new_with_mock_reference(obj)
7575
end
7676
end
7777
end
78-
78+
7979
alias_method :new_without_mock_reference, :new
8080
alias_method :new, :new_with_mock_reference
8181
end
8282
end
83-
83+
8484
# Stop using mock references.
8585
def cleanup
8686
@object_space = nil
8787
class << ObjectSpace
8888
alias_method :define_finalizer_with_mock_reference, :define_finalizer
8989
alias_method :define_finalizer, :define_finalizer_without_mock_reference
9090
end
91-
91+
9292
class << WeakReference
9393
alias_method :new_with_mock_reference, :new
9494
alias_method :new, :new_without_mock_reference
9595
end
96-
96+
9797
class << SoftReference
9898
alias_method :new_with_mock_reference, :new
9999
alias_method :new, :new_without_mock_reference
@@ -121,15 +121,15 @@ def gc(*objects)
121121
end
122122
end
123123
end
124-
124+
125125
module MockReference #:nodoc:
126126
def initialize(obj)
127127
@object = obj
128128
@referenced_object_id = obj.__id__
129129
raise "Reference::Mock not setup yet" unless Mock.object_space
130130
Mock.object_space[obj.__id__] ||= []
131131
end
132-
132+
133133
def object
134134
if @object && Mock.object_space.include?(@object.__id__)
135135
@object
@@ -138,13 +138,13 @@ def object
138138
end
139139
end
140140
end
141-
141+
142142
class MockWeakReference < WeakReference #:nodoc:
143143
include MockReference
144144
end
145-
145+
146146
class MockSoftReference < SoftReference #:nodoc:
147147
include MockReference
148148
end
149-
end
149+
end
150150
end

0 commit comments

Comments
 (0)