simplify testing

This commit is contained in:
v2ray
2016-05-24 21:55:46 +02:00
parent 3582b9d869
commit fc63f0432c
99 changed files with 726 additions and 817 deletions
+31 -17
View File
@@ -2,11 +2,16 @@ package assert
import (
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/common/serial"
)
func Address(value v2net.Address) *AddressSubject {
return &AddressSubject{value: value}
func (this *Assert) Address(value v2net.Address) *AddressSubject {
return &AddressSubject{
Subject: Subject{
disp: value.String(),
a: this,
},
value: value,
}
}
type AddressSubject struct {
@@ -14,53 +19,62 @@ type AddressSubject struct {
value v2net.Address
}
func (subject *AddressSubject) Named(name string) *AddressSubject {
subject.Subject.Named(name)
return subject
}
func (subject *AddressSubject) DisplayString() string {
return subject.Subject.DisplayString(subject.value.String())
func (subject *AddressSubject) NotEquals(another v2net.Address) {
if subject.value.Equals(another) {
subject.Fail("not equals to", another.String())
}
}
func (subject *AddressSubject) Equals(another v2net.Address) {
if !subject.value.Equals(another) {
subject.Fail(subject.DisplayString(), "equals to", another)
subject.Fail("equals to", another.String())
}
}
func (subject *AddressSubject) NotEqualsString(another string) {
if subject.value.String() == another {
subject.Fail("not equals to string", another)
}
}
func (subject *AddressSubject) EqualsString(another string) {
if subject.value.String() != another {
subject.Fail("equals to string", another)
}
}
func (subject *AddressSubject) IsIPv4() {
if !subject.value.IsIPv4() {
subject.Fail(subject.DisplayString(), "is", serial.StringT("an IPv4 address"))
subject.Fail("is", "an IPv4 address")
}
}
func (subject *AddressSubject) IsNotIPv4() {
if subject.value.IsIPv4() {
subject.Fail(subject.DisplayString(), "is not", serial.StringT("an IPv4 address"))
subject.Fail("is not", "an IPv4 address")
}
}
func (subject *AddressSubject) IsIPv6() {
if !subject.value.IsIPv6() {
subject.Fail(subject.DisplayString(), "is", serial.StringT("an IPv6 address"))
subject.Fail("is", "an IPv6 address")
}
}
func (subject *AddressSubject) IsNotIPv6() {
if subject.value.IsIPv6() {
subject.Fail(subject.DisplayString(), "is not", serial.StringT("an IPv6 address"))
subject.Fail("is not", "an IPv6 address")
}
}
func (subject *AddressSubject) IsDomain() {
if !subject.value.IsDomain() {
subject.Fail(subject.DisplayString(), "is", serial.StringT("a domain address"))
subject.Fail("is", "a domain address")
}
}
func (subject *AddressSubject) IsNotDomain() {
if subject.value.IsDomain() {
subject.Fail(subject.DisplayString(), "is not", serial.StringT("a domain address"))
subject.Fail("is not", "a domain address")
}
}
+13 -9
View File
@@ -1,4 +1,4 @@
package unit
package assert
import (
"bytes"
@@ -8,10 +8,19 @@ import (
"testing"
)
var tGlobal *testing.T
func On(t *testing.T) *Assert {
return &Assert{
t: t,
}
}
func Current(t *testing.T) {
tGlobal = t
type Assert struct {
t *testing.T
}
func (this *Assert) Fail(message string) {
fmt.Println(decorate(message))
this.t.Fail()
}
func getCaller() (string, int) {
@@ -59,8 +68,3 @@ func decorate(s string) string {
buf.WriteByte('\n')
return buf.String()
}
func Fail(message string) {
fmt.Println(decorate(message))
tGlobal.Fail()
}
+42
View File
@@ -0,0 +1,42 @@
package assert
import (
"strconv"
)
// Assert on a boolean variable.
func (this *Assert) Bool(value bool) *BoolSubject {
return &BoolSubject{
Subject: Subject{
disp: strconv.FormatBool(value),
a: this,
},
value: value,
}
}
type BoolSubject struct {
Subject
value bool
}
// to be equal to another boolean variable.
func (subject *BoolSubject) Equals(expectation bool) {
if subject.value != expectation {
subject.Fail("is equal to", strconv.FormatBool(expectation))
}
}
// to be true.
func (subject *BoolSubject) IsTrue() {
if subject.value != true {
subject.Fail("is", "True")
}
}
// to be false.
func (subject *BoolSubject) IsFalse() {
if subject.value != false {
subject.Fail("is", "False")
}
}
-49
View File
@@ -1,49 +0,0 @@
package assert
import (
"strconv"
)
// Assert on a boolean variable.
func Bool(value bool) *BoolSubject {
return &BoolSubject{value: value}
}
type BoolSubject struct {
Subject
value bool
}
func (subject *BoolSubject) Named(name string) *BoolSubject {
subject.Subject.Named(name)
return subject
}
func (subject *BoolSubject) Fail(verb string, other bool) {
subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + strconv.FormatBool(other) + ">.")
}
func (subject *BoolSubject) DisplayString() string {
return subject.Subject.DisplayString(strconv.FormatBool(subject.value))
}
// to be equal to another boolean variable.
func (subject *BoolSubject) Equals(expectation bool) {
if subject.value != expectation {
subject.Fail("is equal to", expectation)
}
}
// to be true.
func (subject *BoolSubject) IsTrue() {
if subject.value != true {
subject.Fail("is", true)
}
}
// to be false.
func (subject *BoolSubject) IsFalse() {
if subject.value != false {
subject.Fail("is", false)
}
}
+38
View File
@@ -0,0 +1,38 @@
package assert
import (
"github.com/v2ray/v2ray-core/common/serial"
)
func (this *Assert) Byte(value byte) *ByteSubject {
return &ByteSubject{
Subject: Subject{
disp: serial.ByteToHexString(value),
a: this,
},
value: value,
}
}
type ByteSubject struct {
Subject
value byte
}
func (subject *ByteSubject) Equals(expectation byte) {
if subject.value != expectation {
subject.Fail("is equal to", serial.ByteToHexString(expectation))
}
}
func (subject *ByteSubject) GreaterThan(expectation byte) {
if subject.value <= expectation {
subject.Fail("is greater than", serial.ByteToHexString(expectation))
}
}
func (subject *ByteSubject) LessThan(expectation byte) {
if subject.value >= expectation {
subject.Fail("is less than", serial.ByteToHexString(expectation))
}
}
+34
View File
@@ -0,0 +1,34 @@
package assert
import (
"bytes"
"github.com/v2ray/v2ray-core/common/serial"
)
func (this *Assert) Bytes(value []byte) *BytesSubject {
return &BytesSubject{
Subject: Subject{
disp: serial.BytesToHexString(value),
a: this,
},
value: value,
}
}
type BytesSubject struct {
Subject
value []byte
}
func (subject *BytesSubject) Equals(expectation []byte) {
if !bytes.Equal(subject.value, expectation) {
subject.Fail("is equal to", serial.BytesToHexString(expectation))
}
}
func (subject *BytesSubject) NotEquals(expectation []byte) {
if bytes.Equal(subject.value, expectation) {
subject.Fail("is not equal to", serial.BytesToHexString(expectation))
}
}
-41
View File
@@ -1,41 +0,0 @@
package assert
import (
"bytes"
"fmt"
)
func Bytes(value []byte) *BytesSubject {
return &BytesSubject{value: value}
}
type BytesSubject struct {
Subject
value []byte
}
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)
}
}
func (subject *BytesSubject) NotEquals(expectation []byte) {
if bytes.Equal(subject.value, expectation) {
subject.Fail("is not equal to", expectation)
}
}
-45
View File
@@ -1,45 +0,0 @@
package assert
import (
"strconv"
)
func Byte(value byte) *ByteSubject {
return &ByteSubject{value: value}
}
type ByteSubject struct {
Subject
value byte
}
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)
}
}
+26 -16
View File
@@ -2,11 +2,16 @@ package assert
import (
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/common/serial"
)
func Destination(value v2net.Destination) *DestinationSubject {
return &DestinationSubject{value: value}
func (this *Assert) Destination(value v2net.Destination) *DestinationSubject {
return &DestinationSubject{
Subject: Subject{
disp: value.String(),
a: this,
},
value: value,
}
}
type DestinationSubject struct {
@@ -14,35 +19,40 @@ type DestinationSubject struct {
value v2net.Destination
}
func (this *DestinationSubject) Named(name string) *DestinationSubject {
this.Subject.Named(name)
return this
}
func (this *DestinationSubject) DisplayString() string {
return this.Subject.DisplayString(this.value.String())
}
func (this *DestinationSubject) IsTCP() {
if !this.value.IsTCP() {
this.Fail(this.DisplayString(), "is", serial.StringT("a TCP destination"))
this.Fail("is", "a TCP destination")
}
}
func (this *DestinationSubject) IsNotTCP() {
if this.value.IsTCP() {
this.Fail(this.DisplayString(), "is not", serial.StringT("a TCP destination"))
this.Fail("is not", "a TCP destination")
}
}
func (this *DestinationSubject) IsUDP() {
if !this.value.IsUDP() {
this.Fail(this.DisplayString(), "is", serial.StringT("a UDP destination"))
this.Fail("is", "a UDP destination")
}
}
func (this *DestinationSubject) IsNotUDP() {
if this.value.IsUDP() {
this.Fail(this.DisplayString(), "is not", serial.StringT("a UDP destination"))
this.Fail("is not", "a UDP destination")
}
}
func (this *DestinationSubject) EqualsString(another string) {
if this.value.String() != another {
this.Fail("not equals to string", another)
}
}
func (this *DestinationSubject) HasAddress() *AddressSubject {
return this.a.Address(this.value.Address())
}
func (this *DestinationSubject) HasPort() *PortSubject {
return this.a.Port(this.value.Port())
}
+38
View File
@@ -0,0 +1,38 @@
package assert
func (this *Assert) Error(value error) *ErrorSubject {
valueStr := ""
if value != nil {
valueStr = value.Error()
}
return &ErrorSubject{
Subject: Subject{
a: this,
disp: valueStr,
},
value: value,
}
}
type ErrorSubject struct {
Subject
value error
}
func (subject *ErrorSubject) Equals(expectation error) {
if subject.value != expectation {
subject.Fail("is equal to", expectation.Error())
}
}
func (subject *ErrorSubject) IsNil() {
if subject.value != nil {
subject.Fail("is", "nil")
}
}
func (subject *ErrorSubject) IsNotNil() {
if subject.value == nil {
subject.Fail("is not", "nil")
}
}
-41
View File
@@ -1,41 +0,0 @@
package assert
func Error(value error) *ErrorSubject {
return &ErrorSubject{value: value}
}
type ErrorSubject struct {
Subject
value error
}
func (subject *ErrorSubject) Named(name string) *ErrorSubject {
subject.Subject.Named(name)
return subject
}
func (subject *ErrorSubject) Fail(verb string, other error) {
subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + other.Error() + ">.")
}
func (subject *ErrorSubject) DisplayString() string {
return subject.Subject.DisplayString(subject.value.Error())
}
func (subject *ErrorSubject) Equals(expectation error) {
if subject.value != expectation {
subject.Fail("is equal to", expectation)
}
}
func (subject *ErrorSubject) IsNil() {
if subject.value != nil {
subject.FailWithMessage("Not true that " + subject.DisplayString() + " is nil.")
}
}
func (subject *ErrorSubject) IsNotNil() {
if subject.value == nil {
subject.FailWithMessage("Not true that the error is not nil.")
}
}
+44
View File
@@ -0,0 +1,44 @@
package assert
import (
"github.com/v2ray/v2ray-core/common/serial"
)
func (this *Assert) Int64(value int64) *Int64Subject {
return &Int64Subject{
Subject: Subject{
a: this,
disp: serial.Int64ToString(value),
},
value: value,
}
}
type Int64Subject struct {
Subject
value int64
}
func (subject *Int64Subject) Equals(expectation int64) {
if subject.value != expectation {
subject.Fail("is equal to", serial.Int64ToString(expectation))
}
}
func (subject *Int64Subject) GreaterThan(expectation int64) {
if subject.value <= expectation {
subject.Fail("is greater than", serial.Int64ToString(expectation))
}
}
func (subject *Int64Subject) AtMost(expectation int64) {
if subject.value > expectation {
subject.Fail("is at most", serial.Int64ToString(expectation))
}
}
func (subject *Int64Subject) AtLeast(expectation int64) {
if subject.value < expectation {
subject.Fail("is at least", serial.Int64ToString(expectation))
}
}
-51
View File
@@ -1,51 +0,0 @@
package assert
import (
"strconv"
)
func Int64(value int64) *Int64Subject {
return &Int64Subject{value: value}
}
type Int64Subject struct {
Subject
value int64
}
func (subject *Int64Subject) Named(name string) *Int64Subject {
subject.Subject.Named(name)
return subject
}
func (subject *Int64Subject) Fail(verb string, other int64) {
subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + strconv.FormatInt(other, 10) + ">.")
}
func (subject *Int64Subject) DisplayString() string {
return subject.Subject.DisplayString(strconv.FormatInt(subject.value, 10))
}
func (subject *Int64Subject) Equals(expectation int64) {
if subject.value != expectation {
subject.Fail("is equal to", expectation)
}
}
func (subject *Int64Subject) GreaterThan(expectation int64) {
if subject.value <= expectation {
subject.Fail("is greater than", expectation)
}
}
func (subject *Int64Subject) AtMost(expectation int64) {
if subject.value > expectation {
subject.Fail("is at most", expectation)
}
}
func (subject *Int64Subject) AtLeast(expectation int64) {
if subject.value < expectation {
subject.Fail("is at least", expectation)
}
}
+15 -18
View File
@@ -4,38 +4,35 @@ import (
"github.com/v2ray/v2ray-core/common/serial"
)
func Int(value int) *IntSubject {
return &IntSubject{value: serial.IntLiteral(value)}
func (this *Assert) Int(value int) *IntSubject {
return &IntSubject{
Subject: Subject{
a: this,
disp: serial.IntToString(value),
},
value: value,
}
}
type IntSubject struct {
Subject
value serial.IntLiteral
}
func (subject *IntSubject) Named(name string) *IntSubject {
subject.Subject.Named(name)
return subject
}
func (subject *IntSubject) DisplayString() string {
return subject.Subject.DisplayString(subject.value.String())
value int
}
func (subject *IntSubject) Equals(expectation int) {
if subject.value.Value() != expectation {
subject.Fail(subject.DisplayString(), "is equal to", serial.IntLiteral(expectation))
if subject.value != expectation {
subject.Fail("is equal to", serial.IntToString(expectation))
}
}
func (subject *IntSubject) GreaterThan(expectation int) {
if subject.value.Value() <= expectation {
subject.Fail(subject.DisplayString(), "is greater than", serial.IntLiteral(expectation))
if subject.value <= expectation {
subject.Fail("is greater than", serial.IntToString(expectation))
}
}
func (subject *IntSubject) LessThan(expectation int) {
if subject.value.Value() >= expectation {
subject.Fail(subject.DisplayString(), "is less than", serial.IntLiteral(expectation))
if subject.value >= expectation {
subject.Fail("is less than", serial.IntToString(expectation))
}
}
+10 -15
View File
@@ -3,12 +3,16 @@ package assert
import (
"bytes"
"net"
"github.com/v2ray/v2ray-core/common/serial"
)
func IP(value net.IP) *IPSubject {
return &IPSubject{value: value}
func (this *Assert) IP(value net.IP) *IPSubject {
return &IPSubject{
Subject: Subject{
a: this,
disp: value.String(),
},
value: value,
}
}
type IPSubject struct {
@@ -16,23 +20,14 @@ type IPSubject struct {
value net.IP
}
func (subject *IPSubject) Named(name string) *IPSubject {
subject.Subject.Named(name)
return subject
}
func (subject *IPSubject) DisplayString() string {
return subject.Subject.DisplayString(subject.value.String())
}
func (subject *IPSubject) IsNil() {
if subject.value != nil {
subject.Fail(subject.DisplayString(), "is", serial.StringT("nil"))
subject.Fail("is", "nil")
}
}
func (subject *IPSubject) Equals(ip net.IP) {
if !bytes.Equal([]byte(subject.value), []byte(ip)) {
subject.Fail(subject.DisplayString(), "equals to", ip)
subject.Fail("equals to", ip.String())
}
}
+38
View File
@@ -0,0 +1,38 @@
package assert
import (
"github.com/v2ray/v2ray-core/common/serial"
)
func (this *Assert) Pointer(value interface{}) *PointerSubject {
return &PointerSubject{
Subject: Subject{
a: this,
disp: serial.PointerToString(value),
},
value: value,
}
}
type PointerSubject struct {
Subject
value interface{}
}
func (subject *PointerSubject) Equals(expectation interface{}) {
if subject.value != expectation {
subject.Fail("is equal to", serial.PointerToString(expectation))
}
}
func (subject *PointerSubject) IsNil() {
if subject.value != nil {
subject.Fail("is", "nil")
}
}
func (subject *PointerSubject) IsNotNil() {
if subject.value == nil {
subject.Fail("is not", "nil")
}
}
-45
View File
@@ -1,45 +0,0 @@
package assert
import (
"fmt"
)
func Pointer(value interface{}) *PointerSubject {
return &PointerSubject{value: value}
}
type PointerSubject struct {
Subject
value interface{}
}
func (subject *PointerSubject) Named(name string) *PointerSubject {
subject.Subject.Named(name)
return subject
}
func (subject *PointerSubject) Fail(verb string, other interface{}) {
subject.FailWithMessage(fmt.Sprintf("Not true that %s %s <%v>.", subject.DisplayString(), verb, other))
}
func (subject *PointerSubject) DisplayString() string {
return subject.Subject.DisplayString(fmt.Sprintf("%v", subject.value))
}
func (subject *PointerSubject) Equals(expectation interface{}) {
if subject.value != expectation {
subject.Fail("is equal to", expectation)
}
}
func (subject *PointerSubject) IsNil() {
if subject.value != nil {
subject.FailWithMessage("Not true that " + subject.DisplayString() + " is nil.")
}
}
func (subject *PointerSubject) IsNotNil() {
if subject.value == nil {
subject.FailWithMessage("Not true that " + subject.DisplayString() + " is not nil.")
}
}
+12 -16
View File
@@ -2,11 +2,16 @@ package assert
import (
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/common/serial"
)
func Port(value v2net.Port) *PortSubject {
return &PortSubject{value: value}
func (this *Assert) Port(value v2net.Port) *PortSubject {
return &PortSubject{
Subject: Subject{
a: this,
disp: value.String(),
},
value: value,
}
}
type PortSubject struct {
@@ -14,35 +19,26 @@ type PortSubject struct {
value v2net.Port
}
func (subject *PortSubject) Named(name string) *PortSubject {
subject.Subject.Named(name)
return subject
}
func (subject *PortSubject) DisplayString() string {
return subject.Subject.DisplayString(subject.value.String())
}
func (subject *PortSubject) Equals(expectation v2net.Port) {
if subject.value.Value() != expectation.Value() {
subject.Fail(subject.DisplayString(), "is equal to", expectation)
subject.Fail("is equal to", expectation.String())
}
}
func (subject *PortSubject) GreaterThan(expectation v2net.Port) {
if subject.value.Value() <= expectation.Value() {
subject.Fail(subject.DisplayString(), "is greater than", expectation)
subject.Fail("is greater than", expectation.String())
}
}
func (subject *PortSubject) LessThan(expectation v2net.Port) {
if subject.value.Value() >= expectation.Value() {
subject.Fail(subject.DisplayString(), "is less than", expectation)
subject.Fail("is less than", expectation.String())
}
}
func (subject *PortSubject) IsValid() {
if subject.value == 0 {
subject.Fail(subject.DisplayString(), "is", serial.StringT("a valid port"))
subject.Fail("is", "a valid port")
}
}
+44
View File
@@ -0,0 +1,44 @@
package assert
import (
"strings"
)
func (this *Assert) String(value string) *StringSubject {
return &StringSubject{
Subject: Subject{
a: this,
disp: value,
},
value: value,
}
}
type StringSubject struct {
Subject
value string
}
func (subject *StringSubject) Equals(expectation string) {
if subject.value != expectation {
subject.Fail("is equal to", expectation)
}
}
func (subject *StringSubject) NotEquals(expectation string) {
if subject.value == expectation {
subject.Fail("is not equal to ", expectation)
}
}
func (subject *StringSubject) Contains(substring string) {
if !strings.Contains(subject.value, substring) {
subject.Fail("contains", substring)
}
}
func (subject *StringSubject) NotContains(substring string) {
if strings.Contains(subject.value, substring) {
subject.Fail("doesn't contain", substring)
}
}
-53
View File
@@ -1,53 +0,0 @@
package assert
import (
"strings"
"github.com/v2ray/v2ray-core/common/serial"
)
func StringLiteral(value string) *StringSubject {
return String(serial.StringT((value)))
}
func String(value serial.String) *StringSubject {
return &StringSubject{value: value}
}
type StringSubject struct {
Subject
value serial.String
}
func (subject *StringSubject) Named(name string) *StringSubject {
subject.Subject.Named(name)
return subject
}
func (subject *StringSubject) DisplayString() string {
return subject.Subject.DisplayString(subject.value.String())
}
func (subject *StringSubject) Equals(expectation string) {
if subject.value.String() != expectation {
subject.Fail(subject.DisplayString(), "is equal to", serial.StringT(expectation))
}
}
func (subject *StringSubject) NotEquals(expectation string) {
if subject.value.String() == expectation {
subject.Fail(subject.DisplayString(), "is not equal to ", serial.StringT(expectation))
}
}
func (subject *StringSubject) Contains(substring serial.String) {
if !strings.Contains(subject.value.String(), substring.String()) {
subject.Fail(subject.DisplayString(), "contains", substring)
}
}
func (subject *StringSubject) NotContains(substring serial.String) {
if strings.Contains(subject.value.String(), substring.String()) {
subject.Fail(subject.DisplayString(), "doesn't contain", substring)
}
}
+8 -24
View File
@@ -1,38 +1,22 @@
package assert
import (
"github.com/v2ray/v2ray-core/common/serial"
v2testing "github.com/v2ray/v2ray-core/testing"
)
type Subject struct {
name string
disp string
a *Assert
}
func NewSubject() *Subject {
return &Subject{
name: "",
}
}
func (subject *Subject) Fail(displayString string, verb string, other serial.String) {
subject.FailWithMessage("Not true that " + displayString + " " + verb + " <" + other.String() + ">.")
func (subject *Subject) Fail(verb string, other string) {
subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + other + ">.")
}
func (subject *Subject) FailWithMessage(message string) {
v2testing.Fail(message)
subject.a.Fail(message)
}
func (subject *Subject) Named(name string) {
subject.name = name
}
func (subject *Subject) DisplayString(value string) string {
func (subject *Subject) DisplayString() string {
value := subject.disp
if len(value) == 0 {
value = "unknown"
}
if len(subject.name) == 0 {
return "<" + value + ">"
}
return subject.name + "(<" + value + ">)"
return "<" + value + ">"
}
+50
View File
@@ -0,0 +1,50 @@
package assert
import (
"github.com/v2ray/v2ray-core/common/serial"
)
func (this *Assert) Uint16(value uint16) *Uint16Subject {
return &Uint16Subject{
Subject: Subject{
a: this,
disp: serial.Uint16ToString(value),
},
value: value,
}
}
type Uint16Subject struct {
Subject
value uint16
}
func (subject *Uint16Subject) Equals(expectation uint16) {
if subject.value != expectation {
subject.Fail("is equal to", serial.Uint16ToString(expectation))
}
}
func (subject *Uint16Subject) GreaterThan(expectation uint16) {
if subject.value <= expectation {
subject.Fail("is greater than", serial.Uint16ToString(expectation))
}
}
func (subject *Uint16Subject) LessThan(expectation uint16) {
if subject.value >= expectation {
subject.Fail("is less than", serial.Uint16ToString(expectation))
}
}
func (subject *Uint16Subject) IsPositive() {
if subject.value <= 0 {
subject.Fail("is", "positive")
}
}
func (subject *Uint16Subject) IsNegative() {
if subject.value >= 0 {
subject.Fail("is not", "negative")
}
}
-47
View File
@@ -1,47 +0,0 @@
package assert
import (
"github.com/v2ray/v2ray-core/common/serial"
)
func Uint16(value uint16) *Uint16Subject {
return &Uint16Subject{value: serial.Uint16Literal(value)}
}
type Uint16Subject struct {
Subject
value serial.Uint16Literal
}
func (subject *Uint16Subject) Named(name string) *Uint16Subject {
subject.Subject.Named(name)
return subject
}
func (subject *Uint16Subject) DisplayString() string {
return subject.Subject.DisplayString(subject.value.String())
}
func (subject *Uint16Subject) Equals(expectation uint16) {
if subject.value.Value() != expectation {
subject.Fail(subject.DisplayString(), "is equal to", serial.Uint16Literal(expectation))
}
}
func (subject *Uint16Subject) GreaterThan(expectation uint16) {
if subject.value.Value() <= expectation {
subject.Fail(subject.DisplayString(), "is greater than", serial.Uint16Literal(expectation))
}
}
func (subject *Uint16Subject) LessThan(expectation uint16) {
if subject.value.Value() >= expectation {
subject.Fail(subject.DisplayString(), "is less than", serial.Uint16Literal(expectation))
}
}
func (subject *Uint16Subject) Positive() {
if subject.value.Value() <= 0 {
subject.FailWithMessage("Not true that " + subject.DisplayString() + " is positive.")
}
}
+2 -3
View File
@@ -5,13 +5,12 @@ import (
"testing"
v2net "github.com/v2ray/v2ray-core/common/net"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
"github.com/v2ray/v2ray-core/testing/servers/tcp"
)
func TestDokodemoTCP(t *testing.T) {
v2testing.Current(t)
assert := assert.On(t)
tcpServer := &tcp.Server{
Port: v2net.Port(50016),
@@ -47,7 +46,7 @@ func TestDokodemoTCP(t *testing.T) {
response := make([]byte, 1024)
nBytes, err = conn.Read(response)
assert.Error(err).IsNil()
assert.StringLiteral("Processed: " + payload).Equals(string(response[:nBytes]))
assert.String("Processed: " + payload).Equals(string(response[:nBytes]))
conn.Close()
}
+2 -3
View File
@@ -7,13 +7,12 @@ import (
"testing"
v2net "github.com/v2ray/v2ray-core/common/net"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
"github.com/v2ray/v2ray-core/testing/servers/tcp"
)
func TestDynamicVMess(t *testing.T) {
v2testing.Current(t)
assert := assert.On(t)
tcpServer := &tcp.Server{
Port: v2net.Port(50032),
@@ -47,7 +46,7 @@ func TestDynamicVMess(t *testing.T) {
response := bytes.NewBuffer(nil)
_, err = io.Copy(response, conn)
assert.Error(err).IsNil()
assert.StringLiteral("Processed: " + payload).Equals(string(response.Bytes()))
assert.String("Processed: " + payload).Equals(string(response.Bytes()))
conn.Close()
}
+2 -3
View File
@@ -7,13 +7,12 @@ import (
"testing"
v2net "github.com/v2ray/v2ray-core/common/net"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
v2http "github.com/v2ray/v2ray-core/testing/servers/http"
)
func TestHttpProxy(t *testing.T) {
v2testing.Current(t)
assert := assert.On(t)
httpServer := &v2http.Server{
Port: v2net.Port(50042),
@@ -41,7 +40,7 @@ func TestHttpProxy(t *testing.T) {
content, err := ioutil.ReadAll(resp.Body)
assert.Error(err).IsNil()
assert.StringLiteral(string(content)).Equals("Home")
assert.String(string(content)).Equals("Home")
CloseAllServers()
}
+2 -3
View File
@@ -5,13 +5,12 @@ import (
"testing"
v2net "github.com/v2ray/v2ray-core/common/net"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
"github.com/v2ray/v2ray-core/testing/servers/tcp"
)
func TestRouter(t *testing.T) {
v2testing.Current(t)
assert := assert.On(t)
tcpServer := &tcp.Server{
Port: v2net.Port(50024),
@@ -56,7 +55,7 @@ func TestRouter(t *testing.T) {
response := make([]byte, 1024)
nBytes, err = conn.Read(response)
assert.Error(err).IsNil()
assert.StringLiteral("Processed: " + payload).Equals(string(response[:nBytes]))
assert.String("Processed: " + payload).Equals(string(response[:nBytes]))
conn.Close()
conn, err = net.DialTCP("tcp", nil, &net.TCPAddr{
+4 -5
View File
@@ -5,7 +5,6 @@ import (
"testing"
v2net "github.com/v2ray/v2ray-core/common/net"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
"github.com/v2ray/v2ray-core/testing/servers/tcp"
@@ -13,7 +12,7 @@ import (
)
func TestShadowsocksTCP(t *testing.T) {
v2testing.Current(t)
assert := assert.On(t)
tcpServer := &tcp.Server{
Port: v2net.Port(50052),
@@ -47,7 +46,7 @@ func TestShadowsocksTCP(t *testing.T) {
response := make([]byte, 1024)
nBytes, err = conn.Read(response)
assert.Error(err).IsNil()
assert.StringLiteral("Processed: " + payload).Equals(string(response[:nBytes]))
assert.String("Processed: " + payload).Equals(string(response[:nBytes]))
conn.Close()
cipher, err = ssclient.NewCipher("aes-128-cfb", "v2ray-another")
@@ -66,7 +65,7 @@ func TestShadowsocksTCP(t *testing.T) {
response = make([]byte, 1024)
nBytes, err = conn.Read(response)
assert.Error(err).IsNil()
assert.StringLiteral("Processed: " + payload).Equals(string(response[:nBytes]))
assert.String("Processed: " + payload).Equals(string(response[:nBytes]))
conn.Close()
cipher, err = ssclient.NewCipher("chacha20", "new-password")
@@ -85,7 +84,7 @@ func TestShadowsocksTCP(t *testing.T) {
response = make([]byte, 1024)
nBytes, err = conn.Read(response)
assert.Error(err).IsNil()
assert.StringLiteral("Processed: " + payload).Equals(string(response[:nBytes]))
assert.String("Processed: " + payload).Equals(string(response[:nBytes]))
conn.Close()
CloseAllServers()
+4 -5
View File
@@ -6,14 +6,13 @@ import (
v2net "github.com/v2ray/v2ray-core/common/net"
v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
"github.com/v2ray/v2ray-core/testing/servers/tcp"
"github.com/v2ray/v2ray-core/testing/servers/udp"
)
func TestTCPConnection(t *testing.T) {
v2testing.Current(t)
assert := assert.On(t)
targetPort := v2nettesting.PickPort()
tcpServer := &tcp.Server{
@@ -79,7 +78,7 @@ func TestTCPConnection(t *testing.T) {
nResponse += nBytes
conn.CloseWrite()
assert.StringLiteral(string(actualResponse[:nResponse])).Equals("Processed: Request to target server.Processed: Request to target server again.")
assert.String(string(actualResponse[:nResponse])).Equals("Processed: Request to target server.Processed: Request to target server again.")
conn.Close()
}
@@ -88,7 +87,7 @@ func TestTCPConnection(t *testing.T) {
}
func TestTCPBind(t *testing.T) {
v2testing.Current(t)
assert := assert.On(t)
targetPort := v2nettesting.PickPort()
tcpServer := &tcp.Server{
@@ -139,7 +138,7 @@ func TestTCPBind(t *testing.T) {
}
func TestUDPAssociate(t *testing.T) {
v2testing.Current(t)
assert := assert.On(t)
targetPort := v2nettesting.PickPort()
udpServer := &udp.Server{