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 Each(t Testable, iteratee interface{}) (count int)
- type Assertion
- func (as Assertion) E(args ...interface{})
- func (as Assertion) Eq(a, b interface{}) (result Result)
- func (as Assertion) Equal(a, b interface{}) (result Result)
- func (as Assertion) Err(args ...interface{}) (result Result)
- func (as Assertion) False(a bool) (result Result)
- func (as Assertion) Gt(a, b interface{}) (result Result)
- func (as Assertion) Gte(a, b interface{}) (result Result)
- func (as Assertion) Has(container, str string) (result Result)
- func (as Assertion) Is(a, b interface{}) (result Result)
- func (as Assertion) Len(list interface{}, l int) (result Result)
- func (as Assertion) Lt(a, b interface{}) (result Result)
- func (as Assertion) Lte(a, b interface{}) (result Result)
- func (as Assertion) Neq(a, b interface{}) (result Result)
- func (as Assertion) Nil(args ...interface{}) (result Result)
- func (as Assertion) NotNil(args ...interface{}) (result Result)
- func (as Assertion) Panic(fn func()) (result Result)
- func (as Assertion) Regex(pattern, str string) (result Result)
- func (as Assertion) True(a bool) (result Result)
- type Context
- type G
- type Helper
- func (hp Helper) Context() Context
- func (hp Helper) Error(args ...interface{})
- func (hp Helper) Errorf(format string, args ...interface{})
- func (hp Helper) Fatal(args ...interface{})
- func (hp Helper) Fatalf(format string, args ...interface{})
- func (hp Helper) HandleHTTP(file string, value ...interface{}) func(http.ResponseWriter, *http.Request)
- func (hp Helper) Log(args ...interface{})
- func (hp Helper) Open(create bool, path ...string) (f *os.File)
- func (hp Helper) Parallel()
- func (hp Helper) Read(r io.Reader) []byte
- func (hp Helper) ReadJSON(r io.Reader) (v interface{})
- func (hp Helper) ReadString(r io.Reader) string
- func (hp Helper) Req(method, url string, body ...interface{}) *ResHelper
- func (hp Helper) Serve() *Router
- func (hp Helper) Skip(args ...interface{})
- func (hp Helper) Skipf(format string, args ...interface{})
- func (hp Helper) Srand(l int) string
- func (hp Helper) Timeout(d time.Duration) Context
- func (hp Helper) Write(obj interface{}) (writer func(io.Writer))
- type Only
- type Options
- type ResHelper
- type Result
- type Router
- type Skip
- type Testable
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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.
Types ¶
type Assertion ¶ added in v0.0.3
type Assertion struct {
Testable
// contains filtered or unexported fields
}
Assertion helpers
func (Assertion) E ¶ added in v0.0.6
func (as Assertion) E(args ...interface{})
E is a shortcut for Nil(args...).Must()
type Helper ¶ added in v0.4.0
type Helper struct {
Testable
}
Helper for commonly used methods
func (Helper) Error ¶ added in v0.4.0
func (hp Helper) Error(args ...interface{})
Error is the same as testing.common.Error
func (Helper) Fatal ¶ added in v0.4.0
func (hp Helper) Fatal(args ...interface{})
Fatal is the same as testing.common.Fatal
func (Helper) HandleHTTP ¶ added in v0.4.0
func (hp Helper) 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 (Helper) Log ¶ added in v0.4.0
func (hp Helper) Log(args ...interface{})
Log is the same as testing.common.Log
func (Helper) Open ¶ added in v0.4.0
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 (Helper) Parallel ¶ added in v0.4.0
func (hp Helper) Parallel()
Parallel is the same as testing.T.Parallel
func (Helper) ReadString ¶ added in v0.4.0
ReadString from r
func (Helper) Req ¶ added in v0.4.0
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 (Helper) Serve ¶ added in v0.4.0
Serve http on a random port. The server will be auto-closed after the test.
func (Helper) Skip ¶ added in v0.4.0
func (hp Helper) Skip(args ...interface{})
Skip is the same as testing.common.Skip
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
}
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.