Class: On::TestHelper::Recorder

Inherits:
Object
  • Object
show all
Defined in:
lib/on/test_helper.rb

Overview

Record callbacks.

Defined Under Namespace

Classes: Callback

Instance Method Summary collapse

Constructor Details

#initializeRecorder

Returns a new instance of Recorder.



78
79
80
81
# File 'lib/on/test_helper.rb', line 78

def initialize
  @block      = false
  @callbacks  = []
end

Instance Method Details

#block_called?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/on/test_helper.rb', line 83

def block_called?
  @block
end

#callback_recorded(name, args = []) ⇒ Object



128
129
130
131
# File 'lib/on/test_helper.rb', line 128

def callback_recorded(name, args=[])
  callback = Callback === name ? name : Callback.new(name, args)
  @callbacks << callback
end

#empty?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/on/test_helper.rb', line 87

def empty?
  @callbacks.empty?
end

#last_recordObject

Pops the last recorded recorded.



92
93
94
# File 'lib/on/test_helper.rb', line 92

def last_record
  @callbacks.pop
end

#record_allObject

Records block and all defined callbacks.



97
98
99
100
101
102
# File 'lib/on/test_helper.rb', line 97

def record_all
  proc do |on|
    record_block
    record_callback(on, *on.callbacks)
  end
end

#record_blockObject

Records a block.

recorder = Recorder.new
On.new(:success) do |callback|
  recorder.record_block
  callback.on :success do
    # assert...
  end
end

assert recorder.block_called?


115
116
117
# File 'lib/on/test_helper.rb', line 115

def record_block
  @block = true
end

#record_callback(on, *names) ⇒ Object

Records a single callback.



120
121
122
123
124
125
126
# File 'lib/on/test_helper.rb', line 120

def record_callback(on, *names)
  names.each do |name|
    on.on name do |*args|
      callback_recorded(name, args)
    end
  end
end

#to_procObject

Short-hand for &recorder.record_all.



134
135
136
# File 'lib/on/test_helper.rb', line 134

def to_proc
  record_all
end