From 911bb7b54c535bd79f92db60d1a7f0d027ebc4e5 Mon Sep 17 00:00:00 2001 From: Kenta Murata Date: Mon, 24 May 2021 23:21:54 +0900 Subject: [PATCH] Add initialized event Co-authored-by: Yuki INOUE --- lib/iruby/kernel.rb | 7 ++++++- test/iruby/kernel_test.rb | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/iruby/kernel.rb b/lib/iruby/kernel.rb index d05ddfe..d30a2b2 100644 --- a/lib/iruby/kernel.rb +++ b/lib/iruby/kernel.rb @@ -5,7 +5,10 @@ class Kernel RED = "\e[31m" RESET = "\e[0m" - class<< self + @events = EventManager.new([:initialized]) + + class << self + attr_reader :events attr_accessor :instance end @@ -33,6 +36,8 @@ def initialize(config_file, session_adapter_name=nil) @execution_count = 0 @backend = create_backend @running = true + + self.class.events.trigger(:initialized, self) end attr_reader :events diff --git a/test/iruby/kernel_test.rb b/test/iruby/kernel_test.rb index af7e1f5..db317a1 100644 --- a/test/iruby/kernel_test.rb +++ b/test/iruby/kernel_test.rb @@ -8,6 +8,25 @@ def setup @kernel = IRuby::Kernel.instance end + sub_test_case("iruby_initialized event") do + def setup + super + @initialized_kernel = nil + @callback = IRuby::Kernel.events.register(:initialized) do |kernel| + @initialized_kernel = kernel + end + end + + def teardown + IRuby::Kernel.events.unregister(:initialized, @callback) + end + + def test_iruby_initialized_event + with_session_adapter("test") + assert_same(IRuby::Kernel.instance, @initialized_kernel) + end + end + def test_execute_request obj = Object.new