From 10e2a940ff1ce574e82a786c943a94717a347847 Mon Sep 17 00:00:00 2001 From: Nick Sutterer Date: Fri, 6 Dec 2024 19:58:13 +0100 Subject: [PATCH] both assertions can wtf. --- lib/trailblazer/test/assertion.rb | 8 ++--- lib/trailblazer/test/assertion/assert_fail.rb | 4 +-- lib/trailblazer/test/assertion/assert_pass.rb | 7 ++-- test/assertion_test.rb | 33 +++++++++++++++++-- 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/lib/trailblazer/test/assertion.rb b/lib/trailblazer/test/assertion.rb index 7c164bf..64f19d6 100644 --- a/lib/trailblazer/test/assertion.rb +++ b/lib/trailblazer/test/assertion.rb @@ -3,14 +3,14 @@ module Test module Assertion # DISCUSS: move to Assertion::Minitest? # Test case instance method. Specific to Minitest. - def assert_pass(activity, options, test: self, assertion: AssertPass, **kws, &block) - assertion.(activity, options, test: test, user_block: block, **kws) # Forward {#assert_pass} to {AssertPass.call} or wherever your implementation sits. + def assert_pass(activity, options, assertion: AssertPass, **kws, &block) + assertion.(activity, options, test: self, user_block: block, **kws) # Forward {#assert_pass} to {AssertPass.call} or wherever your implementation sits. end # DISCUSS: move to Assertion::Minitest? # Test case instance method. Specific to Minitest. - def assert_fail(activity, options, *args, test: self, assertion: AssertFail, **kws, &block) - assertion.(activity, options, *args, test: test, user_block: block, **kws) # Forward {#assert_fail} to {AssertFail.call} or wherever your implementation sits. + def assert_fail(activity, options, *args, assertion: AssertFail, **kws, &block) + assertion.(activity, options, *args, test: self, user_block: block, **kws) # Forward {#assert_fail} to {AssertFail.call} or wherever your implementation sits. end # Evaluate value if it's a lambda, and let the caller know whether we need an diff --git a/lib/trailblazer/test/assertion/assert_fail.rb b/lib/trailblazer/test/assertion/assert_fail.rb index 74c6f7e..12ab6f2 100644 --- a/lib/trailblazer/test/assertion/assert_fail.rb +++ b/lib/trailblazer/test/assertion/assert_fail.rb @@ -7,8 +7,8 @@ module AssertFail extend AssertPass::Utils # {expected_errors} can be nil when using the {#assert_fail} block syntax. - def call(activity, ctx, expected_errors=nil, test:, **kws) - result, ctx, _ = call_operation(ctx, operation: activity) # FIXME: remove kws? + def call(activity, ctx, expected_errors=nil, test:, invoke_method: :call, **kws) + result, ctx, _ = call_operation(ctx, operation: activity, invoke_method: invoke_method) # FIXME: remove kws? assert_fail_with_model(result, ctx, expected_errors: expected_errors, test: test, operation: activity, **kws) end diff --git a/lib/trailblazer/test/assertion/assert_pass.rb b/lib/trailblazer/test/assertion/assert_pass.rb index 1440610..7bc5631 100644 --- a/lib/trailblazer/test/assertion/assert_pass.rb +++ b/lib/trailblazer/test/assertion/assert_pass.rb @@ -4,8 +4,8 @@ module Assertion module AssertPass module_function - def call(activity, ctx, use_wtf: false, model_at: :model, test:, user_block:, **expected_model_attributes) - result, ctx, kws = call_operation(ctx, operation: activity) + def call(activity, ctx, invoke_method: :call, model_at: :model, test:, user_block:, **expected_model_attributes) + result, ctx, kws = call_operation(ctx, operation: activity, invoke_method: invoke_method) assert_pass_with_model(result, ctx, expected_model_attributes: expected_model_attributes, test: test, user_block: user_block, model_at: model_at, operation: activity) end @@ -36,7 +36,8 @@ def error_message_for_assert_pass(result, operation:, **) end module Utils - def call_operation(ctx, operation:, invoke_method: :call, **) + + def call_operation(ctx, operation:, invoke_method:, **) operation.send(invoke_method, ctx) end diff --git a/test/assertion_test.rb b/test/assertion_test.rb index 44207ff..2981726 100644 --- a/test/assertion_test.rb +++ b/test/assertion_test.rb @@ -70,10 +70,11 @@ def validate(ctx, params:, **) Actual: 1>) end - # include Trailblazer::Test::Assertion - # include Trailblazer::Test::Assertion::AssertExposes + include Trailblazer::Test::Assertion + include Trailblazer::Test::Assertion::AssertExposes it "#assert_pass" do - # assert_pass Create, {params: {title: 1}}, id: 1 + # assert_pass Create, {params: {title: 1}}, id: 1, invoke_method: :wtf? + test = Class.new(Test) do include Trailblazer::Test::Assertion @@ -114,6 +115,15 @@ def validate(ctx, params:, **) assert_equal result[:model].class, "Song" # fails end end + + # test_0005_anonymous + it do + stdout, _ = capture_io do + assert_pass Create, {params: {title: "Somewhere Far Beyond"}}, title: "Somewhere Far Beyond", invoke_method: :wtf? + end + + assert_equal stdout, %(`-- AssertionsTest::Create\n |-- \e[32mStart.default\e[0m\n |-- \e[32mmodel\e[0m\n `-- End.success\n) + end end test_1 = test.new(:test_0001_anonymous) @@ -141,6 +151,10 @@ def validate(ctx, params:, **) -AssertionsTest::Record(keyword_init: true) +"Song" >) + + test_5 = test.new(:test_0005_anonymous) + failures = test_5.() + assert_equal failures.size, 0 end include Trailblazer::Test::Assertion @@ -211,6 +225,15 @@ def validate(ctx, params:, **) @_m = true end end + + # test_0009_anonymous + it do + stdout, _ = capture_io do + assert_fail Update, {params: {title: nil}}, [:title], invoke_method: :wtf? + end + + assert_equal stdout, %(`-- AssertionsTest::Update\n |-- \e[32mStart.default\e[0m\n |-- \e[33mvalidate\e[0m\n `-- End.failure\n) + end end test_1 = test.new(:test_0001_anonymous) @@ -260,5 +283,9 @@ def validate(ctx, params:, **) assert_equal failures[0].inspect, %(#[\"is missing\"]}\e[0m. Expected: [:title_XXX] Actual: [:title]>) + + test_9 = test.new(:test_0009_anonymous) + failures = test_9.() + assert_equal failures.size, 0 end end