Skip to content

Commit

Permalink
remove testify dependency, use own test utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
jxsl13 committed Jul 5, 2024
1 parent fa045c6 commit 0d3af0a
Show file tree
Hide file tree
Showing 12 changed files with 730 additions and 40 deletions.
16 changes: 5 additions & 11 deletions browser/browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

"github.com/jxsl13/twapi/browser"
"github.com/stretchr/testify/require"
"github.com/jxsl13/twapi/internal/testutils/require"
)

func TestGetServerAddresses(t *testing.T) {
Expand All @@ -14,12 +14,8 @@ func TestGetServerAddresses(t *testing.T) {
start := time.Now()
u, err := browser.GetServerAddresses()
diff := time.Since(start)
if err != nil {
t.Fatal(err)
}
if len(u) == 0 {
t.Errorf("found %d server addresses in %d milliseconds", len(u), diff.Milliseconds())
}
require.NoError(t, err)
require.NotZero(t, len(u), "found %d server addresses in %d milliseconds", len(u), diff.Milliseconds())
}

func TestGetServerInfos(t *testing.T) {
Expand All @@ -28,9 +24,7 @@ func TestGetServerInfos(t *testing.T) {
start := time.Now()
u, err := browser.GetServerInfos()
diff := time.Since(start)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
t.Logf("found %d server infos in %d milliseconds", len(u), diff.Milliseconds())
}

Expand All @@ -41,7 +35,7 @@ func TestServerInfoOfSingleServer(t *testing.T) {
u, err := browser.GetServerInfosOf(SimplyzCatch)
diff := time.Since(start)
require.NoError(t, err)
require.Len(t, u, 1)
require.Len(t, 1, u)

t.Logf("found %d server infos in %d milliseconds", len(u), diff.Milliseconds())
}
4 changes: 2 additions & 2 deletions compression/huffman_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"testing"

"github.com/jxsl13/twapi/compression"
"github.com/jxsl13/twapi/internal/testutils/require"
"github.com/jxsl13/twapi/protocol"
"github.com/stretchr/testify/require"
)

func FuzzNewHuffman(f *testing.F) {
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestHuffmanCompress(t *testing.T) {
compressed := make([]byte, 1500)
n, err := h.Compress(src, compressed)
require.NoError(t, err)
require.Greater(t, n, 0)
require.Greater(t, 0, n)
}

func TestHuffmanCompressDecompress(t *testing.T) {
Expand Down
174 changes: 173 additions & 1 deletion compression/packer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"sync"
"testing"

"github.com/stretchr/testify/require"
"github.com/jxsl13/twapi/internal/testutils/require"
)

func TestReset(t *testing.T) {
Expand Down Expand Up @@ -99,5 +99,177 @@ func TestPackerAndUnpacker(t *testing.T) {
}

wg.Wait()
}

// rest

func TestUnpackRest(t *testing.T) {
u := NewUnpacker([]byte{0x01, 0xff, 0xaa})

{
got, err := u.NextInt()
require.NoError(t, err)
require.Equal(t, 1, got)
}

{
want := []byte{0xff, 0xaa}
got := u.Bytes()
require.Equal(t, want, got)
}
}

func TestUnpackClientInfo(t *testing.T) {
require := require.New(t)
u := NewUnpacker([]byte{
0x24, 0x00, 0x01, 0x00, 0x67, 0x6f, 0x70, 0x68, 0x65, 0x72, 0x00,
0x00, 0x40, 0x67, 0x72, 0x65, 0x65, 0x6e, 0x73, 0x77, 0x61, 0x72,
0x64, 0x00, 0x64, 0x75, 0x6f, 0x64, 0x6f, 0x6e, 0x6e, 0x79, 0x00,
0x00, 0x73, 0x74, 0x61, 0x6e, 0x64, 0x61, 0x72, 0x64, 0x00, 0x73,
0x74, 0x61, 0x6e, 0x64, 0x61, 0x72, 0x64, 0x00, 0x73, 0x74, 0x61,
0x6e, 0x64, 0x61, 0x72, 0x64, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00,
0x00, 0x80, 0xfc, 0xaf, 0x05, 0xeb, 0x83, 0xd0, 0x0a, 0x80, 0xfe,
0x07, 0x80, 0xfe, 0x07, 0x80, 0xfe, 0x07, 0x80, 0xfe, 0x07, 0x00,
})

{
// message id
want := 36
got, err := u.NextInt()
require.NoError(err)
require.Equal(want, got)

// client id
want = 0
got, err = u.NextInt()
require.NoError(err)
require.Equal(want, got)

_, err = u.NextBool() // Local bool
require.NoError(err)
_, err = u.NextInt() // Team int
require.NoError(err)
}

{
// name
want := "gopher"
got, err := u.NextString()
require.NoError(err)
require.Equal(want, got)

// clan
want = ""
got, err = u.NextString()
require.NoError(err)
require.Equal(want, got)

}

{
// country
want := -1
got, err := u.NextInt()
require.NoError(err)
require.Equal(want, got)
}

{
// body
want := "greensward"
got, err := u.NextString()
require.NoError(err)
require.Equal(want, got)
}
}

// unpack with state

func TestUnpackSimpleInts(t *testing.T) {
require := require.New(t)
u := NewUnpacker([]byte{0x01, 0x02, 0x03, 0x0f})

want := 1
got, err := u.NextInt()
require.NoError(err)
require.Equal(want, got)

want = 2
got, err = u.NextInt()
require.NoError(err)
require.Equal(want, got)

want = 3
got, err = u.NextInt()
require.NoError(err)
require.Equal(want, got)

want = 15
got, err = u.NextInt()
require.NoError(err)
require.Equal(want, got)
}

func TestUnpackString(t *testing.T) {
require := require.New(t)
u := NewUnpacker([]byte{'f', 'o', 'o', 0x00})

want := "foo"
got, err := u.NextString()
require.NoError(err)
require.Equal(want, got)
}

func TestUnpackTwoStrings(t *testing.T) {
require := require.New(t)
u := NewUnpacker([]byte{'f', 'o', 'o', 0x00, 'b', 'a', 'r', 0x00})

want := "foo"
got, err := u.NextString()
require.NoError(err)
require.Equal(want, got)

want = "bar"
got, err = u.NextString()
require.NoError(err)
require.Equal(want, got)
}

func TestUnpackMixed(t *testing.T) {
require := require.New(t)
u := NewUnpacker([]byte{0x0F, 0x0F, 'f', 'o', 'o', 0x00, 'b', 'a', 'r', 0x00, 0x01})

// ints
{
want := 15
got, err := u.NextInt()
require.NoError(err)
require.Equal(want, got)

want = 15
got, err = u.NextInt()
require.NoError(err)
require.Equal(want, got)
}

// strings
{
want := "foo"
got, err := u.NextString()
require.NoError(err)
require.Equal(want, got)

want = "bar"
got, err = u.NextString()
require.NoError(err)
require.Equal(want, got)
}

// ints
{
want := 1
got, err := u.NextInt()
require.NoError(err)
require.Equal(want, got)
}
}
Loading

0 comments on commit 0d3af0a

Please sign in to comment.