Class: Speck::Check

Inherits:
Object
  • Object
show all
Defined in:
lib/speck/check.rb

Overview

Represents a queued thing to be checked of some sort, within a ‘Speck`.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target = nil, &expectation) ⇒ Check

Returns a new instance of Check.



26
27
28
29
30
# File 'lib/speck/check.rb', line 26

def initialize(target = nil, &expectation)
  @target = target.respond_to?(:call) ? target : ->{target}
  @expectation = expectation
  Speck.current.checks << self if Speck.current
end

Instance Attribute Details

#expectationObject

The block to be executed, determining the success or failure of this particular ‘Check`. If it accepts an argument, the result of the target block will be passed as that argument.



9
10
11
# File 'lib/speck/check.rb', line 9

def expectation
  @expectation
end

#statusObject

The status of the ‘Check`. `nil` indicates the `Check` hasn’t been executed, and `true` or `false` indicate the success of the latest execution



21
22
23
# File 'lib/speck/check.rb', line 21

def status
  @status
end

#targetObject

The ‘target` of a `Check` is a block that returns an object to be passed to the `expectation`. It represents the object that the `Check` is intended to, well, check.



15
16
17
# File 'lib/speck/check.rb', line 15

def target
  @target
end

Instance Method Details

#executeObject

Executes this ‘Check`, raising an error if the expectation returns nil or false.



35
36
37
38
39
40
# File 'lib/speck/check.rb', line 35

def execute
  call = @expectation.arity == 0 ? ->{@target.call; @expectation.call} : ->{@expectation[@target.call]}
  @status = call.call ? :passed : :failed
  raise Exception::CheckFailed unless passed?
  return self
end

#passed?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/speck/check.rb', line 22

def passed?
  status == :passed
end