]>
Commit | Line | Data |
---|---|---|
1 | package hotline | |
2 | ||
3 | import ( | |
4 | "bytes" | |
5 | "github.com/stretchr/testify/assert" | |
6 | "testing" | |
7 | ) | |
8 | ||
9 | func TestReadUser(t *testing.T) { | |
10 | type args struct { | |
11 | b []byte | |
12 | } | |
13 | tests := []struct { | |
14 | name string | |
15 | args args | |
16 | want *User | |
17 | wantErr bool | |
18 | }{ | |
19 | { | |
20 | name: "returns expected User struct", | |
21 | args: args{ | |
22 | b: []byte{ | |
23 | 0x00, 0x01, | |
24 | 0x07, 0xd0, | |
25 | 0x00, 0x01, | |
26 | 0x00, 0x03, | |
27 | 0x61, 0x61, 0x61, | |
28 | }, | |
29 | }, | |
30 | want: &User{ | |
31 | ID: [2]byte{ | |
32 | 0x00, 0x01, | |
33 | }, | |
34 | Icon: []byte{ | |
35 | 0x07, 0xd0, | |
36 | }, | |
37 | Flags: []byte{ | |
38 | 0x00, 0x01, | |
39 | }, | |
40 | Name: "aaa", | |
41 | }, | |
42 | wantErr: false, | |
43 | }, | |
44 | } | |
45 | for _, tt := range tests { | |
46 | t.Run(tt.name, func(t *testing.T) { | |
47 | var user User | |
48 | _, err := user.Write(tt.args.b) | |
49 | if (err != nil) != tt.wantErr { | |
50 | t.Errorf("ReadUser() error = %v, wantErr %v", err, tt.wantErr) | |
51 | return | |
52 | } | |
53 | if !assert.Equal(t, tt.want, &user) { | |
54 | t.Errorf("ReadUser() got = %v, want %v", user, tt.want) | |
55 | } | |
56 | }) | |
57 | } | |
58 | } | |
59 | ||
60 | func TestNegatedUserString(t *testing.T) { | |
61 | type args struct { | |
62 | encodedString []byte | |
63 | } | |
64 | tests := []struct { | |
65 | name string | |
66 | args args | |
67 | want []byte | |
68 | }{ | |
69 | { | |
70 | name: "encodes bytes to expected string", | |
71 | args: args{ | |
72 | encodedString: []byte("guest"), | |
73 | }, | |
74 | want: []byte{0x98, 0x8a, 0x9a, 0x8c, 0x8b}, | |
75 | }, | |
76 | { | |
77 | name: "encodes bytes with numerals to expected string", | |
78 | args: args{ | |
79 | encodedString: []byte("foo1"), | |
80 | }, | |
81 | want: []byte{0x99, 0x90, 0x90, 0xce}, | |
82 | }, | |
83 | } | |
84 | for _, tt := range tests { | |
85 | t.Run(tt.name, func(t *testing.T) { | |
86 | if got := EncodeString(tt.args.encodedString); !bytes.Equal(got, tt.want) { | |
87 | t.Errorf("NegatedUserString() = %x, want %x", got, tt.want) | |
88 | } | |
89 | }) | |
90 | } | |
91 | } |