]>
Commit | Line | Data |
---|---|---|
6988a057 JH |
1 | package hotline |
2 | ||
3 | import ( | |
b25c4a19 | 4 | "bytes" |
6988a057 JH |
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{ | |
a2ef262a | 31 | ID: [2]byte{ |
6988a057 JH |
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) { | |
9cf66aea JH |
47 | var user User |
48 | _, err := user.Write(tt.args.b) | |
6988a057 JH |
49 | if (err != nil) != tt.wantErr { |
50 | t.Errorf("ReadUser() error = %v, wantErr %v", err, tt.wantErr) | |
51 | return | |
52 | } | |
9cf66aea JH |
53 | if !assert.Equal(t, tt.want, &user) { |
54 | t.Errorf("ReadUser() got = %v, want %v", user, tt.want) | |
6988a057 JH |
55 | } |
56 | }) | |
57 | } | |
58 | } | |
59 | ||
6988a057 JH |
60 | func TestNegatedUserString(t *testing.T) { |
61 | type args struct { | |
62 | encodedString []byte | |
63 | } | |
64 | tests := []struct { | |
65 | name string | |
66 | args args | |
b25c4a19 | 67 | want []byte |
6988a057 JH |
68 | }{ |
69 | { | |
b25c4a19 | 70 | name: "encodes bytes to expected string", |
6988a057 JH |
71 | args: args{ |
72 | encodedString: []byte("guest"), | |
73 | }, | |
b25c4a19 JH |
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 | }, | |
5c34f875 | 81 | want: []byte{0x99, 0x90, 0x90, 0xce}, |
6988a057 JH |
82 | }, |
83 | } | |
84 | for _, tt := range tests { | |
85 | t.Run(tt.name, func(t *testing.T) { | |
fd740bc4 | 86 | if got := EncodeString(tt.args.encodedString); !bytes.Equal(got, tt.want) { |
b25c4a19 | 87 | t.Errorf("NegatedUserString() = %x, want %x", got, tt.want) |
6988a057 JH |
88 | } |
89 | }) | |
90 | } | |
91 | } |