spec/hooks_spec.rb in puppet-debugger-0.19.0 vs spec/hooks_spec.rb in puppet-debugger-1.0.0

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: true + require_relative 'spec_helper' describe PuppetDebugger::Hooks do before do @hooks = PuppetDebugger::Hooks.new @@ -10,12 +12,12 @@ end let(:debugger) do PuppetDebugger::Cli.new(out_buffer: output) end - - describe "adding a new hook" do + + describe 'adding a new hook' do it 'should not execute hook while adding it' do run = false @hooks.add_hook(:test_hook, :my_name) { run = true } expect(run).to eq false end @@ -25,16 +27,16 @@ expect { @hooks.add_hook(:test_hook, :my_name) {} }.to raise_error ArgumentError end it 'should create a new hook with a block' do - @hooks.add_hook(:test_hook, :my_name) { } + @hooks.add_hook(:test_hook, :my_name) {} expect(@hooks.hook_count(:test_hook)).to eq 1 end it 'should create a new hook with a callable' do - @hooks.add_hook(:test_hook, :my_name, proc { }) + @hooks.add_hook(:test_hook, :my_name, proc {}) expect(@hooks.hook_count(:test_hook)).to eq 1 end it 'should use block if given both block and callable' do run = false @@ -59,12 +61,12 @@ it 'should return a count of 0 for an empty hook' do expect(@hooks.hook_count(:test_hook)).to eq 0 end end - describe "PuppetDebugger::Hooks#merge" do - describe "merge!" do + describe 'PuppetDebugger::Hooks#merge' do + describe 'merge!' do it 'should merge in the PuppetDebugger::Hooks' do h1 = PuppetDebugger::Hooks.new.add_hook(:test_hook, :testing) {} h2 = PuppetDebugger::Hooks.new h2.merge!(h1) @@ -99,26 +101,23 @@ expect(h2.get_hook(:test_hook, :testing)).to eq callable1 expect(h2.hook_count(:test_hook)).to eq 1 end it 'should preserve hook order' do - name = "" + test_hook_name = String.new h1 = PuppetDebugger::Hooks.new - h1.add_hook(:test_hook, :testing3) { name << "h" } - h1.add_hook(:test_hook, :testing4) { name << "n" } + h1.add_hook(:test_hook, :testing3) { test_hook_name << 'h' } + h1.add_hook(:test_hook, :testing4) { test_hook_name << 'n' } h2 = PuppetDebugger::Hooks.new - h2.add_hook(:test_hook, :testing1) { name << "j" } - h2.add_hook(:test_hook, :testing2) { name << "o" } - + h2.add_hook(:test_hook, :testing1) { test_hook_name << 'j' } + h2.add_hook(:test_hook, :testing2) { test_hook_name << 'o' } h2.merge!(h1) - h2.exec_hook(:test_hook) - - expect(name).to eq "john" + expect(h2.exec_hook(:test_hook)).to eq 'john' end - describe "merge" do + describe 'merge' do it 'should return a fresh, independent instance' do h1 = PuppetDebugger::Hooks.new.add_hook(:test_hook, :testing) {} h2 = PuppetDebugger::Hooks.new h3 = h2.merge(h1) @@ -144,15 +143,14 @@ expect(h1.get_hook(:test_hook3, :testing)).to eq nil expect(h2.get_hook(:test_hook3, :testing)).to eq nil end end - end end - describe "dupping a PuppetDebugger::Hooks instance" do + describe 'dupping a PuppetDebugger::Hooks instance' do it 'should share hooks with original' do @hooks.add_hook(:test_hook, :testing) do :none_such end @@ -175,15 +173,14 @@ hooks_dup.add_hook(:test_hook, :testing2) { :okay_man } expect(hooks_dup.get_hook(:test_hook, :testing2)).not_to eq @hooks.get_hook(:test_hook, :testing2) end - end - describe "getting hooks" do - describe "get_hook" do + describe 'getting hooks' do + describe 'get_hook' do it 'should return the correct requested hook' do run = false fun = false @hooks.add_hook(:test_hook, :my_name) { run = true } @hooks.add_hook(:test_hook, :my_name2) { fun = true } @@ -195,11 +192,11 @@ it 'should return nil if hook does not exist' do expect(@hooks.get_hook(:test_hook, :my_name)).to eq nil end end - describe "get_hooks" do + describe 'get_hooks' do it 'should return a hash of hook names/hook functions for an event' do hook1 = proc { 1 } hook2 = proc { 2 } @hooks.add_hook(:test_hook, :my_name1, hook1) @hooks.add_hook(:test_hook, :my_name2, hook2) @@ -213,21 +210,21 @@ expect(@hooks.get_hooks(:test_hook)).to eq({}) end end end - describe "clearing all hooks for an event" do + describe 'clearing all hooks for an event' do it 'should clear all hooks' do - @hooks.add_hook(:test_hook, :my_name) { } - @hooks.add_hook(:test_hook, :my_name2) { } - @hooks.add_hook(:test_hook, :my_name3) { } + @hooks.add_hook(:test_hook, :my_name) {} + @hooks.add_hook(:test_hook, :my_name2) {} + @hooks.add_hook(:test_hook, :my_name3) {} @hooks.clear_event_hooks(:test_hook) expect(@hooks.hook_count(:test_hook)).to eq 0 end end - describe "deleting a hook" do + describe 'deleting a hook' do it 'should successfully delete a hook' do @hooks.add_hook(:test_hook, :my_name) {} @hooks.delete_hook(:test_hook, :my_name) expect(@hooks.hook_count(:test_hook)).to eq 0 end @@ -242,11 +239,11 @@ it 'should return nil if hook does not exist' do expect(@hooks.delete_hook(:test_hook, :my_name)).to eq nil end end - describe "executing a hook" do + describe 'executing a hook' do it 'should execute block hook' do run = false @hooks.add_hook(:test_hook, :my_name) { run = true } @hooks.exec_hook(:test_hook) expect(run).to eq true @@ -262,11 +259,13 @@ it 'should execute a general callable hook' do callable = Object.new.tap do |obj| obj.instance_variable_set(:@test_var, nil) class << obj attr_accessor :test_var - def call() @test_var = true; end + def call + @test_var = true + end end end @hooks.add_hook(:test_hook, :my_name, callable) @hooks.exec_hook(:test_hook) @@ -302,30 +301,30 @@ it 'should add exceptions to the errors array' do @hooks.add_hook(:test_hook, :foo1) { raise 'one' } @hooks.add_hook(:test_hook, :foo2) { raise 'two' } @hooks.add_hook(:test_hook, :foo3) { raise 'three' } @hooks.exec_hook(:test_hook) - expect(@hooks.errors.map(&:message)).to eq ['one', 'two', 'three'] + expect(@hooks.errors.map(&:message)).to eq %w[one two three] end it 'should return the last exception raised as the return value' do @hooks.add_hook(:test_hook, :foo1) { raise 'one' } @hooks.add_hook(:test_hook, :foo2) { raise 'two' } @hooks.add_hook(:test_hook, :foo3) { raise 'three' } expect(@hooks.exec_hook(:test_hook)).to eq @hooks.errors.last end end - describe "anonymous hooks" do + describe 'anonymous hooks' do it 'should allow adding of hook without a name' do @hooks.add_hook(:test_hook, nil) {} expect(@hooks.hook_count(:test_hook)).to eq 1 end it 'should only allow one anonymous hook to exist' do - @hooks.add_hook(:test_hook, nil) { } - @hooks.add_hook(:test_hook, nil) { } + @hooks.add_hook(:test_hook, nil) {} + @hooks.add_hook(:test_hook, nil) {} expect(@hooks.hook_count(:test_hook)).to eq 1 end it 'should execute most recently added anonymous hook' do x = nil @@ -335,7 +334,6 @@ @hooks.exec_hook(:test_hook) expect(y).to eq nil expect(x).to eq 2 end end - -end \ No newline at end of file +end