Skip to content

Commit

Permalink
both assertions can wtf.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Dec 6, 2024
1 parent 73873b1 commit 10e2a94
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
8 changes: 4 additions & 4 deletions lib/trailblazer/test/assertion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/trailblazer/test/assertion/assert_fail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions lib/trailblazer/test/assertion/assert_pass.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
33 changes: 30 additions & 3 deletions test/assertion_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -260,5 +283,9 @@ def validate(ctx, params:, **)
assert_equal failures[0].inspect, %(#<Minitest::Assertion: Actual contract errors: \e[33m{:title=>[\"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

0 comments on commit 10e2a94

Please sign in to comment.