mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-11-10 17:27:18 -05:00
Unit test library to simplify test writing
This commit is contained in:
31
testing/unit/assertions.go
Normal file
31
testing/unit/assertions.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package unit
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
type Assertion struct {
|
||||
t *testing.T
|
||||
}
|
||||
|
||||
func Assert(t *testing.T) *Assertion {
|
||||
assert := new(Assertion)
|
||||
assert.t = t
|
||||
return assert
|
||||
}
|
||||
|
||||
func (a *Assertion) Int(value int) *IntSubject {
|
||||
return NewIntSubject(NewSubject(a), value)
|
||||
}
|
||||
|
||||
func (a *Assertion) Uint16(value uint16) *Uint16Subject {
|
||||
return NewUint16Subject(NewSubject(a), value)
|
||||
}
|
||||
|
||||
func (a *Assertion) Byte(value byte) *ByteSubject {
|
||||
return NewByteSubject(NewSubject(a), value)
|
||||
}
|
||||
|
||||
func (a *Assertion) Bytes(value []byte) *BytesSubject {
|
||||
return NewBytesSubject(NewSubject(a), value)
|
||||
}
|
||||
38
testing/unit/bytessubject.go
Normal file
38
testing/unit/bytessubject.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package unit
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type BytesSubject struct {
|
||||
*Subject
|
||||
value []byte
|
||||
}
|
||||
|
||||
func NewBytesSubject(base *Subject, value []byte) *BytesSubject {
|
||||
subject := new(BytesSubject)
|
||||
subject.Subject = base
|
||||
subject.value = value
|
||||
return subject
|
||||
}
|
||||
|
||||
func (subject *BytesSubject) Named(name string) *BytesSubject {
|
||||
subject.Subject.Named(name)
|
||||
return subject
|
||||
}
|
||||
|
||||
func (subject *BytesSubject) Fail(verb string, other []byte) {
|
||||
otherString := fmt.Sprintf("%v", other)
|
||||
subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + otherString + ">.")
|
||||
}
|
||||
|
||||
func (subject *BytesSubject) DisplayString() string {
|
||||
return subject.Subject.DisplayString(fmt.Sprintf("%v", subject.value))
|
||||
}
|
||||
|
||||
func (subject *BytesSubject) Equals(expectation []byte) {
|
||||
if !bytes.Equal(subject.value, expectation) {
|
||||
subject.Fail("is equal to", expectation)
|
||||
}
|
||||
}
|
||||
48
testing/unit/bytesubject.go
Normal file
48
testing/unit/bytesubject.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package unit
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type ByteSubject struct {
|
||||
*Subject
|
||||
value byte
|
||||
}
|
||||
|
||||
func NewByteSubject(base *Subject, value byte) *ByteSubject {
|
||||
subject := new(ByteSubject)
|
||||
subject.Subject = base
|
||||
subject.value = value
|
||||
return subject
|
||||
}
|
||||
|
||||
func (subject *ByteSubject) Named(name string) *ByteSubject {
|
||||
subject.Subject.Named(name)
|
||||
return subject
|
||||
}
|
||||
|
||||
func (subject *ByteSubject) Fail(verb string, other byte) {
|
||||
subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + strconv.Itoa(int(other)) + ">.")
|
||||
}
|
||||
|
||||
func (subject *ByteSubject) DisplayString() string {
|
||||
return subject.Subject.DisplayString(strconv.Itoa(int(subject.value)))
|
||||
}
|
||||
|
||||
func (subject *ByteSubject) Equals(expectation byte) {
|
||||
if subject.value != expectation {
|
||||
subject.Fail("is equal to", expectation)
|
||||
}
|
||||
}
|
||||
|
||||
func (subject *ByteSubject) GreaterThan(expectation byte) {
|
||||
if subject.value <= expectation {
|
||||
subject.Fail("is greater than", expectation)
|
||||
}
|
||||
}
|
||||
|
||||
func (subject *ByteSubject) LessThan(expectation byte) {
|
||||
if subject.value >= expectation {
|
||||
subject.Fail("is less than", expectation)
|
||||
}
|
||||
}
|
||||
48
testing/unit/intsubject.go
Normal file
48
testing/unit/intsubject.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package unit
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type IntSubject struct {
|
||||
*Subject
|
||||
value int
|
||||
}
|
||||
|
||||
func NewIntSubject(base *Subject, value int) *IntSubject {
|
||||
subject := new(IntSubject)
|
||||
subject.Subject = base
|
||||
subject.value = value
|
||||
return subject
|
||||
}
|
||||
|
||||
func (subject *IntSubject) Named(name string) *IntSubject {
|
||||
subject.Subject.Named(name)
|
||||
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))
|
||||
}
|
||||
|
||||
func (subject *IntSubject) Equals(expectation int) {
|
||||
if subject.value != expectation {
|
||||
subject.Fail("is equal to", expectation)
|
||||
}
|
||||
}
|
||||
|
||||
func (subject *IntSubject) GreaterThan(expectation int) {
|
||||
if subject.value <= expectation {
|
||||
subject.Fail("is greater than", expectation)
|
||||
}
|
||||
}
|
||||
|
||||
func (subject *IntSubject) LessThan(expectation int) {
|
||||
if subject.value >= expectation {
|
||||
subject.Fail("is less than", expectation)
|
||||
}
|
||||
}
|
||||
31
testing/unit/subject.go
Normal file
31
testing/unit/subject.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package unit
|
||||
|
||||
type Subject struct {
|
||||
assert *Assertion
|
||||
name string
|
||||
}
|
||||
|
||||
func NewSubject(assert *Assertion) *Subject {
|
||||
subject := new(Subject)
|
||||
subject.assert = assert
|
||||
subject.name = ""
|
||||
return subject
|
||||
}
|
||||
|
||||
func (subject *Subject) FailWithMessage(message string) {
|
||||
subject.assert.t.Error(message)
|
||||
}
|
||||
|
||||
func (subject *Subject) Named(name string) {
|
||||
subject.name = name
|
||||
}
|
||||
|
||||
func (subject *Subject) DisplayString(value string) string {
|
||||
if len(value) == 0 {
|
||||
value = "unknown"
|
||||
}
|
||||
if len(subject.name) == 0 {
|
||||
return "<" + value + ">"
|
||||
}
|
||||
return subject.name + "(<" + value + ">)"
|
||||
}
|
||||
48
testing/unit/uint16subject.go
Normal file
48
testing/unit/uint16subject.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package unit
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Uint16Subject struct {
|
||||
*Subject
|
||||
value uint16
|
||||
}
|
||||
|
||||
func NewUint16Subject(base *Subject, value uint16) *Uint16Subject {
|
||||
subject := new(Uint16Subject)
|
||||
subject.Subject = base
|
||||
subject.value = value
|
||||
return subject
|
||||
}
|
||||
|
||||
func (subject *Uint16Subject) Named(name string) *Uint16Subject {
|
||||
subject.Subject.Named(name)
|
||||
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)))
|
||||
}
|
||||
|
||||
func (subject *Uint16Subject) Equals(expectation uint16) {
|
||||
if subject.value != expectation {
|
||||
subject.Fail("is equal to", expectation)
|
||||
}
|
||||
}
|
||||
|
||||
func (subject *Uint16Subject) GreaterThan(expectation uint16) {
|
||||
if subject.value <= expectation {
|
||||
subject.Fail("is greater than", expectation)
|
||||
}
|
||||
}
|
||||
|
||||
func (subject *Uint16Subject) LessThan(expectation uint16) {
|
||||
if subject.value >= expectation {
|
||||
subject.Fail("is less than", expectation)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user