Class: RSpec::BecomeMatcher::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/become_matcher.rb

Constant Summary collapse

DEFAULT_LIMIT =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expected, &block) ⇒ Matcher

Returns a new instance of Matcher.


9
10
11
12
13
# File 'lib/rspec/become_matcher.rb', line 9

def initialize(expected, &block)
  @expected = expected
  @expected_block = block
  @limit = DEFAULT_LIMIT
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current


7
8
9
# File 'lib/rspec/become_matcher.rb', line 7

def current
  @current
end

#expectedObject (readonly)

Returns the value of attribute expected


7
8
9
# File 'lib/rspec/become_matcher.rb', line 7

def expected
  @expected
end

#expected_blockObject (readonly)

Returns the value of attribute expected_block


7
8
9
# File 'lib/rspec/become_matcher.rb', line 7

def expected_block
  @expected_block
end

#limitObject (readonly)

Returns the value of attribute limit


7
8
9
# File 'lib/rspec/become_matcher.rb', line 7

def limit
  @limit
end

#wasObject (readonly)

Returns the value of attribute was


7
8
9
# File 'lib/rspec/become_matcher.rb', line 7

def was
  @was
end

Instance Method Details

#failure_messageObject


41
42
43
44
45
# File 'lib/rspec/become_matcher.rb', line 41

def failure_message
  <<~MESSAGE
  Expected #{was} to become #{expected || 'given block'} in #{limit.to_i} second, but not
  MESSAGE
end

#failure_message_when_negatedObject


47
48
49
50
51
# File 'lib/rspec/become_matcher.rb', line 47

def failure_message_when_negated
  <<~MESSAGE
  Expected #{was} not to become #{expected || 'given block'} in #{limit.to_i} second
  MESSAGE
end

#in(limit) ⇒ Object


36
37
38
39
# File 'lib/rspec/become_matcher.rb', line 36

def in(limit)
  @limit = limit
  self
end

#matches?(block) ⇒ Boolean

Returns:

  • (Boolean)

15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rspec/become_matcher.rb', line 15

def matches?(block)
  Timeout.timeout(limit) do
    @was = @current = block.call
    @current = begin
                 sleep 0.1
                 block.call
               end until matching_now?
    true
  end
rescue Timeout::Error
  false
end

#matching_now?Boolean

Returns:

  • (Boolean)

28
29
30
31
32
33
34
# File 'lib/rspec/become_matcher.rb', line 28

def matching_now?
  if expected_block
    expected_block.call(current)
  else
    current == expected
  end
end

#supports_block_expectations?Boolean

Returns:

  • (Boolean)

53
54
55
# File 'lib/rspec/become_matcher.rb', line 53

def supports_block_expectations?
  true
end