mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-22 10:08:15 -05:00
more literals
This commit is contained in:
parent
f3f1c09e49
commit
e42510aca5
@ -1,10 +1,10 @@
|
|||||||
package net
|
package net
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"github.com/v2ray/v2ray-core/common/serial"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Port uint16
|
type Port serial.Uint16Literal
|
||||||
|
|
||||||
func PortFromBytes(port []byte) Port {
|
func PortFromBytes(port []byte) Port {
|
||||||
return Port(uint16(port[0])<<8 + uint16(port[1]))
|
return Port(uint16(port[0])<<8 + uint16(port[1]))
|
||||||
@ -19,5 +19,5 @@ func (this Port) Bytes() []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this Port) String() string {
|
func (this Port) String() string {
|
||||||
return strconv.Itoa(int(this))
|
return serial.Uint16Literal(this).String()
|
||||||
}
|
}
|
||||||
|
33
common/serial/numbers.go
Normal file
33
common/serial/numbers.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package serial
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Uint16 interface {
|
||||||
|
Value() uint16
|
||||||
|
}
|
||||||
|
|
||||||
|
type Uint16Literal uint16
|
||||||
|
|
||||||
|
func (this Uint16Literal) String() string {
|
||||||
|
return strconv.Itoa(int(this))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this Uint16Literal) Value() uint16 {
|
||||||
|
return uint16(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Int interface {
|
||||||
|
Value() int
|
||||||
|
}
|
||||||
|
|
||||||
|
type IntLiteral int
|
||||||
|
|
||||||
|
func (this IntLiteral) String() string {
|
||||||
|
return strconv.Itoa(int(this))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this IntLiteral) Value() int {
|
||||||
|
return int(this)
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
package serial
|
package serial
|
||||||
|
|
||||||
|
// An interface for any objects that has string presentation.
|
||||||
type String interface {
|
type String interface {
|
||||||
String() string
|
String() string
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
package assert
|
package assert
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"github.com/v2ray/v2ray-core/common/serial"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Int(value int) *IntSubject {
|
func Int(value int) *IntSubject {
|
||||||
return &IntSubject{value: value}
|
return &IntSubject{value: serial.IntLiteral(value)}
|
||||||
}
|
}
|
||||||
|
|
||||||
type IntSubject struct {
|
type IntSubject struct {
|
||||||
Subject
|
Subject
|
||||||
value int
|
value serial.IntLiteral
|
||||||
}
|
}
|
||||||
|
|
||||||
func (subject *IntSubject) Named(name string) *IntSubject {
|
func (subject *IntSubject) Named(name string) *IntSubject {
|
||||||
@ -18,28 +18,24 @@ func (subject *IntSubject) Named(name string) *IntSubject {
|
|||||||
return subject
|
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 {
|
func (subject *IntSubject) DisplayString() string {
|
||||||
return subject.Subject.DisplayString(strconv.Itoa(subject.value))
|
return subject.Subject.DisplayString(subject.value.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (subject *IntSubject) Equals(expectation int) {
|
func (subject *IntSubject) Equals(expectation int) {
|
||||||
if subject.value != expectation {
|
if subject.value.Value() != expectation {
|
||||||
subject.Fail("is equal to", expectation)
|
subject.Fail(subject.DisplayString(), "is equal to", serial.IntLiteral(expectation))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (subject *IntSubject) GreaterThan(expectation int) {
|
func (subject *IntSubject) GreaterThan(expectation int) {
|
||||||
if subject.value <= expectation {
|
if subject.value.Value() <= expectation {
|
||||||
subject.Fail("is greater than", expectation)
|
subject.Fail(subject.DisplayString(), "is greater than", serial.IntLiteral(expectation))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (subject *IntSubject) LessThan(expectation int) {
|
func (subject *IntSubject) LessThan(expectation int) {
|
||||||
if subject.value >= expectation {
|
if subject.value.Value() >= expectation {
|
||||||
subject.Fail("is less than", expectation)
|
subject.Fail(subject.DisplayString(), "is less than", serial.IntLiteral(expectation))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
package assert
|
package assert
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"github.com/v2ray/v2ray-core/common/serial"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Uint16(value uint16) *Uint16Subject {
|
func Uint16(value uint16) *Uint16Subject {
|
||||||
return &Uint16Subject{value: value}
|
return &Uint16Subject{value: serial.Uint16Literal(value)}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Uint16Subject struct {
|
type Uint16Subject struct {
|
||||||
Subject
|
Subject
|
||||||
value uint16
|
value serial.Uint16Literal
|
||||||
}
|
}
|
||||||
|
|
||||||
func (subject *Uint16Subject) Named(name string) *Uint16Subject {
|
func (subject *Uint16Subject) Named(name string) *Uint16Subject {
|
||||||
@ -18,34 +18,30 @@ func (subject *Uint16Subject) Named(name string) *Uint16Subject {
|
|||||||
return subject
|
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 {
|
func (subject *Uint16Subject) DisplayString() string {
|
||||||
return subject.Subject.DisplayString(strconv.Itoa(int(subject.value)))
|
return subject.Subject.DisplayString(subject.value.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (subject *Uint16Subject) Equals(expectation uint16) {
|
func (subject *Uint16Subject) Equals(expectation uint16) {
|
||||||
if subject.value != expectation {
|
if subject.value.Value() != expectation {
|
||||||
subject.Fail("is equal to", expectation)
|
subject.Fail(subject.DisplayString(), "is equal to", serial.Uint16Literal(expectation))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (subject *Uint16Subject) GreaterThan(expectation uint16) {
|
func (subject *Uint16Subject) GreaterThan(expectation uint16) {
|
||||||
if subject.value <= expectation {
|
if subject.value.Value() <= expectation {
|
||||||
subject.Fail("is greater than", expectation)
|
subject.Fail(subject.DisplayString(), "is greater than", serial.Uint16Literal(expectation))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (subject *Uint16Subject) LessThan(expectation uint16) {
|
func (subject *Uint16Subject) LessThan(expectation uint16) {
|
||||||
if subject.value >= expectation {
|
if subject.value.Value() >= expectation {
|
||||||
subject.Fail("is less than", expectation)
|
subject.Fail(subject.DisplayString(), "is less than", serial.Uint16Literal(expectation))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (subject *Uint16Subject) Positive() {
|
func (subject *Uint16Subject) Positive() {
|
||||||
if subject.value <= 0 {
|
if subject.value.Value() <= 0 {
|
||||||
subject.FailWithMessage("Not true that " + subject.DisplayString() + " is positive.")
|
subject.FailWithMessage("Not true that " + subject.DisplayString() + " is positive.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user