Browse Source

remove dependency on assert lib

Darien Raymond 6 years ago
parent
commit
8e131bcd1f

+ 12 - 11
common/buf/multi_buffer_test.go

@@ -9,33 +9,34 @@ import (
 
 	"v2ray.com/core/common"
 	. "v2ray.com/core/common/buf"
-	. "v2ray.com/ext/assert"
 )
 
 func TestMultiBufferRead(t *testing.T) {
-	assert := With(t)
-
 	b1 := New()
-	b1.WriteString("ab")
+	common.Must2(b1.WriteString("ab"))
 
 	b2 := New()
-	b2.WriteString("cd")
+	common.Must2(b2.WriteString("cd"))
 	mb := MultiBuffer{b1, b2}
 
 	bs := make([]byte, 32)
 	_, nBytes := SplitBytes(mb, bs)
-	assert(nBytes, Equals, 4)
-	assert(bs[:nBytes], Equals, []byte("abcd"))
+	if nBytes != 4 {
+		t.Error("expect 4 bytes split, but got ", nBytes)
+	}
+	if r := cmp.Diff(bs[:nBytes], []byte("abcd")); r != "" {
+		t.Error(r)
+	}
 }
 
 func TestMultiBufferAppend(t *testing.T) {
-	assert := With(t)
-
 	var mb MultiBuffer
 	b := New()
-	b.WriteString("ab")
+	common.Must2(b.WriteString("ab"))
 	mb = append(mb, b)
-	assert(mb.Len(), Equals, int32(2))
+	if mb.Len() != 2 {
+		t.Error("expected length 2, but got ", mb.Len())
+	}
 }
 
 func TestMultiBufferSliceBySizeLarge(t *testing.T) {

+ 29 - 14
common/common_test.go

@@ -5,25 +5,40 @@ import (
 	"testing"
 
 	. "v2ray.com/core/common"
-	. "v2ray.com/ext/assert"
 )
 
 func TestMust(t *testing.T) {
-	assert := With(t)
-
-	f := func() error {
-		return errors.New("test error")
+	hasPanic := func(f func()) (ret bool) {
+		defer func() {
+			if r := recover(); r != nil {
+				ret = true
+			}
+		}()
+		f()
+		return false
 	}
 
-	assert(func() { Must(f()) }, Panics)
-}
-
-func TestMust2(t *testing.T) {
-	assert := With(t)
-
-	f := func() (interface{}, error) {
-		return nil, errors.New("test error")
+	testCases := []struct {
+		Input func()
+		Panic bool
+	}{
+		{
+			Panic: true,
+			Input: func() { Must(func() error { return errors.New("test error") }()) },
+		},
+		{
+			Panic: true,
+			Input: func() { Must2(func() (int, error) { return 0, errors.New("test error") }()) },
+		},
+		{
+			Panic: false,
+			Input: func() { Must(func() error { return nil }()) },
+		},
 	}
 
-	assert(func() { Must2(f()) }, Panics)
+	for idx, test := range testCases {
+		if hasPanic(test.Input) != test.Panic {
+			t.Error("test case #", idx, " expect panic ", test.Panic, " but actually not")
+		}
+	}
 }

+ 6 - 5
transport/internet/headers/srtp/srtp_test.go

@@ -4,17 +4,15 @@ import (
 	"context"
 	"testing"
 
+	"v2ray.com/core/common"
 	"v2ray.com/core/common/buf"
 	. "v2ray.com/core/transport/internet/headers/srtp"
-	. "v2ray.com/ext/assert"
 )
 
 func TestSRTPWrite(t *testing.T) {
-	assert := With(t)
-
 	content := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g'}
 	srtpRaw, err := New(context.Background(), &Config{})
-	assert(err, IsNil)
+	common.Must(err)
 
 	srtp := srtpRaw.(*SRTP)
 
@@ -22,5 +20,8 @@ func TestSRTPWrite(t *testing.T) {
 	srtp.Serialize(payload.Extend(srtp.Size()))
 	payload.Write(content)
 
-	assert(payload.Len(), Equals, int32(len(content))+srtp.Size())
+	expectedLen := int32(len(content)) + srtp.Size()
+	if payload.Len() != expectedLen {
+		t.Error("expected ", expectedLen, " of bytes, but got ", payload.Len())
+	}
 }

+ 11 - 9
transport/internet/kcp/crypt_test.go

@@ -3,13 +3,13 @@ package kcp_test
 import (
 	"testing"
 
+	"github.com/google/go-cmp/cmp"
+
+	"v2ray.com/core/common"
 	. "v2ray.com/core/transport/internet/kcp"
-	. "v2ray.com/ext/assert"
 )
 
 func TestSimpleAuthenticator(t *testing.T) {
-	assert := With(t)
-
 	cache := make([]byte, 512)
 
 	payload := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g'}
@@ -17,13 +17,13 @@ func TestSimpleAuthenticator(t *testing.T) {
 	auth := NewSimpleAuthenticator()
 	b := auth.Seal(cache[:0], nil, payload, nil)
 	c, err := auth.Open(cache[:0], nil, b, nil)
-	assert(err, IsNil)
-	assert(c, Equals, payload)
+	common.Must(err)
+	if r := cmp.Diff(c, payload); r != "" {
+		t.Error(r)
+	}
 }
 
 func TestSimpleAuthenticator2(t *testing.T) {
-	assert := With(t)
-
 	cache := make([]byte, 512)
 
 	payload := []byte{'a', 'b'}
@@ -31,6 +31,8 @@ func TestSimpleAuthenticator2(t *testing.T) {
 	auth := NewSimpleAuthenticator()
 	b := auth.Seal(cache[:0], nil, payload, nil)
 	c, err := auth.Open(cache[:0], nil, b, nil)
-	assert(err, IsNil)
-	assert(c, Equals, payload)
+	common.Must(err)
+	if r := cmp.Diff(c, payload); r != "" {
+		t.Error(r)
+	}
 }

+ 41 - 45
transport/pipe/pipe_test.go

@@ -1,19 +1,17 @@
 package pipe_test
 
 import (
-	"context"
+	"errors"
 	"io"
-	"sync"
 	"testing"
 	"time"
 
 	"github.com/google/go-cmp/cmp"
+	"golang.org/x/sync/errgroup"
 
 	"v2ray.com/core/common"
 	"v2ray.com/core/common/buf"
-	"v2ray.com/core/common/task"
 	. "v2ray.com/core/transport/pipe"
-	. "v2ray.com/ext/assert"
 )
 
 func TestPipeReadWrite(t *testing.T) {
@@ -52,37 +50,41 @@ func TestPipeInterrupt(t *testing.T) {
 }
 
 func TestPipeClose(t *testing.T) {
-	assert := With(t)
-
 	pReader, pWriter := New(WithSizeLimit(1024))
 	payload := []byte{'a', 'b', 'c', 'd'}
 	b := buf.New()
-	b.Write(payload)
-	assert(pWriter.WriteMultiBuffer(buf.MultiBuffer{b}), IsNil)
-	assert(pWriter.Close(), IsNil)
+	common.Must2(b.Write(payload))
+	common.Must(pWriter.WriteMultiBuffer(buf.MultiBuffer{b}))
+	common.Must(pWriter.Close())
 
 	rb, err := pReader.ReadMultiBuffer()
-	assert(err, IsNil)
-	assert(rb.String(), Equals, b.String())
+	common.Must(err)
+	if rb.String() != string(payload) {
+		t.Fatal("expect content ", string(payload), " but actually ", rb.String())
+	}
 
 	rb, err = pReader.ReadMultiBuffer()
-	assert(err, Equals, io.EOF)
-	assert(rb.IsEmpty(), IsTrue)
+	if err != io.EOF {
+		t.Fatal("expected EOF, but got ", err)
+	}
+	if !rb.IsEmpty() {
+		t.Fatal("expect empty buffer, but got ", rb.String())
+	}
 }
 
 func TestPipeLimitZero(t *testing.T) {
-	assert := With(t)
-
 	pReader, pWriter := New(WithSizeLimit(0))
 	bb := buf.New()
-	bb.Write([]byte{'a', 'b'})
-	assert(pWriter.WriteMultiBuffer(buf.MultiBuffer{bb}), IsNil)
+	common.Must2(bb.Write([]byte{'a', 'b'}))
+	common.Must(pWriter.WriteMultiBuffer(buf.MultiBuffer{bb}))
 
-	err := task.Run(context.Background(), func() error {
+	var errg errgroup.Group
+	errg.Go(func() error {
 		b := buf.New()
 		b.Write([]byte{'c', 'd'})
 		return pWriter.WriteMultiBuffer(buf.MultiBuffer{b})
-	}, func() error {
+	})
+	errg.Go(func() error {
 		time.Sleep(time.Second)
 
 		var container buf.MultiBufferContainer
@@ -90,49 +92,43 @@ func TestPipeLimitZero(t *testing.T) {
 			return err
 		}
 
-		assert(container.String(), Equals, "abcd")
+		if r := cmp.Diff(container.String(), "abcd"); r != "" {
+			return errors.New(r)
+		}
 		return nil
-	}, func() error {
+	})
+	errg.Go(func() error {
 		time.Sleep(time.Second * 2)
-		pWriter.Close()
-		return nil
+		return pWriter.Close()
 	})
-
-	assert(err, IsNil)
+	if err := errg.Wait(); err != nil {
+		t.Error(err)
+	}
 }
 
 func TestPipeWriteMultiThread(t *testing.T) {
-	assert := With(t)
-
 	pReader, pWriter := New(WithSizeLimit(0))
 
-	var wg sync.WaitGroup
-
+	var errg errgroup.Group
 	for i := 0; i < 10; i++ {
-		wg.Add(1)
-		go func() {
+		errg.Go(func() error {
 			b := buf.New()
 			b.WriteString("abcd")
-			pWriter.WriteMultiBuffer(buf.MultiBuffer{b})
-			wg.Done()
-		}()
+			return pWriter.WriteMultiBuffer(buf.MultiBuffer{b})
+		})
 	}
-
-	time.Sleep(time.Millisecond * 100)
-
-	pWriter.Close()
-	wg.Wait()
+	errg.Wait()
 
 	b, err := pReader.ReadMultiBuffer()
-	assert(err, IsNil)
-	assert(b[0].Bytes(), Equals, []byte{'a', 'b', 'c', 'd'})
+	common.Must(err)
+	if r := cmp.Diff(b[0].Bytes(), []byte{'a', 'b', 'c', 'd'}); r != "" {
+		t.Error(r)
+	}
 }
 
 func TestInterfaces(t *testing.T) {
-	assert := With(t)
-
-	assert((*Reader)(nil), Implements, (*buf.Reader)(nil))
-	assert((*Reader)(nil), Implements, (*buf.TimeoutReader)(nil))
+	_ = (buf.Reader)(new(Reader))
+	_ = (buf.TimeoutReader)(new(Reader))
 }
 
 func BenchmarkPipeReadWrite(b *testing.B) {