errors - errors_test.go
1 package errors
2
3 import "testing"
4
5 func TestErrors(t *testing.T) {
6 const (
7 A Error = "A"
8 B Error = "B"
9 )
10 var err error = A
11 if err != A {
12 t.Errorf("expecting err to match A")
13 }
14 if err == B {
15 t.Errorf("not expecting err to match B")
16 }
17 if err.Error() != "A" {
18 t.Errorf("expecting error string \"A\" got %q", err.Error())
19 }
20 err = B
21 if err == A {
22 t.Errorf("not expecting err to match A")
23 }
24 if err != B {
25 t.Errorf("expecting err to match B")
26 }
27 if err.Error() != "B" {
28 t.Errorf("expecting error string \"B\" got %q", err.Error())
29 }
30 if c := WithContext("CONTEXT", A); Unwrap(c) != A {
31 t.Errorf("expecting error %q, got %q", A, c)
32 }
33 }
34
35 func TestItobs(t *testing.T) {
36 tests := []struct {
37 Input int
38 Output []byte
39 }{
40 {0, []byte{'0'}},
41 {1, []byte{'1'}},
42 {2, []byte{'2'}},
43 {10, []byte{'1', '0'}},
44 {11, []byte{'1', '1'}},
45 {32, []byte{'3', '2'}},
46 {100, []byte{'1', '0', '0'}},
47 {-1, []byte{'-', '1'}},
48 {-2, []byte{'-', '2'}},
49 {-10, []byte{'-', '1', '0'}},
50 {-100, []byte{'-', '1', '0', '0'}},
51 }
52 for n, test := range tests {
53 o := itobs(test.Input)
54 if string(o) != string(test.Output) {
55 t.Errorf("test %d: expecting %s, got %s", n+1, test.Output, o)
56 }
57 }
58 }