...
1 package internal
2
3 import (
4 "errors"
5 "strings"
6 "testing"
7 )
8
9 func TestFeatureTest(t *testing.T) {
10 var called bool
11
12 fn := FeatureTest("foo", "1.0", func() error {
13 called = true
14 return nil
15 })
16
17 if called {
18 t.Error("Function was called too early")
19 }
20
21 err := fn()
22 if !called {
23 t.Error("Function wasn't called")
24 }
25
26 if err != nil {
27 t.Error("Unexpected negative result:", err)
28 }
29
30 fn = FeatureTest("bar", "2.1.1", func() error {
31 return ErrNotSupported
32 })
33
34 err = fn()
35 if err == nil {
36 t.Fatal("Unexpected positive result")
37 }
38
39 fte, ok := err.(*UnsupportedFeatureError)
40 if !ok {
41 t.Fatal("Result is not a *UnsupportedFeatureError")
42 }
43
44 if !strings.Contains(fte.Error(), "2.1.1") {
45 t.Error("UnsupportedFeatureError.Error doesn't contain version")
46 }
47
48 if !errors.Is(err, ErrNotSupported) {
49 t.Error("UnsupportedFeatureError is not ErrNotSupported")
50 }
51
52 err2 := fn()
53 if err != err2 {
54 t.Error("Didn't cache an error wrapping ErrNotSupported")
55 }
56
57 fn = FeatureTest("bar", "2.1.1", func() error {
58 return errors.New("foo")
59 })
60
61 err1, err2 := fn(), fn()
62 if err1 == err2 {
63 t.Error("Cached result of unsuccessful execution")
64 }
65 }
66
View as plain text