]>
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: []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 | got, err := ReadUser(tt.args.b) | |
48 | if (err != nil) != tt.wantErr { | |
49 | t.Errorf("ReadUser() error = %v, wantErr %v", err, tt.wantErr) | |
50 | return | |
51 | } | |
52 | if !assert.Equal(t, tt.want, got) { | |
53 | t.Errorf("ReadUser() got = %v, want %v", got, tt.want) | |
54 | } | |
55 | }) | |
56 | } | |
57 | } | |
58 | ||
59 | func TestDecodeUserString(t *testing.T) { | |
60 | type args struct { | |
61 | encodedString []byte | |
62 | } | |
63 | tests := []struct { | |
64 | name string | |
65 | args args | |
66 | wantDecodedString string | |
67 | }{ | |
68 | { | |
69 | name: "decodes bytes to guest", | |
70 | args: args{ | |
71 | encodedString: []byte{ | |
72 | 0x98, 0x8a, 0x9a, 0x8c, 0x8b, | |
73 | }, | |
74 | }, | |
75 | wantDecodedString: "guest", | |
76 | }, | |
77 | } | |
78 | for _, tt := range tests { | |
79 | t.Run(tt.name, func(t *testing.T) { | |
80 | if gotDecodedString := DecodeUserString(tt.args.encodedString); gotDecodedString != tt.wantDecodedString { | |
81 | t.Errorf("DecodeUserString() = %v, want %v", gotDecodedString, tt.wantDecodedString) | |
82 | } | |
83 | }) | |
84 | } | |
85 | } | |
86 | ||
87 | func TestNegatedUserString(t *testing.T) { | |
88 | type args struct { | |
89 | encodedString []byte | |
90 | } | |
91 | tests := []struct { | |
92 | name string | |
93 | args args | |
94 | want []byte | |
95 | }{ | |
96 | { | |
97 | name: "encodes bytes to expected string", | |
98 | args: args{ | |
99 | encodedString: []byte("guest"), | |
100 | }, | |
101 | want: []byte{0x98, 0x8a, 0x9a, 0x8c, 0x8b}, | |
102 | }, | |
103 | { | |
104 | name: "encodes bytes with numerals to expected string", | |
105 | args: args{ | |
106 | encodedString: []byte("foo1"), | |
107 | }, | |
108 | want: []byte{0x99, 0x90, 0x90, 0xce}, | |
109 | }, | |
110 | } | |
111 | for _, tt := range tests { | |
112 | t.Run(tt.name, func(t *testing.T) { | |
113 | if got := negateString(tt.args.encodedString); !bytes.Equal(got, tt.want) { | |
114 | t.Errorf("NegatedUserString() = %x, want %x", got, tt.want) | |
115 | } | |
116 | }) | |
117 | } | |
118 | } |