]>
Commit | Line | Data |
---|---|---|
33265393 JH |
1 | package hotline |
2 | ||
3 | import ( | |
4 | "fmt" | |
5 | "github.com/stretchr/testify/assert" | |
6 | "testing" | |
7 | ) | |
8 | ||
9 | func Test_byteToInt(t *testing.T) { | |
10 | type args struct { | |
11 | bytes []byte | |
12 | } | |
13 | tests := []struct { | |
14 | name string | |
15 | args args | |
16 | want int | |
17 | wantErr assert.ErrorAssertionFunc | |
18 | }{ | |
19 | { | |
20 | name: "with 2 bytes of input", | |
21 | args: args{bytes: []byte{0, 1}}, | |
22 | want: 1, | |
23 | wantErr: assert.NoError, | |
24 | }, | |
25 | { | |
26 | name: "with 4 bytes of input", | |
27 | args: args{bytes: []byte{0, 1, 0, 0}}, | |
28 | want: 65536, | |
29 | wantErr: assert.NoError, | |
30 | }, | |
31 | { | |
32 | name: "with invalid number of bytes of input", | |
33 | args: args{bytes: []byte{1, 0, 0, 0, 0, 0, 0, 0}}, | |
34 | want: 0, | |
35 | wantErr: assert.Error, | |
36 | }, | |
37 | } | |
38 | for _, tt := range tests { | |
39 | t.Run(tt.name, func(t *testing.T) { | |
40 | got, err := byteToInt(tt.args.bytes) | |
41 | if !tt.wantErr(t, err, fmt.Sprintf("byteToInt(%v)", tt.args.bytes)) { | |
42 | return | |
43 | } | |
44 | assert.Equalf(t, tt.want, got, "byteToInt(%v)", tt.args.bytes) | |
45 | }) | |
46 | } | |
47 | } |