From b4f15b841145502fdd2dc711e4a8ff52233ccb59 Mon Sep 17 00:00:00 2001 From: Nick Sutterer Date: Mon, 9 Dec 2024 15:34:27 +0100 Subject: [PATCH] we can pass `assert_pass ..., model_at:, invoke_method:`. --- lib/trailblazer/test/assertion.rb | 13 +++++++++-- lib/trailblazer/test/assertion/assert_pass.rb | 2 +- lib/trailblazer/test/assertion/suite.rb | 6 +++-- test/assertion_test.rb | 23 ++++++++++++++++++- 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/lib/trailblazer/test/assertion.rb b/lib/trailblazer/test/assertion.rb index 64f19d6..24be29e 100644 --- a/lib/trailblazer/test/assertion.rb +++ b/lib/trailblazer/test/assertion.rb @@ -1,10 +1,19 @@ module Trailblazer module Test + # Top-level entry points for end users. + # These methods expose the syntax sugar, not the logic. module Assertion # DISCUSS: move to Assertion::Minitest? # Test case instance method. Specific to Minitest. - 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. + def assert_pass(activity, options, assertion: AssertPass, model_at: :model, invoke_method: :call, **kws, &block) + # DISCUSS: {:model_at} and {:invoke_method} block actual attributes. + assertion.(activity, options, + test: self, + user_block: block, + expected_model_attributes: kws, + model_at: model_at, + invoke_method: invoke_method, + ) # Forward {#assert_pass} to {AssertPass.call} or wherever your implementation sits. end # DISCUSS: move to Assertion::Minitest? diff --git a/lib/trailblazer/test/assertion/assert_pass.rb b/lib/trailblazer/test/assertion/assert_pass.rb index 7bc5631..a2c39d4 100644 --- a/lib/trailblazer/test/assertion/assert_pass.rb +++ b/lib/trailblazer/test/assertion/assert_pass.rb @@ -4,7 +4,7 @@ module Assertion module AssertPass module_function - def call(activity, ctx, invoke_method: :call, model_at: :model, test:, user_block:, **expected_model_attributes) + 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) diff --git a/lib/trailblazer/test/assertion/suite.rb b/lib/trailblazer/test/assertion/suite.rb index 62a398c..fbb13aa 100644 --- a/lib/trailblazer/test/assertion/suite.rb +++ b/lib/trailblazer/test/assertion/suite.rb @@ -50,14 +50,16 @@ def assert_pass(params_fragment, expected_attributes_to_merge, use_wtf=false, de activity = kws[:operation] # FIXME. model_at = kws[:model_at] # FIXME. + # invoke_method = kws[:invoke_method] if kws.key?(:invoke_method) # FIXME. - assertion.(activity, ctx, use_wtf: use_wtf, test: test, model_at: model_at, user_block: user_block, **expected_attributes) + assertion.(activity, ctx, invoke_method: invoke_method, test: test, model_at: model_at, user_block: user_block, **expected_attributes) end def assert_fail(params_fragment, expected_errors, use_wtf=false, assertion:, **kws) ctx, kws = normalize_for(params_fragment, use_wtf, **kws) activity = kws[:operation] # FIXME. + # invoke_method = kws[:invoke_method] # FIXME. assertion.(activity, ctx, expected_errors, **kws) end @@ -75,7 +77,7 @@ def ctx_for_params_fragment(params_fragment, key_in_params:, default_ctx:, **) # Gather all test case configuration. This involves reading all test `let` directives. def normalize_kws(use_wtf, user_block:, test:, operation: test.operation, expected_attributes: test.expected_attributes, contract_name: "default", model_at: :model, **options) kws = { - user_block: user_block, + # user_block: user_block, operation: operation, expected_attributes: expected_attributes, test: test, diff --git a/test/assertion_test.rb b/test/assertion_test.rb index 2981726..53455a0 100644 --- a/test/assertion_test.rb +++ b/test/assertion_test.rb @@ -73,7 +73,8 @@ def validate(ctx, params:, **) include Trailblazer::Test::Assertion include Trailblazer::Test::Assertion::AssertExposes it "#assert_pass" do - # assert_pass Create, {params: {title: 1}}, id: 1, invoke_method: :wtf? +# FIXME: test that assert_* returns {ctx} +# assert_pass Create, {params: {title: "Somewhere Far Beyond"}}, title: "Somewhere Far Beyond", invoke_method: :wtf? test = Class.new(Test) do @@ -117,6 +118,7 @@ def validate(ctx, params:, **) end # test_0005_anonymous + # We accept {:invoke_method} as a first level kw arg, currently. it do stdout, _ = capture_io do assert_pass Create, {params: {title: "Somewhere Far Beyond"}}, title: "Somewhere Far Beyond", invoke_method: :wtf? @@ -124,6 +126,21 @@ def validate(ctx, params:, **) assert_equal stdout, %(`-- AssertionsTest::Create\n |-- \e[32mStart.default\e[0m\n |-- \e[32mmodel\e[0m\n `-- End.success\n) end + + # test_0006_anonymous + # We accept {:model_at} as a first level kw arg, currently. + it do + create = Class.new(Trailblazer::Operation) do + step :model + + def model(ctx, params:, **) + ctx[:song] = Record.new(**params) + end + end + + assert_pass create, {params: {title: "Somewhere Far Beyond"}}, title: "Somewhere Far Beyond", model_at: :song + # assert_pass Create, {params: {title: "Somewhere Far Beyond"}}, {invoke_method: :wtf?, model_at: }, {...} # DISCUSS: this would be an alternative syntax. + end end test_1 = test.new(:test_0001_anonymous) @@ -155,6 +172,10 @@ def validate(ctx, params:, **) test_5 = test.new(:test_0005_anonymous) failures = test_5.() assert_equal failures.size, 0 + + test_6 = test.new(:test_0006_anonymous) + failures = test_6.() + assert_equal failures.size, 0 end include Trailblazer::Test::Assertion