diff options
author | Vinicius Stock <vinicius.stock@shopify.com> | 2023-09-15 15:42:24 -0400 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2023-09-15 22:36:22 +0000 |
commit | 35960ce65e0300d897033f2ff15191bd60a2c53d (patch) | |
tree | e60b761eff73457afeaac29f57dcfede88b152f9 /test/yarp | |
parent | 89bd1ebcb9cb6befef659eb7056569413347f168 (diff) | |
download | ruby-35960ce65e0300d897033f2ff15191bd60a2c53d.tar.gz |
[ruby/yarp] Add node event dispatcher
This commit changes the node template to create a dispatcher class,
which can be used to walk an AST an emit events to all registered
listeners
https://github.com/ruby/yarp/commit/03a45f85e6
Co-authored-by: Kevin Newton <kddnewton@users.noreply.github.com>
Diffstat (limited to 'test/yarp')
-rw-r--r-- | test/yarp/dispatcher_test.rb | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/test/yarp/dispatcher_test.rb b/test/yarp/dispatcher_test.rb new file mode 100644 index 0000000000..52be2ba32f --- /dev/null +++ b/test/yarp/dispatcher_test.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +require_relative "test_helper" + +module YARP + class DispatcherTest < TestCase + def setup + @listener = Class.new do + attr_reader :events_received + + def initialize + @events_received = [] + end + + def call_node_enter(node) + @events_received << :call_node_enter + end + + def call_node_leave(node) + @events_received << :call_node_leave + end + end.new + end + + def test_dispatching_events + dispatcher = Dispatcher.new + dispatcher.register(@listener, :call_node_enter, :call_node_leave) + + root = YARP.parse(<<~RUBY).value + def foo + something(1, 2, 3) + end + RUBY + + dispatcher.dispatch(root) + + assert_equal([:call_node_enter, :call_node_leave], @listener.events_received) + end + end +end |