Class: BeanCounter::EnqueuedExpectation

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/bean_counter/enqueued_expectation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expected) ⇒ EnqueuedExpectation

Create a new enqueued expectation. Uses the given expected Hash to determine if any jobs are enqueued that match the expected options.

Each key in expected is a String or a Symbol that identifies an attribute of a job that the corresponding value should be compared against. All attribute comparisons are performed using the triple-equal (===) operator/method of the given value.

expected may additionally include a count key of 'count' or :count that can be used to specify that a particular number of matching jobs are found.

See Strategy::MATCHABLE_JOB_ATTRIBUTES for a list of attributes that can be used when matching.

Parameters:

  • expected

    [HashSymbol => Numeric, Proc, Range, Regexp, String, Symbol] Options expected when evaluating match

Options Hash (expected):

  • :count (Numeric, Range) — default: nil

    A particular number of matching jobs expected

See Also:



92
93
94
95
# File 'lib/bean_counter/enqueued_expectation.rb', line 92

def initialize(expected)
  @expected = expected
  @expected_count = [expected.delete(:count), expected.delete('count')].compact.first
end

Instance Attribute Details

#expectedHash (readonly)

The Hash of options given at instantiation that the expectation expects when matching.

Returns:

  • (Hash)


10
11
12
# File 'lib/bean_counter/enqueued_expectation.rb', line 10

def expected
  @expected
end

#expected_countNumeric, Range (readonly)

The number of matching jobs the expectation expects

Returns:

  • (Numeric, Range)


14
15
16
# File 'lib/bean_counter/enqueued_expectation.rb', line 14

def expected_count
  @expected_count
end

#foundArray<Strategy::Job> (readonly)

The jobs found by the expecation during matching

Returns:

  • (Array<Strategy::Job>)


18
19
20
# File 'lib/bean_counter/enqueued_expectation.rb', line 18

def found
  @found
end

Instance Method Details

#collection_matcher(collection) ⇒ Boolean

Iterates over the provided collection searching for jobs matching the Hash of expected options provided during instantiation.

Parameters:

  • collection (Array<Strategy::Job>)

    A collection of jobs as implemented by the strategy to evaluate for a match.

Returns:

  • (Boolean)

    true if the collection matches the expected options and count



28
29
30
31
32
33
34
35
36
# File 'lib/bean_counter/enqueued_expectation.rb', line 28

def collection_matcher(collection)
  @found = collection.send(expected_count? ? :select : :detect) do |job|
    strategy.job_matches?(job, expected)
  end

  @found = [@found] unless expected_count? || @found.nil?

  expected_count? ? expected_count === found.to_a.length : !found.nil?
end

#expected_count?Boolean

Returns a Boolean indicating whether a specific number of jobs are expected.

Returns:

  • (Boolean)

    true if a specifc number of jobs are expected, otherwise false



43
44
45
# File 'lib/bean_counter/enqueued_expectation.rb', line 43

def expected_count?
  return !!expected_count
end

#failure_messageString

Builds the failure message used in the event of a positive expectation failure.

Returns:

  • (String)

    The failure message for use in the event of a positive expectation failure.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bean_counter/enqueued_expectation.rb', line 53

def failure_message
  if found.nil?
    found_count = 'none'
    found_string = nil
  else
    materialized_found = found.to_a
    found_count = "#{materialized_found.length}:"
    found_string = materialized_found.map {|job| strategy.pretty_print_job(job) }.join("\n")
  end
  [
    "expected #{expected_count || 'any number of'} jobs matching #{expected.to_s},",
    "found #{found_count}",
    found_string,
  ].compact.join(' ')
end

#matches?(given = nil) ⇒ Boolean

Checks the beanstalkd pool for jobs matching the Hash of expected options provided at instantiation.

If no count option is provided, the expectation succeeds if any job is found that matches all of the expected options. If no jobs are found that match the expected options, the expecation fails.

If a count option is provided the expectation only succeeds if the triple-equal (===) operator/method of the value of count evaluates to true when given the total number of matching jobs. Otherwise the expecation fails. The use of === allows for more advanced comparisons using Procs, Ranges, Regexps, etc.

See Strategy#job_matches? and/or the #job_matches? method of the strategy in use for more detailed information on how it is determined whether or not a job matches the options expected.

See Strategy::MATCHABLE_JOB_ATTRIBUTES for a list of attributes that can be used when matching.

Parameters:

  • given (Proc) (defaults to: nil)

    If a Proc is provided, only jobs enqueued during the execution of the Proc are considered when looking for a match. Otherwise all jobs available to the strategy will be evaluated for a match.

Returns:

  • (Boolean)

    If a match is found, returns true. Otherwise, returns false.

See Also:



125
126
127
128
129
130
131
# File 'lib/bean_counter/enqueued_expectation.rb', line 125

def matches?(given = nil)
  if given.kind_of?(Proc)
    return proc_matcher(given)
  else
    return collection_matcher(strategy.jobs)
  end
end

#negative_failure_messageString

Builds the failure message used in the event of a negative expectation failure

Returns:

  • (String)

    The failure message for use in the event of a negative expectation failure.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/bean_counter/enqueued_expectation.rb', line 139

def negative_failure_message
  return '' if found.nil? || found == []

  found_count = found.length
  found_string = found.map {|job| strategy.pretty_print_job(job) }.join("\n")
  if expected_count?
    job_count = expected_count
    job_word = expected_count == 1 ? 'job' : 'jobs'
  else
    job_count = 'any'
    job_word = 'jobs'
  end
  return [
    "did not expect #{job_count} #{job_word} matching #{expected.to_s},",
    "found #{found_count}:",
     found_string,
  ].join(' ')
end

#proc_matcher(block) ⇒ Boolean

Evaluates jobs enqueued during the execution of the provided block to determine if any jobs were enqueued that match the expected options provided at instantiation

Parameters:

  • block (Proc)

    A Proc that when executed should demonstrate that expected behavior.

Returns:

  • (Boolean)

    If the jobs enqueued during the execution of the block include a match, returns true. Otherwise, returns false.



167
168
169
170
# File 'lib/bean_counter/enqueued_expectation.rb', line 167

def proc_matcher(block)
  new_jobs = strategy.collect_new_jobs(&block)
  return collection_matcher(new_jobs)
end