pax_global_header00006660000000000000000000000064140676673210014526gustar00rootroot0000000000000052 comment=d17860d172abca8d8ee415d7e079b6f834955055 golang-github-smallstep-assert-0.0~git20200723.82e2b9b/000077500000000000000000000000001406766732100222135ustar00rootroot00000000000000golang-github-smallstep-assert-0.0~git20200723.82e2b9b/.gitignore000066400000000000000000000003001406766732100241740ustar00rootroot00000000000000# Binaries for programs and plugins *.exe *.exe~ *.dll *.so *.dylib # Test binary, build with `go test -c` *.test # Output of the go coverage tool, specifically when used with LiteIDE *.out golang-github-smallstep-assert-0.0~git20200723.82e2b9b/LICENSE000066400000000000000000000020521406766732100232170ustar00rootroot00000000000000MIT License Copyright (c) 2018 Smallstep 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. golang-github-smallstep-assert-0.0~git20200723.82e2b9b/README.md000066400000000000000000000006231406766732100234730ustar00rootroot00000000000000assert ======= [![GoDoc](https://godoc.org/github.com/smallstep/assert?status.svg)](http://godoc.org/github.com/smallstep/assert) [![Go Report Card](https://goreportcard.com/badge/github.com/smallstep/assert)](https://goreportcard.com/report/github.com/smallstep/assert) A simple assertion framework for [Go](http://golang.org/). Read online reference at http://godoc.org/github.com/smallstep/assert golang-github-smallstep-assert-0.0~git20200723.82e2b9b/assert.go000066400000000000000000000144521406766732100240510ustar00rootroot00000000000000package assert import ( "fmt" "reflect" "strings" ) // Tester is an interface that testing.T implements, It has the methods used // in the implementation of this package. type Tester interface { Errorf(format string, args ...interface{}) Fatalf(format string, args ...interface{}) Helper() } func reportError(t Tester, msg []interface{}) { args := append([]interface{}{}, msg...) t.Helper() t.Errorf(fmt.Sprintln(args...)) } func message(msg []interface{}, format string, a ...interface{}) []interface{} { if len(msg) > 0 { return msg } str := fmt.Sprintf(format, a...) return []interface{}{str} } // True checks that a condition is true. func True(t Tester, condition bool, msg ...interface{}) bool { if !condition { t.Helper() reportError(t, message(msg, "assert condition is not true")) return false } return true } // False checks that a condition is false. func False(t Tester, condition bool, msg ...interface{}) bool { if condition { t.Helper() reportError(t, message(msg, "assert condition is not false")) return false } return true } // Fatal checks that a condition is true or marks the test as failed and stop // it's execution. func Fatal(t Tester, condition bool, msg ...interface{}) { if !condition { msg = message(msg, "assert condition is not true") args := append([]interface{}{}, msg...) t.Helper() t.Fatalf(fmt.Sprintln(args...)) } } // FatalError checks that a error is nil or marks the test as failed and stop // it's execution. func FatalError(t Tester, err error, msg ...interface{}) { if err != nil { msg = message(msg, "error '%s' not expected", err) args := append([]interface{}{}, msg...) t.Helper() t.Fatalf(fmt.Sprintln(args...)) } } // Error checks if err is not nil. func Error(t Tester, err error, msg ...interface{}) bool { if err == nil { t.Helper() reportError(t, message(msg, "error expected but not found")) return false } return true } // NoError checks if err nil. func NoError(t Tester, err error, msg ...interface{}) bool { if err != nil { t.Helper() reportError(t, message(msg, "error '%s' not expected", err)) return false } return true } // Equals checks that expected and actual are equal. func Equals(t Tester, expected, actual interface{}, msg ...interface{}) bool { if reflect.DeepEqual(expected, actual) { return true } v1 := reflect.ValueOf(expected) v2 := reflect.ValueOf(actual) if isNilable(v1) && isNilable(v2) { switch { case v1.IsValid() && !v2.IsValid() && v1.IsNil(): return true case !v1.IsValid() && v2.IsValid() && v2.IsNil(): return true case v1.IsValid() && v2.IsValid() && v1.Type() == v2.Type() && v1.IsNil() && v2.IsNil(): return true } } t.Helper() reportError(t, message(msg, "'%v' and '%v' are not equal", expected, actual)) return false } // NotEquals checks that expected and actual are not equal. func NotEquals(t Tester, expected, actual interface{}, msg ...interface{}) bool { v1 := reflect.ValueOf(expected) v2 := reflect.ValueOf(actual) if isNilable(v1) && isNilable(v2) { switch { case v1.IsValid() && !v2.IsValid() && v1.IsNil(): fallthrough case !v1.IsValid() && v2.IsValid() && v2.IsNil(): fallthrough case v1.IsValid() && v2.IsValid() && v1.Type() == v2.Type() && v1.IsNil() && v2.IsNil(): t.Helper() reportError(t, message(msg, "'%v' and '%v' are equal", expected, actual)) return false } } if reflect.DeepEqual(expected, actual) { t.Helper() reportError(t, message(msg, "'%v' and '%v' are equal", expected, actual)) return false } return true } // Nil checks that the value is nil. func Nil(t Tester, value interface{}, msg ...interface{}) bool { var ret bool v := reflect.ValueOf(value) if isNilable(v) { ret = !v.IsValid() || v.IsNil() } if !ret { t.Helper() reportError(t, message(msg, "nil expected and found '%v'", value)) return false } return true } // NotNil checks that the value is not nil. func NotNil(t Tester, value interface{}, msg ...interface{}) bool { v := reflect.ValueOf(value) if isNilable(v) { if !v.IsValid() || v.IsNil() { t.Helper() reportError(t, message(msg, "not nil expected and found '%v'", value)) return false } } return true } // Len checks that the application of len() to value match the expected value. func Len(t Tester, expected int, value interface{}, msg ...interface{}) bool { v := reflect.ValueOf(value) switch v.Kind() { case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String: if v.Len() != expected { t.Helper() reportError(t, message(msg, "len '%d' expected and found '%d'", expected, v.Len())) return false } return true default: t.Helper() reportError(t, message(msg, "cannot apply built-in function len to '%s' (%v)", v.Kind(), value)) return false } } // Panic checks that the passed function panics. func Panic(t Tester, f func(), msg ...interface{}) (ret bool) { t.Helper() defer func() { ret = true if r := recover(); r == nil { t.Helper() reportError(t, message(msg, "function did not panic")) ret = false } }() f() return } // Type checks that the value matches the type of expected. func Type(t Tester, expected, value interface{}, msg ...interface{}) bool { te := reflect.TypeOf(expected) tv := reflect.TypeOf(value) if te == tv { return true } t.Helper() reportError(t, message(msg, "type '%T' expected and found '%T'", expected, value)) return false } // isNilable returns if the kind of v can be nil or not. It will return true // for invalid values or if the kind is chan, func, interface, map, pointer, // or slice; it will return false for the rest. func isNilable(v reflect.Value) bool { switch v.Kind() { case reflect.Invalid, reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Slice: return true default: return false } } // HasPrefix checks that the string contains the given prefix. func HasPrefix(t Tester, s, p string, msg ...interface{}) bool { if strings.HasPrefix(s, p) { return true } t.Helper() reportError(t, message(msg, "'%s' is not a prefix of '%s'", p, s)) return false } // HasSuffix checks that the string ends with the given suffix. func HasSuffix(t Tester, s, suffix string, msg ...interface{}) bool { if strings.HasSuffix(s, suffix) { return true } t.Helper() reportError(t, message(msg, "'%s' is not a suffix of '%s'", suffix, s)) return false } golang-github-smallstep-assert-0.0~git20200723.82e2b9b/assert_test.go000066400000000000000000000156441406766732100251140ustar00rootroot00000000000000package assert import ( "errors" "reflect" "testing" ) type tester struct { method string format string args []interface{} } func tt() *tester { return &tester{} } func (t *tester) Errorf(format string, args ...interface{}) { t.method = "Errorf" t.format = format t.args = args } func (t *tester) Fatalf(format string, args ...interface{}) { t.method = "Fatalf" t.format = format t.args = args } func (t *tester) Helper() {} func TestMessage(t *testing.T) { args := []interface{}{"%s", "a message"} if !reflect.DeepEqual(args, message(args, "default message")) { t.Fail() } if !reflect.DeepEqual([]interface{}{"default message"}, message(nil, "default message")) { t.Fail() } } func TestTrue(t *testing.T) { if True(tt(), false) { t.Fail() } if !True(tt(), true) { t.Fail() } } func TestFalse(t *testing.T) { if False(tt(), true) { t.Fail() } if !False(tt(), false) { t.Fail() } } func TestFatal(t *testing.T) { t1 := tt() Fatal(t1, false) if t1.method != "Fatalf" { t.Fail() } t2 := tt() Fatal(t2, true) if t2.method != "" { t.Fail() } } func TestFatalError(t *testing.T) { var err error t1 := tt() FatalError(t1, err) if t1.method != "" { t.Fail() } t2 := tt() FatalError(t2, errors.New("an error")) if t2.method != "Fatalf" { t.Fail() } } func TestError(t *testing.T) { var err error if Error(tt(), err) { t.Fail() } if !Error(tt(), errors.New("an error")) { t.Fail() } } func TestNoError(t *testing.T) { if NoError(tt(), errors.New("an error")) { t.Fail() } var err error if !NoError(tt(), err) { t.Fail() } } func TestEquals(t *testing.T) { type myint int var nilPtr *int var myintPtr *myint var nilInterface interface{} var notNilInterface interface{} val, myval := 123, myint(123) notNilPtr := &val notNilInterface = val myintPtr = &myval type aType struct{} var nilType *aType notNilType := new(aType) tests := []struct { a, b interface{} res bool }{ {nil, nil, true}, {0, 0, true}, {0, nil, false}, {nilPtr, nil, true}, {notNilPtr, nil, false}, {nilInterface, nil, true}, {notNilInterface, nil, false}, {nilPtr, nilPtr, true}, {notNilPtr, notNilPtr, true}, {nilInterface, nilInterface, true}, {notNilInterface, notNilInterface, true}, {nilPtr, nilInterface, true}, {myint(123), myint(123), true}, {nilType, nil, true}, {nilType, nilType, true}, {notNilType, nil, false}, {notNilType, notNilType, true}, {nil, nilPtr, true}, {nil, nilInterface, true}, {nil, nilType, true}, {nilType, interface{}(nilType), true}, {interface{}(notNilType), interface{}(notNilType), true}, {interface{}(nilType), interface{}(nilType), true}, {notNilType, interface{}(notNilType), true}, {interface{}(notNilType), interface{}(notNilType), true}, // not same type {123, myint(123), false}, {notNilPtr, notNilInterface, false}, {notNilPtr, myintPtr, false}, {*notNilPtr, *myintPtr, false}, } for i, tc := range tests { if Equals(tt(), tc.a, tc.b) != tc.res { _ = i t.Errorf("test %d with %v and %v failed", i, tc.a, tc.b) } } } func TestNotEquals(t *testing.T) { type myint int var nilPtr *int var myintPtr *myint var nilInterface interface{} var notNilInterface interface{} val, myval := 123, myint(123) notNilPtr := &val notNilInterface = val myintPtr = &myval type aType struct{} var nilType *aType notNilType := new(aType) tests := []struct { a, b interface{} res bool }{ {nil, nil, false}, {0, 0, false}, {0, nil, true}, {nilPtr, nil, false}, {notNilPtr, nil, true}, {nilInterface, nil, false}, {notNilInterface, nil, true}, {nilPtr, nilPtr, false}, {notNilPtr, notNilPtr, false}, {nilInterface, nilInterface, false}, {notNilInterface, notNilInterface, false}, {nilPtr, nilInterface, false}, {myint(123), myint(123), false}, {nilType, nil, false}, {nilType, nilType, false}, {notNilType, nil, true}, {notNilType, notNilType, false}, {nil, nilPtr, false}, {nil, nilInterface, false}, {nil, nilType, false}, {nilType, interface{}(nilType), false}, {interface{}(notNilType), interface{}(notNilType), false}, {interface{}(nilType), interface{}(nilType), false}, {notNilType, interface{}(notNilType), false}, {interface{}(notNilType), interface{}(notNilType), false}, // not same type {123, myint(123), true}, {notNilPtr, notNilInterface, true}, {notNilPtr, myintPtr, true}, {*notNilPtr, *myintPtr, true}, } for i, tc := range tests { if i == 3 || true { if NotEquals(tt(), tc.a, tc.b) != tc.res { t.Errorf("test %d with %v and %v failed", i, tc.a, tc.b) } } } } func TestNil(t *testing.T) { var nilPtr *int var nilInterface interface{} var notNilInterface interface{} val := 123 notNilPtr := &val notNilInterface = val type aType struct{} var nilType *aType notNilType := new(aType) tests := []struct { v interface{} res bool }{ {nil, true}, {0, false}, {1, false}, {nilPtr, true}, {notNilPtr, false}, {nilInterface, true}, {notNilInterface, false}, {nilType, true}, {notNilType, false}, } for i, tc := range tests { if Nil(tt(), tc.v) != tc.res { t.Errorf("test %d with %v failed", i, tc.v) } } } func TestNotNil(t *testing.T) { var nilPtr *int var nilInterface interface{} var notNilInterface interface{} val := 123 notNilPtr := &val notNilInterface = val type aType struct{} var nilType *aType notNilType := new(aType) tests := []struct { v interface{} res bool }{ {nil, false}, {0, true}, {1, true}, {nilPtr, false}, {notNilPtr, true}, {nilInterface, false}, {notNilInterface, true}, {nilType, false}, {notNilType, true}, } for i, tc := range tests { if NotNil(tt(), tc.v) != tc.res { t.Errorf("test %d with %v failed", i, tc.v) } } } func TestLen(t *testing.T) { tests := []struct { v interface{} expected int res bool }{ {nil, 0, false}, {1234, 0, false}, {"", 0, true}, {"1234", 4, true}, {[]int(nil), 0, true}, {[]int{}, 0, true}, {[]int{1, 2, 3}, 3, true}, {[2]string{"foo", "bar"}, 2, true}, {[...]string{"foo", "bar", "zar"}, 3, true}, {map[string]int{}, 0, true}, {map[string]int{"foo": 123}, 1, true}, {make(chan int), 0, true}, } for i, tc := range tests { if Len(tt(), tc.expected, tc.v) != tc.res { t.Errorf("test %d with %v failed", i, tc.v) } } } func TestPanic(t *testing.T) { withPanic := func() { panic("an error") } t1 := tt() if !Panic(t1, withPanic) || t1.method != "" { t.Fail() } t2 := tt() if Panic(t2, func() {}) || t2.method != "Errorf" { t.Fail() } } func TestType(t *testing.T) { type mytype string tests := []struct { e interface{} v interface{} res bool }{ {0, 1, true}, {0, "0", false}, {mytype("a"), mytype("a"), true}, {mytype("a"), mytype("b"), true}, {mytype("a"), "a", false}, {&tester{}, tt(), true}, {tester{}, tt(), false}, } for i, tc := range tests { if Type(tt(), tc.e, tc.v) != tc.res { t.Errorf("test %d with %v failed", i, tc.v) } } } golang-github-smallstep-assert-0.0~git20200723.82e2b9b/go.mod000066400000000000000000000000541406766732100233200ustar00rootroot00000000000000module github.com/smallstep/assert go 1.13