mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-12-29 05:25:21 -05:00
simplify test code
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Assertion is an assertion library inspired by Truth.
|
||||
// See http://google.github.io/truth/
|
||||
type Assertion struct {
|
||||
t *testing.T
|
||||
}
|
||||
@@ -29,3 +31,11 @@ func (a *Assertion) Byte(value byte) *ByteSubject {
|
||||
func (a *Assertion) Bytes(value []byte) *BytesSubject {
|
||||
return NewBytesSubject(NewSubject(a), value)
|
||||
}
|
||||
|
||||
func (a *Assertion) String(value string) *StringSubject {
|
||||
return NewStringSubject(NewSubject(a), value)
|
||||
}
|
||||
|
||||
func (a *Assertion) Error(value error) *ErrorSubject {
|
||||
return NewErrorSubject(NewSubject(a), value)
|
||||
}
|
||||
|
||||
42
testing/unit/errorsubject.go
Normal file
42
testing/unit/errorsubject.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package unit
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type ErrorSubject struct {
|
||||
*Subject
|
||||
value error
|
||||
}
|
||||
|
||||
func NewErrorSubject(base *Subject, value error) *ErrorSubject {
|
||||
subject := new(StringSubject)
|
||||
subject.Subject = base
|
||||
subject.value = value
|
||||
return subject
|
||||
}
|
||||
|
||||
func (subject *ErrorSubject) Named(name string) *ErrorSubject {
|
||||
subject.Subject.Named(name)
|
||||
return subject
|
||||
}
|
||||
|
||||
func (subject *ErrorSubject) Fail(verb string, other error) {
|
||||
subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + other.Error() + ">.")
|
||||
}
|
||||
|
||||
func (subject *ErrorSubject) DisplayString() string {
|
||||
return subject.Subject.DisplayString(subject.value.Error())
|
||||
}
|
||||
|
||||
func (subject *ErrorSubject) Equals(expectation error) {
|
||||
if subject.value != expectation {
|
||||
subject.Fail("is equal to", expectation)
|
||||
}
|
||||
}
|
||||
|
||||
func (subject *ErrorSubject) IsNil() {
|
||||
if subject.value != nil {
|
||||
subject.FailWithMethod("Not true that " + subject.DisplayString() + " is nil.")
|
||||
}
|
||||
}
|
||||
32
testing/unit/stringsubject.go
Normal file
32
testing/unit/stringsubject.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package unit
|
||||
|
||||
type StringSubject struct {
|
||||
*Subject
|
||||
value string
|
||||
}
|
||||
|
||||
func NewStringSubject(base *Subject, value string) *StringSubject {
|
||||
subject := new(StringSubject)
|
||||
subject.Subject = base
|
||||
subject.value = value
|
||||
return subject
|
||||
}
|
||||
|
||||
func (subject *StringSubject) Named(name string) *StringSubject {
|
||||
subject.Subject.Named(name)
|
||||
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)
|
||||
}
|
||||
|
||||
func (subject *StringSubject) Equals(expectation string) {
|
||||
if subject.value != expectation {
|
||||
subject.Fail("is equal to", expectation)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user