# frozen_string_literal: true require_relative "helper" class PluginTest < Minitest::Test def setup @app = new_app end def test_class_methods mod = Module.new do def foo "foo" end end plugin = Module.new plugin.const_set(:ClassMethods, mod) @app.plugin(plugin) assert_equal "foo", @app.foo end def test_instance_methods mod = Module.new do def bar "bar" end end plugin = Module.new plugin.const_set(:InstanceMethods, mod) @app.plugin(plugin) assert_equal "bar", @app.new(proc {}).bar end def test_setup plugin = Module.new def plugin.run? @run end def plugin.setup(_) @run = true end @app.plugin(plugin) assert_equal true, plugin.run? end def test_setup_passes_app plugin = Module.new def plugin.app @__app end def plugin.setup(app) @__app = app end @app.plugin(plugin) assert_equal @app, plugin.app end def test_setup_with_arguments plugin = Module.new def plugin.args @args end def plugin.setup(_, foo, bar) @args = [foo, bar] end @app.plugin(plugin, "foo", "bar") assert_equal %w(foo bar), plugin.args end def test_setup_with_keyword_arguments plugin = Module.new def plugin.args @args end def plugin.setup(_, foo:, bar: "bar") @args = [foo, bar] end @app.plugin(plugin, foo: "foo") assert_equal %w(foo bar), plugin.args end def test_setup_with_block plugin = Module.new def plugin.block @block end def plugin.setup(_, &block) @block = block end @app.plugin(plugin) do "foo" end assert_equal "foo", plugin.block.call end end