diff --git a/lib/bcdd/result.rb b/lib/bcdd/result.rb index 2e27bf7a..b6032834 100644 --- a/lib/bcdd/result.rb +++ b/lib/bcdd/result.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +require 'set' require 'singleton' require_relative 'result/version' @@ -153,7 +154,7 @@ def known(block) def call_and_then_source_method(method_name, injected_value) method = source.method(method_name) - Transitions.tracking.record_and_then(method, injected_value, source) do + Transitions.tracking.record_and_then(method, injected_value) do result = call_and_then_source_method!(method, injected_value) ensure_result_object(result, origin: :method) @@ -170,7 +171,7 @@ def call_and_then_source_method!(method, injected_value) end def call_and_then_block(block) - Transitions.tracking.record_and_then(:block, nil, source) do + Transitions.tracking.record_and_then(:block, nil) do result = call_and_then_block!(block) ensure_result_object(result, origin: :block) diff --git a/lib/bcdd/result/callable_and_then/caller.rb b/lib/bcdd/result/callable_and_then/caller.rb index c43459e4..948cadc4 100644 --- a/lib/bcdd/result/callable_and_then/caller.rb +++ b/lib/bcdd/result/callable_and_then/caller.rb @@ -5,7 +5,7 @@ class CallableAndThen::Caller def self.call(source, value:, injected_value:, method_name:) method = callable_method(source, method_name) - Transitions.tracking.record_and_then(method, injected_value, source) do + Transitions.tracking.record_and_then(method, injected_value) do result = if source.is_a?(::Proc) call_proc!(source, value, injected_value) diff --git a/lib/bcdd/result/transitions/tracking/disabled.rb b/lib/bcdd/result/transitions/tracking/disabled.rb index 45e467e8..f651d30a 100644 --- a/lib/bcdd/result/transitions/tracking/disabled.rb +++ b/lib/bcdd/result/transitions/tracking/disabled.rb @@ -10,7 +10,7 @@ def self.reset!; end def self.record(result); end - def self.record_and_then(_type, _data, _source) + def self.record_and_then(_type, _data) yield end diff --git a/lib/bcdd/result/transitions/tracking/enabled.rb b/lib/bcdd/result/transitions/tracking/enabled.rb index 8f34924a..67d555ff 100644 --- a/lib/bcdd/result/transitions/tracking/enabled.rb +++ b/lib/bcdd/result/transitions/tracking/enabled.rb @@ -30,11 +30,11 @@ def record(result) track(result, time: ::Time.now.getutc) end - def record_and_then(type_arg, arg, source) + def record_and_then(type_arg, arg) type = type_arg.instance_of?(::Method) ? :method : type_arg unless tree.frozen? - current_and_then = { type: type, arg: arg, source: source } + current_and_then = { type: type, arg: arg } current_and_then[:method_name] = type_arg.name if type == :method tree.current.value[1] = current_and_then @@ -84,13 +84,14 @@ def root_start(name_and_desc) end def track(result, time:) - result = result.data.to_h + result_data = result.data.to_h + result_data[:source] = result.send(:source) root, = tree.root_value parent, = tree.parent_value current, and_then = tree.current_value - records << { root: root, parent: parent, current: current, result: result, and_then: and_then, time: time } + records << { root: root, parent: parent, current: current, result: result_data, and_then: and_then, time: time } end def now_in_milliseconds diff --git a/sig/bcdd/result/transitions.rbs b/sig/bcdd/result/transitions.rbs index 7e489079..ecb492cd 100644 --- a/sig/bcdd/result/transitions.rbs +++ b/sig/bcdd/result/transitions.rbs @@ -58,7 +58,7 @@ class BCDD::Result def exec: (String, String) { () -> untyped } -> BCDD::Result def reset!: () -> void def record: (BCDD::Result) -> void - def record_and_then: ((untyped), untyped, untyped) { () -> BCDD::Result } -> BCDD::Result + def record_and_then: ((untyped), untyped) { () -> BCDD::Result } -> BCDD::Result def reset_and_then!: () -> void private @@ -79,7 +79,7 @@ class BCDD::Result def self.exec: (String, String) { () -> untyped } -> BCDD::Result def self.reset!: () -> void def self.record: (BCDD::Result) -> void - def self.record_and_then: ((untyped), untyped, untyped) { () -> BCDD::Result } -> BCDD::Result + def self.record_and_then: ((untyped), untyped) { () -> BCDD::Result } -> BCDD::Result def self.reset_and_then!: () -> void private diff --git a/test/bcdd/result/callable_and_then/results_from_different_sources_test.rb b/test/bcdd/result/callable_and_then/results_from_different_sources_test.rb index e0f1ed93..676ac3d4 100644 --- a/test/bcdd/result/callable_and_then/results_from_different_sources_test.rb +++ b/test/bcdd/result/callable_and_then/results_from_different_sources_test.rb @@ -84,26 +84,30 @@ def teardown root = { id: 0, name: 'NormalizeAndValidateEmail', desc: nil } + source = -> { _1 == NormalizeAndValidateEmail } + { root: root, parent: root, current: root, - result: { kind: :success, type: :given, value: 1 } + result: { kind: :success, type: :given, value: 1, source: source } }.then { assert_transition_record(result1, 0, _1) } + source = -> { _1 == NormalizeEmail } + { root: root, parent: root, current: { id: 1, name: 'NormalizeEmail', desc: nil }, - result: { kind: :success, type: :given, value: 1 } + result: { kind: :success, type: :given, value: 1, source: source } }.then { assert_transition_record(result1, 1, _1) } { root: root, parent: root, current: { id: 1, name: 'NormalizeEmail', desc: nil }, - result: { kind: :failure, type: :invalid_input, value: 'input must be a String' }, - and_then: { type: :method, arg: nil, source: -> { _1 == NormalizeEmail }, method_name: :normalize } + result: { kind: :failure, type: :invalid_input, value: 'input must be a String', source: source }, + and_then: { type: :method, arg: nil, method_name: :normalize } }.then { assert_transition_record(result1, 2, _1) } # --- @@ -116,41 +120,45 @@ def teardown root = { id: 0, name: 'NormalizeAndValidateEmail', desc: nil } + source = -> { _1 == NormalizeAndValidateEmail } + { root: root, parent: root, current: root, - result: { kind: :success, type: :given, value: " FOO@bAr.com \n" } + result: { kind: :success, type: :given, value: " FOO@bAr.com \n", source: source } }.then { assert_transition_record(result2, 0, _1) } + source = -> { _1 == NormalizeEmail } + { root: root, parent: root, current: { id: 1, name: 'NormalizeEmail', desc: nil }, - result: { kind: :success, type: :given, value: " FOO@bAr.com \n" } + result: { kind: :success, type: :given, value: " FOO@bAr.com \n", source: source } }.then { assert_transition_record(result2, 1, _1) } { root: root, parent: root, current: { id: 1, name: 'NormalizeEmail', desc: nil }, - result: { kind: :success, type: :normalized_input, value: 'foo@bar.com' }, - and_then: { type: :method, arg: nil, source: -> { _1 == NormalizeEmail }, method_name: :normalize } + result: { kind: :success, type: :normalized_input, value: 'foo@bar.com', source: source }, + and_then: { type: :method, arg: nil, method_name: :normalize } }.then { assert_transition_record(result2, 2, _1) } { root: root, parent: root, current: { id: 2, name: 'EmailValidation', desc: nil }, - result: { kind: :success, type: :given, value: 'foo@bar.com' } + result: { kind: :success, type: :given, value: 'foo@bar.com', source: EmailValidation } }.then { assert_transition_record(result2, 3, _1) } { root: root, parent: root, current: { id: 2, name: 'EmailValidation', desc: nil }, - result: { kind: :success, type: :valid_email, value: 'foo@bar.com' }, - and_then: { type: :method, arg: nil, source: EmailValidation, method_name: :validate } + result: { kind: :success, type: :valid_email, value: 'foo@bar.com', source: EmailValidation }, + and_then: { type: :method, arg: nil, method_name: :validate } }.then { assert_transition_record(result2, 4, _1) } end end diff --git a/test/bcdd/result/context/callable_and_then/accumulation_test.rb b/test/bcdd/result/context/callable_and_then/accumulation_test.rb index 53a866ef..896d6396 100644 --- a/test/bcdd/result/context/callable_and_then/accumulation_test.rb +++ b/test/bcdd/result/context/callable_and_then/accumulation_test.rb @@ -68,45 +68,45 @@ def call_g(f:, **) root: root, parent: root, current: root, - result: { kind: :success, type: :given, value: { a: 1 } } + result: { kind: :success, type: :given, value: { a: 1 }, source: root_process } }.then { assert_transition_record(result, 0, _1) } { root: root, parent: root, current: root, - result: { kind: :success, type: :b, value: { b: 2 } }, - and_then: { type: :method, arg: { b: 2 }, source: root_process, method_name: :call_b } + result: { kind: :success, type: :b, value: { b: 2 }, source: root_process }, + and_then: { type: :method, arg: { b: 2 }, method_name: :call_b } }.then { assert_transition_record(result, 1, _1) } { root: root, parent: root, current: { id: 1, name: 'CallC', desc: nil }, - result: { kind: :success, type: :c, value: { c: 3 } } + result: { kind: :success, type: :c, value: { c: 3 }, source: nil } }.then { assert_transition_record(result, 2, _1) } { root: root, parent: root, current: root, - result: { kind: :success, type: :d, value: { d: 4 } }, - and_then: { type: :method, arg: -> { _1.is_a?(Hash) && _1.empty? }, source: root_process, method_name: :call_d } + result: { kind: :success, type: :d, value: { d: 4 }, source: root_process }, + and_then: { type: :method, arg: -> { _1.is_a?(Hash) && _1.empty? }, method_name: :call_d } }.then { assert_transition_record(result, 3, _1) } { root: root, parent: root, current: { id: 2, name: 'CallE', desc: nil }, - result: { kind: :success, type: :e, value: { e: 5 } } + result: { kind: :success, type: :e, value: { e: 5 }, source: nil } }.then { assert_transition_record(result, 4, _1) } { root: root, parent: root, current: root, - result: { kind: :success, type: :g, value: { g: 6 } }, - and_then: { type: :method, arg: { h: 7 }, source: root_process, method_name: :call_g } + result: { kind: :success, type: :g, value: { g: 6 }, source: root_process }, + and_then: { type: :method, arg: { h: 7 }, method_name: :call_g } }.then { assert_transition_record(result, 5, _1) } { diff --git a/test/bcdd/result/context/callable_and_then/results_from_different_sources_test.rb b/test/bcdd/result/context/callable_and_then/results_from_different_sources_test.rb index 138d6d8d..7d6d576c 100644 --- a/test/bcdd/result/context/callable_and_then/results_from_different_sources_test.rb +++ b/test/bcdd/result/context/callable_and_then/results_from_different_sources_test.rb @@ -84,26 +84,30 @@ def teardown root = { id: 0, name: 'NormalizeAndValidateEmail', desc: nil } + source = -> { _1 == NormalizeAndValidateEmail } + { root: root, parent: root, current: root, - result: { kind: :success, type: :given, value: { input: 1 } } + result: { kind: :success, type: :given, value: { input: 1 }, source: source } }.then { assert_transition_record(result1, 0, _1) } + source = -> { _1 == NormalizeEmail } + { root: root, parent: root, current: { id: 1, name: 'NormalizeEmail', desc: nil }, - result: { kind: :success, type: :given, value: { input: 1 } } + result: { kind: :success, type: :given, value: { input: 1 }, source: source } }.then { assert_transition_record(result1, 1, _1) } { root: root, parent: root, current: { id: 1, name: 'NormalizeEmail', desc: nil }, - result: { kind: :failure, type: :invalid_input, value: { message: 'input must be a String' } }, - and_then: { type: :method, arg: {}, source: -> { _1 == NormalizeEmail }, method_name: :normalize } + result: { kind: :failure, type: :invalid_input, value: { message: 'input must be a String' }, source: source }, + and_then: { type: :method, arg: {}, method_name: :normalize } }.then { assert_transition_record(result1, 2, _1) } # --- @@ -116,41 +120,45 @@ def teardown root = { id: 0, name: 'NormalizeAndValidateEmail', desc: nil } + source = -> { _1 == NormalizeAndValidateEmail } + { root: root, parent: root, current: root, - result: { kind: :success, type: :given, value: { input: " FOO@bAr.com \n" } } + result: { kind: :success, type: :given, value: { input: " FOO@bAr.com \n" }, source: source } }.then { assert_transition_record(result2, 0, _1) } + source = -> { _1 == NormalizeEmail } + { root: root, parent: root, current: { id: 1, name: 'NormalizeEmail', desc: nil }, - result: { kind: :success, type: :given, value: { input: " FOO@bAr.com \n" } } + result: { kind: :success, type: :given, value: { input: " FOO@bAr.com \n" }, source: source } }.then { assert_transition_record(result2, 1, _1) } { root: root, parent: root, current: { id: 1, name: 'NormalizeEmail', desc: nil }, - result: { kind: :success, type: :normalized_input, value: { input: 'foo@bar.com' } }, - and_then: { type: :method, arg: {}, source: -> { _1 == NormalizeEmail }, method_name: :normalize } + result: { kind: :success, type: :normalized_input, value: { input: 'foo@bar.com' }, source: source }, + and_then: { type: :method, arg: {}, method_name: :normalize } }.then { assert_transition_record(result2, 2, _1) } { root: root, parent: root, current: { id: 2, name: 'EmailValidation', desc: nil }, - result: { kind: :success, type: :given, value: { input: 'foo@bar.com' } } + result: { kind: :success, type: :given, value: { input: 'foo@bar.com' }, source: EmailValidation } }.then { assert_transition_record(result2, 3, _1) } { root: root, parent: root, current: { id: 2, name: 'EmailValidation', desc: nil }, - result: { kind: :success, type: :valid_email, value: { email: 'foo@bar.com' } }, - and_then: { type: :method, arg: {}, source: EmailValidation, method_name: :validate } + result: { kind: :success, type: :valid_email, value: { email: 'foo@bar.com' }, source: EmailValidation }, + and_then: { type: :method, arg: {}, method_name: :validate } }.then { assert_transition_record(result2, 4, _1) } end end diff --git a/test/bcdd/result/context/transitions/enabled/with_source/singleton/nested_test.rb b/test/bcdd/result/context/transitions/enabled/with_source/singleton/nested_test.rb index 988fd3f1..91d822a1 100644 --- a/test/bcdd/result/context/transitions/enabled/with_source/singleton/nested_test.rb +++ b/test/bcdd/result/context/transitions/enabled/with_source/singleton/nested_test.rb @@ -125,28 +125,29 @@ def divide_by_two(num) root: root, parent: root, current: { id: Integer, name: nil, desc: nil }, - result: { kind: :failure, type: :invalid_arg, value: { message: 'num1 must be numeric' } } + result: { kind: :failure, type: :invalid_arg, value: { message: 'num1 must be numeric' }, source: Division } }.then { |spec| assert_transition_record(result, 0, spec) } { root: root, parent: root, current: { id: Integer, name: nil, desc: nil }, - result: { kind: :failure, type: :invalid_arg, value: { message: 'num1 must be numeric' } } + result: { kind: :failure, type: :invalid_arg, value: { message: 'num1 must be numeric' }, source: Division } }.then { |spec| assert_transition_record(result, 1, spec) } { root: root, parent: root, current: { id: Integer, name: nil, desc: nil }, - result: { kind: :failure, type: :invalid_arg, value: { message: 'num1 must be numeric' } } + result: { kind: :failure, type: :invalid_arg, value: { message: 'num1 must be numeric' }, source: Division } }.then { |spec| assert_transition_record(result, 2, spec) } { root: root, parent: root, current: root, - result: { kind: :failure, type: :errors, value: { messages: ['num1 must be numeric'] * 3 } } + result: { kind: :failure, type: :errors, value: { messages: ['num1 must be numeric'] * 3 }, + source: SumDivisionsByTwo } }.then { |spec| assert_transition_record(result, 3, spec) } # --- @@ -173,7 +174,7 @@ def divide_by_two(num) root: root, parent: root, current: { id: Integer, name: nil, desc: nil }, - result: { kind: :failure, type: :invalid_arg, value: { message: 'num1 must be numeric' } } + result: { kind: :failure, type: :invalid_arg, value: { message: 'num1 must be numeric' }, source: Division } }.then { |spec| assert_transition_record(result, 0, spec) } # 2nd division transitions @@ -187,23 +188,23 @@ def divide_by_two(num) root: root, parent: root, current: { id: Integer, name: nil, desc: nil }, - result: { kind: :success, type: :continued, value: { num1: 20, num2: 2 } } + result: { kind: :success, type: :continued, value: { num1: 20, num2: 2 }, source: Division } }.then { |spec| assert_transition_record(result, 1, spec) } { root: root, parent: root, current: { id: Integer, name: nil, desc: nil }, - result: { kind: :success, type: :continued, value: {} }, - and_then: { type: :method, arg: { useless_arg: true }, source: Division, method_name: :validate_nonzero } + result: { kind: :success, type: :continued, value: {}, source: Division }, + and_then: { type: :method, arg: { useless_arg: true }, method_name: :validate_nonzero } }.then { |spec| assert_transition_record(result, 2, spec) } { root: root, parent: root, current: { id: Integer, name: nil, desc: nil }, - result: { kind: :success, type: :division_completed, value: { number: 10 } }, - and_then: { type: :method, arg: {}, source: Division, method_name: :divide } + result: { kind: :success, type: :division_completed, value: { number: 10 }, source: Division }, + and_then: { type: :method, arg: {}, method_name: :divide } }.then { |spec| assert_transition_record(result, 3, spec) } # 3rd division transitions @@ -217,23 +218,23 @@ def divide_by_two(num) root: root, parent: root, current: { id: Integer, name: nil, desc: nil }, - result: { kind: :success, type: :continued, value: { num1: 0, num2: 2 } } + result: { kind: :success, type: :continued, value: { num1: 0, num2: 2 }, source: Division } }.then { |spec| assert_transition_record(result, 4, spec) } { root: root, parent: root, current: { id: Integer, name: nil, desc: nil }, - result: { kind: :success, type: :continued, value: {} }, - and_then: { type: :method, arg: { useless_arg: true }, source: Division, method_name: :validate_nonzero } + result: { kind: :success, type: :continued, value: {}, source: Division }, + and_then: { type: :method, arg: { useless_arg: true }, method_name: :validate_nonzero } }.then { |spec| assert_transition_record(result, 5, spec) } { root: root, parent: root, current: { id: Integer, name: nil, desc: nil }, - result: { kind: :success, type: :division_completed, value: { number: 0 } }, - and_then: { type: :method, arg: {}, source: Division, method_name: :divide } + result: { kind: :success, type: :division_completed, value: { number: 0 }, source: Division }, + and_then: { type: :method, arg: {}, method_name: :divide } }.then { |spec| assert_transition_record(result, 6, spec) } # Final result transition diff --git a/test/bcdd/result/transitions/enabled/with_source/instance/flat_test.rb b/test/bcdd/result/transitions/enabled/with_source/instance/flat_test.rb index 24dfab91..e5d0fe43 100644 --- a/test/bcdd/result/transitions/enabled/with_source/instance/flat_test.rb +++ b/test/bcdd/result/transitions/enabled/with_source/instance/flat_test.rb @@ -131,8 +131,8 @@ def divide((num1, num2)) }.then { |spec| assert_division_transition(result3, 0, spec) } { - result: { kind: :failure, type: :division_by_zero, value: 'num2 cannot be zero' }, - and_then: { type: :method, arg: nil, source: division3, method_name: :check_for_zeros } + result: { kind: :failure, type: :division_by_zero, value: 'num2 cannot be zero', source: division3 }, + and_then: { type: :method, arg: nil, method_name: :check_for_zeros } }.then { |spec| assert_division_transition(result3, 1, spec) } # --- @@ -147,8 +147,8 @@ def divide((num1, num2)) }.then { |spec| assert_division_transition(result4, 0, spec) } { - result: { kind: :success, type: :division_completed, value: 0 }, - and_then: { type: :method, arg: nil, source: division4, method_name: :check_for_zeros } + result: { kind: :success, type: :division_completed, value: 0, source: division4 }, + and_then: { type: :method, arg: nil, method_name: :check_for_zeros } }.then { |spec| assert_division_transition(result4, 1, spec) } # --- @@ -163,13 +163,13 @@ def divide((num1, num2)) }.then { |spec| assert_division_transition(result5, 0, spec) } { - result: { kind: :success, type: :continued, value: [4, 2] }, - and_then: { type: :method, arg: nil, source: division5, method_name: :check_for_zeros } + result: { kind: :success, type: :continued, value: [4, 2], source: division5 }, + and_then: { type: :method, arg: nil, method_name: :check_for_zeros } }.then { |spec| assert_division_transition(result5, 1, spec) } { - result: { kind: :success, type: :division_completed, value: 2 }, - and_then: { type: :method, arg: nil, source: division5, method_name: :divide } + result: { kind: :success, type: :division_completed, value: 2, source: division5 }, + and_then: { type: :method, arg: nil, method_name: :divide } }.then { |spec| assert_division_transition(result5, 2, spec) } # --- diff --git a/test/bcdd/result/transitions/enabled/with_source/instance/nested_test.rb b/test/bcdd/result/transitions/enabled/with_source/instance/nested_test.rb index db34c785..1652f9f0 100644 --- a/test/bcdd/result/transitions/enabled/with_source/instance/nested_test.rb +++ b/test/bcdd/result/transitions/enabled/with_source/instance/nested_test.rb @@ -138,21 +138,21 @@ def divide_by_two(num) root: root, parent: root, current: { id: Integer, name: 'Division', desc: nil }, - result: { kind: :failure, type: :invalid_arg, value: 'num1 must be numeric' } + result: { kind: :failure, type: :invalid_arg, value: 'num1 must be numeric', source: Division } }.then { |spec| assert_transition_record(result, 0, spec) } { root: root, parent: root, current: { id: Integer, name: 'Division', desc: nil }, - result: { kind: :failure, type: :invalid_arg, value: 'num1 must be numeric' } + result: { kind: :failure, type: :invalid_arg, value: 'num1 must be numeric', source: Division } }.then { |spec| assert_transition_record(result, 1, spec) } { root: root, parent: root, current: { id: Integer, name: 'Division', desc: nil }, - result: { kind: :failure, type: :invalid_arg, value: 'num1 must be numeric' } + result: { kind: :failure, type: :invalid_arg, value: 'num1 must be numeric', source: Division } }.then { |spec| assert_transition_record(result, 2, spec) } { @@ -186,7 +186,7 @@ def divide_by_two(num) root: root, parent: root, current: { id: Integer, name: 'Division', desc: nil }, - result: { kind: :failure, type: :invalid_arg, value: 'num1 must be numeric' } + result: { kind: :failure, type: :invalid_arg, value: 'num1 must be numeric', source: Division } }.then { |spec| assert_transition_record(result, 0, spec) } # 2nd division transitions @@ -200,29 +200,29 @@ def divide_by_two(num) root: root, parent: root, current: { id: Integer, name: 'Division', desc: nil }, - result: { kind: :success, type: :ok, value: [20, 2] } + result: { kind: :success, type: :ok, value: [20, 2], source: Division } }.then { |spec| assert_transition_record(result, 1, spec) } { root: root, parent: { id: Integer, name: 'CheckForZeros', desc: nil }, current: { id: Integer, name: 'DetectZero', desc: nil }, - result: { kind: :failure, type: :not_zero, value: nil } + result: { kind: :failure, type: :not_zero, value: nil, source: nil } }.then { |spec| assert_transition_record(result, 2, spec) } { root: root, parent: { id: Integer, name: 'Division', desc: nil }, current: { id: Integer, name: 'CheckForZeros', desc: nil }, - result: { kind: :failure, type: :no_zeros, value: nil } + result: { kind: :failure, type: :no_zeros, value: nil, source: nil } }.then { |spec| assert_transition_record(result, 3, spec) } { root: root, parent: root, current: { id: Integer, name: 'Division', desc: nil }, - result: { kind: :success, type: :division_completed, value: 10 }, - and_then: { type: :method, arg: 'useless_arg', source: -> { _1.is_a?(Division) }, method_name: :divide } + result: { kind: :success, type: :division_completed, value: 10, source: Division }, + and_then: { type: :method, arg: 'useless_arg', method_name: :divide } }.then { |spec| assert_transition_record(result, 4, spec) } # 3rd division transitions @@ -236,29 +236,29 @@ def divide_by_two(num) root: root, parent: root, current: { id: Integer, name: 'Division', desc: nil }, - result: { kind: :success, type: :ok, value: [0, 2] } + result: { kind: :success, type: :ok, value: [0, 2], source: Division } }.then { |spec| assert_transition_record(result, 5, spec) } { root: root, parent: { id: Integer, name: 'CheckForZeros', desc: nil }, current: { id: Integer, name: 'DetectZero', desc: nil }, - result: { kind: :failure, type: :not_zero, value: nil } + result: { kind: :failure, type: :not_zero, value: nil, source: nil } }.then { |spec| assert_transition_record(result, 6, spec) } { root: root, parent: { id: Integer, name: 'Division', desc: nil }, current: { id: Integer, name: 'CheckForZeros', desc: nil }, - result: { kind: :success, type: :num1_is_zero, value: nil } + result: { kind: :success, type: :num1_is_zero, value: nil, source: nil } }.then { |spec| assert_transition_record(result, 7, spec) } { root: root, parent: root, current: { id: Integer, name: 'Division', desc: nil }, - result: { kind: :success, type: :division_completed, value: 0 }, - and_then: { type: :method, arg: 'useless_arg', source: -> { _1.is_a?(Division) }, method_name: :divide } + result: { kind: :success, type: :division_completed, value: 0, source: Division }, + and_then: { type: :method, arg: 'useless_arg', method_name: :divide } }.then { |spec| assert_transition_record(result, 8, spec) } # Final result transition diff --git a/test/test_helper.rb b/test/test_helper.rb index 649c4474..41e3fdae 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -118,17 +118,22 @@ def assert_transitions_metadata(result) assert_instance_of(Array, metadata[:tree_map]) end + TimeValue = ->(value) { value.is_a?(::Time) && value.utc? } + AndThenValue = ->(value) { value.is_a?(::Hash) && value.empty? } + def assert_transition_record(result, index, options) transition = result.transitions[:records][index] - root, parent, current, result = options.fetch_values(:root, :parent, :current, :result) + root, parent, current, result_data = options.fetch_values(:root, :parent, :current, :result) + + result_data[:source] = result.send(:source) unless result_data.key?(:source) - and_then = options.fetch(:and_then) { ->(value) { value.is_a?(::Hash) && value.empty? } } + and_then = options.fetch(:and_then) { AndThenValue } - time = options.fetch(:time) { ->(value) { value.is_a?(::Time) && value.utc? } } + time = options.fetch(:time) { TimeValue } assert_hash_schema!( - { root: root, parent: parent, current: current, result: result, and_then: and_then, time: time }, + { root: root, parent: parent, current: current, result: result_data, and_then: and_then, time: time }, transition ) end