Skip to content

Commit

Permalink
we can pass assert_pass ..., model_at:, invoke_method:.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Dec 9, 2024
1 parent 10e2a94 commit b4f15b8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
13 changes: 11 additions & 2 deletions lib/trailblazer/test/assertion.rb
Original file line number Diff line number Diff line change
@@ -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?
Expand Down
2 changes: 1 addition & 1 deletion lib/trailblazer/test/assertion/assert_pass.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions lib/trailblazer/test/assertion/suite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
23 changes: 22 additions & 1 deletion test/assertion_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -117,13 +118,29 @@ 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?
end

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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b4f15b8

Please sign in to comment.