deterministic 0.14.1 → 0.15.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +108 -41
- data/lib/deterministic.rb +1 -0
- data/lib/deterministic/enum.rb +203 -0
- data/lib/deterministic/monad.rb +19 -5
- data/lib/deterministic/option.rb +40 -80
- data/lib/deterministic/protocol.rb +103 -0
- data/lib/deterministic/result.rb +49 -91
- data/lib/deterministic/version.rb +1 -1
- data/spec/examples/amount_spec.rb +67 -0
- data/spec/examples/config_spec.rb +5 -2
- data/spec/examples/controller_spec.rb +1 -1
- data/spec/examples/list.rb +183 -0
- data/spec/examples/list_spec.rb +186 -0
- data/spec/examples/logger_spec.rb +13 -8
- data/spec/examples/validate_address_spec.rb +5 -4
- data/spec/lib/deterministic/class_mixin_spec.rb +3 -4
- data/spec/lib/deterministic/core_ext/result_spec.rb +4 -2
- data/spec/lib/deterministic/currify_spec.rb +88 -0
- data/spec/lib/deterministic/monad_spec.rb +3 -3
- data/spec/lib/deterministic/option_spec.rb +107 -98
- data/spec/lib/deterministic/protocol_spec.rb +43 -0
- data/spec/lib/deterministic/result/failure_spec.rb +1 -7
- data/spec/lib/deterministic/result/{result_map.rb → result_map_spec.rb} +9 -9
- data/spec/lib/deterministic/result/success_spec.rb +1 -2
- data/spec/lib/deterministic/result_spec.rb +44 -15
- data/spec/lib/enum_spec.rb +108 -0
- data/spec/spec_helper.rb +2 -1
- metadata +17 -8
- data/.rspec +0 -2
- data/spec/examples/bookings_spec.rb +0 -72
- data/spec/lib/deterministic/result/match_spec.rb +0 -116
@@ -1,116 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
|
4
|
-
describe Deterministic::Result::Match do
|
5
|
-
include Deterministic
|
6
|
-
|
7
|
-
it "can match Success" do
|
8
|
-
expect(
|
9
|
-
Success(1).match do
|
10
|
-
success { |v| "Success #{v}" }
|
11
|
-
failure { |v| "Failure #{v}" }
|
12
|
-
end
|
13
|
-
).to eq "Success 1"
|
14
|
-
end
|
15
|
-
|
16
|
-
it "can match Failure" do
|
17
|
-
expect(
|
18
|
-
Failure(1).match do
|
19
|
-
success { |v| "Success #{v}" }
|
20
|
-
failure { |v| "Failure #{v}" }
|
21
|
-
end
|
22
|
-
).to eq "Failure 1"
|
23
|
-
end
|
24
|
-
|
25
|
-
it "can match with values" do
|
26
|
-
expect(
|
27
|
-
Failure(2).match do
|
28
|
-
success { |v| "not matched s" }
|
29
|
-
success(1) { |v| "not matched s1" }
|
30
|
-
failure(1) { |v| "not matched f1" }
|
31
|
-
failure(2) { |v| "matched #{v}" }
|
32
|
-
failure(3) { |v| "not matched f3" }
|
33
|
-
end
|
34
|
-
).to eq "matched 2"
|
35
|
-
end
|
36
|
-
|
37
|
-
it "can match with classes" do
|
38
|
-
expect(
|
39
|
-
Success([1, 2, 3]).match do
|
40
|
-
success(Array) { |v| v.first }
|
41
|
-
end
|
42
|
-
).to eq 1
|
43
|
-
|
44
|
-
expect(
|
45
|
-
Success(1).match do
|
46
|
-
success(Fixnum) { |v| v }
|
47
|
-
end
|
48
|
-
).to eq 1
|
49
|
-
end
|
50
|
-
|
51
|
-
it "catch-all" do
|
52
|
-
expect(
|
53
|
-
Success(1).match do
|
54
|
-
any { "catch-all" }
|
55
|
-
end
|
56
|
-
).to eq "catch-all"
|
57
|
-
end
|
58
|
-
|
59
|
-
it "can match result" do
|
60
|
-
expect(
|
61
|
-
Failure(2).match do
|
62
|
-
success { |v| "not matched s" }
|
63
|
-
result(2) { |v| "result #{v}" }
|
64
|
-
failure(3) { |v| "not matched f3" }
|
65
|
-
end
|
66
|
-
).to eq "result 2"
|
67
|
-
end
|
68
|
-
|
69
|
-
it "can match with lambdas" do
|
70
|
-
expect(
|
71
|
-
Success(1).match do
|
72
|
-
failure { "not me" }
|
73
|
-
success ->(v) { v == 1 } { |v| "matched #{v}" }
|
74
|
-
end
|
75
|
-
).to eq "matched 1"
|
76
|
-
end
|
77
|
-
|
78
|
-
it "no match" do
|
79
|
-
expect {
|
80
|
-
Success(1).match do
|
81
|
-
failure { "you'll never get me" }
|
82
|
-
end
|
83
|
-
}.to raise_error Deterministic::PatternMatching::NoMatchError
|
84
|
-
end
|
85
|
-
|
86
|
-
it "can use methods from the caller's binding scope by default" do
|
87
|
-
def my_method
|
88
|
-
"it works"
|
89
|
-
end
|
90
|
-
expect(
|
91
|
-
Success(1).match do
|
92
|
-
success { my_method }
|
93
|
-
end
|
94
|
-
).to eq "it works"
|
95
|
-
end
|
96
|
-
|
97
|
-
it "allows for Result values to play with pattern matching comparisons" do
|
98
|
-
class MyErrorHash < Hash
|
99
|
-
def ==(error_type_symbol)
|
100
|
-
self[:type] == error_type_symbol
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
error_hash = MyErrorHash.new.merge(:type => :needs_more_salt, :arbitrary => 'data')
|
105
|
-
|
106
|
-
expect(
|
107
|
-
Failure(error_hash).match do
|
108
|
-
failure(:null) {|v| raise "We should not get to this point" }
|
109
|
-
failure(:needs_more_salt) do |error|
|
110
|
-
"Successfully failed with #{error[:arbitrary]}!"
|
111
|
-
end
|
112
|
-
failure { raise "We should also not get to this point" }
|
113
|
-
end
|
114
|
-
).to eq "Successfully failed with data!"
|
115
|
-
end
|
116
|
-
end
|