Skip to content

Commit

Permalink
introduce Assertion.module! as a builder, includes the correct module…
Browse files Browse the repository at this point in the history
…s for you.
  • Loading branch information
apotonick committed Dec 22, 2024
1 parent 7f973f0 commit 4f3f0e7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 52 deletions.
61 changes: 37 additions & 24 deletions lib/trailblazer/test/assertion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@ module Test
# Top-level entry points for end users.
# These methods expose the end user syntax, not the logic.
module Assertion
def self.module!(receiver, activity: false, suite: false)
modules =
if activity && suite
[AssertExposes, Suite, Assertion::Activity]
elsif activity
[AssertExposes, Assertion, Assertion::Activity]
elsif suite # operation
[AssertExposes, Suite]
else
[AssertExposes, Assertion]
end

receiver.include(*modules.reverse)
end

SUCCESS_TERMINI = [:success, :pass_fast] # DISCUSS: where should this be defined?

# @private
Expand All @@ -21,6 +36,17 @@ def self.invoke_operation_with_wtf(operation, ctx)
return result.terminus, result
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)
value.is_a?(Proc) ? [value.(actual: actual, asserted: asserted), false] : [value, true]
end

# # Read the actual value from the asserted object (e.g. a model).
def self.actual(asserted, reader, name)
reader ? asserted.public_send(reader, name) : asserted.public_send(name)
end

# DISCUSS: move to Assertion::Minitest?
# Test case instance method. Specific to Minitest.
def assert_pass(activity, options, invoke: Assertion.method(:invoke_operation), model_at: :model, **kws, &block)
Expand Down Expand Up @@ -48,17 +74,6 @@ def assert_fail?(*args, **options, &block)
assert_fail(*args, **options, invoke: Assertion.method(:invoke_operation_with_wtf), &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)
value.is_a?(Proc) ? [value.(actual: actual, asserted: asserted), false] : [value, true]
end

# # Read the actual value from the asserted object (e.g. a model).
def self.actual(asserted, reader, name)
reader ? asserted.public_send(reader, name) : asserted.public_send(name)
end

# Assertions for Activity, not for Operation.
module Activity
def self.invoke_activity(activity, ctx)
Expand All @@ -73,22 +88,20 @@ def self.invoke_activity_with_tracing(activity, ctx)
return signal, ctx
end

module Assert
def assert_pass(*args, invoke: Activity.method(:invoke_activity), **options, &block)
super(*args, **options, invoke: invoke, &block)
end
def assert_pass(*args, invoke: Activity.method(:invoke_activity), **options, &block)
super(*args, **options, invoke: invoke, &block)
end

def assert_fail(*args, invoke: Activity.method(:invoke_activity), **options, &block)
super(*args, **options, invoke: invoke, &block)
end
def assert_fail(*args, invoke: Activity.method(:invoke_activity), **options, &block)
super(*args, **options, invoke: invoke, &block)
end

def assert_pass?(*args, **options, &block)
assert_pass(*args, **options, invoke: Activity.method(:invoke_activity_with_tracing), &block)
end
def assert_pass?(*args, **options, &block)
assert_pass(*args, **options, invoke: Activity.method(:invoke_activity_with_tracing), &block)
end

def assert_fail?(*args, **options, &block)
assert_fail(*args, **options, invoke: Activity.method(:invoke_activity_with_tracing), &block)
end
def assert_fail?(*args, **options, &block)
assert_fail(*args, **options, invoke: Activity.method(:invoke_activity_with_tracing), &block)
end
end
end
Expand Down
6 changes: 2 additions & 4 deletions lib/trailblazer/test/assertion/suite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ def self.included(includer)
includer.let(:key_in_params) { false }
includer.let(:expected_attributes) { {} } # You need to override this in your tests.
includer.let(:default_ctx) { {} }

include AssertExposes
end

def assert_pass(params_fragment, expected_attributes_to_merge={}, assertion: AssertPass, **options, &block)
Assert.assert_pass(params_fragment, test: self, user_block: block, assertion: assertion, expected_attributes_to_merge: expected_attributes_to_merge, **options)
def assert_pass(params_fragment, expected_attributes_to_merge={}, assertion: AssertPass, **kws, &block)
Assert.assert_pass(params_fragment, test: self, user_block: block, assertion: assertion, expected_attributes_to_merge: expected_attributes_to_merge, **kws)
end

def assert_fail(params_fragment, expected_errors, assertion: AssertFail, **kws, &block)
Expand Down
27 changes: 8 additions & 19 deletions test/assertion_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ def call
run
@failures
end

# include Trailblazer::Test::Assertions
end

# Create = Trailblazer::Test::Testing::Song::Operation::Create
class Create < Trailblazer::Operation
step :model

Expand All @@ -37,7 +34,6 @@ def validate(ctx, params:, **)
test =
Class.new(Test) do
include Trailblazer::Test::Assertion::AssertExposes
# include Trailblazer::Test::Operation::Assertions

# test_0001_anonymous
it do
Expand Down Expand Up @@ -70,16 +66,14 @@ def validate(ctx, params:, **)
Actual: 1>)
end

include Trailblazer::Test::Assertion
include Trailblazer::Test::Assertion::AssertExposes
Trailblazer::Test::Assertion.module!(self)
it "#assert_pass" do
# FIXME: test that assert_* returns {ctx}
assert_pass? Create, {params: {title: "Somewhere Far Beyond"}}, title: "Somewhere Far Beyond"

test =
Class.new(Test) do
include Trailblazer::Test::Assertion
include Trailblazer::Test::Assertion::AssertExposes
Trailblazer::Test::Assertion.module!(self)

# test_0001_anonymous
it do
Expand Down Expand Up @@ -218,7 +212,7 @@ def model(ctx, params:, **)

test =
Class.new(Test) do
include Trailblazer::Test::Assertion
Trailblazer::Test::Assertion.module!(self)

# test_0001_anonymous
it do
Expand Down Expand Up @@ -383,6 +377,8 @@ def model(ctx, params:, **)
end

class AssertionActivityTest < Minitest::Spec
Trailblazer::Test::Assertion.module!(self, activity: true)

Record = AssertionsTest::Record

class Create < Trailblazer::Activity::FastTrack
Expand All @@ -401,10 +397,6 @@ def model(ctx, params:, **)
end
end

include Trailblazer::Test::Assertion
include Trailblazer::Test::Assertion::Activity::Assert
include Trailblazer::Test::Assertion::AssertExposes

it do
assert_pass Create, {params: {title: "Roxanne"}},
title: "Roxanne"
Expand All @@ -416,14 +408,11 @@ def model(ctx, params:, **)
end

# Test with the Assertion::Suite "DSL" module.
class AssertionActivitySuiteTest < Minitest::Spec
class SuiteWithActivityTest < Minitest::Spec
Trailblazer::Test::Assertion.module!(self, activity: true, suite: true)
Record = AssertionsTest::Record
Create = AssertionActivityTest::Create

include Trailblazer::Test::Assertion::Suite
include Trailblazer::Test::Assertion::Activity::Assert
include Trailblazer::Test::Assertion::AssertExposes

let(:operation) { Create }

it do
Expand Down Expand Up @@ -459,7 +448,7 @@ class EndpointWithActivityTest < Minitest::Spec
Create = AssertionActivityTest::Create

include Trailblazer::Test::Assertion
include Trailblazer::Test::Assertion::Activity::Assert
include Trailblazer::Test::Assertion::Activity
include Trailblazer::Test::Assertion::AssertExposes

def self.__(activity, options, **kws, &block) # TODO: move this to endpoint.
Expand Down
7 changes: 2 additions & 5 deletions test/docs/suite_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
# ...

class OperationSpec < Minitest::Spec
# include Trailblazer::Test::Assertions
include Trailblazer::Test::Assertion::Suite
Trailblazer::Test::Assertion.module!(self, suite: true)
end
#:operation-spec end

Expand Down Expand Up @@ -380,14 +379,12 @@ def call
@failures
end

include Trailblazer::Test::Assertion::Suite
Trailblazer::Test::Assertion.module!(self, suite: true)
end

it "gives colored error messages for {assert_pass} and {assert_fail}" do
test =
Class.new(Test) {
include Trailblazer::Test::Assertion::Suite

# What are we passing into the operation?
let(:default_ctx) do
{
Expand Down

0 comments on commit 4f3f0e7

Please sign in to comment.