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