|
| 1 | +package errors_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/sokool/errors" |
| 8 | +) |
| 9 | + |
| 10 | +func TestErrorf(t *testing.T) { |
| 11 | + type scenario struct { |
| 12 | + description string |
| 13 | + err *errors.Error // given |
| 14 | + name, code, message, string string // expects |
| 15 | + } |
| 16 | + |
| 17 | + cases := []scenario{ |
| 18 | + { |
| 19 | + description: "empty", |
| 20 | + err: errors.Errorf(""), |
| 21 | + string: "github.com/sokool/errors_test.TestErrorf#error_test.go@L20", |
| 22 | + name: "github.com/sokool/errors_test.TestErrorf", |
| 23 | + code: "error_test.go@L20", |
| 24 | + }, |
| 25 | + { |
| 26 | + description: "just message", |
| 27 | + err: errors.Errorf("hi there"), |
| 28 | + string: "github.com/sokool/errors_test.TestErrorf#error_test.go@L27: hi there", |
| 29 | + name: "github.com/sokool/errors_test.TestErrorf", |
| 30 | + code: "error_test.go@L27", |
| 31 | + message: "hi there", |
| 32 | + }, |
| 33 | + { |
| 34 | + description: "message with arguments", |
| 35 | + err: errors.Errorf("hi there %s", "man"), |
| 36 | + string: "github.com/sokool/errors_test.TestErrorf#error_test.go@L35: hi there man", |
| 37 | + name: "github.com/sokool/errors_test.TestErrorf", |
| 38 | + code: "error_test.go@L35", |
| 39 | + message: "hi there man", |
| 40 | + }, |
| 41 | + { |
| 42 | + description: "just name", |
| 43 | + err: errors.Errorf("test:"), |
| 44 | + string: "test:", |
| 45 | + name: "test", |
| 46 | + }, |
| 47 | + { |
| 48 | + description: "just code", |
| 49 | + err: errors.Errorf("#h6b7:"), |
| 50 | + string: "#h6b7", |
| 51 | + code: "h6b7", |
| 52 | + }, |
| 53 | + { |
| 54 | + description: "with name,message", |
| 55 | + err: errors.Errorf("test:hi there"), |
| 56 | + string: "test: hi there", |
| 57 | + name: "test", |
| 58 | + message: "hi there", |
| 59 | + }, |
| 60 | + { |
| 61 | + description: "with name,code,message", |
| 62 | + err: errors.Errorf("email#h1: invalid hostname "), |
| 63 | + string: "email#h1: invalid hostname", |
| 64 | + name: "email", |
| 65 | + code: "h1", |
| 66 | + message: "invalid hostname", |
| 67 | + }, |
| 68 | + { |
| 69 | + description: "with name,code,message from arguments", |
| 70 | + err: errors.Errorf("%s#%s: invalid %s", "email", "h45", "username"), |
| 71 | + string: "email#h45: invalid username", |
| 72 | + name: "email", |
| 73 | + code: "h45", |
| 74 | + message: "invalid username", |
| 75 | + }, |
| 76 | + { |
| 77 | + description: "with code,message", |
| 78 | + err: errors.Errorf("#e87:failed"), |
| 79 | + string: "#e87: failed", |
| 80 | + code: "e87", |
| 81 | + message: "failed", |
| 82 | + }, |
| 83 | + { |
| 84 | + description: "with code,name signs in wrong place", |
| 85 | + err: errors.Errorf("failed due abc: #triggered"), |
| 86 | + name: "github.com/sokool/errors_test.TestErrorf", |
| 87 | + string: "github.com/sokool/errors_test.TestErrorf#error_test.go@L85: failed due abc: #triggered", |
| 88 | + code: "error_test.go@L85", |
| 89 | + message: "failed due abc: #triggered", |
| 90 | + }, |
| 91 | + } |
| 92 | + |
| 93 | + for _, c := range cases { |
| 94 | + t.Run(c.description, func(t *testing.T) { |
| 95 | + if s := c.err.Name(); s != c.name { |
| 96 | + t.Fatalf("expected name `%s`, got `%s`", c.name, s) |
| 97 | + } |
| 98 | + if s := c.err.Code(); s != c.code { |
| 99 | + t.Fatalf("expected code `%s`, got `%s`", c.code, s) |
| 100 | + } |
| 101 | + if s := c.err.Message(); s != c.message { |
| 102 | + t.Fatalf("expected message `%s`, got `%s`", c.message, s) |
| 103 | + } |
| 104 | + if s := c.err.Error(); s != c.string { |
| 105 | + t.Fatalf("expected string `%s`, got `%s`", c.string, s) |
| 106 | + } |
| 107 | + }) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func TestGetErr(t *testing.T) { |
| 112 | + a := errors.Errorf("eloszki") |
| 113 | + b := fmt.Errorf("second %w", a) |
| 114 | + c := fmt.Errorf("third %w", b) |
| 115 | + d := errors.Err(c) |
| 116 | + |
| 117 | + if a != d { |
| 118 | + t.Fatal() |
| 119 | + } |
| 120 | + |
| 121 | + //a1 := errors.Errorf("#1") |
| 122 | + //b1 := fmt.Errorf("#2 %w", a1) |
| 123 | + |
| 124 | + x := errors.Errorf("#4 %s %w %%nio %d", "elo", fmt.Errorf("#3 %w", fmt.Errorf("#2 %w", fmt.Errorf("#1"))), 13) |
| 125 | + |
| 126 | + y := errors.ErrL(x) |
| 127 | + fmt.Println("----") |
| 128 | + |
| 129 | + for _, err := range y { |
| 130 | + fmt.Println(err) |
| 131 | + } |
| 132 | + |
| 133 | + //fmt.Println(errors.Unwrap(d1)) |
| 134 | + |
| 135 | +} |
0 commit comments