Browse Source

introduce go-cmp

Darien Raymond 7 years ago
parent
commit
ac4f868078
3 changed files with 39 additions and 34 deletions
  1. 23 21
      common/buf/buffer_test.go
  2. 4 3
      proxy/shadowsocks/config_test.go
  3. 12 10
      proxy/shadowsocks/ota_test.go

+ 23 - 21
common/buf/buffer_test.go

@@ -5,43 +5,45 @@ import (
 	"crypto/rand"
 	"crypto/rand"
 	"testing"
 	"testing"
 
 
+	"github.com/google/go-cmp/cmp"
 	"v2ray.com/core/common"
 	"v2ray.com/core/common"
 	. "v2ray.com/core/common/buf"
 	. "v2ray.com/core/common/buf"
-	"v2ray.com/core/common/compare"
-	. "v2ray.com/ext/assert"
 )
 )
 
 
 func TestBufferClear(t *testing.T) {
 func TestBufferClear(t *testing.T) {
-	assert := With(t)
-
 	buffer := New()
 	buffer := New()
 	defer buffer.Release()
 	defer buffer.Release()
 
 
 	payload := "Bytes"
 	payload := "Bytes"
 	buffer.Write([]byte(payload))
 	buffer.Write([]byte(payload))
-	assert(buffer.Len(), Equals, int32(len(payload)))
+	if diff := cmp.Diff(buffer.Bytes(), payload); diff != "" {
+		t.Error(diff)
+	}
 
 
 	buffer.Clear()
 	buffer.Clear()
-	assert(buffer.Len(), Equals, int32(0))
+	if buffer.Len() != 0 {
+		t.Error("expect 0 lenght, but got ", buffer.Len())
+	}
 }
 }
 
 
 func TestBufferIsEmpty(t *testing.T) {
 func TestBufferIsEmpty(t *testing.T) {
-	assert := With(t)
-
 	buffer := New()
 	buffer := New()
 	defer buffer.Release()
 	defer buffer.Release()
 
 
-	assert(buffer.IsEmpty(), IsTrue)
+	if buffer.IsEmpty() != true {
+		t.Error("expect empty buffer, but not")
+	}
 }
 }
 
 
 func TestBufferString(t *testing.T) {
 func TestBufferString(t *testing.T) {
-	assert := With(t)
-
 	buffer := New()
 	buffer := New()
 	defer buffer.Release()
 	defer buffer.Release()
 
 
-	common.Must2(buffer.WriteString("Test String"))
-	assert(buffer.String(), Equals, "Test String")
+	const payload = "Test String"
+	common.Must2(buffer.WriteString(payload))
+	if buffer.String() != payload {
+		t.Error("expect buffer content as ", payload, " but actually ", buffer.String())
+	}
 }
 }
 
 
 func TestBufferSlice(t *testing.T) {
 func TestBufferSlice(t *testing.T) {
@@ -49,8 +51,8 @@ func TestBufferSlice(t *testing.T) {
 		b := New()
 		b := New()
 		common.Must2(b.Write([]byte("abcd")))
 		common.Must2(b.Write([]byte("abcd")))
 		bytes := b.BytesFrom(-2)
 		bytes := b.BytesFrom(-2)
-		if err := compare.BytesEqualWithDetail(bytes, []byte{'c', 'd'}); err != nil {
-			t.Error(err)
+		if diff := cmp.Diff(bytes, []byte{'c', 'd'}); diff != "" {
+			t.Error(diff)
 		}
 		}
 	}
 	}
 
 
@@ -58,8 +60,8 @@ func TestBufferSlice(t *testing.T) {
 		b := New()
 		b := New()
 		common.Must2(b.Write([]byte("abcd")))
 		common.Must2(b.Write([]byte("abcd")))
 		bytes := b.BytesTo(-2)
 		bytes := b.BytesTo(-2)
-		if err := compare.BytesEqualWithDetail(bytes, []byte{'a', 'b'}); err != nil {
-			t.Error(err)
+		if diff := cmp.Diff(bytes, []byte{'a', 'b'}); diff != "" {
+			t.Error(diff)
 		}
 		}
 	}
 	}
 
 
@@ -67,8 +69,8 @@ func TestBufferSlice(t *testing.T) {
 		b := New()
 		b := New()
 		common.Must2(b.Write([]byte("abcd")))
 		common.Must2(b.Write([]byte("abcd")))
 		bytes := b.BytesRange(-3, -1)
 		bytes := b.BytesRange(-3, -1)
-		if err := compare.BytesEqualWithDetail(bytes, []byte{'b', 'c'}); err != nil {
-			t.Error(err)
+		if diff := cmp.Diff(bytes, []byte{'b', 'c'}); diff != "" {
+			t.Error(diff)
 		}
 		}
 	}
 	}
 }
 }
@@ -85,8 +87,8 @@ func TestBufferReadFullFrom(t *testing.T) {
 		t.Error("expect reading 1024 bytes, but actually ", n)
 		t.Error("expect reading 1024 bytes, but actually ", n)
 	}
 	}
 
 
-	if err := compare.BytesEqualWithDetail(payload, b.Bytes()); err != nil {
-		t.Error(err)
+	if diff := cmp.Diff(payload, b.Bytes()); diff != "" {
+		t.Error(diff)
 	}
 	}
 }
 }
 
 

+ 4 - 3
proxy/shadowsocks/config_test.go

@@ -4,9 +4,10 @@ import (
 	"crypto/rand"
 	"crypto/rand"
 	"testing"
 	"testing"
 
 
+	"github.com/google/go-cmp/cmp"
+
 	"v2ray.com/core/common"
 	"v2ray.com/core/common"
 	"v2ray.com/core/common/buf"
 	"v2ray.com/core/common/buf"
-	"v2ray.com/core/common/compare"
 	"v2ray.com/core/proxy/shadowsocks"
 	"v2ray.com/core/proxy/shadowsocks"
 )
 )
 
 
@@ -32,7 +33,7 @@ func TestAEADCipherUDP(t *testing.T) {
 	common.Must(cipher.EncodePacket(key, b1))
 	common.Must(cipher.EncodePacket(key, b1))
 
 
 	common.Must(cipher.DecodePacket(key, b1))
 	common.Must(cipher.DecodePacket(key, b1))
-	if err := compare.BytesEqualWithDetail(b1.Bytes(), payload); err != nil {
-		t.Error(err)
+	if diff := cmp.Diff(b1.Bytes(), payload); diff != "" {
+		t.Error(diff)
 	}
 	}
 }
 }

+ 12 - 10
proxy/shadowsocks/ota_test.go

@@ -3,33 +3,35 @@ package shadowsocks_test
 import (
 import (
 	"testing"
 	"testing"
 
 
+	"github.com/google/go-cmp/cmp"
+
+	"v2ray.com/core/common"
 	"v2ray.com/core/common/buf"
 	"v2ray.com/core/common/buf"
 	. "v2ray.com/core/proxy/shadowsocks"
 	. "v2ray.com/core/proxy/shadowsocks"
-	. "v2ray.com/ext/assert"
 )
 )
 
 
 func TestNormalChunkReading(t *testing.T) {
 func TestNormalChunkReading(t *testing.T) {
-	assert := With(t)
-
 	buffer := buf.New()
 	buffer := buf.New()
 	buffer.Write([]byte{0, 8, 39, 228, 69, 96, 133, 39, 254, 26, 201, 70, 11, 12, 13, 14, 15, 16, 17, 18})
 	buffer.Write([]byte{0, 8, 39, 228, 69, 96, 133, 39, 254, 26, 201, 70, 11, 12, 13, 14, 15, 16, 17, 18})
 	reader := NewChunkReader(buffer, NewAuthenticator(ChunkKeyGenerator(
 	reader := NewChunkReader(buffer, NewAuthenticator(ChunkKeyGenerator(
 		[]byte{21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36})))
 		[]byte{21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36})))
 	payload, err := reader.ReadMultiBuffer()
 	payload, err := reader.ReadMultiBuffer()
-	assert(err, IsNil)
-	assert(payload[0].Bytes(), Equals, []byte{11, 12, 13, 14, 15, 16, 17, 18})
+	common.Must(err)
+
+	if diff := cmp.Diff(payload[0].Bytes(), []byte{11, 12, 13, 14, 15, 16, 17, 18}); diff != "" {
+		t.Error(diff)
+	}
 }
 }
 
 
 func TestNormalChunkWriting(t *testing.T) {
 func TestNormalChunkWriting(t *testing.T) {
-	assert := With(t)
-
 	buffer := buf.New()
 	buffer := buf.New()
 	writer := NewChunkWriter(buffer, NewAuthenticator(ChunkKeyGenerator(
 	writer := NewChunkWriter(buffer, NewAuthenticator(ChunkKeyGenerator(
 		[]byte{21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36})))
 		[]byte{21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36})))
 
 
 	b := buf.New()
 	b := buf.New()
 	b.Write([]byte{11, 12, 13, 14, 15, 16, 17, 18})
 	b.Write([]byte{11, 12, 13, 14, 15, 16, 17, 18})
-	err := writer.WriteMultiBuffer(buf.NewMultiBufferValue(b))
-	assert(err, IsNil)
-	assert(buffer.Bytes(), Equals, []byte{0, 8, 39, 228, 69, 96, 133, 39, 254, 26, 201, 70, 11, 12, 13, 14, 15, 16, 17, 18})
+	common.Must(writer.WriteMultiBuffer(buf.NewMultiBufferValue(b)))
+	if diff := cmp.Diff(buffer.Bytes(), []byte{0, 8, 39, 228, 69, 96, 133, 39, 254, 26, 201, 70, 11, 12, 13, 14, 15, 16, 17, 18}); diff != "" {
+		t.Error(diff)
+	}
 }
 }