Skip to content

Commit

Permalink
Add test for callback
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyCTHsu committed May 7, 2024
1 parent 9e66eca commit 6b27741
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/graft/callback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
84 changes: 84 additions & 0 deletions test/graft/callback_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6b27741

Please sign in to comment.