Module: Wires::Test::Helper

Defined in:
lib/wires/test.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#wires_eventsObject (readonly)

Returns the value of attribute wires_events.



6
7
8
# File 'lib/wires/test.rb', line 6

def wires_events
  @wires_events
end

Instance Method Details

#clear_firedObject



67
68
69
# File 'lib/wires/test.rb', line 67

def clear_fired
  @wires_events.clear
end

#fired?(event, channel = :__no_channel_was_specified__, clear: false, exclusive: false, plurality: nil, exact_event: false, exact_channel: false, &block) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/wires/test.rb', line 22

def fired?(event, channel=:__no_channel_was_specified__, 
           clear:false, exclusive:false, plurality:nil,
           exact_event:false, exact_channel:false,
           &block)
  key_chan = 
    case channel
    when :__no_channel_was_specified__
      nil
    when Channel
      channel
    else
      Channel[channel]
    end
  
  key_event = Event.list_from event
  
  case key_event.count
  when 0
    raise ArgumentError,"Can't create an event from input: "\
                        "#{event.inspect}"
  when 1
    key_event = key_event.first
  else
    raise ArgumentError,"Can't check for fired? on multiple events: "\
                        "#{key_event.inspect}"
  end
  
  results = @wires_events.select { |e,c|
    c = Channel[c]
    (exact_event   ? (key_event == e) : (key_event =~ e)) && (!key_chan ||
    (exact_channel ? (key_chan  == c) : (key_chan  =~ c)))
  }
  
  # If passed a block, use it to determine
  results.select! { |e,c| yield e,c } if block_given?
  
  clear_fired if clear
  
  return false if results.empty?
  return false if exclusive and (@wires_events != results)
  return false if plurality and (results.size != plurality)
  
  true
end

#wires_test_channel_from_kwargs(**kwargs) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/wires/test.rb', line 71

def wires_test_channel_from_kwargs **kwargs
  # Get channel_name from keyword arguments
  channel_name = \
    kwargs.has_key?(:channel_name) ?
      kwargs[:channel_name]        :
      kwargs.has_key?(:to)           ?
        eval(kwargs[:to].to_s)       :
        subject
  # Get channel object from channel_name or keyword argument :channel_obj
  channel_obj = \
    kwargs.has_key?(:channel_obj) ?
      kwargs[:channel_obj]        :
      Wires::Channel[channel_name]
end

#wires_test_setupObject



8
9
10
11
12
13
14
# File 'lib/wires/test.rb', line 8

def wires_test_setup
  @wires_events = []
  @wires_test_fire_hook = \
  Channel.add_hook(:@before_fire) { |e,c| 
    @wires_events << [e,c]
  }
end

#wires_test_teardownObject



16
17
18
19
20
# File 'lib/wires/test.rb', line 16

def wires_test_teardown
  Wires::Launcher.join_children
  @wires_events = nil
  Channel.remove_hook(:@before_fire, &@wires_test_fire_hook)
end