Skip to content

Commit

Permalink
add Assertion#assert_pass? and friends.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Dec 19, 2024
1 parent 821a870 commit ca3fe01
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/trailblazer/test/assertion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ def assert_fail(activity, options, *args, assertion: AssertFail, invoke: Asserti
assertion.(activity, options, *args, test: self, user_block: block, invoke: invoke, **kws) # Forward {#assert_fail} to {AssertFail.call} or wherever your implementation sits.
end

def assert_pass?(*args, **options, &block)
assert_pass(*args, **options, invoke: Assertion::Wtf.method(:invoke_activity), &block)
end

def assert_fail?(*args, **options, &block)
assert_fail(*args, **options, invoke: Assertion::Wtf.method(:invoke_activity), &block)
end

# Evaluate value if it's a lambda, and let the caller know whether we need an
# assert_equal or an assert.
def self.expected(asserted, value, actual)
Expand Down
35 changes: 34 additions & 1 deletion test/assertion_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def validate(ctx, params:, **)
include Trailblazer::Test::Assertion::AssertExposes
it "#assert_pass" do
# FIXME: test that assert_* returns {ctx}
# assert_pass Create, {params: {title: "Somewhere Far Beyond"}}, title: "Somewhere Far Beyond", invoke: Trailblazer::Test::Assertion::Wtf.method(:invoke_activity)
assert_pass? Create, {params: {title: "Somewhere Far Beyond"}}, title: "Somewhere Far Beyond"

test =
Class.new(Test) do
Expand Down Expand Up @@ -149,6 +149,22 @@ def model(ctx, params:, **)

assert_equal result[:model].title, "Somewhere Far Beyond"
end

# test_0008_anonymous
# {#assert_pass?}
it do
out, _ = capture_io do
result = assert_pass? Create, {params: {title: "Somewhere Far Beyond"}}, title: "Somewhere Far Beyond"

assert_equal result[:model].title, "Somewhere Far Beyond"
end

assert_equal out, %(AssertionsTest::Create
|-- \e[32mStart.default\e[0m
|-- \e[32mmodel\e[0m
`-- End.success
)
end
end

test_1 = test.new(:test_0001_anonymous)
Expand Down Expand Up @@ -188,6 +204,10 @@ def model(ctx, params:, **)
test_7 = test.new(:test_0007_anonymous)
failures = test_7.()
assert_equal failures.size, 0

test_8 = test.new(:test_0008_anonymous)
failures = test_8.()
assert_equal failures.size, 0
end

# include Trailblazer::Test::Assertion
Expand Down Expand Up @@ -285,6 +305,15 @@ def model(ctx, params:, **)

assert_equal result.keys.inspect, %([:params])
end

# test_0012_anonymous
it do
stdout, _ = capture_io do
assert_fail? Update, {params: {title: nil}}, [:title]
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 @@ -346,6 +375,10 @@ def model(ctx, params:, **)
test_11 = test.new(:test_0011_anonymous)
failures = test_11.()
assert_equal failures.size, 0

test_12 = test.new(:test_0012_anonymous)
failures = test_12.()
assert_equal failures.size, 0
end
end

Expand Down

0 comments on commit ca3fe01

Please sign in to comment.