1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-27 09:55:22 +00:00

leverage serial.String in assert

This commit is contained in:
Darien Raymond 2015-12-02 15:41:19 +00:00
parent 7960b97c8e
commit 3c12469ae5
11 changed files with 38 additions and 34 deletions

View File

@ -26,5 +26,5 @@ func TestSimpleRouter(t *testing.T) {
tag, err := router.TakeDetour(v2net.NewTCPDestination(v2net.DomainAddress("v2ray.com", 80)))
assert.Error(err).IsNil()
assert.String(tag).Equals("test")
assert.StringLiteral(tag).Equals("test")
}

View File

@ -22,7 +22,7 @@ func TestIPv4Address(t *testing.T) {
v2netassert.Address(addr).IsNotDomain()
assert.Bytes(addr.IP()).Equals(ip)
v2netassert.Port(addr.Port()).Equals(port)
assert.String(addr.String()).Equals("1.2.3.4:80")
assert.String(addr).Equals("1.2.3.4:80")
}
func TestIPv6Address(t *testing.T) {
@ -42,7 +42,7 @@ func TestIPv6Address(t *testing.T) {
v2netassert.Address(addr).IsNotDomain()
assert.Bytes(addr.IP()).Equals(ip)
assert.Uint16(addr.Port().Value()).Equals(port.Value())
assert.String(addr.String()).Equals("[102:304:102:304:102:304:102:304]:443")
assert.String(addr).Equals("[102:304:102:304:102:304:102:304]:443")
}
func TestDomainAddress(t *testing.T) {
@ -55,9 +55,9 @@ func TestDomainAddress(t *testing.T) {
v2netassert.Address(addr).IsDomain()
v2netassert.Address(addr).IsNotIPv6()
v2netassert.Address(addr).IsNotIPv4()
assert.String(addr.Domain()).Equals(domain)
assert.StringLiteral(addr.Domain()).Equals(domain)
assert.Uint16(addr.Port().Value()).Equals(port.Value())
assert.String(addr.String()).Equals("v2ray.com:443")
assert.String(addr).Equals("v2ray.com:443")
}
func TestNetIPv4Address(t *testing.T) {
@ -67,5 +67,5 @@ func TestNetIPv4Address(t *testing.T) {
port := v2net.NewPort(80)
addr := v2net.IPAddress(ip, port.Value())
v2netassert.Address(addr).IsIPv4()
assert.String(addr.String()).Equals("1.2.3.4:80")
assert.String(addr).Equals("1.2.3.4:80")
}

View File

@ -14,7 +14,7 @@ func TestTCPDestination(t *testing.T) {
dest := v2net.NewTCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}, 80))
assert.Bool(dest.IsTCP()).IsTrue()
assert.Bool(dest.IsUDP()).IsFalse()
assert.String(dest.String()).Equals("tcp:1.2.3.4:80")
assert.StringLiteral(dest.String()).Equals("tcp:1.2.3.4:80")
}
func TestUDPDestination(t *testing.T) {
@ -23,5 +23,5 @@ func TestUDPDestination(t *testing.T) {
dest := v2net.NewUDPDestination(v2net.IPAddress([]byte{0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88}, 53))
assert.Bool(dest.IsTCP()).IsFalse()
assert.Bool(dest.IsUDP()).IsTrue()
assert.String(dest.String()).Equals("udp:[2001:4860:4860::8888]:53")
assert.StringLiteral(dest.String()).Equals("udp:[2001:4860:4860::8888]:53")
}

View File

@ -75,7 +75,7 @@ func TestDokodemoTCP(t *testing.T) {
assert.Error(err).IsNil()
tcpClient.Close()
assert.String("Processed: " + data2Send).Equals(string(response[:nBytes]))
assert.StringLiteral("Processed: " + data2Send).Equals(string(response[:nBytes]))
}
func TestDokodemoUDP(t *testing.T) {
@ -136,5 +136,5 @@ func TestDokodemoUDP(t *testing.T) {
assert.Error(err).IsNil()
udpClient.Close()
assert.String("Processed: " + data2Send).Equals(string(response[:nBytes]))
assert.StringLiteral("Processed: " + data2Send).Equals(string(response[:nBytes]))
}

View File

@ -28,7 +28,7 @@ func TestDefaultIPAddress(t *testing.T) {
v2testing.Current(t)
socksConfig := jsonconfig.CreateConfig("socks", config.TypeInbound).(*SocksConfig)
assert.String(socksConfig.IP().String()).Equals("127.0.0.1")
assert.StringLiteral(socksConfig.IP().String()).Equals("127.0.0.1")
}
func TestIPAddressParsing(t *testing.T) {
@ -37,7 +37,7 @@ func TestIPAddressParsing(t *testing.T) {
var ipAddress IPAddress
err := json.Unmarshal([]byte("\"1.2.3.4\""), &ipAddress)
assert.Error(err).IsNil()
assert.String(net.IP(ipAddress).String()).Equals("1.2.3.4")
assert.StringLiteral(net.IP(ipAddress).String()).Equals("1.2.3.4")
}
func TestNoAuthConfig(t *testing.T) {
@ -48,7 +48,7 @@ func TestNoAuthConfig(t *testing.T) {
assert.Error(err).IsNil()
assert.Bool(config.IsNoAuth()).IsTrue()
assert.Bool(config.IsPassword()).IsFalse()
assert.String(config.IP().String()).Equals("8.8.8.8")
assert.StringLiteral(config.IP().String()).Equals("8.8.8.8")
assert.Bool(config.UDPEnabled).IsFalse()
}

View File

@ -31,6 +31,6 @@ func TestDomainAddressRequest(t *testing.T) {
assert.Error(err).IsNil()
assert.Byte(request.Fragment).Equals(1)
assert.String(request.Address.String()).Equals("v2ray.com:80")
assert.StringLiteral(request.Address.String()).Equals("v2ray.com:80")
assert.Bytes(request.Data.Value).Equals([]byte("Actual payload"))
}

View File

@ -71,7 +71,7 @@ func TestSocksTcpConnect(t *testing.T) {
assert.Bytes([]byte(data2Send)).Equals(connOutput.Bytes())
assert.Bytes(dataReturned).Equals(connInput)
assert.String(targetServer).Equals(och.Destination.Address().String())
assert.StringLiteral(targetServer).Equals(och.Destination.Address().String())
}
func TestSocksTcpConnectWithUserPass(t *testing.T) {
@ -129,7 +129,7 @@ func TestSocksTcpConnectWithUserPass(t *testing.T) {
assert.Bytes([]byte(data2Send)).Equals(connOutput.Bytes())
assert.Bytes(dataReturned).Equals(connInput)
assert.String(targetServer).Equals(och.Destination.Address().String())
assert.StringLiteral(targetServer).Equals(och.Destination.Address().String())
}
func TestSocksTcpConnectWithWrongUserPass(t *testing.T) {
@ -277,5 +277,5 @@ func TestSocksUdpSend(t *testing.T) {
assert.Error(err).IsNil()
assert.Bytes(response[10:nBytes]).Equals(connInput)
assert.Bytes(data2Send).Equals(connOutput.Bytes())
assert.String(och.Destination.String()).Equals("udp:8.8.4.4:53")
assert.StringLiteral(och.Destination.String()).Equals("udp:8.8.4.4:53")
}

View File

@ -73,12 +73,12 @@ func TestVMessSerialization(t *testing.T) {
}
assert.Byte(actualRequest.Version).Named("Version").Equals(byte(0x01))
assert.String(actualRequest.User.ID().String).Named("UserId").Equals(request.User.ID().String)
assert.StringLiteral(actualRequest.User.ID().String).Named("UserId").Equals(request.User.ID().String)
assert.Bytes(actualRequest.RequestIV).Named("RequestIV").Equals(request.RequestIV[:])
assert.Bytes(actualRequest.RequestKey).Named("RequestKey").Equals(request.RequestKey[:])
assert.Bytes(actualRequest.ResponseHeader).Named("ResponseHeader").Equals(request.ResponseHeader[:])
assert.Byte(actualRequest.Command).Named("Command").Equals(request.Command)
assert.String(actualRequest.Address.String()).Named("Address").Equals(request.Address.String())
assert.StringLiteral(actualRequest.Address.String()).Named("Address").Equals(request.Address.String())
}
func TestReadSingleByte(t *testing.T) {

View File

@ -27,10 +27,10 @@ func TestClientSampleConfig(t *testing.T) {
assert.Pointer(pointConfig.InboundConfig()).IsNotNil()
assert.Pointer(pointConfig.OutboundConfig()).IsNotNil()
assert.String(pointConfig.InboundConfig().Protocol()).Equals("socks")
assert.StringLiteral(pointConfig.InboundConfig().Protocol()).Equals("socks")
assert.Pointer(pointConfig.InboundConfig().Settings()).IsNotNil()
assert.String(pointConfig.OutboundConfig().Protocol()).Equals("vmess")
assert.StringLiteral(pointConfig.OutboundConfig().Protocol()).Equals("vmess")
assert.Pointer(pointConfig.OutboundConfig().Settings()).IsNotNil()
}
@ -47,10 +47,10 @@ func TestServerSampleConfig(t *testing.T) {
assert.Pointer(pointConfig.InboundConfig()).IsNotNil()
assert.Pointer(pointConfig.OutboundConfig()).IsNotNil()
assert.String(pointConfig.InboundConfig().Protocol()).Equals("vmess")
assert.StringLiteral(pointConfig.InboundConfig().Protocol()).Equals("vmess")
assert.Pointer(pointConfig.InboundConfig().Settings()).IsNotNil()
assert.String(pointConfig.OutboundConfig().Protocol()).Equals("freedom")
assert.StringLiteral(pointConfig.OutboundConfig().Protocol()).Equals("freedom")
assert.Pointer(pointConfig.OutboundConfig().Settings()).IsNotNil()
}
@ -67,7 +67,7 @@ func TestDetourConfig(t *testing.T) {
assert.Int(len(detours)).Equals(1)
detour := detours[0]
assert.String(detour.Protocol()).Equals("dokodemo-door")
assert.StringLiteral(detour.Protocol()).Equals("dokodemo-door")
assert.Uint16(detour.PortRange().From().Value()).Equals(uint16(28394))
assert.Uint16(detour.PortRange().To().Value()).Equals(uint16(28394))
assert.Pointer(detour.Settings()).IsNotNil()

View File

@ -1,12 +1,20 @@
package assert
func String(value string) *StringSubject {
import (
"github.com/v2ray/v2ray-core/common/serial"
)
func StringLiteral(value string) *StringSubject {
return String(serial.StringLiteral((value)))
}
func String(value serial.String) *StringSubject {
return &StringSubject{value: value}
}
type StringSubject struct {
Subject
value string
value serial.String
}
func (subject *StringSubject) Named(name string) *StringSubject {
@ -14,16 +22,12 @@ func (subject *StringSubject) Named(name string) *StringSubject {
return subject
}
func (subject *StringSubject) Fail(verb string, other string) {
subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + other + ">.")
}
func (subject *StringSubject) DisplayString() string {
return subject.Subject.DisplayString(subject.value)
return subject.Subject.DisplayString(subject.value.String())
}
func (subject *StringSubject) Equals(expectation string) {
if subject.value != expectation {
subject.Fail("is equal to", expectation)
if subject.value.String() != expectation {
subject.Fail(subject.DisplayString(), "is equal to", serial.StringLiteral(expectation))
}
}

View File

@ -72,7 +72,7 @@ func TestTCPConnection(t *testing.T) {
actualResponse := make([]byte, 1024)
nBytes, err = conn.Read(actualResponse)
assert.Error(err).IsNil()
assert.String(string(actualResponse[:nBytes])).Equals("Processed: Request to target server.Request to target server again.")
assert.StringLiteral(string(actualResponse[:nBytes])).Equals("Processed: Request to target server.Request to target server again.")
conn.Close()
}