waiting_rspec_matchers 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d8cbc120933f6ab6460124a0cd2382bf96c1c6f7
4
- data.tar.gz: 7b2ab64c10d3dadbc4367cee535c97b35a69e039
3
+ metadata.gz: 99be2f59b452f566aa05847760a02bd088b4384d
4
+ data.tar.gz: 6b694444e1a1ded769882a57514f3abadbb4b498
5
5
  SHA512:
6
- metadata.gz: 2aae9a6546d15e32025975e08defb8d1bd4ea6c23eb488f4801dcd40006c939b35b02a5b0a65f475c413e80d557031ab43eb45cfa80722aa8bf1bbcdafa94af7
7
- data.tar.gz: 2bcfe967fd2d61bcada02684da836a4000ba67ac37a9755c49ca5624a321c24aa2fab3b39bc89d75eeb17c38eff61e9c8d171b7fc14e36f7f9c2a201f4529e3e
6
+ metadata.gz: 39d425a4a73b8ecaa10e9b80634db1ae6ccc423212f8ae5c7f019fe227a8e9816cdb85743426fa29c01e2a4d39083046dc1d25b0aef8594c3c7dc307433bb8b8
7
+ data.tar.gz: 51c18ae54c6b0dd88a6dde556b7652bdf8932189fa022cd0c57a6b457315c96ed691767cde3bddfed37ec8258e3f1e94f8ea2b520a9981b44b660076ed94371b
@@ -17,106 +17,107 @@ module WaitingRspecMatchers
17
17
  config.default_delay = 0.05
18
18
  end
19
19
 
20
- private
21
-
22
- BECOME_REGEX = /^become_(.+)/
20
+ # @api private
21
+ # Provides the implementation for `become_<matcher_name>`.
22
+ # Not intended to be instantiated directly.
23
+ class Become
24
+ include ::RSpec::Matchers
25
+ include ::RSpec::Matchers::Composable
26
+
27
+ def initialize(rspec_matcher_name, where_included, *args, &expected_block)
28
+ @rspec_matcher_name = rspec_matcher_name
29
+ @where_included = where_included
30
+ @args = args
31
+ @expected_block = expected_block
32
+ @chains = []
33
+ end
23
34
 
24
- def method_missing(method_name, *args, &expected_block)
25
- if m = method_name.to_s.match(BECOME_REGEX)
26
- rspec_matcher_name = m[1]
27
- unless respond_to?(rspec_matcher_name)
28
- raise ArgumentError, "Matcher #{rspec_matcher_name.inspect} doesn't exist"
29
- end
30
- matcher_class_name = "Become#{rspec_matcher_name.capitalize}Matcher"
31
- if ::WaitingRspecMatchers.const_defined?(matcher_class_name)
32
- k = ::WaitingRspecMatchers.const_get(matcher_class_name)
33
- else
34
- k = define_matcher_class
35
- ::WaitingRspecMatchers.const_set(matcher_class_name, k)
36
- end
35
+ # @api public
36
+ # Specifies a number of seconds that WaitingRspecMatchers should wait for a matcher to succeed
37
+ # @param [Numeric]
38
+ def during(max_wait_time_in_seconds)
39
+ @max_wait_time = max_wait_time_in_seconds
40
+ self
41
+ end
37
42
 
38
- k.new(rspec_matcher_name, self, *args, &expected_block)
39
- else
40
- super
43
+ # @api public
44
+ # Specifies period between tries that WaitingRspecMatchers should make when waiting for a matcher to succeed
45
+ # @param [Numeric]
46
+ def delay(seconds)
47
+ @delay = seconds
48
+ self
41
49
  end
42
- end
43
50
 
44
- def respond_to_missing?(method_name, *)
45
- method_name.to_s =~ BECOME_REGEX || super
46
- end
51
+ def method_missing(method, *args)
52
+ @chains << [method, args]
53
+ self
54
+ end
47
55
 
48
- def define_matcher_class
49
- Class.new do
50
- include ::RSpec::Matchers
51
- include ::RSpec::Matchers::Composable
52
-
53
- def initialize(rspec_matcher_name, where_included, *args, &expected_block)
54
- @rspec_matcher_name = rspec_matcher_name
55
- @where_included = where_included
56
- @args = args
57
- @expected_block = expected_block
58
- @chains = []
59
- end
56
+ def matches?(block)
57
+ match_helper(:to, &block)
58
+ end
60
59
 
61
- def during(max_wait_time_in_seconds)
62
- @max_wait_time = max_wait_time_in_seconds
63
- self
64
- end
60
+ def does_not_match?(block)
61
+ match_helper(:not_to, &block)
62
+ end
65
63
 
66
- def max_wait_time
67
- @max_wait_time || WaitingRspecMatchers.default_wait_time
68
- end
64
+ def failure_message
65
+ @failure_message
66
+ end
69
67
 
70
- def delay(seconds)
71
- @delay = seconds
72
- self
73
- end
68
+ def failure_message_when_negated
69
+ @failure_message
70
+ end
74
71
 
75
- def method_missing(method, *args)
76
- @chains << [method, args]
77
- self
78
- end
72
+ def supports_block_expectations?
73
+ true
74
+ end
79
75
 
80
- def match_helper(to_or_not_to, &block)
81
- start_time = Time.now
82
- begin
83
- matcher = @where_included.__send__(@rspec_matcher_name.to_sym, *@args, &@expected_block)
84
- @chains.each do |chain|
85
- matcher = matcher.__send__(chain[0], *chain[1])
86
- end
87
- if matcher.respond_to?(:supports_block_expectations?) && matcher.supports_block_expectations?
88
- expect { block.call }.__send__(to_or_not_to, matcher)
89
- else
90
- expect(block.call).__send__(to_or_not_to, matcher)
91
- end
92
- rescue RSpec::Expectations::ExpectationNotMetError => e
93
- @failure_message = e.message
94
- return false if (Time.now - start_time) >= max_wait_time
95
- sleep @delay || WaitingRspecMatchers.default_delay
96
- retry
97
- end
98
- true
99
- end
76
+ private
100
77
 
101
- def matches?(block)
102
- match_helper(:to, &block)
103
- end
78
+ def max_wait_time
79
+ @max_wait_time || WaitingRspecMatchers.default_wait_time
80
+ end
104
81
 
105
- def does_not_match?(block)
106
- match_helper(:not_to, &block)
82
+ def match_helper(to_or_not_to, &block)
83
+ start_time = Time.now
84
+ begin
85
+ matcher = @where_included.__send__(@rspec_matcher_name.to_sym, *@args, &@expected_block)
86
+ @chains.each do |chain|
87
+ matcher = matcher.__send__(chain[0], *chain[1])
88
+ end
89
+ if matcher.respond_to?(:supports_block_expectations?) && matcher.supports_block_expectations?
90
+ expect { block.call }.__send__(to_or_not_to, matcher)
91
+ else
92
+ expect(block.call).__send__(to_or_not_to, matcher)
93
+ end
94
+ rescue ::RSpec::Expectations::ExpectationNotMetError => e
95
+ @failure_message = e.message
96
+ return false if (Time.now - start_time) >= max_wait_time
97
+ sleep @delay || WaitingRspecMatchers.default_delay
98
+ retry
107
99
  end
100
+ true
101
+ end
102
+ end
108
103
 
109
- def failure_message
110
- @failure_message
111
- end
104
+ private
112
105
 
113
- def failure_message_when_negated
114
- @failure_message
115
- end
106
+ BECOME_REGEX = /^become_(.+)/
116
107
 
117
- def supports_block_expectations?
118
- true
108
+ def method_missing(method_name, *args, &expected_block)
109
+ if m = method_name.to_s.match(BECOME_REGEX)
110
+ rspec_matcher_name = m[1]
111
+ unless respond_to?(rspec_matcher_name)
112
+ raise ArgumentError, "Method `#{rspec_matcher_name}` doesn't exist. It was expected to exist and return instance of class that implements RSpec's matcher protocol."
119
113
  end
114
+ Become.new(rspec_matcher_name, self, *args, &expected_block)
115
+ else
116
+ super
120
117
  end
121
118
  end
119
+
120
+ def respond_to_missing?(method_name, *)
121
+ method_name.to_s =~ BECOME_REGEX || super
122
+ end
122
123
  end
@@ -1,3 +1,3 @@
1
1
  module WaitingRspecMatchers
2
- VERSION = "0.2"
2
+ VERSION = "0.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: waiting_rspec_matchers
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: '0.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Botalov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-04 00:00:00.000000000 Z
11
+ date: 2014-07-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec-expectations
@@ -97,5 +97,5 @@ rubyforge_project:
97
97
  rubygems_version: 2.2.2
98
98
  signing_key:
99
99
  specification_version: 4
100
- summary: New become_* RSpec matchers that do the same as * matchers but also wait
100
+ summary: become_* RSpec matchers that do the same as * matchers but also wait
101
101
  test_files: []