This repository has been archived by the owner on Oct 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 171
/
example_methods.rb
144 lines (120 loc) · 3.8 KB
/
example_methods.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
module Spec
module Example
module ExampleMethods
extend Spec::Example::ModuleReopeningFix
include Spec::Example::Subject::ExampleMethods
def violated(message="")
raise Spec::Expectations::ExpectationNotMetError.new(message)
end
# Declared description for this example:
#
# describe Account do
# it "should start with a balance of 0" do
# ...
#
# description
# => "should start with a balance of 0"
def description
if description = @_proxy.description || ::Spec::Matchers.generated_description
description
else
raise Spec::Example::NoDescriptionError.new("example", @_proxy.location)
end
end
def options # :nodoc:
@_proxy.options
end
def execute(run_options, instance_variables) # :nodoc:
puts caller unless caller(0)[1] =~ /example_group_methods/
run_options.reporter.example_started(@_proxy)
set_instance_variables_from_hash(instance_variables)
execution_error = nil
Timeout.timeout(run_options.timeout) do
begin
before_each_example
instance_eval(&@_implementation)
rescue Exception => e
execution_error ||= e
end
begin
after_each_example
rescue Exception => e
execution_error ||= e
end
end
run_options.reporter.example_finished(@_proxy.update(description), execution_error)
success = execution_error.nil? || ExamplePendingError === execution_error
end
module Aliases
alias_method :to, :should
alias_method :to_not, :should_not
end
def expect(&block)
block.extend Aliases
end
def eval_each_fail_fast(blocks) # :nodoc:
blocks.each {|block| instance_eval(&block)}
end
def eval_each_fail_slow(blocks) # :nodoc:
first_exception = nil
blocks.each do |block|
begin
instance_eval(&block)
rescue Exception => e
first_exception ||= e
end
end
raise first_exception if first_exception
end
def instance_variable_hash # :nodoc:
instance_variables.inject({}) do |variable_hash, variable_name|
variable_hash[variable_name] = instance_variable_get(variable_name)
variable_hash
end
end
def set_instance_variables_from_hash(ivars) # :nodoc:
ivars.each do |variable_name, value|
# Ruby 1.9 requires variable.to_s on the next line
unless ['@_proxy', '@_implementation', '@method_name'].include?(variable_name.to_s)
instance_variable_set variable_name, value
end
end
end
# Run all the before(:each) blocks for this example
def run_before_each
example_group_hierarchy.run_before_each(self)
end
# Run all the after(:each) blocks for this example
def run_after_each
example_group_hierarchy.run_after_each(self)
end
def initialize(example_proxy, &implementation)
@_proxy = example_proxy
@_implementation = implementation
@_backtrace = caller
end
private
include Matchers
include Pending
def before_each_example
setup_mocks_for_rspec
run_before_each
end
def after_each_example
run_after_each
verify_mocks_for_rspec
ensure
teardown_mocks_for_rspec
end
def described_class
self.class.described_class
end
def description_args
self.class.description_args
end
def example_group_hierarchy
self.class.example_group_hierarchy
end
end
end
end