diff --git a/test/docs/autogenerated/mechanics_test.rb b/test/docs/autogenerated/mechanics_test.rb new file mode 100644 index 0000000..c7d0747 --- /dev/null +++ b/test/docs/autogenerated/mechanics_test.rb @@ -0,0 +1,368 @@ +require "test_helper" +# THIS FILE IS AUTOGENERATED FROM trailblazer-activity-dsl-linear/test/docs/mechanics_test.rb + +module Y + class DocsMechanicsTest < Minitest::Spec + Memo = Module.new + it "what" do + #:instance-method + module Memo::Operation + class Create < Trailblazer::Operation + step :validate + + #~meths + def validate(ctx, params:, **) + params.key?(:memo) ? true : false # return value matters! + end + #~meths end + end + end + #:instance-method end + + #:instance-method-call + result = Memo::Operation::Create.call(params: {memo: nil}) + #:instance-method-call end + assert_equal result.success?, true + + #:instance-method-implicit-call + result = Memo::Operation::Create.(params: {memo: nil}) + # #:instance-method-implicit-call end + assert_equal result.success?, true + end + end +end + +class ReadfromCtx_DocsMechanicsTest < Minitest::Spec + Memo = Module.new + it "what" do + #:ctx-read + module Memo::Operation + class Create < Trailblazer::Operation + step :validate + #~meths + step :save + + def save(*); true; end + #~meths end + def validate(ctx, **) + p ctx[:params] #=> {:memo=>nil} + end + end + end + #:ctx-read end + + #:ctx-read-call + result = Memo::Operation::Create.(params: {memo: nil}) + #:ctx-read-call end + assert_equal result.success?, true + end +end + +class ReadfromCtxKwargs_DocsMechanicsTest < Minitest::Spec + Memo = Module.new + it "what" do + module Memo::Operation + class Create < Trailblazer::Operation + step :validate + #~meths + step :save + + def save(*); true; end + #~meths end + #:ctx-read-kwargs + def validate(ctx, params:, **) + p params #=> {:memo=>nil} + end + #:ctx-read-kwargs end + end + end + + result = Memo::Operation::Create.(params: {memo: nil}) + assert_equal result.success?, true + + user = Object + assert_raises ArgumentError do + #:kwargs-error + result = Memo::Operation::Create.(current_user: user) + #=> ArgumentError: missing keyword: :params + # memo/operation/create.rb:9:in `validate' + #:kwargs-error end + end + end +end + +class WriteToCtx_DocsMechanicsTest < Minitest::Spec + class Memo + def initialize(*); end + end + it "what" do + #:ctx-write-read + module Memo::Operation + class Create < Trailblazer::Operation + step :validate + step :save # sets ctx[:model] + step :notify + #~body + #~meths + def validate(ctx, params:, **) + true + end + + def send_email(*) + true + end + #~meths end + #:ctx-write + def save(ctx, params:, **) + ctx[:model] = Memo.new(params[:memo]) + end + #~body end + #:ctx-write end + def notify(ctx, model:, **) + send_email(model) + end + end + end + #:ctx-write-read end + + #:result-read + result = Memo::Operation::Create.(params: {memo: {content: "remember that"}}) + + result[:model] #=> # + #:result-read end + + #:result-success + puts result.success? #=> true + #:result-success end + + assert_equal result[:model].class, Memo + assert_equal result.success?, true + + user = Object + assert_raises ArgumentError do + #:kwargs-error + result = Memo::Operation::Create.(current_user: user) + #=> ArgumentError: missing keyword: :params + # memo/operation/create.rb:9:in `validate' + #:kwargs-error end + end + end +end + +class ReturnValueSuccess_DocsMechanicsTest < Minitest::Spec + Memo = Module.new + it "what" do + module Memo::Operation + class Create < Trailblazer::Operation + step :validate + #~meths + step :save + + def save(*); true; end + #~meths end + #:return-success + def validate(ctx, params:, **) + params.key?(:memo) # => true/false + end + #:return-success end + end + end + + result = Memo::Operation::Create.(params: {memo: nil}) + assert_equal result.success?, true + end +end + +class ReturnValueFailure_DocsMechanicsTest < Minitest::Spec + Memo = Module.new + it "what" do + module Memo::Operation + class Create < Trailblazer::Operation + step :validate + #~meths + step :save + + def save(*); true; end + #~meths end + #:return-failure + def validate(ctx, params:, **) + nil + end + #:return-failure end + end + end + + result = Memo::Operation::Create.(params: {memo: nil}) + assert_equal result.success?, false + end +end + +class ReturnSignal_DocsMechanicsTest < Minitest::Spec + Memo = Module.new + it "what" do + #:signal-operation + module Memo::Operation + class Create < Trailblazer::Operation + class NetworkError < Trailblazer::Activity::Signal + end + #~meths + #:signal-steps + step :validate + step :save + left :handle_errors + step :notify, + Output(NetworkError, :network_error) => End(:network_error) + #:signal-steps end + def save(ctx, **) + ctx[:model] = Object + end + def validate(ctx, params:, **) + true + end + def send_email(model) + true + end + def check_network(params) + ! params[:network_broken] + end + + #:return-signal + def notify(ctx, model:, params:, **) + return NetworkError unless check_network(params) + + send_email(model) + end + #:return-signal end + #~meths end + end + end + #:signal-operation end + + result = Memo::Operation::Create.(params: {memo: nil, network_broken: false}) + assert_equal result.success?, true + + result = Memo::Operation::Create.(params: {memo: nil, network_broken: true}) + assert_equal result.success?, false + assert_equal result.terminus.inspect, %(#) + end +end + +class Classmethod_DocsMechanicsTest < Minitest::Spec + Memo = Module.new + it "what" do + #:class-method + module Memo::Operation + class Create < Trailblazer::Operation + #~meths + # Define {Memo::Operation::Create.validate} + def self.validate(ctx, params:, **) + params.key?(:memo) ? true : false # return value matters! + end + #~meths end + + step method(:validate) + end + end + #:class-method end + end +end + +class Module_Classmethod_DocsMechanicsTest < Minitest::Spec + Memo = Module.new + it "what" do + #:module-step + # Reusable steps in a module. + module Steps + def self.validate(ctx, params:, **) + params.key?(:memo) ? true : false # return value matters! + end + end + #:module-step end + + #:module-method + module Memo::Operation + class Create < Trailblazer::Operation + step Steps.method(:validate) + end + end + #:module-method end + end +end + +class Callable_DocsMechanicsTest < Minitest::Spec + Memo = Module.new + it "what" do + #:callable-step + module Validate + def self.call(ctx, params:, **) + valid?(params) ? true : false # return value matters! + end + + def valid?(params) + params.key?(:memo) + end + end + #:callable-step end + + #:callable-method + module Memo::Operation + class Create < Trailblazer::Operation + step Validate + end + end + #:callable-method end + end +end + +class Lambda_DocsMechanicsTest < Minitest::Spec + Memo = Module.new + it "what" do + #:lambda-step + module Memo::Operation + class Create < Trailblazer::Operation + step ->(ctx, params:, **) { p params.inspect } + end + end + #:lambda-step end + end +end + +class Inheritance_DocsMechanicsTest < Minitest::Spec + Memo = Module.new + it "what" do + #:inherit-create + module Memo::Operation + class Create < Trailblazer::Operation + step :create_model + step :validate + step :save + #~meths + include T.def_steps(:create_model, :validate, :save) + #~meths end + end + end + #:inherit-create end + + #:inherit-update-empty + module Memo::Operation + class Update < Create + end + end + #:inherit-update-empty end + + #:inherit-update + module Memo::Operation + class Update < Create + step :find_model, replace: :create_model + #~meths + include T.def_steps(:find_model) + #~meths end + end + end + #:inherit-update end + + assert_invoke Memo::Operation::Create, seq: "[:create_model, :validate, :save]" + assert_invoke Memo::Operation::Update, seq: "[:find_model, :validate, :save]" + end +end + diff --git a/test/docs/autogenerated/sequence_options_test.rb b/test/docs/autogenerated/sequence_options_test.rb new file mode 100644 index 0000000..b224da9 --- /dev/null +++ b/test/docs/autogenerated/sequence_options_test.rb @@ -0,0 +1,202 @@ +require "test_helper" +# THIS FILE IS AUTOGENERATED FROM trailblazer-activity-dsl-linear/test/docs/sequence_options_test.rb +# require "trailblazer/developer" + +module A + class Id_DocSeqOptionsTest < Minitest::Spec + Memo = Struct.new(:text) + + #:id + module Memo::Operation + class Create < Trailblazer::Operation + step :validate + step :save, id: :save_the_world + step :notify + #~meths + include T.def_steps(:validate, :save, :notify) + #~meths end + end + end + #:id end + + it ":id shows up in introspect" do +=begin + output = + #:id-inspect + Trailblazer::Developer.railway(Memo::Operation::Create) + #=> [>validate,>save_the_world,>notify] + #:id-inspect end +=end + +#~ignore end + + assert Activity::Introspect.Nodes(Memo::Operation::Create, id: :save_the_world) + #:id-introspect + puts Activity::Introspect.Nodes(Memo::Operation::Create, id: :save_the_world) + #=> # + #:id-introspect end + end + end +end + +module B + class Delete_DocsSequenceOptionsTest < Minitest::Spec + Memo = A::Id_DocSeqOptionsTest::Memo + + it ":delete removes step" do + #:delete + module Memo::Operation + class Admin < Create + step nil, delete: :validate + end + end + #:delete end + +=begin + output = + #:delete-inspect + Trailblazer::Developer.railway(Memo::Operation::Admin) + #=> [>save_the_world,>notify] + #:delete-inspect end +=end + +#~ignore end + end + end +end + +module C + class Before_DocsSequenceOptionsTest < Minitest::Spec + it ":before" do + Memo = A::Id_DocSeqOptionsTest::Memo + + #:before + module Memo::Operation + class Authorized < Memo::Operation::Create + step :policy, before: :validate + #~meths + include T.def_steps(:policy) + #~meths end + end + end + #:before end + +=begin + #:before-inspect + Trailblazer::Developer.railway(Memo::Operation::Authorized) + #=> [>policy,>validate,>save_the_world,>notify] + #:before-inspect end +=end + +#~ignore end + end + + end +end + +module D + class After_DocsSequenceOptionsTest < Minitest::Spec + it ":after" do + Memo = Class.new(A::Id_DocSeqOptionsTest::Memo) + Memo::Operation = Module.new + Memo::Operation::Create = Class.new(A::Id_DocSeqOptionsTest::Memo::Operation::Create) + + #:after + module Memo::Operation + class Authorized < Memo::Operation::Create + step :policy, after: :validate + #~meths + include T.def_steps(:policy) + #~meths end + end + end + #:after end + +=begin + #:after-inspect + Trailblazer::Developer.railway(Memo::Operation::Authorized) + #=> [>validate,>policy,>save_the_world,>notify] + #:after-inspect end +=end + +#~ignore end + end + end +end + +module E + class Replace_DocsSequenceOptionsTest < Minitest::Spec + Memo = Class.new + module Memo::Operation + class Create < Trailblazer::Operation + step :validate + step :save + step :notify + #~meths + include T.def_steps(:validate, :save, :notify) + #~meths end + end + end + + it "{:replace} automatically assigns ID" do + #:replace + module Memo::Operation + class Update < Create + step :update, replace: :save + #~meths + include T.def_steps(:update) + #~meths end + end + + end + #:replace end + +=begin + #:replace-inspect + Trailblazer::Developer.railway(Memo::Operation::Update) + #=> [>validate,>update,>notify] + #:replace-inspect end +=end + assert Activity::Introspect.Nodes(Memo::Operation::Update, id: :update) + +#~ignore end + end + end +end + +#@ {#replace} with {:id} +module E_2 + class Replace_With_ID_DocsSequenceOptionsTest < Minitest::Spec + Memo = Class.new + module Memo::Operation + class Create < Trailblazer::Operation + step :validate + step :save + step :notify + #~meths + include T.def_steps(:validate, :save, :notify) + #~meths end + end + end + + it "{:replace} allows explicit ID" do + #:replace-id + module Memo::Operation + class Update < Create + step :update, replace: :save, id: :update_memo + #~meths + include T.def_steps(:update) + #~meths end + end + + end + #:replace-id end + + # assert_equal Trailblazer::Developer.railway(Memo::Operation::Update), %([>validate,>update_memo,>notify]) + + assert Activity::Introspect.Nodes(Memo::Operation::Update, id: :update_memo) + +#~ignore end + end + end +end