typed segments

This commit is contained in:
v2ray
2016-06-27 22:22:01 +02:00
parent 27b8715b63
commit 6915095a0a
5 changed files with 317 additions and 1 deletions
+19 -1
View File
@@ -1,6 +1,8 @@
package assert
import (
"reflect"
"github.com/v2ray/v2ray-core/common/serial"
)
@@ -26,7 +28,15 @@ func (subject *PointerSubject) Equals(expectation interface{}) {
}
func (subject *PointerSubject) IsNil() {
if subject.value != nil {
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")
}
}
@@ -35,4 +45,12 @@ 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")
}
}
+50
View File
@@ -0,0 +1,50 @@
package assert
import (
"github.com/v2ray/v2ray-core/common/serial"
)
func (this *Assert) Uint32(value uint32) *Uint32Subject {
return &Uint32Subject{
Subject: Subject{
a: this,
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")
}
}