Documentation
¶
Overview ¶
Example (Standlone) ¶
it can even run without the Go test framework
package main
import (
"fmt"
"github.com/ysmood/got"
)
// it can even run without the Go test framework
func main() {
tester := &T{}
got.Each(tester, t{})
}
type t struct {
got.G
}
func (t t) A() {
t.Eq(1, 2)
}
func (t t) B() {
t.Gt(1, 1)
}
// T is a an empty tester.
// You can config it to fit your specific requirements.
type T struct {
}
func (t *T) Run(name string, fn func(*T)) { fn(t) }
func (t *T) Logf(f string, args ...interface{}) { fmt.Printf(f+"\n", args...) }
func (t *T) Name() string { return "" }
func (t *T) Skipped() bool { return false }
func (t *T) Failed() bool { return false }
func (t *T) Helper() {}
func (t *T) Cleanup(func()) {}
func (t *T) SkipNow() {}
func (t *T) Fail() {}
func (t *T) FailNow() {}
Output: 1 ⦗not ≂⦘ 2 1 ⦗not >⦘ 1
Index ¶
- func DefaultFlags(flags ...string)
- func Each(t Testable, iteratee interface{}) (count int)
- func EnsureCoverage(path string, min float64) error
- func Parallel() (n int)
- type Assertions
- func (as Assertions) Count(times int) func()
- func (as Assertions) E(args ...interface{})
- func (as Assertions) Eq(a, b interface{}) (result Result)
- func (as Assertions) Equal(a, b interface{}) (result Result)
- func (as Assertions) Err(args ...interface{}) (result Result)
- func (as Assertions) False(a bool) (result Result)
- func (as Assertions) Gt(a, b interface{}) (result Result)
- func (as Assertions) Gte(a, b interface{}) (result Result)
- func (as Assertions) Has(container, str string) (result Result)
- func (as Assertions) Is(a, b interface{}) (result Result)
- func (as Assertions) Len(list interface{}, l int) (result Result)
- func (as Assertions) Lt(a, b interface{}) (result Result)
- func (as Assertions) Lte(a, b interface{}) (result Result)
- func (as Assertions) Neq(a, b interface{}) (result Result)
- func (as Assertions) Nil(args ...interface{}) (result Result)
- func (as Assertions) NotNil(args ...interface{}) (result Result)
- func (as Assertions) Panic(fn func()) (result Result)
- func (as Assertions) Regex(pattern, str string) (result Result)
- func (as Assertions) True(a bool) (result Result)
- type Context
- type G
- type Only
- type Options
- type ResHelper
- type Result
- type Router
- type Skip
- type Testable
- type Utils
- func (ut Utils) Context() Context
- func (ut Utils) DoAfter(d time.Duration, do func()) (cancel func())
- func (ut Utils) Error(args ...interface{})
- func (ut Utils) Errorf(format string, args ...interface{})
- func (ut Utils) Fatal(args ...interface{})
- func (ut Utils) Fatalf(format string, args ...interface{})
- func (ut Utils) HandleHTTP(file string, value ...interface{}) func(http.ResponseWriter, *http.Request)
- func (ut Utils) JSON(src interface{}) (v interface{})
- func (ut Utils) Log(args ...interface{})
- func (ut Utils) Open(create bool, path ...string) (f *os.File)
- func (ut Utils) PanicAfter(d time.Duration) (cancel func())
- func (ut Utils) Parallel() Utils
- func (ut Utils) Read(r io.Reader) *bytes.Buffer
- func (ut Utils) Req(method, url string, body ...interface{}) *ResHelper
- func (ut Utils) Serve() *Router
- func (ut Utils) Skip(args ...interface{})
- func (ut Utils) Skipf(format string, args ...interface{})
- func (ut Utils) Srand(l int) string
- func (ut Utils) Timeout(d time.Duration) Context
- func (ut Utils) ToJSON(obj interface{}) *bytes.Buffer
- func (ut Utils) ToJSONString(obj interface{}) string
- func (ut Utils) Write(obj interface{}) (writer func(io.Writer))
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultFlags ¶ added in v0.6.2
func DefaultFlags(flags ...string)
DefaultFlags will set the "go test" flag if not yet presented. It must be executed in the init() function. Such as the timeout:
DefaultFlags("timeout=10s")
func Each ¶ added in v0.0.3
Each runs each exported method Fn on type Ctx as a subtest of t. The iteratee can be a struct Ctx or:
iteratee(t Testable) (ctx Ctx)
Each Fn will be called like:
ctx.Fn()
If iteratee is Ctx, its G field will be set to New(t) for each test. Any Fn that has the same name with the embedded one will be ignored.
func EnsureCoverage ¶ added in v0.8.7
EnsureCoverage via report file generated from, for example:
go test -coverprofile=coverage.txt
Return error if any functions's coverage is less than min, min is a percentage value.
Types ¶
type Assertions ¶ added in v0.4.1
type Assertions struct {
Testable
// contains filtered or unexported fields
}
Assertions helpers
func (Assertions) Count ¶ added in v0.8.4
func (as Assertions) Count(times int) func()
Count returns a function that must be called with the specified times
func (Assertions) E ¶ added in v0.4.1
func (as Assertions) E(args ...interface{})
E is a shortcut for Nil(args...).Must()
func (Assertions) Eq ¶ added in v0.4.1
func (as Assertions) Eq(a, b interface{}) (result Result)
Eq a ≂ b
func (Assertions) Equal ¶ added in v0.4.1
func (as Assertions) Equal(a, b interface{}) (result Result)
Equal a == b
func (Assertions) Err ¶ added in v0.4.1
func (as Assertions) Err(args ...interface{}) (result Result)
Err fails if last arg is not error
func (Assertions) False ¶ added in v0.4.1
func (as Assertions) False(a bool) (result Result)
False a == false
func (Assertions) Gt ¶ added in v0.4.1
func (as Assertions) Gt(a, b interface{}) (result Result)
Gt a > b
func (Assertions) Gte ¶ added in v0.4.1
func (as Assertions) Gte(a, b interface{}) (result Result)
Gte a >= b
func (Assertions) Has ¶ added in v0.4.1
func (as Assertions) Has(container, str string) (result Result)
Has str in container
func (Assertions) Is ¶ added in v0.4.1
func (as Assertions) Is(a, b interface{}) (result Result)
Is fails if a is not kind of b
func (Assertions) Len ¶ added in v0.4.1
func (as Assertions) Len(list interface{}, l int) (result Result)
Len len(list) == l
func (Assertions) Lt ¶ added in v0.4.1
func (as Assertions) Lt(a, b interface{}) (result Result)
Lt a < b
func (Assertions) Lte ¶ added in v0.4.1
func (as Assertions) Lte(a, b interface{}) (result Result)
Lte a <= b
func (Assertions) Neq ¶ added in v0.4.1
func (as Assertions) Neq(a, b interface{}) (result Result)
Neq a != b
func (Assertions) Nil ¶ added in v0.4.1
func (as Assertions) Nil(args ...interface{}) (result Result)
Nil fails if last arg is not nil
func (Assertions) NotNil ¶ added in v0.4.1
func (as Assertions) NotNil(args ...interface{}) (result Result)
NotNil fails if last arg is nil
func (Assertions) Panic ¶ added in v0.4.1
func (as Assertions) Panic(fn func()) (result Result)
Panic fails if fn doesn't panic
func (Assertions) Regex ¶ added in v0.4.1
func (as Assertions) Regex(pattern, str string) (result Result)
Regex matches str
func (Assertions) True ¶ added in v0.4.1
func (as Assertions) True(a bool) (result Result)
True a == true
type G ¶ added in v0.2.0
type G struct {
Testable
Assertions
Utils
}
G is the helper context, it hold some useful helpers to write tests
type Options ¶ added in v0.1.0
type Options struct {
// Dump a value to human readable string
Dump func(interface{}) string
// Format keywords in the assertion message.
// Such as color it for CLI output.
Keyword func(string) string
// Diff function for Assertions.Eq
Diff func(a, b interface{}) string
}
Options for Assertion
type Result ¶ added in v0.1.0
type Result struct {
// contains filtered or unexported fields
}
Result helper
type Router ¶ added in v0.2.0
type Router struct {
HostURL *url.URL
Server *http.Server
Mux *http.ServeMux
// contains filtered or unexported fields
}
Router of a http server
type Testable ¶ added in v0.0.3
type Testable interface {
Name() string // same as testing.common.Name
Skipped() bool // same as testing.common.Skipped
Failed() bool // same as testing.common.Failed
Cleanup(func()) // same as testing.common.Cleanup
FailNow() // same as testing.common.FailNow
Fail() // same as testing.common.Fail
Helper() // same as testing.common.Helper
Logf(format string, args ...interface{}) // same as testing.common.Logf
SkipNow() // same as testing.common.Skip
}
Testable interface. Usually, you use *testing.T as it.
type Utils ¶ added in v0.4.2
type Utils struct {
Testable
}
Utils for commonly used methods
func (Utils) Error ¶ added in v0.4.2
func (ut Utils) Error(args ...interface{})
Error is the same as testing.common.Error
func (Utils) Fatal ¶ added in v0.4.2
func (ut Utils) Fatal(args ...interface{})
Fatal is the same as testing.common.Fatal
func (Utils) HandleHTTP ¶ added in v0.4.2
func (ut Utils) HandleHTTP(file string, value ...interface{}) func(http.ResponseWriter, *http.Request)
HandleHTTP handles a request. If file exists serve the file content. The file will be used to set the Content-Type header. If the file doesn't exist, the value will be encoded by G.Write(value) and used as the response body.
func (Utils) JSON ¶ added in v0.4.2
func (ut Utils) JSON(src interface{}) (v interface{})
JSON from string, []byte, or io.Reader
func (Utils) Log ¶ added in v0.4.2
func (ut Utils) Log(args ...interface{})
Log is the same as testing.common.Log
func (Utils) Open ¶ added in v0.4.2
Open a file. Override it if create is true. Directories will be auto-created. path will be joined with filepath.Join so that it's cross-platform
func (Utils) PanicAfter ¶ added in v0.7.0
PanicAfter d duration if the test is still running
func (Utils) Req ¶ added in v0.4.2
Req the url. The method is the http method. The body will be encoded by G.Write(body) . When the len(body) is greater than 2, the first item should be a file extension string for the Content-Type header, such as ".json", ".jpg".
func (Utils) Serve ¶ added in v0.4.2
Serve http on a random port. The server will be auto-closed after the test.
func (Utils) Skip ¶ added in v0.4.2
func (ut Utils) Skip(args ...interface{})
Skip is the same as testing.common.Skip
func (Utils) ToJSONString ¶ added in v0.10.0
ToJSONString convert obj to JSON string