From b5a14f9c5f3b039205e31e0aaa64a9bbd1cb32ca Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Tue, 7 May 2024 14:32:43 +0200 Subject: [PATCH 1/2] Add test for callback --- lib/graft/callback.rb | 1 + test/graft/callback_test.rb | 84 +++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 test/graft/callback_test.rb diff --git a/lib/graft/callback.rb b/lib/graft/callback.rb index 929b293..9326667 100644 --- a/lib/graft/callback.rb +++ b/lib/graft/callback.rb @@ -4,6 +4,7 @@ module Graft class Callback attr_reader :name + # NOTE: opts is not used in the current implementation def initialize(name = nil, opts = {}, &block) @name = name @opts = opts diff --git a/test/graft/callback_test.rb b/test/graft/callback_test.rb new file mode 100644 index 0000000..a850f5b --- /dev/null +++ b/test/graft/callback_test.rb @@ -0,0 +1,84 @@ +require "minitest/autorun" + +require "graft/callback" + +class TestCallback < Minitest::Test + def test_initialize + callback = Graft::Callback.new + + assert_nil callback.name + end + + def test_initialize_with_name + name = "Tony Stark" + + callback = Graft::Callback.new(name) + + assert_equal name, callback.name + end + + def test_enable_by_default + callback = Graft::Callback.new + + refute_predicate callback, :disabled? + + callback.disable + + assert_predicate callback, :disabled? + + callback.enable + + refute_predicate callback, :disabled? + end + + def invocations + [ + ->(c) { c.call }, # with no args + ->(c) { c.call(:foo) }, # with positional arg + ->(c) { c.call(:foo, :bar) }, # with 2 positional args + ->(c) { c.call({foo: :bar}) }, # with positional arg(hash) + ->(c) { c.call(foo: :bar) }, # with kwargs + ->(c) { c.call { :baz } }, # with block + ->(c) { c.call(:foo) { :baz } }, # with positional arg + block + ->(c) { c.call(:foo, :bar) { :baz } }, # with 2 positional arg + block + ->(c) { c.call({foo: :bar}) { :baz } }, # with positional arg(hash) + block + ->(c) { c.call(foo: :bar) { :baz } } # with kwargs + block + # Add more invocations here... + ] + end + + def test_without_block_call + callback = Graft::Callback.new("name", {}) + + callback.enable + + invocations.each do |i| + assert_raises NoMethodError do + i.call(callback) + end + end + + callback.disable + + invocations.each do |i| + assert_nil i.call(callback) + end + end + + def test_with_block_call + callback = Graft::Callback.new("name", {}) { :wuff } + # NOTE: What is missing? + + callback.enable + + invocations.each do |i, v| + assert_equal :wuff, i.call(callback) + end + + callback.disable + + invocations.each do |i| + assert_nil i.call(callback) + end + end +end From 8ca0b4eb61539943494e07f36fd20384a1bcd945 Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Mon, 13 May 2024 14:44:31 +0200 Subject: [PATCH 2/2] Migrate to `minitest/spec` style --- lib/graft/callback.rb | 14 ++--- test/graft/callback_test.rb | 121 ++++++++++++++++++------------------ 2 files changed, 69 insertions(+), 66 deletions(-) diff --git a/lib/graft/callback.rb b/lib/graft/callback.rb index 9326667..857b5eb 100644 --- a/lib/graft/callback.rb +++ b/lib/graft/callback.rb @@ -9,33 +9,33 @@ def initialize(name = nil, opts = {}, &block) @name = name @opts = opts @block = block - @disabled = false + @enabled = true end if RUBY_VERSION < "3.0" def call(*args, &block) - return if @disabled + return unless enabled? @block.call(*args, &block) end else def call(*args, **kwargs, &block) - return if @disabled + return unless enabled? @block.call(*args, **kwargs, &block) end end def disable - @disabled = true + @enabled = false end def enable - @disabled = false + @enabled = true end - def disabled? - @disabled + def enabled? + @enabled end end end diff --git a/test/graft/callback_test.rb b/test/graft/callback_test.rb index a850f5b..8aeecfd 100644 --- a/test/graft/callback_test.rb +++ b/test/graft/callback_test.rb @@ -1,84 +1,87 @@ +require "minitest/spec" require "minitest/autorun" require "graft/callback" -class TestCallback < Minitest::Test - def test_initialize - callback = Graft::Callback.new +describe Graft::Callback do + describe "initialize" do + it do + callback = Graft::Callback.new - assert_nil callback.name - end - - def test_initialize_with_name - name = "Tony Stark" - - callback = Graft::Callback.new(name) - - assert_equal name, callback.name - end - - def test_enable_by_default - callback = Graft::Callback.new + _(callback.name).must_be_nil + end - refute_predicate callback, :disabled? + it do + name = "Tony Stark" - callback.disable + callback = Graft::Callback.new(name) - assert_predicate callback, :disabled? + _(callback.name).must_be :==, name + end - callback.enable + describe "enabled?" do + it do + callback = Graft::Callback.new - refute_predicate callback, :disabled? - end + _(callback).must_be :enabled? - def invocations - [ - ->(c) { c.call }, # with no args - ->(c) { c.call(:foo) }, # with positional arg - ->(c) { c.call(:foo, :bar) }, # with 2 positional args - ->(c) { c.call({foo: :bar}) }, # with positional arg(hash) - ->(c) { c.call(foo: :bar) }, # with kwargs - ->(c) { c.call { :baz } }, # with block - ->(c) { c.call(:foo) { :baz } }, # with positional arg + block - ->(c) { c.call(:foo, :bar) { :baz } }, # with 2 positional arg + block - ->(c) { c.call({foo: :bar}) { :baz } }, # with positional arg(hash) + block - ->(c) { c.call(foo: :bar) { :baz } } # with kwargs + block - # Add more invocations here... - ] - end + callback.disable - def test_without_block_call - callback = Graft::Callback.new("name", {}) + _(callback).wont_be :enabled? - callback.enable + callback.enable - invocations.each do |i| - assert_raises NoMethodError do - i.call(callback) + _(callback).must_be :enabled? end end - callback.disable - - invocations.each do |i| - assert_nil i.call(callback) - end - end + describe "#call" do + invocations = [ + ->(c) { c.call }, # with no args + ->(c) { c.call(:foo) }, # with positional arg + ->(c) { c.call(:foo, :bar) }, # with 2 positional args + ->(c) { c.call({foo: :bar}) }, # with positional arg(hash) + ->(c) { c.call(foo: :bar) }, # with kwargs + ->(c) { c.call { :baz } }, # with block + ->(c) { c.call(:foo) { :baz } }, # with positional arg + block + ->(c) { c.call(:foo, :bar) { :baz } }, # with 2 positional arg + block + ->(c) { c.call({foo: :bar}) { :baz } }, # with positional arg(hash) + block + ->(c) { c.call(foo: :bar) { :baz } } # with kwargs + block + # Add more invocations here... + ] + + it do + callback = Graft::Callback.new("name", {}) + + callback.enable + + invocations.each do |i| + _ { i.call(callback) }.must_raise NoMethodError + end + + callback.disable + + invocations.each do |i| + _(i.call(callback)).must_be_nil + end + end - def test_with_block_call - callback = Graft::Callback.new("name", {}) { :wuff } - # NOTE: What is missing? + it do + callback = Graft::Callback.new("name", {}) { :wuff } + # NOTE: What is missing? - callback.enable + callback.enable - invocations.each do |i, v| - assert_equal :wuff, i.call(callback) - end + invocations.each do |i, v| + _(i.call(callback)).must_be :==, :wuff + end - callback.disable + callback.disable - invocations.each do |i| - assert_nil i.call(callback) + invocations.each do |i| + _(i.call(callback)).must_be_nil + end + end end end end