-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
73 lines (64 loc) · 1.28 KB
/
util.go
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
package easyfl
import (
"bytes"
"encoding/hex"
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func concat(data ...interface{}) []byte {
var buf bytes.Buffer
for _, d := range data {
switch d := d.(type) {
case byte:
buf.WriteByte(d)
case []byte:
buf.Write(d)
case interface{ Bytes() []byte }:
buf.Write(d.Bytes())
case int:
if d < 0 || d > 255 {
panic("not a 1 byte integer value")
}
buf.WriteByte(byte(d))
default:
panic("must be 'byte', '[]byte' or 'interface{ Bytes() []byte }'")
}
}
return buf.Bytes()
}
func CatchPanicOrError(f func() error) error {
var err error
func() {
defer func() {
r := recover()
if r == nil {
return
}
var ok bool
if err, ok = r.(error); !ok {
err = fmt.Errorf("%v", r)
}
}()
err = f()
}()
return err
}
func RequireErrorWith(t *testing.T, err error, s string) {
require.Error(t, err)
require.Contains(t, err.Error(), s)
}
func Assert(cond bool, format string, args ...interface{}) {
if !cond {
panic(fmt.Sprintf("assertion failed:: "+format, args...))
}
}
func AssertNoError(err error) {
Assert(err == nil, "error: %v", err)
}
func Hex(data []byte) string {
return fmt.Sprintf("%dx%s", len(data), hex.EncodeToString(data))
}
func Fmt(data []byte) string {
return Hex(data)
}