1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-27 22:36:12 -04:00

remove old assertion lib

This commit is contained in:
Darien Raymond 2017-10-24 17:17:10 +02:00
parent 74cf833758
commit 522ac2a2e9
16 changed files with 0 additions and 761 deletions

View File

@ -1,80 +0,0 @@
package assert
import (
"v2ray.com/core/common/net"
)
func (v *Assert) Address(value net.Address) *AddressSubject {
return &AddressSubject{
Subject: Subject{
disp: value.String(),
a: v,
},
value: value,
}
}
type AddressSubject struct {
Subject
value net.Address
}
func (subject *AddressSubject) NotEquals(another net.Address) {
if subject.value == another {
subject.Fail("not equals to", another.String())
}
}
func (subject *AddressSubject) Equals(another net.Address) {
if subject.value != 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.Family().IsIPv4() {
subject.Fail("is", "an IPv4 address")
}
}
func (subject *AddressSubject) IsNotIPv4() {
if subject.value.Family().IsIPv4() {
subject.Fail("is not", "an IPv4 address")
}
}
func (subject *AddressSubject) IsIPv6() {
if !subject.value.Family().IsIPv6() {
subject.Fail("is", "an IPv6 address")
}
}
func (subject *AddressSubject) IsNotIPv6() {
if subject.value.Family().IsIPv6() {
subject.Fail("is not", "an IPv6 address")
}
}
func (subject *AddressSubject) IsDomain() {
if !subject.value.Family().IsDomain() {
subject.Fail("is", "a domain address")
}
}
func (subject *AddressSubject) IsNotDomain() {
if subject.value.Family().IsDomain() {
subject.Fail("is not", "a domain address")
}
}

View File

@ -1,69 +0,0 @@
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 (v *Assert) Fail(message string) {
v.t.Fatal(decorate(message))
}
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()
}

View File

@ -1,42 +0,0 @@
package assert
import (
"strconv"
)
// Assert on a boolean variable.
func (v *Assert) Bool(value bool) *BoolSubject {
return &BoolSubject{
Subject: Subject{
disp: strconv.FormatBool(value),
a: v,
},
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")
}
}

View File

@ -1,38 +0,0 @@
package assert
import (
"v2ray.com/core/common/serial"
)
func (v *Assert) Byte(value byte) *ByteSubject {
return &ByteSubject{
Subject: Subject{
disp: serial.ByteToHexString(value),
a: v,
},
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))
}
}

View File

@ -1,43 +0,0 @@
package assert
import (
"bytes"
"fmt"
"v2ray.com/core/common/serial"
)
func (v *Assert) Bytes(value []byte) *BytesSubject {
return &BytesSubject{
Subject: Subject{
disp: serial.BytesToHexString(value),
a: v,
},
value: value,
}
}
type BytesSubject struct {
Subject
value []byte
}
func (subject *BytesSubject) Equals(expectation []byte) {
if len(subject.value) != len(expectation) {
subject.FailWithMessage(fmt.Sprint("Bytes arrays have differen size: expected ", len(expectation), ", actual ", len(subject.value)))
return
}
for idx, b := range expectation {
if subject.value[idx] != b {
subject.FailWithMessage(fmt.Sprint("Bytes are different: ", b, " vs ", subject.value[idx], " at pos ", idx))
return
}
}
}
func (subject *BytesSubject) NotEquals(expectation []byte) {
if bytes.Equal(subject.value, expectation) {
subject.Fail("is not equal to", serial.BytesToHexString(expectation))
}
}

View File

@ -1,64 +0,0 @@
package assert
import (
"v2ray.com/core/common/net"
)
func (v *Assert) Destination(value net.Destination) *DestinationSubject {
return &DestinationSubject{
Subject: Subject{
disp: value.String(),
a: v,
},
value: value,
}
}
type DestinationSubject struct {
Subject
value net.Destination
}
func (v *DestinationSubject) IsTCP() {
if v.value.Network != net.Network_TCP {
v.Fail("is", "a TCP destination")
}
}
func (v *DestinationSubject) IsNotTCP() {
if v.value.Network == net.Network_TCP {
v.Fail("is not", "a TCP destination")
}
}
func (v *DestinationSubject) IsUDP() {
if v.value.Network != net.Network_UDP {
v.Fail("is", "a UDP destination")
}
}
func (v *DestinationSubject) IsNotUDP() {
if v.value.Network == net.Network_UDP {
v.Fail("is not", "a UDP destination")
}
}
func (v *DestinationSubject) EqualsString(another string) {
if v.value.String() != another {
v.Fail("not equals to string", another)
}
}
func (v *DestinationSubject) Equals(another net.Destination) {
if v.value != another {
v.Fail("not equals to", another.String())
}
}
func (v *DestinationSubject) HasAddress() *AddressSubject {
return v.a.Address(v.value.Address)
}
func (v *DestinationSubject) HasPort() *PortSubject {
return v.a.Port(v.value.Port)
}

View File

@ -1,38 +0,0 @@
package assert
func (v *Assert) Error(value error) *ErrorSubject {
valueStr := ""
if value != nil {
valueStr = value.Error()
}
return &ErrorSubject{
Subject: Subject{
a: v,
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")
}
}

View File

@ -1,44 +0,0 @@
package assert
import (
"v2ray.com/core/common/serial"
)
func (v *Assert) Int64(value int64) *Int64Subject {
return &Int64Subject{
Subject: Subject{
a: v,
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))
}
}

View File

@ -1,38 +0,0 @@
package assert
import (
"v2ray.com/core/common/serial"
)
func (v *Assert) Int(value int) *IntSubject {
return &IntSubject{
Subject: Subject{
a: v,
disp: serial.IntToString(value),
},
value: value,
}
}
type IntSubject struct {
Subject
value int
}
func (subject *IntSubject) Equals(expectation int) {
if subject.value != expectation {
subject.Fail("is equal to", serial.IntToString(expectation))
}
}
func (subject *IntSubject) GreaterThan(expectation int) {
if subject.value <= expectation {
subject.Fail("is greater than", serial.IntToString(expectation))
}
}
func (subject *IntSubject) LessThan(expectation int) {
if subject.value >= expectation {
subject.Fail("is less than", serial.IntToString(expectation))
}
}

View File

@ -1,33 +0,0 @@
package assert
import (
"bytes"
"net"
)
func (v *Assert) IP(value net.IP) *IPSubject {
return &IPSubject{
Subject: Subject{
a: v,
disp: value.String(),
},
value: value,
}
}
type IPSubject struct {
Subject
value net.IP
}
func (subject *IPSubject) IsNil() {
if subject.value != nil {
subject.Fail("is", "nil")
}
}
func (subject *IPSubject) Equals(ip net.IP) {
if !bytes.Equal([]byte(subject.value), []byte(ip)) {
subject.Fail("equals to", ip.String())
}
}

View File

@ -1,56 +0,0 @@
package assert
import (
"reflect"
"v2ray.com/core/common/serial"
)
func (v *Assert) Pointer(value interface{}) *PointerSubject {
return &PointerSubject{
Subject: Subject{
a: v,
disp: serial.ToString(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.ToString(expectation))
}
}
func (subject *PointerSubject) IsNil() {
if subject.value == nil {
return
}
valueType := reflect.TypeOf(subject.value)
nilType := reflect.Zero(valueType)
realValue := reflect.ValueOf(subject.value)
if nilType != realValue {
subject.Fail("is", "nil")
}
}
func (subject *PointerSubject) IsNotNil() {
if subject.value == nil {
subject.Fail("is not", "nil")
}
valueType := reflect.TypeOf(subject.value)
nilType := reflect.Zero(valueType)
realValue := reflect.ValueOf(subject.value)
if nilType == realValue {
subject.Fail("is not", "nil")
}
}

View File

@ -1,44 +0,0 @@
package assert
import (
"v2ray.com/core/common/net"
)
func (v *Assert) Port(value net.Port) *PortSubject {
return &PortSubject{
Subject: Subject{
a: v,
disp: value.String(),
},
value: value,
}
}
type PortSubject struct {
Subject
value net.Port
}
func (subject *PortSubject) Equals(expectation net.Port) {
if subject.value.Value() != expectation.Value() {
subject.Fail("is equal to", expectation.String())
}
}
func (subject *PortSubject) GreaterThan(expectation net.Port) {
if subject.value.Value() <= expectation.Value() {
subject.Fail("is greater than", expectation.String())
}
}
func (subject *PortSubject) LessThan(expectation net.Port) {
if subject.value.Value() >= expectation.Value() {
subject.Fail("is less than", expectation.String())
}
}
func (subject *PortSubject) IsValid() {
if subject.value == 0 {
subject.Fail("is", "a valid port")
}
}

View File

@ -1,50 +0,0 @@
package assert
import (
"strings"
)
func (v *Assert) String(value string) *StringSubject {
return &StringSubject{
Subject: Subject{
a: v,
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)
}
}
func (subject *StringSubject) IsEmpty() {
if len(subject.value) > 0 {
subject.FailWithMessage("is not empty.")
}
}

View File

@ -1,22 +0,0 @@
package assert
type Subject struct {
disp string
a *Assert
}
func (subject *Subject) Fail(verb string, other string) {
subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + other + ">.")
}
func (subject *Subject) FailWithMessage(message string) {
subject.a.Fail(message)
}
func (subject *Subject) DisplayString() string {
value := subject.disp
if len(value) == 0 {
value = "unknown"
}
return "<" + value + ">"
}

View File

@ -1,50 +0,0 @@
package assert
import (
"v2ray.com/core/common/serial"
)
func (v *Assert) Uint16(value uint16) *Uint16Subject {
return &Uint16Subject{
Subject: Subject{
a: v,
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")
}
}

View File

@ -1,50 +0,0 @@
package assert
import (
"v2ray.com/core/common/serial"
)
func (v *Assert) Uint32(value uint32) *Uint32Subject {
return &Uint32Subject{
Subject: Subject{
a: v,
disp: serial.Uint32ToString(value),
},
value: value,
}
}
type Uint32Subject struct {
Subject
value uint32
}
func (subject *Uint32Subject) Equals(expectation uint32) {
if subject.value != expectation {
subject.Fail("is equal to", serial.Uint32ToString(expectation))
}
}
func (subject *Uint32Subject) GreaterThan(expectation uint32) {
if subject.value <= expectation {
subject.Fail("is greater than", serial.Uint32ToString(expectation))
}
}
func (subject *Uint32Subject) LessThan(expectation uint32) {
if subject.value >= expectation {
subject.Fail("is less than", serial.Uint32ToString(expectation))
}
}
func (subject *Uint32Subject) IsPositive() {
if subject.value <= 0 {
subject.Fail("is", "positive")
}
}
func (subject *Uint32Subject) IsNegative() {
if subject.value >= 0 {
subject.Fail("is not", "negative")
}
}