From e42510aca5b517fcaff7a927f4d7870ed6856d19 Mon Sep 17 00:00:00 2001 From: v2ray Date: Thu, 3 Dec 2015 16:57:23 +0100 Subject: [PATCH] more literals --- common/net/port.go | 6 +++--- common/serial/numbers.go | 33 +++++++++++++++++++++++++++++++++ common/serial/string.go | 1 + testing/assert/intsubject.go | 24 ++++++++++-------------- testing/assert/uint16subject.go | 26 +++++++++++--------------- 5 files changed, 58 insertions(+), 32 deletions(-) create mode 100644 common/serial/numbers.go diff --git a/common/net/port.go b/common/net/port.go index eb62704f2..00f510b25 100644 --- a/common/net/port.go +++ b/common/net/port.go @@ -1,10 +1,10 @@ package net import ( - "strconv" + "github.com/v2ray/v2ray-core/common/serial" ) -type Port uint16 +type Port serial.Uint16Literal func PortFromBytes(port []byte) Port { return Port(uint16(port[0])<<8 + uint16(port[1])) @@ -19,5 +19,5 @@ func (this Port) Bytes() []byte { } func (this Port) String() string { - return strconv.Itoa(int(this)) + return serial.Uint16Literal(this).String() } diff --git a/common/serial/numbers.go b/common/serial/numbers.go new file mode 100644 index 000000000..5e0d8389d --- /dev/null +++ b/common/serial/numbers.go @@ -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) +} diff --git a/common/serial/string.go b/common/serial/string.go index e5c47671b..7891df5ab 100644 --- a/common/serial/string.go +++ b/common/serial/string.go @@ -1,5 +1,6 @@ package serial +// An interface for any objects that has string presentation. type String interface { String() string } diff --git a/testing/assert/intsubject.go b/testing/assert/intsubject.go index 6b3c27238..36a5b6672 100644 --- a/testing/assert/intsubject.go +++ b/testing/assert/intsubject.go @@ -1,16 +1,16 @@ package assert import ( - "strconv" + "github.com/v2ray/v2ray-core/common/serial" ) func Int(value int) *IntSubject { - return &IntSubject{value: value} + return &IntSubject{value: serial.IntLiteral(value)} } type IntSubject struct { Subject - value int + value serial.IntLiteral } func (subject *IntSubject) Named(name string) *IntSubject { @@ -18,28 +18,24 @@ func (subject *IntSubject) Named(name string) *IntSubject { 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 { - return subject.Subject.DisplayString(strconv.Itoa(subject.value)) + return subject.Subject.DisplayString(subject.value.String()) } func (subject *IntSubject) Equals(expectation int) { - if subject.value != expectation { - subject.Fail("is equal to", expectation) + if subject.value.Value() != expectation { + subject.Fail(subject.DisplayString(), "is equal to", serial.IntLiteral(expectation)) } } func (subject *IntSubject) GreaterThan(expectation int) { - if subject.value <= expectation { - subject.Fail("is greater than", expectation) + if subject.value.Value() <= expectation { + subject.Fail(subject.DisplayString(), "is greater than", serial.IntLiteral(expectation)) } } func (subject *IntSubject) LessThan(expectation int) { - if subject.value >= expectation { - subject.Fail("is less than", expectation) + if subject.value.Value() >= expectation { + subject.Fail(subject.DisplayString(), "is less than", serial.IntLiteral(expectation)) } } diff --git a/testing/assert/uint16subject.go b/testing/assert/uint16subject.go index ad10b4432..8f0747e3c 100644 --- a/testing/assert/uint16subject.go +++ b/testing/assert/uint16subject.go @@ -1,16 +1,16 @@ package assert import ( - "strconv" + "github.com/v2ray/v2ray-core/common/serial" ) func Uint16(value uint16) *Uint16Subject { - return &Uint16Subject{value: value} + return &Uint16Subject{value: serial.Uint16Literal(value)} } type Uint16Subject struct { Subject - value uint16 + value serial.Uint16Literal } func (subject *Uint16Subject) Named(name string) *Uint16Subject { @@ -18,34 +18,30 @@ func (subject *Uint16Subject) Named(name string) *Uint16Subject { 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 { - return subject.Subject.DisplayString(strconv.Itoa(int(subject.value))) + return subject.Subject.DisplayString(subject.value.String()) } func (subject *Uint16Subject) Equals(expectation uint16) { - if subject.value != expectation { - subject.Fail("is equal to", expectation) + if subject.value.Value() != expectation { + subject.Fail(subject.DisplayString(), "is equal to", serial.Uint16Literal(expectation)) } } func (subject *Uint16Subject) GreaterThan(expectation uint16) { - if subject.value <= expectation { - subject.Fail("is greater than", expectation) + if subject.value.Value() <= expectation { + subject.Fail(subject.DisplayString(), "is greater than", serial.Uint16Literal(expectation)) } } func (subject *Uint16Subject) LessThan(expectation uint16) { - if subject.value >= expectation { - subject.Fail("is less than", expectation) + if subject.value.Value() >= expectation { + subject.Fail(subject.DisplayString(), "is less than", serial.Uint16Literal(expectation)) } } func (subject *Uint16Subject) Positive() { - if subject.value <= 0 { + if subject.value.Value() <= 0 { subject.FailWithMessage("Not true that " + subject.DisplayString() + " is positive.") } }