-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathutil_aliasing_spec.rb
66 lines (54 loc) · 2.22 KB
/
util_aliasing_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require_relative 'spec_helper'
describe RDF::Util::Aliasing do
before :all do
module ::RDF::Util::Aliasing
class Aliased
def original(*args, &block)
"original return value: #{args.join(',')} and #{block.call if block_given?}"
end
alias_method :rebound, :original # early-bound alias
extend RDF::Util::Aliasing::LateBound # magic happens
alias_method :aliased, :original # late-bound alias
end unless defined?(Aliased)
end
end
subject {RDF::Util::Aliasing::Aliased.new}
context "when aliasing a method" do
it "should create a new instance method with the given name" do
expect(subject).to respond_to(:aliased)
end
end
context "the aliased method" do
it "should accept any arguments" do
expect { subject.aliased }.not_to raise_error
expect { subject.aliased(1) }.not_to raise_error
expect { subject.aliased(1, 2) }.not_to raise_error
end
it "should accept a block" do
expect { subject.aliased(1, 2) do 3 end }.not_to raise_error
end
it "should invoke the original method with the given arguments" do
expect(subject.aliased).to eq subject.original
expect(subject.aliased(1, 2)).to eq subject.original(1, 2)
expect(subject.aliased(1, 2, 3)).to eq subject.original(1, 2, 3)
end
it "should invoke the original method with the given block" do
expect(subject.aliased { 1 }).to eq subject.original { 1 }
expect(subject.aliased(1) { 2 }).to eq subject.original(1) { 2 }
expect(subject.aliased(1, 2) { 3 }).to eq subject.original(1, 2) { 3 }
end
it "should update if the original method is redefined" do
module ::RDF::Util::Aliasing
class Aliased
def original(*args, &block)
"redefined return value: #{args.join('::')} and #{block.call if block_given?}"
end
end
end
expect(subject.rebound(1, 2)).not_to eq subject.original(1, 2)
expect(subject.aliased(1, 2)).to eq subject.original(1, 2)
expect(subject.rebound(1, 2) { 3 }).not_to eq subject.original(1, 2) { 3 }
expect(subject.aliased(1, 2) { 3 }).to eq subject.original(1, 2) { 3 }
end
end
end