expectation 0.2.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -24,27 +24,35 @@
24
24
  # end
25
25
 
26
26
  module Expectation
27
- # Verifies a number of expectations. Raises an ArgumentError if one
28
- # or more expectations are not met.
29
- #
30
- # In contrast to Expectation#expect this method can not be
31
- # disabled at runtime.
27
+ def self.timeout=(timeout)
28
+ Thread.current[:expectation_timeout] = timeout
29
+ end
30
+
31
+ def self.timeout
32
+ Thread.current[:expectation_timeout]
33
+ end
34
+
35
+ def self.last_error=(error)
36
+ Thread.current[:expectation_last_error] = error
37
+ end
38
+
39
+ def self.last_error
40
+ Thread.current[:expectation_last_error]
41
+ end
42
+
32
43
  def expect!(*expectations, &block)
33
- if block_given?
34
- Expectation.verify! true, block
35
- end
36
-
37
- expectations.each do |expectation|
38
- case expectation
39
- when Hash
40
- expectation.each do |value, e|
41
- Expectation.verify! value, e
42
- end
43
- else
44
- Expectation.verify! expectation, :truish
45
- end
44
+ return if Expectation.met_expectations?(*expectations, &block)
45
+
46
+ # build exception with adjusted backtrace.
47
+ backtrace = caller #[3 .. -1]
48
+
49
+ e = ArgumentError.new Expectation.last_error
50
+ e.singleton_class.send(:define_method, :backtrace) do
51
+ backtrace
46
52
  end
47
- end
53
+
54
+ raise e
55
+ end
48
56
 
49
57
  # Verifies a number of expectations. If one or more expectations are
50
58
  # not met it raises an ArgumentError.
@@ -59,6 +67,41 @@ module Expectation
59
67
  # Expectation are disabled.
60
68
  def expect_dummy!(*expectations, &block) #:nodoc:
61
69
  end
70
+
71
+ # Verifies a number of expectations. Raises an ArgumentError if one
72
+ # or more expectations are not met.
73
+ #
74
+ # In contrast to Expectation#expect this method can not be
75
+ # disabled at runtime.
76
+ def self.met_expectations?(*expectations, &block)
77
+ return false unless expectations.all? do |expectation|
78
+ case expectation
79
+ when Hash
80
+ expectation.all? { |value, e| Expectation.verify! value, e }
81
+ else
82
+ Expectation.verify! expectation, :truish
83
+ end
84
+ end
85
+
86
+ return true unless block_given?
87
+
88
+ # Dynamic expectation? When Expectation.timeout is set, we test the
89
+ # dynamic expectation, i.e. the block, for a certain time period.
90
+ begin
91
+ timeout, Expectation.timeout = Expectation.timeout, nil
92
+
93
+ runs = timeout ? (timeout / 0.05).to_i : 0
94
+ runs = 1 if runs < 1
95
+
96
+ runs.times.any? do
97
+ next true if Expectation.verify! 1, block
98
+ Thread.send :sleep, 0.05
99
+ false
100
+ end
101
+ ensure
102
+ Expectation.timeout = timeout
103
+ end
104
+ end
62
105
 
63
106
  # Does a value meet the expectation? Retruns +true+ or +false+.
64
107
  def self.met?(value, expectation) #:nodoc:
@@ -72,8 +115,9 @@ module Expectation
72
115
  end
73
116
  end
74
117
 
75
- # Verifies a value against an expectation. Builds and raises
76
- # an ArgumentError exception if the expectation is not met.
118
+ # Verifies a value against an expectation; returns true if ok,
119
+ # false if the expectation was not met. In case of an error
120
+ # the Expectation.last_error value is set.
77
121
  def self.verify!(value, expectation)
78
122
  failed_value, failed_expectation, message = value, expectation, nil
79
123
 
@@ -93,16 +137,11 @@ module Expectation
93
137
  end
94
138
 
95
139
  # are we good?
96
- return if good
97
-
98
- # build exception with adjusted backtrace.
99
- backtrace = caller[5 .. -1]
140
+ return true if good
100
141
 
101
- e = ArgumentError.new "#{failed_value.inspect} does not meet expectation #{failed_expectation.inspect}#{message && ", #{message}"}"
102
- e.singleton_class.send(:define_method, :backtrace) do
103
- backtrace
104
- end
105
- raise e
142
+ Expectation.last_error = "#{failed_value.inspect} does not meet expectation #{failed_expectation.inspect}#{message && ", #{message}"}"
143
+
144
+ return false
106
145
  end
107
146
 
108
147
  # Enable the Expectation#expect method.
@@ -26,23 +26,16 @@ end
26
26
  # end
27
27
  #
28
28
  module Expectation::Assertions
29
- alias_method :original_expect!, :expect! #:nodoc:
30
-
31
- def assert_expectation(should_succeed, *expectation, &block) #:nodoc:
32
- original_expect! *expectation, &block
33
- assert_block { should_succeed }
34
- rescue ArgumentError
35
- assert_block($!.to_s) { !should_succeed }
36
- end
37
-
38
29
  # verifies the passed in expectations
39
30
  def expect!(*expectation, &block)
40
- assert_expectation true, *expectation, &block
31
+ good = Expectation.met_expectations?(*expectation, &block)
32
+ assert_block(Expectation.last_error) { good }
41
33
  end
42
34
 
43
35
  # verifies the failure of the passed in expectations
44
36
  def inexpect!(*expectation, &block)
45
- assert_expectation false, *expectation, &block
37
+ good = Expectation.met_expectations?(*expectation, &block)
38
+ assert_block("Expectation(s) should fail, but didn't") { !good }
46
39
  end
47
40
  end
48
41
 
@@ -4,5 +4,5 @@
4
4
  # License:: Distributes under the terms of the Modified BSD License, see LICENSE.BSD for details.
5
5
  module Expectation
6
6
  # The expectation gem's version.
7
- VERSION="0.2.0"
7
+ VERSION="0.3.1"
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: expectation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-20 00:00:00.000000000 Z
12
+ date: 2013-01-08 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Defensive programming with expectations
15
15
  email: eno@radiospiel.org
@@ -38,7 +38,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
38
38
  version: '0'
39
39
  segments:
40
40
  - 0
41
- hash: 2749941318204507479
41
+ hash: 4441031152163392854
42
42
  required_rubygems_version: !ruby/object:Gem::Requirement
43
43
  none: false
44
44
  requirements:
@@ -47,7 +47,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
47
47
  version: '0'
48
48
  segments:
49
49
  - 0
50
- hash: 2749941318204507479
50
+ hash: 4441031152163392854
51
51
  requirements: []
52
52
  rubyforge_project:
53
53
  rubygems_version: 1.8.24