Skip to content

Commit

Permalink
assert_fail can now deal with a block.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Dec 6, 2024
1 parent 1886874 commit 43db471
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 49 deletions.
16 changes: 9 additions & 7 deletions lib/trailblazer/test/assertion/assert_fail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,26 @@ module AssertFail

extend AssertPass::Utils

def call(activity, ctx, expected_errors, use_wtf=false, test:, **kws, &block)
# {expected_errors} can be nil when using the {#assert_fail} block syntax.
def call(activity, ctx, expected_errors=nil, test:, **kws, &block)
result, ctx, _ = call_operation(ctx, operation: activity) # FIXME: remove kws?

assert_fail_with_model(result, ctx, expected_errors: expected_errors, test: test, user_block: block, operation: activity)
end

# @private
def assert_fail_with_model(result, ctx, test:, **options, &block)
def assert_fail_with_model(result, ctx, test:, **options)
assert_after_call(result, **options) do |result|

test.assert_equal *arguments_for_assert_fail(result), error_message_for_assert_fail_after_call(result, **options)

if options[:expected_errors]
# TODO: allow error messages from somewhere else.
# only test _if_ errors are present, not the content.
colored_errors = colored_errors_for(result)

# TODO: allow error messages from somewhere else.
# only test _if_ errors are present, not the content.
colored_errors = colored_errors_for(result)

test.assert_equal *arguments_for_assert_contract_errors(result, contract_name: :default, **options), "Actual contract errors: #{colored_errors}"
test.assert_equal *arguments_for_assert_contract_errors(result, contract_name: :default, **options), "Actual contract errors: #{colored_errors}"
end
end
end

Expand Down
3 changes: 2 additions & 1 deletion lib/trailblazer/test/assertion/assert_pass.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def call_operation(ctx, operation:, invoke_method: :call, **)
# @private
def assert_after_call(result, user_block: raise, **kws)
yield(result)

user_block.call(result) if user_block

result
Expand All @@ -59,7 +60,7 @@ def colored_errors_for(result)

colored_errors = %{\e[33m#{errors}\e[0m}
end
end
end # Utils

extend Utils
end
Expand Down
38 changes: 0 additions & 38 deletions lib/trailblazer/test/operation/assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,7 @@ def expected_attributes_for(expected_attributes_to_merge, expected_attributes:,
# operation.send(invoke_method, ctx)
# end

# @private
class CtxHash < Hash
include Hashie::Extensions::DeepMerge
end

# @private
def merge_for(dest, source, deep_merge)
return dest.merge(source) unless deep_merge

CtxHash[dest].deep_merge(CtxHash[source]) # FIXME: this subclasses ActiveRecord classes in dest like {class: ReportSubscription}
end

# @private
# def colored_errors_for(result)
Expand All @@ -204,35 +194,7 @@ def merge_for(dest, source, deep_merge)
# result[model_at]
# end

def Ctx(merge_with_ctx={}, exclude: false, merge: true, **kws)
if merge
options = normalize_kws_for_ctx(**kws)
key_in_params = options[:key_in_params]
default_ctx = options[:default_ctx]

# Extract {:params} from {default_ctx}
default_params = key_in_params ? default_ctx[:params][key_in_params] : default_ctx[:params]

# Remove {:exclude} variables from the {params:} part
filtered_default_params =
if exclude
default_params.slice(*(default_params.keys - exclude))
else
default_params # use original params if no filtering configured.
end

# FIXME: very, very redundant.
default_params_for_ctx = key_in_params ? {key_in_params => filtered_default_params} : filtered_default_params

ctx = default_ctx.merge({params: default_params_for_ctx})
else # FIXME: if/else here sucks.
ctx = {}
end

ctx = Assert.merge_for(ctx, merge_with_ctx, true) # merge injections

Trailblazer::Test::Context[ctx] # this signals "pass-through"
end
end # Assert

# @private
Expand Down
35 changes: 32 additions & 3 deletions test/assertion_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ def validate(ctx, params:, **)

include Trailblazer::Test::Assertion
it "#assert_fail" do
assert_fail Update, {params: {bla: 1}}, [:title]
# assert_fail Update, {params: {bla: 1}}, [:title]
# assert_fail Update, {params: {bla: 1}} do |result|
# end

test =
Class.new(Test) do
Expand All @@ -166,7 +168,6 @@ def validate(ctx, params:, **)
{title: ["is missing"]}
end


# test_0003_anonymous
it do
assert_fail Update, {params: {record: true}}, [:title]
Expand All @@ -178,6 +179,21 @@ def validate(ctx, params:, **)
# expected:
{title: ["is XXX"]} # this is wrong.
end

# test_0005_anonymous
it do
assert_fail Update, {params: {title: nil}} do |result|
@_m = true
assert_equal result[:"contract.default"].errors.messages, {:title=>["is missing"]}
end
end

# test_0006_anonymous
it do
assert_fail Update, {params: {record: true}} do |result| # this actually passes.
@_m = true
end
end
end

test_1 = test.new(:test_0001_anonymous)
Expand All @@ -195,11 +211,24 @@ def validate(ctx, params:, **)
Expected: false
Actual: true>)

test_4 = test.new(:test_0004_anonymous)
test_4 = test.new(:test_0004_anonymous)
failures = test_4.()
assert_equal failures.size, 1
failures[0].inspect.must_equal %(#<Minitest::Assertion: Actual contract errors: \e[33m{:title=>[\"is missing\"]}\e[0m.
Expected: {:title=>[\"is XXX\"]}
Actual: {:title=>[\"is missing\"]}>)

test_5 = test.new(:test_0005_anonymous)
failures = test_5.()
assert_equal test_5.instance_variable_get(:@_m), true
assert_equal failures.size, 0

test_6 = test.new(:test_0006_anonymous)
failures = test_6.()
assert_nil test_6.instance_variable_get(:@_m) # block is not executed.
assert_equal failures.size, 1
failures[0].inspect.must_equal %(#<Minitest::Assertion: {AssertionsTest::Update} didn't fail, it passed.
Expected: false
Actual: true>)
end
end

0 comments on commit 43db471

Please sign in to comment.