Class: BitGirder::Event::Logger::Testing::EventAccumulator

Inherits:
BitGirderClass
  • Object
show all
Includes:
Testing::AssertMethods
Defined in:
lib/bitgirder/event/logger/testing.rb

Overview

A simple event listener (see BitGirder::EventLogger) which accumulates all events into an unbounded list. Test classses should make sure to remove this listener from its associated engine after assertions are complete, either explicitly or via ensure_removed, lest it continue to amass large amounts of unneeded events.

Constant Summary

Constants included from Core::BitGirderMethods

Core::BitGirderMethods::PARAM_TYPE_ARG, Core::BitGirderMethods::PARAM_TYPE_ENVVAR, Core::BitGirderMethods::PARAM_TYPE_KEY

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Testing::AssertMethods

#assert, #assert_equal, #assert_equal_i, #assert_equal_meth, #assert_equal_s, #assert_false, #assert_match, #assert_nil, #assert_raised, #fail_test, #get_expect_raised_pat

Methods included from Core::BitGirderMethods

argv_to_argh, check_fail_prefix, class_name_to_sym, code, compares_to, console, ext_to_class_name, ext_to_sym, has_env, has_key, has_keys, nonnegative, not_nil, positive, raisef, set_from_key, set_var, split_argv, sym_to_cli_switch, sym_to_ext_id, to_bool, unpack_argv_array, unpack_argv_hash, warn

Class Method Details

.create(*argv) ⇒ Object

Convenience method to create, register, and return an instance which accumulates events from the given EventLogger::Engine



126
127
128
# File 'lib/bitgirder/event/logger/testing.rb', line 126

def self.create( *argv )
    self.new( *argv ).tap { |acc| acc.engine.add_listener( acc ) }
end

.while_accumulating(*argv) ⇒ Object



130
131
132
133
134
# File 'lib/bitgirder/event/logger/testing.rb', line 130

def self.while_accumulating( *argv )

    acc = self.create( *argv )
    acc.ensure_removed { yield( acc ) }
end

Instance Method Details

#assert_logged(expct = nil, &blk) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/bitgirder/event/logger/testing.rb', line 76

def assert_logged( expct = nil, &blk )
    
    if expct
        if blk
            raise "Illegal combination of expect val and block"
        else
            blk = lambda { |ev| ev == expct }
        end
    else
        raise "Block missing" unless blk
    end

    assert events.find( &blk ), "Block did not match any events"
end

#ensure_removedObject



116
117
118
119
120
121
122
# File 'lib/bitgirder/event/logger/testing.rb', line 116

def ensure_removed
    begin
        yield( self )
    ensure
        @engine.remove_listener( self )
    end
end

#event_logged(ev) ⇒ Object



65
66
67
# File 'lib/bitgirder/event/logger/testing.rb', line 65

def event_logged( ev )
    @mut.synchronize { @events << ev }
end

#eventsObject



71
72
73
# File 'lib/bitgirder/event/logger/testing.rb', line 71

def events
    @mut.synchronize { Array.new( @events ) }
end

#impl_initializeObject

Creates a new instance associated with the given engine



57
58
59
60
61
# File 'lib/bitgirder/event/logger/testing.rb', line 57

def impl_initialize
    
    @mut = Mutex.new
    @events = []
end