Documentation
¶
Overview ¶
Package check provides dead-simple assertions and utilities for testing.
All tests created with check.New() run in parallel, so be warned.
Example (Check) ¶
package main import ( "fmt" "os" "path/filepath" "github.com/iheartradio/cog/check" ) func main() { // Typically you would pass in your *testing.T or *testing.B here c := check.New(nil) // These are just a few of the provided functions. Check out the full // documentation for everything. c.Equal(1, 1, "the universe is falling apart") c.NotEqual(1, 2, "those can't be equal!") panics := func() { panic("i get nervous sometimes") } c.Panics(panics, "this should always panic") // Make absolute path relative for example output checking wd, _ := os.Getwd() rel, _ := filepath.Rel(wd, c.FS.Path("test_file")) // Assuming you're using check.Main() in a TestMain() function, the test // data directory is wiped out when all tests pass, so go ahead and make // things messy. fmt.Println("test-specific file:", rel) }
Output: test-specific file: test_data/Example_check/test_file
Example (Errorer) ¶
package main import ( "bytes" "errors" "fmt" "io" "github.com/iheartradio/cog/check" ) type MockWriter struct { check.Errorer io.Writer } func (mw *MockWriter) Write(b []byte) (int, error) { if mw.Fail() { return 0, fmt.Errorf("errorer forced to fail") } return mw.Writer.Write(b) } func main() { buff := &bytes.Buffer{} w := MockWriter{Writer: buff} // Errors are returned based on the call stack: on the first call from a // given stack, Fail() returns true. On any successive calls, Fail() returns // false. // // By doing this, you can test all errors by repeatedly calling the same // function until it succeeds. This allows you to test all error pathways // and ensure that everything works right. The following code demonstrates // this idea. err := errors.New("") for err != nil { _, err = w.Write([]byte("important data")) fmt.Println("Error from write:", err) } }
Output: Error from write: errorer forced to fail Error from write: <nil>
Index ¶
- Constants
- Variables
- func Cleanup()
- func Fixture(parts ...string) string
- func GetTestDetails() (path, name string)
- func GetTestName() string
- func Main(m *testing.M)
- func Setup()
- type C
- func (c *C) B() *testing.B
- func (c *C) Contains(iter, v interface{}, msg ...interface{}) bool
- func (c *C) Equal(e, g interface{}, msg ...interface{}) bool
- func (c *C) Error(err error, msg ...interface{}) bool
- func (c *C) False(cond bool, msg ...interface{}) bool
- func (c *C) Is(e, g interface{}, msg ...interface{}) bool
- func (c *C) IsNot(e, g interface{}, msg ...interface{}) bool
- func (c *C) Len(v interface{}, l int, msg ...interface{}) (eq bool)
- func (c *C) LenNot(v interface{}, l int, msg ...interface{}) (eq bool)
- func (c *C) MustBe(e, g interface{}, msg ...interface{})
- func (c *C) MustContain(iter, v interface{}, msg ...interface{})
- func (c *C) MustEqual(e, g interface{}, msg ...interface{})
- func (c *C) MustError(err error, msg ...interface{})
- func (c *C) MustFalse(cond bool, msg ...interface{})
- func (c *C) MustLen(v interface{}, l int, msg ...interface{})
- func (c *C) MustLenNot(v interface{}, l int, msg ...interface{})
- func (c *C) MustNotBe(e, g interface{}, msg ...interface{})
- func (c *C) MustNotContain(iter, v interface{}, msg ...interface{})
- func (c *C) MustNotEqual(e, g interface{}, msg ...interface{})
- func (c *C) MustNotError(err error, msg ...interface{})
- func (c *C) MustNotPanic(fn func(), msg ...interface{})
- func (c *C) MustPanic(fn func(), msg ...interface{})
- func (c *C) MustTrue(cond bool, msg ...interface{})
- func (c *C) NotContains(iter, v interface{}, msg ...interface{}) bool
- func (c *C) NotEqual(e, g interface{}, msg ...interface{}) bool
- func (c *C) NotError(err error, msg ...interface{}) bool
- func (c *C) NotPanic(fn func(), msg ...interface{}) (ok bool)
- func (c *C) Panics(fn func(), msg ...interface{}) (ok bool)
- func (c *C) T() *testing.T
- func (c *C) True(cond bool, msg ...interface{}) bool
- func (c *C) Until(wait time.Duration, fn func() bool, msg ...interface{})
- type Errorer
- type FS
- func (fs *FS) CleanDataDir()
- func (fs *FS) ContentsEqual(path string, b []byte)
- func (fs *FS) DirExists(path string)
- func (fs *FS) DirNotExists(path string)
- func (fs *FS) DumpTree(path string)
- func (fs *FS) FileExists(path string)
- func (fs *FS) FileNotExists(path string)
- func (fs *FS) GetDataDir() string
- func (fs *FS) Path(parts ...string) string
- func (fs *FS) ReadFile(path string) []byte
- func (fs *FS) SContentsEqual(path string, b string)
- func (fs *FS) SReadFile(path string) string
- func (fs *FS) SWriteFile(path, contents string)
- func (fs *FS) WriteFile(path string, contents []byte)
Examples ¶
Constants ¶
const DataDir = "test_data"
DataDir is the name of the directory where test-specific files live; that is, files created specifically for a single test while running the test.
Variables ¶
var ( // FixtureDir is the directory that all of your fixtures live in. This // directory is located by walking up the tree, starting at the CWD until a // directory with the given name is found. Typically, this will be // "test_fixtures", but you may choose your own name. FixtureDir = "test_fixtures" )
Functions ¶
func Cleanup ¶
func Cleanup()
Cleanup removes any and all testing state
This is typically only called from Main(), so you needn't worry about it unless you're implementing your own TestMain().
func Fixture ¶
Fixture gets the path to the fixture described by `parts`. If the FixtureDir is not found, this panics.
The fixture directory is found by climbing the FS tree until a directory containing the directory defined by the constant FixtureDir is found. If found, the absolute path to that directory is joined with the given parts is returned.
func GetTestDetails ¶
func GetTestDetails() (path, name string)
GetTestDetails gets the file path and name of the test.
func Main ¶
Main provides an alternative main for testing. This sets the testing environment up and also cleans it up on success; on failure, test files are left so that you can better inspect the failure.
If you want to use this functionality, add the following somewhere in your tests:
func TestMain(m *testing.M) { check.Main(m) }
Types ¶
type C ¶
C is like *testing.T/*testing.B, but with more fun
func (*C) B ¶
B provides access to the underlying *testing.B. If C was not instantiated with a *testing.B, this panics.
func (*C) Contains ¶
Contains checks that iter contains v. Returns true if it does, false otherwise.
The following checks are done:
- If a map, checks if the map contains key v.
- If iter is a slice/array and v is a slice/array, checks to see if v is a subset of iter.
- If iter is a slice/array and v is not, checks if any element in iter equals v.
- If iter is a string, falls back to strings.Contains.
func (*C) Equal ¶
Equal compares to things, ensuring that they are equal to each other. `e` is the expected value; `g` is the value you got somewhere else. Returns true if they not equal, false otherwise.
Equal takes special care of floating point numbers, ensuring that any precision loss doesn't affect their equality.
If `e` is nil, `g` will be checked for nil, and if it's an interface, its value will be checked for nil. Keep in mind that, for interfaces, this is _not_ a strict `g == nil` comparison.
func (*C) Error ¶
Error ensures that an error is not nil. Returns true if an error was received, false otherwise.
func (*C) False ¶
False checks that the given bool is false. Returns the value opposite value of the bool.
func (*C) Is ¶
Is ensures that g is the same type as e. Returns true if they are the same type, false otherwise.
func (*C) IsNot ¶
IsNot ensures that g is not the same type as e. Returns true if they are not the same type, false otherwise.
func (*C) Len ¶
Len checks that the length of the given v is l. Returns true if equal, false otherwise.
func (*C) LenNot ¶
LenNot checks that the length of the given v is not l. Returns true if not equal, false otherwise.
func (*C) MustBe ¶
func (c *C) MustBe(e, g interface{}, msg ...interface{})
MustBe is like Is, except it panics on failure.
func (*C) MustContain ¶
func (c *C) MustContain(iter, v interface{}, msg ...interface{})
MustContain is like Contains, except it panics on failure.
func (*C) MustEqual ¶
func (c *C) MustEqual(e, g interface{}, msg ...interface{})
MustEqual is like Equal, except it panics on failure.
func (*C) MustLenNot ¶
MustLenNot is like LenNot, except it panics on failure.
func (*C) MustNotBe ¶
func (c *C) MustNotBe(e, g interface{}, msg ...interface{})
MustNotBe is like IsNot, except it panics on failure.
func (*C) MustNotContain ¶
func (c *C) MustNotContain(iter, v interface{}, msg ...interface{})
MustNotContain is like NotContains, except it panics on failure.
func (*C) MustNotEqual ¶
func (c *C) MustNotEqual(e, g interface{}, msg ...interface{})
MustNotEqual is like NotEqual, except it panics on failure.
func (*C) MustNotError ¶
MustNotError is like NotError, except it panics on failure.
func (*C) MustNotPanic ¶
func (c *C) MustNotPanic(fn func(), msg ...interface{})
MustNotPanic is like NotPanic, except it panics on failure.
func (*C) MustPanic ¶
func (c *C) MustPanic(fn func(), msg ...interface{})
MustPanic is like Panic, except it panics on failure.
func (*C) NotContains ¶
NotContains checks that v does not contain c. Returns true if it does, false otherwise.
func (*C) NotEqual ¶
NotEqual compares to things, ensuring that they do not equal each other. Returns true if they are not equal, false otherwise.
NotEqual takes special care of floating point numbers, ensuring that any precision loss doesn't affect their equality.
func (*C) NotError ¶
NotError ensures that an error is nil. Returns true if no error was found, false otherwise.
func (*C) T ¶
T provides access to the underlying *testing.T. If C was not instantiated with a *testing.T, this panics.
type Errorer ¶
type Errorer struct { // If test functions should not be considered when comparing stack traces IgnoreTestFns bool // Only functions with these names should be considered for errors OnlyIn []string // contains filtered or unexported fields }
Errorer is useful for mocking out things that return errors. It will return an error for every unique stack trace that it sees, but only on the first run. This allows you to run the same code many times in succession until it succeeds. By doing this, you can test that all your error pathways function correctly.
type FS ¶
type FS struct {
// contains filtered or unexported fields
}
FS provides access to a test's directory
func (*FS) CleanDataDir ¶
func (fs *FS) CleanDataDir()
CleanDataDir wipes out this test's data directory
func (*FS) ContentsEqual ¶
ContentsEqual checks that the contents of the given file exactly equal the given byte slice.
func (*FS) DirNotExists ¶
DirNotExists checks that the given directory exists
func (*FS) DumpTree ¶
DumpTree walks the tree at the given path and writes each file to the test log
func (*FS) FileExists ¶
FileExists checks that the given file exists
func (*FS) FileNotExists ¶
FileNotExists checks that the given file does not exist
func (*FS) GetDataDir ¶
GetDataDir gets the test's data directory. On the first call, this also clears out any data directory that existed previously, giving your test a clean space to run.
If the DataDir is not found, this panics.
func (*FS) Path ¶
Path gets the absolute path of the test's data dir joined with the parts. The parent dirs are created, assuming that the last part is the file.
func (*FS) SContentsEqual ¶
SContentsEqual is like ContentsEqual, but with strings.
func (*FS) SWriteFile ¶
SWriteFile is like WriteFile, but it works on strings
Directories
¶
Path | Synopsis |
---|---|
Package chlog provides clog-based logging for testing Usage is really simple: import "github.com/iheartradio/cog/check/chlog" func TestStuff(t *testing.T) { log := chlog.New(t) }
|
Package chlog provides clog-based logging for testing Usage is really simple: import "github.com/iheartradio/cog/check/chlog" func TestStuff(t *testing.T) { log := chlog.New(t) } |