]> git.r.bdr.sh - rbdr/mobius/blob - hotline/user_test.go
6258316f94d5f3186baa696008009d82d4e14acb
[rbdr/mobius] / hotline / user_test.go
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 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 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) {
81 if gotDecodedString := decodeString(tt.args.encodedString); gotDecodedString != tt.wantDecodedString {
82 t.Errorf("decodeString() = %v, want %v", gotDecodedString, tt.wantDecodedString)
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
95 want []byte
96 }{
97 {
98 name: "encodes bytes to expected string",
99 args: args{
100 encodedString: []byte("guest"),
101 },
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 },
109 want: []byte{0x99, 0x90, 0x90, 0xce},
110 },
111 }
112 for _, tt := range tests {
113 t.Run(tt.name, func(t *testing.T) {
114 if got := encodeString(tt.args.encodedString); !bytes.Equal(got, tt.want) {
115 t.Errorf("NegatedUserString() = %x, want %x", got, tt.want)
116 }
117 })
118 }
119 }