much-result 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,102 @@
1
+ require "assert"
2
+ require "much-result/transaction"
3
+
4
+ class MuchResult::Transaction
5
+ class UnitTests < Assert::Context
6
+ desc "MuchResult::Transaction"
7
+ subject { unit_class }
8
+
9
+ let(:unit_class) { MuchResult::Transaction }
10
+
11
+ let(:receiver1) { Factory.transaction_receiver }
12
+ let(:value1) { Factory.value }
13
+ let(:kargs1) {
14
+ { value: value1 }
15
+ }
16
+ let(:block1) {
17
+ ->(transaction) { transaction.set(block_called: true) }
18
+ }
19
+
20
+ should have_imeths :halt_throw_value, :call
21
+
22
+ should "know its halt throw value" do
23
+ assert_that(subject.halt_throw_value).equals(:muchresult_transaction_halt)
24
+ end
25
+
26
+ should "call transactions on a transaction receiver" do
27
+ MuchStub.tap_on_call(unit_class, :new) { |transaction, new_call|
28
+ @new_call = new_call
29
+ MuchStub.on_call(transaction, :call) { |transaction_call|
30
+ @transaction_call = transaction_call
31
+ }
32
+ }
33
+
34
+ subject.call(receiver1, **kargs1, &block1)
35
+
36
+ assert_that(@new_call.pargs).equals([receiver1])
37
+ assert_that(@new_call.kargs).equals(kargs1)
38
+ assert_that(@transaction_call.block).equals(block1)
39
+ end
40
+ end
41
+
42
+ class InitTests < UnitTests
43
+ desc "when init"
44
+ subject { unit_class.new(receiver1, **kargs1) }
45
+
46
+ should have_imeths :result, :call, :rollback, :halt
47
+
48
+ should "know its result" do
49
+ assert_that(subject.result).is_instance_of(MuchResult)
50
+ assert_that(subject.result.value).equals(value1)
51
+ assert_that(subject.result.much_result_transaction_rolled_back).is_false
52
+ assert_that(subject.result.much_result_transaction_halted).is_false
53
+ end
54
+
55
+ should "delegate result methods to its result" do
56
+ assert_that(subject.value).equals(subject.result.value)
57
+ assert_that(subject.success?).equals(subject.result.success?)
58
+ assert_that(subject.results).equals(subject.result.results)
59
+ end
60
+
61
+ should "call transactions on the transaction receiver" do
62
+ MuchStub.tap_on_call(block1, :call) { |_, call|
63
+ @block_call = call
64
+ }
65
+
66
+ result = subject.call(&block1)
67
+
68
+ assert_that(result).equals(subject.result)
69
+ assert_that(subject.block_called).is_true
70
+ assert_that(@block_call.args).equals([subject])
71
+ assert_that(receiver1.last_transaction_call.block).is_not_nil
72
+ end
73
+
74
+ should "rollback transactions on the transaction receiver" do
75
+ assert_that(-> { subject.rollback }).raises(MuchResult::Rollback)
76
+
77
+ block1 = ->(transaction) { transaction.rollback }
78
+ assert_that(-> {subject.call(&block1)}).does_not_raise
79
+ assert_that(receiver1.rolled_back?).is_true
80
+
81
+ block1 = ->(transaction) { raise StandardError }
82
+ assert_that(-> {subject.call(&block1)}).raises(StandardError)
83
+ assert_that(receiver1.rolled_back?).is_true
84
+
85
+ assert_that(subject.result.much_result_transaction_rolled_back).is_true
86
+ assert_that(subject.result.much_result_transaction_halted).is_false
87
+ end
88
+
89
+ should "halt transactions" do
90
+ catch(unit_class.halt_throw_value) do
91
+ subject.capture { "something1" }
92
+ subject.halt
93
+ subject.capture { "something2" }
94
+ end
95
+
96
+ assert_that(subject.sub_results.size).equals(1)
97
+
98
+ assert_that(subject.result.much_result_transaction_rolled_back).is_false
99
+ assert_that(subject.result.much_result_transaction_halted).is_true
100
+ end
101
+ end
102
+ end
File without changes
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: much-result
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kelly Redding
8
+ - Collin Redding
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2020-09-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: assert
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: 2.18.4
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: 2.18.4
28
+ description: API for managing the results of operations.
29
+ email:
30
+ - kelly@kellyredding.com
31
+ - collin.redding@me.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - Gemfile
37
+ - LICENSE
38
+ - README.md
39
+ - lib/much-result.rb
40
+ - lib/much-result/aggregate.rb
41
+ - lib/much-result/transaction.rb
42
+ - lib/much-result/version.rb
43
+ - log/.keep
44
+ - much-result.gemspec
45
+ - test/helper.rb
46
+ - test/support/factory.rb
47
+ - test/system/.keep
48
+ - test/unit/aggregate_tests.rb
49
+ - test/unit/much-result_tests.rb
50
+ - test/unit/transaction_tests.rb
51
+ - tmp/.gitkeep
52
+ homepage: https://github.com/redding/much-result
53
+ licenses:
54
+ - MIT
55
+ metadata: {}
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - "~>"
63
+ - !ruby/object:Gem::Version
64
+ version: '2.5'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubygems_version: 3.1.2
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: API for managing the results of operations.
75
+ test_files:
76
+ - test/helper.rb
77
+ - test/support/factory.rb
78
+ - test/system/.keep
79
+ - test/unit/aggregate_tests.rb
80
+ - test/unit/much-result_tests.rb
81
+ - test/unit/transaction_tests.rb