From 08a96e5fe12e9020d27269fe6eb2da815ec81551 Mon Sep 17 00:00:00 2001 From: V2Ray Date: Wed, 9 Sep 2015 14:51:26 +0200 Subject: [PATCH] Unit test library to simplify test writing --- io/socks/socks_test.go | 55 ++++++++++++++--------------------- testing/unit/assertions.go | 31 ++++++++++++++++++++ testing/unit/bytessubject.go | 38 ++++++++++++++++++++++++ testing/unit/bytesubject.go | 48 ++++++++++++++++++++++++++++++ testing/unit/intsubject.go | 48 ++++++++++++++++++++++++++++++ testing/unit/subject.go | 31 ++++++++++++++++++++ testing/unit/uint16subject.go | 48 ++++++++++++++++++++++++++++++ 7 files changed, 266 insertions(+), 33 deletions(-) create mode 100644 testing/unit/assertions.go create mode 100644 testing/unit/bytessubject.go create mode 100644 testing/unit/bytesubject.go create mode 100644 testing/unit/intsubject.go create mode 100644 testing/unit/subject.go create mode 100644 testing/unit/uint16subject.go diff --git a/io/socks/socks_test.go b/io/socks/socks_test.go index f1b48c5c5..4e05fe0da 100644 --- a/io/socks/socks_test.go +++ b/io/socks/socks_test.go @@ -3,9 +3,13 @@ package socks import ( "bytes" "testing" + + "github.com/v2ray/v2ray-core/testing/unit" ) func TestAuthenticationRequestRead(t *testing.T) { + assert := unit.Assert(t) + rawRequest := []byte{ 0x05, // version 0x01, // nMethods @@ -15,31 +19,26 @@ func TestAuthenticationRequestRead(t *testing.T) { if err != nil { t.Errorf("Unexpected error %v", err) } - if request.version != 0x05 { - t.Errorf("Expected version 5, but got %d", request.version) - } - if request.nMethods != 0x01 { - t.Errorf("Expected nMethod 1, but got %d", request.nMethods) - } - if request.authMethods[0] != 0x02 { - t.Errorf("Expected method 2, but got %d", request.authMethods[0]) - } + assert.Byte(request.version).Named("Version").Equals(0x05) + assert.Byte(request.nMethods).Named("#Methods").Equals(0x01) + assert.Byte(request.authMethods[0]).Named("Auth Method").Equals(0x02) } func TestAuthenticationResponseToBytes(t *testing.T) { + assert := unit.Assert(t) + socksVersion := uint8(5) authMethod := uint8(1) response := Socks5AuthenticationResponse{socksVersion, authMethod} bytes := response.ToBytes() - if bytes[0] != socksVersion { - t.Errorf("Unexpected Socks version %d", bytes[0]) - } - if bytes[1] != authMethod { - t.Errorf("Unexpected Socks auth method %d", bytes[1]) - } + + assert.Byte(bytes[0]).Named("Version").Equals(socksVersion) + assert.Byte(bytes[1]).Named("Auth Method").Equals(authMethod) } func TestRequestRead(t *testing.T) { + assert := unit.Assert(t) + rawRequest := []byte{ 0x05, // version 0x01, // cmd connect @@ -52,24 +51,16 @@ func TestRequestRead(t *testing.T) { if err != nil { t.Errorf("Unexpected error %v", err) } - if request.Version != 0x05 { - t.Errorf("Expected version 5, but got %d", request.Version) - } - if request.Command != 0x01 { - t.Errorf("Expected command 1, but got %d", request.Command) - } - if request.AddrType != 0x01 { - t.Errorf("Expected addresstype 1, but got %d", request.AddrType) - } - if !bytes.Equal([]byte{0x72, 0x72, 0x72, 0x72}, request.IPv4[:]) { - t.Errorf("Expected IPv4 address 114.114.114.114, but got %v", request.IPv4[:]) - } - if request.Port != 53 { - t.Errorf("Expected port 53, but got %d", request.Port) - } + assert.Byte(request.Version).Named("Version").Equals(0x05) + assert.Byte(request.Command).Named("Command").Equals(0x01) + assert.Byte(request.AddrType).Named("Address Type").Equals(0x01) + assert.Bytes(request.IPv4[:]).Named("IPv4").Equals([]byte{0x72, 0x72, 0x72, 0x72}) + assert.Uint16(request.Port).Named("Port").Equals(53) } func TestResponseToBytes(t *testing.T) { + assert := unit.Assert(t) + response := Socks5Response{ socksVersion, ErrorSuccess, @@ -88,7 +79,5 @@ func TestResponseToBytes(t *testing.T) { 0x72, 0x72, 0x72, 0x72, byte(0x00), byte(0x035), } - if !bytes.Equal(rawResponse, expectedBytes) { - t.Errorf("Expected response %v, but got %v", expectedBytes, rawResponse) - } + assert.Bytes(rawResponse).Named("raw response").Equals(expectedBytes) } diff --git a/testing/unit/assertions.go b/testing/unit/assertions.go new file mode 100644 index 000000000..889863a1c --- /dev/null +++ b/testing/unit/assertions.go @@ -0,0 +1,31 @@ +package unit + +import ( + "testing" +) + +type Assertion struct { + t *testing.T +} + +func Assert(t *testing.T) *Assertion { + assert := new(Assertion) + assert.t = t + return assert +} + +func (a *Assertion) Int(value int) *IntSubject { + return NewIntSubject(NewSubject(a), value) +} + +func (a *Assertion) Uint16(value uint16) *Uint16Subject { + return NewUint16Subject(NewSubject(a), value) +} + +func (a *Assertion) Byte(value byte) *ByteSubject { + return NewByteSubject(NewSubject(a), value) +} + +func (a *Assertion) Bytes(value []byte) *BytesSubject { + return NewBytesSubject(NewSubject(a), value) +} diff --git a/testing/unit/bytessubject.go b/testing/unit/bytessubject.go new file mode 100644 index 000000000..e68e79e89 --- /dev/null +++ b/testing/unit/bytessubject.go @@ -0,0 +1,38 @@ +package unit + +import ( + "bytes" + "fmt" +) + +type BytesSubject struct { + *Subject + value []byte +} + +func NewBytesSubject(base *Subject, value []byte) *BytesSubject { + subject := new(BytesSubject) + subject.Subject = base + subject.value = value + return subject +} + +func (subject *BytesSubject) Named(name string) *BytesSubject { + subject.Subject.Named(name) + return subject +} + +func (subject *BytesSubject) Fail(verb string, other []byte) { + otherString := fmt.Sprintf("%v", other) + subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + otherString + ">.") +} + +func (subject *BytesSubject) DisplayString() string { + return subject.Subject.DisplayString(fmt.Sprintf("%v", subject.value)) +} + +func (subject *BytesSubject) Equals(expectation []byte) { + if !bytes.Equal(subject.value, expectation) { + subject.Fail("is equal to", expectation) + } +} diff --git a/testing/unit/bytesubject.go b/testing/unit/bytesubject.go new file mode 100644 index 000000000..58277384a --- /dev/null +++ b/testing/unit/bytesubject.go @@ -0,0 +1,48 @@ +package unit + +import ( + "strconv" +) + +type ByteSubject struct { + *Subject + value byte +} + +func NewByteSubject(base *Subject, value byte) *ByteSubject { + subject := new(ByteSubject) + subject.Subject = base + subject.value = value + return subject +} + +func (subject *ByteSubject) Named(name string) *ByteSubject { + subject.Subject.Named(name) + return subject +} + +func (subject *ByteSubject) Fail(verb string, other byte) { + subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + strconv.Itoa(int(other)) + ">.") +} + +func (subject *ByteSubject) DisplayString() string { + return subject.Subject.DisplayString(strconv.Itoa(int(subject.value))) +} + +func (subject *ByteSubject) Equals(expectation byte) { + if subject.value != expectation { + subject.Fail("is equal to", expectation) + } +} + +func (subject *ByteSubject) GreaterThan(expectation byte) { + if subject.value <= expectation { + subject.Fail("is greater than", expectation) + } +} + +func (subject *ByteSubject) LessThan(expectation byte) { + if subject.value >= expectation { + subject.Fail("is less than", expectation) + } +} diff --git a/testing/unit/intsubject.go b/testing/unit/intsubject.go new file mode 100644 index 000000000..5dec9a401 --- /dev/null +++ b/testing/unit/intsubject.go @@ -0,0 +1,48 @@ +package unit + +import ( + "strconv" +) + +type IntSubject struct { + *Subject + value int +} + +func NewIntSubject(base *Subject, value int) *IntSubject { + subject := new(IntSubject) + subject.Subject = base + subject.value = value + return subject +} + +func (subject *IntSubject) Named(name string) *IntSubject { + subject.Subject.Named(name) + return subject +} + +func (subject *IntSubject) Fail(verb string, other int) { + subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + strconv.Itoa(other) + ">.") +} + +func (subject *IntSubject) DisplayString() string { + return subject.Subject.DisplayString(strconv.Itoa(subject.value)) +} + +func (subject *IntSubject) Equals(expectation int) { + if subject.value != expectation { + subject.Fail("is equal to", expectation) + } +} + +func (subject *IntSubject) GreaterThan(expectation int) { + if subject.value <= expectation { + subject.Fail("is greater than", expectation) + } +} + +func (subject *IntSubject) LessThan(expectation int) { + if subject.value >= expectation { + subject.Fail("is less than", expectation) + } +} diff --git a/testing/unit/subject.go b/testing/unit/subject.go new file mode 100644 index 000000000..c6d835143 --- /dev/null +++ b/testing/unit/subject.go @@ -0,0 +1,31 @@ +package unit + +type Subject struct { + assert *Assertion + name string +} + +func NewSubject(assert *Assertion) *Subject { + subject := new(Subject) + subject.assert = assert + subject.name = "" + return subject +} + +func (subject *Subject) FailWithMessage(message string) { + subject.assert.t.Error(message) +} + +func (subject *Subject) Named(name string) { + subject.name = name +} + +func (subject *Subject) DisplayString(value string) string { + if len(value) == 0 { + value = "unknown" + } + if len(subject.name) == 0 { + return "<" + value + ">" + } + return subject.name + "(<" + value + ">)" +} diff --git a/testing/unit/uint16subject.go b/testing/unit/uint16subject.go new file mode 100644 index 000000000..b1a3cace6 --- /dev/null +++ b/testing/unit/uint16subject.go @@ -0,0 +1,48 @@ +package unit + +import ( + "strconv" +) + +type Uint16Subject struct { + *Subject + value uint16 +} + +func NewUint16Subject(base *Subject, value uint16) *Uint16Subject { + subject := new(Uint16Subject) + subject.Subject = base + subject.value = value + return subject +} + +func (subject *Uint16Subject) Named(name string) *Uint16Subject { + subject.Subject.Named(name) + return subject +} + +func (subject *Uint16Subject) Fail(verb string, other uint16) { + subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + strconv.Itoa(int(other)) + ">.") +} + +func (subject *Uint16Subject) DisplayString() string { + return subject.Subject.DisplayString(strconv.Itoa(int(subject.value))) +} + +func (subject *Uint16Subject) Equals(expectation uint16) { + if subject.value != expectation { + subject.Fail("is equal to", expectation) + } +} + +func (subject *Uint16Subject) GreaterThan(expectation uint16) { + if subject.value <= expectation { + subject.Fail("is greater than", expectation) + } +} + +func (subject *Uint16Subject) LessThan(expectation uint16) { + if subject.value >= expectation { + subject.Fail("is less than", expectation) + } +}