-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompat_test.go
More file actions
110 lines (92 loc) · 2.37 KB
/
compat_test.go
File metadata and controls
110 lines (92 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package result
import (
"errors"
"testing"
)
func TestTry(t *testing.T) {
t.Run("captures successful execution", func(t *testing.T) {
result := Try(func() int {
return 42
})
if !result.IsOk() {
t.Error("Try should succeed for non-panicking function")
}
if result.Unwrap() != 42 {
t.Errorf("value = %d, want 42", result.Unwrap())
}
})
t.Run("captures panic as error", func(t *testing.T) {
result := Try(func() int {
panic("something went wrong")
})
if !result.IsErr() {
t.Error("Try should capture panic as error")
}
err := result.UnwrapErr()
if err == nil {
t.Fatal("error should not be nil")
}
if !contains(err.Error(), "panic") {
t.Errorf("error should mention panic, got: %v", err)
}
})
t.Run("captures panic with error value", func(t *testing.T) {
result := Try(func() string {
panic(errors.New("test error"))
})
if !result.IsErr() {
t.Error("Try should capture panic")
}
})
t.Run("handles nil panic", func(t *testing.T) {
result := Try(func() int {
panic(nil)
})
if !result.IsErr() {
t.Error("Try should capture nil panic")
}
})
}
func TestTryWith(t *testing.T) {
t.Run("converts successful (T, error) to Ok", func(t *testing.T) {
result := TryWith(func() (int, error) {
return 42, nil
})
if !result.IsOk() {
t.Error("TryWith should succeed when error is nil")
}
if result.Unwrap() != 42 {
t.Errorf("value = %d, want 42", result.Unwrap())
}
})
t.Run("converts (T, error) with error to Err", func(t *testing.T) {
expectedErr := errors.New("test error")
result := TryWith(func() (int, error) {
return 0, expectedErr
})
if !result.IsErr() {
t.Error("TryWith should fail when error is non-nil")
}
if result.UnwrapErr() != expectedErr {
t.Errorf("error = %v, want %v", result.UnwrapErr(), expectedErr)
}
})
t.Run("is equivalent to From", func(t *testing.T) {
fn := func() (string, error) {
return "hello", nil
}
r1 := TryWith(fn)
r2 := From(fn())
if r1.IsErr() != r2.IsErr() {
t.Error("TryWith and From should have same result")
}
if r1.IsOk() && r1.Unwrap() != r2.Unwrap() {
t.Error("TryWith and From should have same value")
}
})
}
// Helper function for tests
func contains(s, substr string) bool {
return len(s) >= len(substr) && (s == substr || len(s) > len(substr) &&
(s[:len(substr)] == substr || s[len(s)-len(substr):] == substr))
}