This repository has been archived by the owner on Feb 21, 2021. It is now read-only.
/
identify_spec.rb
89 lines (64 loc) · 2.04 KB
/
identify_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require 'spec_helper'
describe Identitee do
it "acts as a constructor" do
TestIdentifyInitialize = Class.new do
include Identitee
end
TestIdentifyInitialize.identify(:test).should be_a TestIdentifyInitialize
end
it "registers the identifiable" do
TestIdentifyRegister = Class.new do
include Identitee
end
instance = TestIdentifyRegister.identify :test
TestIdentifyRegister.find_identifiable(:test).should == instance
end
it "passes extra parameters through to new" do
TestIdentifyMultipleParams = Class.new do
include Identitee
attr_reader :attrib
def initialize attrib
@attrib = attrib
end
end
instance = TestIdentifyMultipleParams.identify :test, "Testing"
instance.attrib.should == "Testing"
end
it "passes the block to the new as well" do
TestIdentifyPassingBlock = Class.new do
include Identitee
attr_reader :attrib
def set_attrib str
@attrib = str
end
end
instance = TestIdentifyPassingBlock.identify :test do
set_attrib "It ran the block!"
end
instance.attrib.should == "It ran the block!"
end
it "simplifies find_identifiable to find" do
TestAddingFind = Class.new
TestAddingFind.should_not respond_to :find
TestAddingFind.send(:include, Identitee)
TestAddingFind.should respond_to :find
end
it "doesn't symplify find_identifiable when find exists" do
TestNotReplacingFind = Class.new do
def self.find key
key
end
end
TestNotReplacingFind.find("something").should == "something"
TestNotReplacingFind.send(:include, Identitee)
TestNotReplacingFind.find("something").should == "something"
TestNotReplacingFind.find_identifiable("something").should == "Unknown"
end
it "finds the key for an identifiable" do
TestFindIdentiteeKey = Class.new do
include Identitee
end
instance = TestFindIdentiteeKey.identify :testing_key
TestFindIdentiteeKey.find_identifiable_key(instance).should eq 'testing_key'
end
end