deterministic 0.14.1 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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