pax_global_header00006660000000000000000000000064135704642550014525gustar00rootroot0000000000000052 comment=af92ad8ba2b5474c9b153873a00bd89f596f4baa test-1.0.6/000077500000000000000000000000001357046425500125105ustar00rootroot00000000000000test-1.0.6/LICENSE.md000066400000000000000000000020621357046425500141140ustar00rootroot00000000000000Copyright (c) 2015 Taco de Wolff Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.test-1.0.6/README.md000066400000000000000000000006101357046425500137640ustar00rootroot00000000000000# Test [![GoDoc](http://godoc.org/github.com/tdewolff/test?status.svg)](http://godoc.org/github.com/tdewolff/test) Test is a helper package written in [Go][1]. It implements a few functions that are useful for io testing, such as readers and writers that fail after N consecutive reads/writes. ## License Released under the [MIT license](LICENSE.md). [1]: http://golang.org/ "Go Language" test-1.0.6/go.mod000066400000000000000000000000511357046425500136120ustar00rootroot00000000000000module github.com/tdewolff/test go 1.12 test-1.0.6/readers.go000066400000000000000000000034361357046425500144720ustar00rootroot00000000000000package test import "io" // PlainReader implements an io.Reader and wraps over an existing io.Reader to hide other functions it implements. type PlainReader struct { r io.Reader } // NewPlainReader returns a new PlainReader. func NewPlainReader(r io.Reader) *PlainReader { return &PlainReader{r} } // Read implements the io.Reader interface. func (r *PlainReader) Read(p []byte) (int, error) { return r.r.Read(p) } //////////////////////////////////////////////////////////////// // ErrorReader implements an io.Reader that will do N successive reads before it returns ErrPlain. type ErrorReader struct { n int } // NewErrorReader returns a new ErrorReader. func NewErrorReader(n int) *ErrorReader { return &ErrorReader{n} } // Read implements the io.Reader interface. func (r *ErrorReader) Read(b []byte) (n int, err error) { if len(b) == 0 { return 0, nil } if r.n == 0 { return 0, ErrPlain } r.n-- b[0] = '.' return 1, nil } //////////////////////////////////////////////////////////////// // InfiniteReader implements an io.Reader that will always read-in one character. type InfiniteReader struct{} // NewInfiniteReader returns a new InfiniteReader. func NewInfiniteReader() *InfiniteReader { return &InfiniteReader{} } // Read implements the io.Reader interface. func (r *InfiniteReader) Read(b []byte) (n int, err error) { if len(b) == 0 { return 0, nil } b[0] = '.' return 1, nil } //////////////////////////////////////////////////////////////// // EmptyReader implements an io.Reader that will always return 0, nil. type EmptyReader struct { } // NewEmptyReader returns a new EmptyReader. func NewEmptyReader() *EmptyReader { return &EmptyReader{} } // Read implements the io.Reader interface. func (r *EmptyReader) Read(b []byte) (n int, err error) { return 0, io.EOF } test-1.0.6/test.go000066400000000000000000000072061357046425500140230ustar00rootroot00000000000000package test import ( "bytes" "errors" "fmt" "math" "reflect" "runtime" "strings" "testing" ) // ErrPlain is the default error that is returned for functions in this package. var ErrPlain = errors.New("error") //////////////////////////////////////////////////////////////// func fileline(i int) string { _, file, line, ok := runtime.Caller(i) if !ok { return "" } parts := strings.Split(file, "/") file = parts[len(parts)-1] return fmt.Sprintf("%s:%d", file, line) } func trace() string { trace2 := fileline(2) trace3 := fileline(3) return "\r " + strings.Repeat(" ", len(fmt.Sprintf("%s:", trace2))) + "\r " + trace3 } func message(msgs ...interface{}) string { if len(msgs) == 0 { return "" } s := fmt.Sprintln(msgs...) s = s[:len(s)-1] // remove newline return ": " + s } func printable(s string) string { s = strings.Replace(s, "\n", `\n`, -1) s = strings.Replace(s, "\r", `\r`, -1) s = strings.Replace(s, "\t", `\t`, -1) s = strings.Replace(s, "\x00", `\0`, -1) return s } const ( Red = "31" Green = "32" ) func color(color string, s interface{}) string { return fmt.Sprintf("\033[00;%sm%v\033[00m", color, s) } ///////////////////////////////////////////////////////////////// func Fail(t *testing.T, msgs ...interface{}) { t.Fatalf("%s%s", trace(), message(msgs...)) } func Error(t *testing.T, err error, msgs ...interface{}) { if err != nil { t.Fatalf("%s%s: %s", trace(), message(msgs...), color(Red, err.Error())) } } func That(t *testing.T, condition bool, msgs ...interface{}) { if !condition { t.Fatalf("%s%s: false", trace(), message(msgs...)) } } func T(t *testing.T, got, wanted interface{}, msgs ...interface{}) { gotType := reflect.TypeOf(got) wantedType := reflect.TypeOf(wanted) if gotType != wantedType { t.Fatalf("%s%s: type %v != %v", trace(), message(msgs...), color(Red, gotType), color(Green, wantedType)) return } if reflect.DeepEqual(got, wanted) { return } if wantedType != nil { if equals, ok := wantedType.MethodByName("Equals"); ok && equals.Type.NumIn() == 2 && equals.Type.NumOut() == 1 && equals.Type.In(0) == wantedType && equals.Type.In(1) == gotType && equals.Type.Out(0).Kind() == reflect.Bool && equals.Func.Call([]reflect.Value{reflect.ValueOf(wanted), reflect.ValueOf(got)})[0].Bool() { return } } t.Fatalf("%s%s: %v != %v", trace(), message(msgs...), color(Red, got), color(Green, wanted)) } func Bytes(t *testing.T, got, wanted []byte, msgs ...interface{}) { if !bytes.Equal(got, wanted) { gotString := printable(string(got)) wantedString := printable(string(wanted)) t.Fatalf("%s%s:\n%s\n%s", trace(), message(msgs...), color(Red, gotString), color(Green, wantedString)) } } func String(t *testing.T, got, wanted string, msgs ...interface{}) { if got != wanted { gotString := printable(got) wantedString := printable(wanted) t.Fatalf("%s%s:\n%s\n%s", trace(), message(msgs...), color(Red, gotString), color(Green, wantedString)) } } func Float(t *testing.T, got, wanted float64, msgs ...interface{}) { if math.IsNaN(wanted) != math.IsNaN(got) || !math.IsNaN(wanted) && math.Abs(got-wanted) > 1e-6 { t.Fatalf("%s%s: %v != %v", trace(), message(msgs...), color(Red, got), color(Green, wanted)) } } func Minify(t *testing.T, input string, err error, got, wanted string, msgs ...interface{}) { inputString := printable(input) if err != nil { t.Fatalf("%s%s:\n%s\n%s", trace(), message(msgs...), inputString, color(Red, err.Error())) return } if got != wanted { gotString := printable(got) wantedString := printable(wanted) t.Fatalf("%s%s:\n%s\n%s\n%s", trace(), message(msgs...), inputString, color(Red, gotString), color(Green, wantedString)) } } test-1.0.6/writers.go000066400000000000000000000006641357046425500145440ustar00rootroot00000000000000package test // ErrorWriter implements an io.Writer that will do N successive writes before it returns ErrPlain. type ErrorWriter struct { n int } // NewErrorWriter returns a new ErrorWriter. func NewErrorWriter(n int) *ErrorWriter { return &ErrorWriter{n} } // Write implements the io.Writer interface. func (w *ErrorWriter) Write(b []byte) (n int, err error) { if w.n == 0 { return 0, ErrPlain } w.n-- return len(b), nil }