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