pax_global_header00006660000000000000000000000064135522444400014515gustar00rootroot0000000000000052 comment=8eb943fbae8448cf47383208d8d0c396e26011a9 assert-2.0.1/000077500000000000000000000000001355224444000130165ustar00rootroot00000000000000assert-2.0.1/.gitignore000066400000000000000000000004121355224444000150030ustar00rootroot00000000000000# Compiled Object files, Static and Dynamic libs (Shared Objects) *.o *.a *.so # Folders _obj _test # Architecture specific extensions/prefixes *.[568vq] [568vq].out *.cgo1.go *.cgo2.c _cgo_defun.c _cgo_gotypes.go _cgo_export.* _testmain.go *.exe *.test *.prof assert-2.0.1/.travis.yml000066400000000000000000000004221355224444000151250ustar00rootroot00000000000000language: go go: - 1.13.1 - tip matrix: allow_failures: - go: tip notifications: email: recipients: dean.karn@gmail.com on_success: change on_failure: always # Only clone the most recent commit. git: depth: 1 script: - go test -v -race ./... assert-2.0.1/LICENSE000066400000000000000000000020651355224444000140260ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2015 Dean Karn 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. assert-2.0.1/README.md000066400000000000000000000037021355224444000142770ustar00rootroot00000000000000Package assert ============== [![Build Status](https://travis-ci.org/go-playground/assert.svg?branch=master)](https://travis-ci.org/go-playground/assert) [![GoDoc](https://godoc.org/github.com/go-playground/assert?status.svg)](https://godoc.org/gopkg.in/go-playground/assert.v1) Package assert is a Basic Assertion library used along side native go testing Installation ------------ Use go get. go get github.com/go-playground/assert Then import the assert package into your own code. import . "github.com/go-playground/assert/v2" Usage and documentation ------ Please see http://godoc.org/github.com/go-playground/assert for detailed usage docs. ##### Example: ```go package whatever import ( "errors" "testing" . "github.com/go-playground/assert/v2" ) func AssertCustomErrorHandler(t *testing.T, errs map[string]string, key, expected string) { val, ok := errs[key] // using EqualSkip and NotEqualSkip as building blocks for my custom Assert function EqualSkip(t, 2, ok, true) NotEqualSkip(t, 2, val, nil) EqualSkip(t, 2, val, expected) } func TestEqual(t *testing.T) { // error comes from your package/library err := errors.New("my error") NotEqual(t, err, nil) Equal(t, err.Error(), "my error") err = nil Equal(t, err, nil) fn := func() { panic("omg omg omg!") } PanicMatches(t, func() { fn() }, "omg omg omg!") PanicMatches(t, func() { panic("omg omg omg!") }, "omg omg omg!") // errs would have come from your package/library errs := map[string]string{} errs["Name"] = "User Name Invalid" errs["Email"] = "User Email Invalid" AssertCustomErrorHandler(t, errs, "Name", "User Name Invalid") AssertCustomErrorHandler(t, errs, "Email", "User Email Invalid") } ``` How to Contribute ------ Make a PR. I strongly encourage everyone whom creates a usefull custom assertion function to contribute them and help make this package even better. License ------ Distributed under MIT License, please see license file in code for more details. assert-2.0.1/assert.go000066400000000000000000000132731355224444000146540ustar00rootroot00000000000000package assert import ( "fmt" "path" "reflect" "regexp" "runtime" "testing" ) // IsEqual returns whether val1 is equal to val2 taking into account Pointers, Interfaces and their underlying types func IsEqual(val1, val2 interface{}) bool { v1 := reflect.ValueOf(val1) v2 := reflect.ValueOf(val2) if v1.Kind() == reflect.Ptr { v1 = v1.Elem() } if v2.Kind() == reflect.Ptr { v2 = v2.Elem() } if !v1.IsValid() && !v2.IsValid() { return true } switch v1.Kind() { case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: if v1.IsNil() { v1 = reflect.ValueOf(nil) } } switch v2.Kind() { case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: if v2.IsNil() { v2 = reflect.ValueOf(nil) } } v1Underlying := reflect.Zero(reflect.TypeOf(v1)).Interface() v2Underlying := reflect.Zero(reflect.TypeOf(v2)).Interface() if v1 == v1Underlying { if v2 == v2Underlying { goto CASE4 } else { goto CASE3 } } else { if v2 == v2Underlying { goto CASE2 } else { goto CASE1 } } CASE1: return reflect.DeepEqual(v1.Interface(), v2.Interface()) CASE2: return reflect.DeepEqual(v1.Interface(), v2) CASE3: return reflect.DeepEqual(v1, v2.Interface()) CASE4: return reflect.DeepEqual(v1, v2) } // NotMatchRegex validates that value matches the regex, either string or *regex // and throws an error with line number func NotMatchRegex(t *testing.T, value string, regex interface{}) { NotMatchRegexSkip(t, 2, value, regex) } // NotMatchRegexSkip validates that value matches the regex, either string or *regex // and throws an error with line number // but the skip variable tells NotMatchRegexSkip how far back on the stack to report the error. // This is a building block to creating your own more complex validation functions. func NotMatchRegexSkip(t *testing.T, skip int, value string, regex interface{}) { if r, ok, err := regexMatches(regex, value); ok || err != nil { _, file, line, _ := runtime.Caller(skip) if err != nil { fmt.Printf("%s:%d %v error compiling regex %v\n", path.Base(file), line, value, r.String()) } else { fmt.Printf("%s:%d %v matches regex %v\n", path.Base(file), line, value, r.String()) } t.FailNow() } } // MatchRegex validates that value matches the regex, either string or *regex // and throws an error with line number func MatchRegex(t *testing.T, value string, regex interface{}) { MatchRegexSkip(t, 2, value, regex) } // MatchRegexSkip validates that value matches the regex, either string or *regex // and throws an error with line number // but the skip variable tells MatchRegexSkip how far back on the stack to report the error. // This is a building block to creating your own more complex validation functions. func MatchRegexSkip(t *testing.T, skip int, value string, regex interface{}) { if r, ok, err := regexMatches(regex, value); !ok { _, file, line, _ := runtime.Caller(skip) if err != nil { fmt.Printf("%s:%d %v error compiling regex %v\n", path.Base(file), line, value, r.String()) } else { fmt.Printf("%s:%d %v does not match regex %v\n", path.Base(file), line, value, r.String()) } t.FailNow() } } func regexMatches(regex interface{}, value string) (*regexp.Regexp, bool, error) { var err error r, ok := regex.(*regexp.Regexp) // must be a string if !ok { if r, err = regexp.Compile(regex.(string)); err != nil { return r, false, err } } return r, r.MatchString(value), err } // Equal validates that val1 is equal to val2 and throws an error with line number func Equal(t *testing.T, val1, val2 interface{}) { EqualSkip(t, 2, val1, val2) } // EqualSkip validates that val1 is equal to val2 and throws an error with line number // but the skip variable tells EqualSkip how far back on the stack to report the error. // This is a building block to creating your own more complex validation functions. func EqualSkip(t *testing.T, skip int, val1, val2 interface{}) { if !IsEqual(val1, val2) { _, file, line, _ := runtime.Caller(skip) fmt.Printf("%s:%d %v does not equal %v\n", path.Base(file), line, val1, val2) t.FailNow() } } // NotEqual validates that val1 is not equal val2 and throws an error with line number func NotEqual(t *testing.T, val1, val2 interface{}) { NotEqualSkip(t, 2, val1, val2) } // NotEqualSkip validates that val1 is not equal to val2 and throws an error with line number // but the skip variable tells NotEqualSkip how far back on the stack to report the error. // This is a building block to creating your own more complex validation functions. func NotEqualSkip(t *testing.T, skip int, val1, val2 interface{}) { if IsEqual(val1, val2) { _, file, line, _ := runtime.Caller(skip) fmt.Printf("%s:%d %v should not be equal %v\n", path.Base(file), line, val1, val2) t.FailNow() } } // PanicMatches validates that the panic output of running fn matches the supplied string func PanicMatches(t *testing.T, fn func(), matches string) { PanicMatchesSkip(t, 2, fn, matches) } // PanicMatchesSkip validates that the panic output of running fn matches the supplied string // but the skip variable tells PanicMatchesSkip how far back on the stack to report the error. // This is a building block to creating your own more complex validation functions. func PanicMatchesSkip(t *testing.T, skip int, fn func(), matches string) { _, file, line, _ := runtime.Caller(skip) defer func() { if r := recover(); r != nil { err := fmt.Sprintf("%s", r) if err != matches { fmt.Printf("%s:%d Panic... expected [%s] received [%s]", path.Base(file), line, matches, err) t.FailNow() } } else { fmt.Printf("%s:%d Panic Expected, none found... expected [%s]", path.Base(file), line, matches) t.FailNow() } }() fn() } assert-2.0.1/assert_test.go000066400000000000000000000040721355224444000157100ustar00rootroot00000000000000package assert import ( "errors" "testing" ) // NOTES: // - Run "go test" to run tests // - Run "gocov test | gocov report" to report on test converage by file // - Run "gocov test | gocov annotate -" to report on all code and functions, those ,marked with "MISS" were never called // // or // // -- may be a good idea to change to output path to somewherelike /tmp // go test -coverprofile cover.out && go tool cover -html=cover.out -o cover.html // func MyCustomErrorHandler(t *testing.T, errs map[string]string, key, expected string) { val, ok := errs[key] EqualSkip(t, 2, ok, true) NotEqualSkip(t, 2, val, nil) EqualSkip(t, 2, val, expected) } func TestRegexMatchAndNotMatch(t *testing.T) { goodRegex := "^(.*/vendor/)?github.com/go-playground/assert$" MatchRegex(t, "github.com/go-playground/assert", goodRegex) MatchRegex(t, "/vendor/github.com/go-playground/assert", goodRegex) NotMatchRegex(t, "/vendor/github.com/go-playground/test", goodRegex) } func TestBasicAllGood(t *testing.T) { err := errors.New("my error") NotEqual(t, err, nil) Equal(t, err.Error(), "my error") err = nil Equal(t, err, nil) fn := func() { panic("omg omg omg!") } PanicMatches(t, func() { fn() }, "omg omg omg!") PanicMatches(t, func() { panic("omg omg omg!") }, "omg omg omg!") /* if you uncomment creates hard fail, that is expected // you cant really do this, but it is here for the sake of completeness fun := func() {} PanicMatches(t, func() { fun() }, "omg omg omg!") */ errs := map[string]string{} errs["Name"] = "User Name Invalid" errs["Email"] = "User Email Invalid" MyCustomErrorHandler(t, errs, "Name", "User Name Invalid") MyCustomErrorHandler(t, errs, "Email", "User Email Invalid") } func TestEquals(t *testing.T) { type Test struct { Name string } tst := &Test{ Name: "joeybloggs", } Equal(t, tst, tst) NotEqual(t, tst, nil) NotEqual(t, nil, tst) type TestMap map[string]string var tm TestMap Equal(t, tm, nil) Equal(t, nil, tm) var iface interface{} var iface2 interface{} iface = 1 Equal(t, iface, 1) NotEqual(t, iface, iface2) } assert-2.0.1/doc.go000066400000000000000000000023241355224444000141130ustar00rootroot00000000000000/* Package assert provides some basic assertion functions for testing and also provides the building blocks for creating your own more complex validations. package whatever import ( "errors" "testing" . "github.com/go-playground/assert.v1" ) func AssertCustomErrorHandler(t *testing.T, errs map[string]string, key, expected string) { val, ok := errs[key] // using EqualSkip and NotEqualSkip as building blocks for my custom Assert function EqualSkip(t, 2, ok, true) NotEqualSkip(t, 2, val, nil) EqualSkip(t, 2, val, expected) } func TestEqual(t *testing.T) { // error comes from your package/library err := errors.New("my error") NotEqual(t, err, nil) Equal(t, err.Error(), "my error") err = nil Equal(t, err, nil) fn := func() { panic("omg omg omg!") } PanicMatches(t, func() { fn() }, "omg omg omg!") PanicMatches(t, func() { panic("omg omg omg!") }, "omg omg omg!") // errs would have come from your package/library errs := map[string]string{} errs["Name"] = "User Name Invalid" errs["Email"] = "User Email Invalid" AssertCustomErrorHandler(t, errs, "Name", "User Name Invalid") AssertCustomErrorHandler(t, errs, "Email", "User Email Invalid") } */ package assert assert-2.0.1/go.mod000066400000000000000000000000631355224444000141230ustar00rootroot00000000000000module github.com/go-playground/assert/v2 go 1.13