mirror of
https://github.com/v2fly/v2ray-core.git
synced 2026-04-19 12:09:10 -04:00
simplify testing
This commit is contained in:
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
70
testing/assert/assert.go
Normal file
70
testing/assert/assert.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package assert
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func On(t *testing.T) *Assert {
|
||||
return &Assert{
|
||||
t: t,
|
||||
}
|
||||
}
|
||||
|
||||
type Assert struct {
|
||||
t *testing.T
|
||||
}
|
||||
|
||||
func (this *Assert) Fail(message string) {
|
||||
fmt.Println(decorate(message))
|
||||
this.t.Fail()
|
||||
}
|
||||
|
||||
func getCaller() (string, int) {
|
||||
stackLevel := 3
|
||||
for {
|
||||
_, file, line, ok := runtime.Caller(stackLevel)
|
||||
if strings.Contains(file, "assert") {
|
||||
stackLevel++
|
||||
} else {
|
||||
if ok {
|
||||
// Truncate file name at last file name separator.
|
||||
if index := strings.LastIndex(file, "/"); index >= 0 {
|
||||
file = file[index+1:]
|
||||
} else if index = strings.LastIndex(file, "\\"); index >= 0 {
|
||||
file = file[index+1:]
|
||||
}
|
||||
} else {
|
||||
file = "???"
|
||||
line = 1
|
||||
}
|
||||
return file, line
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// decorate prefixes the string with the file and line of the call site
|
||||
// and inserts the final newline if needed and indentation tabs for formatting.
|
||||
func decorate(s string) string {
|
||||
file, line := getCaller()
|
||||
buf := new(bytes.Buffer)
|
||||
// Every line is indented at least one tab.
|
||||
buf.WriteString(" ")
|
||||
fmt.Fprintf(buf, "%s:%d: ", file, line)
|
||||
lines := strings.Split(s, "\n")
|
||||
if l := len(lines); l > 1 && lines[l-1] == "" {
|
||||
lines = lines[:l-1]
|
||||
}
|
||||
for i, line := range lines {
|
||||
if i > 0 {
|
||||
// Second and subsequent lines are indented an extra tab.
|
||||
buf.WriteString("\n\t\t")
|
||||
}
|
||||
buf.WriteString(line)
|
||||
}
|
||||
buf.WriteByte('\n')
|
||||
return buf.String()
|
||||
}
|
||||
42
testing/assert/bool.go
Normal file
42
testing/assert/bool.go
Normal 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")
|
||||
}
|
||||
}
|
||||
@@ -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
testing/assert/byte.go
Normal file
38
testing/assert/byte.go
Normal 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
testing/assert/bytes.go
Normal file
34
testing/assert/bytes.go
Normal 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))
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
testing/assert/error.go
Normal file
38
testing/assert/error.go
Normal 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")
|
||||
}
|
||||
}
|
||||
@@ -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
testing/assert/int64.go
Normal file
44
testing/assert/int64.go
Normal 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))
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
testing/assert/pointer.go
Normal file
38
testing/assert/pointer.go
Normal 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")
|
||||
}
|
||||
}
|
||||
@@ -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.")
|
||||
}
|
||||
}
|
||||
@@ -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
testing/assert/string.go
Normal file
44
testing/assert/string.go
Normal 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)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
testing/assert/uint16.go
Normal file
50
testing/assert/uint16.go
Normal 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")
|
||||
}
|
||||
}
|
||||
@@ -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.")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user