Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
---|---|---|---|---|
rcov/ruby/1.8/gems/rspec-core-2.5.1/lib/rspec/core/subject.rb | 177 | 64 | 74.01%
|
29.69%
|
Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.
1 require 'ostruct' |
2 |
3 module RSpec |
4 module Core |
5 module Subject |
6 module InstanceMethods |
7 |
8 # Returns the subject defined by the example group. The subject block is |
9 # only executed once per example, the result of which is cached and |
10 # returned by any subsequent calls to +subject+. |
11 # |
12 # If a class is passed to +describe+ and no subject is explicitly |
13 # declared in the example group, then +subject+ will return a new |
14 # instance of that class. |
15 # |
16 # == Examples |
17 # |
18 # # explicit subject defined by the subject method |
19 # describe Person do |
20 # subject { Person.new(:birthdate => 19.years.ago) } |
21 # it "should be eligible to vote" do |
22 # subject.should be_eligible_to_vote |
23 # end |
24 # end |
25 # |
26 # # implicit subject => { Person.new } |
27 # describe Person do |
28 # it "should be eligible to vote" do |
29 # subject.should be_eligible_to_vote |
30 # end |
31 # end |
32 def subject |
33 if defined?(@original_subject) |
34 @original_subject |
35 else |
36 @original_subject = instance_eval(&self.class.subject) |
37 end |
38 end |
39 |
40 begin |
41 require 'rspec/expectations/extensions/kernel' |
42 alias_method :__should_for_example_group__, :should |
43 alias_method :__should_not_for_example_group__, :should_not |
44 |
45 # When +should+ is called with no explicit receiver, the call is |
46 # delegated to the object returned by +subject+. Combined with |
47 # an implicit subject (see +subject+), this supports very concise |
48 # expressions. |
49 # |
50 # == Examples |
51 # |
52 # describe Person do |
53 # it { should be_eligible_to_vote } |
54 # end |
55 def should(matcher=nil, message=nil) |
56 self == subject ? self.__should_for_example_group__(matcher) : subject.should(matcher,message) |
57 end |
58 |
59 # Just like +should+, +should_not+ delegates to the subject (implicit or |
60 # explicit) of the example group. |
61 # |
62 # == Examples |
63 # |
64 # describe Person do |
65 # it { should_not be_eligible_to_vote } |
66 # end |
67 def should_not(matcher=nil, message=nil) |
68 self == subject ? self.__should_not_for_example_group__(matcher) : subject.should_not(matcher,message) |
69 end |
70 rescue LoadError |
71 end |
72 end |
73 |
74 module ClassMethods |
75 # Creates a nested example group named by the submitted +attribute+, |
76 # and then generates an example using the submitted block. |
77 # |
78 # # This ... |
79 # describe Array do |
80 # its(:size) { should == 0 } |
81 # end |
82 # |
83 # # ... generates the same runtime structure as this: |
84 # describe Array do |
85 # describe "size" do |
86 # it "should == 0" do |
87 # subject.size.should == 0 |
88 # end |
89 # end |
90 # end |
91 # |
92 # The attribute can be a +Symbol+ or a +String+. Given a +String+ |
93 # with dots, the result is as though you concatenated that +String+ |
94 # onto the subject in an expression. |
95 # |
96 # describe Person do |
97 # subject do |
98 # Person.new.tap do |person| |
99 # person.phone_numbers << "555-1212" |
100 # end |
101 # end |
102 # |
103 # its("phone_numbers.first") { should == "555-1212" } |
104 # end |
105 # |
106 # When the subject is a +Hash+, you can refer to the Hash keys by |
107 # specifying a +Symbol+ or +String+ in an array. |
108 # |
109 # describe "a configuration Hash" do |
110 # subject do |
111 # { :max_users => 3, |
112 # 'admin' => :all_permissions } |
113 # end |
114 # |
115 # its([:max_users]) { should == 3 } |
116 # its(['admin']) { should == :all_permissions } |
117 # |
118 # # You can still access to its regular methods this way: |
119 # its(:keys) { should include(:max_users) } |
120 # its(:count) { should == 2 } |
121 # end |
122 def its(attribute, &block) |
123 describe(attribute) do |
124 example do |
125 self.class.class_eval do |
126 define_method(:subject) do |
127 @_subject ||= if super().is_a?(Hash) && attribute.is_a?(Array) |
128 OpenStruct.new(super()).send(attribute.first) |
129 else |
130 attribute.to_s.split('.').inject(super()) do |target, method| |
131 target.send(method) |
132 end |
133 end |
134 end |
135 end |
136 instance_eval(&block) |
137 end |
138 end |
139 end |
140 |
141 # Defines an explicit subject for an example group which can then be the |
142 # implicit receiver (through delegation) of calls to +should+. |
143 # |
144 # == Examples |
145 # |
146 # describe CheckingAccount, "with $50" do |
147 # subject { CheckingAccount.new(:amount => 50, :currency => :USD) } |
148 # it { should have_a_balance_of(50, :USD) } |
149 # it { should_not be_overdrawn } |
150 # end |
151 # |
152 # See +ExampleMethods#should+ for more information about this approach. |
153 def subject(&block) |
154 block ? @explicit_subject_block = block : explicit_subject || implicit_subject |
155 end |
156 |
157 attr_reader :explicit_subject_block # :nodoc: |
158 |
159 private |
160 |
161 def explicit_subject |
162 group = self |
163 while group.respond_to?(:explicit_subject_block) |
164 return group.explicit_subject_block if group.explicit_subject_block |
165 group = group.superclass |
166 end |
167 end |
168 |
169 def implicit_subject |
170 described = describes || description |
171 Class === described ? proc { described.new } : proc { described } |
172 end |
173 end |
174 |
175 end |
176 end |
177 end |
Generated on Fri Apr 22 17:22:41 -0700 2011 with rcov 0.9.8