Skip to content

Commit

Permalink
Release v0.5.0 (#15)
Browse files Browse the repository at this point in the history
* Add NotOK and AnyError for convenience

Signed-off-by: themue <frank.mueller@themue.dev>

* Add test in convert and panic if needed

Signed-off-by: themue <frank.mueller@themue.dev>

* Add OneOf to the generator

Signed-off-by: themue <frank.mueller@themue.dev>

* Testing uses Printer if Failer implements it

* Simplify usage of failer printer

* Added changes to CHANGELOG
  • Loading branch information
themue authored May 29, 2021
1 parent eab38ad commit 3d75fbd
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*.so
*.dylib

# Development environments
.theia

# Test binary, built with `go test -c`
*.test

Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## v0.5.0

* (A) Asserts now contains NotOK() and AnyError()
* (C) Asserts created with NewTesting() now uses the Failable as Printer if
it implements the according interface
* (A) Generator now contains OneOf()

## v0.4.0

* (A) Asserts now contains NotPanics() and PanicsWith()
Expand Down
39 changes: 37 additions & 2 deletions asserts/asserts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Tideland Go Audit - Asserts
//
// Copyright (C) 2012-2020 Frank Mueller / Tideland / Oldenburg / Germany
// Copyright (C) 2012-2021 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
Expand Down Expand Up @@ -101,6 +101,28 @@ func (a *Asserts) OK(obtained interface{}, msgs ...string) bool {
}
}

// NotOK is a convenient metatest depending in the obtained tyoe. In case
// of a bool it has to be false, a func() bool has to return false, an int
// has to be not 0, a string has to be not empty, and a func() error has to
// return an error. Any else value has to be not nil or in case of an ErrorProne
// its Err() has not to return nil.
func (a *Asserts) NotOK(obtained interface{}, msgs ...string) bool {
switch o := obtained.(type) {
case bool:
return a.False(o, msgs...)
case func() bool:
return a.False(o(), msgs...)
case int:
return a.Different(o, 0, msgs...)
case string:
return a.Different(o, "", msgs...)
case func() error:
return a.AnyError(o(), msgs...)
default:
return a.AnyError(obtained, msgs...)
}
}

// True tests if obtained is true.
func (a *Asserts) True(obtained bool, msgs ...string) bool {
if !isTrue(obtained) {
Expand Down Expand Up @@ -158,6 +180,15 @@ func (a *Asserts) NoError(obtained interface{}, msgs ...string) bool {
return true
}

// AnyError tests if the obtained error or ErrorProne.Err() is not nil.
func (a *Asserts) AnyError(obtained interface{}, msgs ...string) bool {
err := ifaceToError(obtained)
if isNil(err) {
return a.failer.Fail(AnyError, err, nil, msgs...)
}
return true
}

// ErrorMatch tests if the obtained error as string matches a
// regular expression.
func (a *Asserts) ErrorMatch(obtained interface{}, regex string, msgs ...string) bool {
Expand Down Expand Up @@ -508,6 +539,9 @@ type errable interface {

// ifaceToError converts an interface{} into an error.
func ifaceToError(obtained interface{}) error {
if obtained == nil {
return nil
}
err, ok := obtained.(error)
if ok {
return err
Expand All @@ -519,7 +553,8 @@ func ifaceToError(obtained interface{}) error {
}
return able.Err()
}
return err
// No error and not errable, so return panic.
panic("invalid type for error assertion")
}

// lenable describes a type able to return its length
Expand Down
48 changes: 45 additions & 3 deletions asserts/asserts_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Tideland Go Audit - Asserts - Unit Tests
//
// Copyright (C) 2012-2020 Frank Mueller / Tideland / Oldenburg / Germany
// Copyright (C) 2012-2021 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
Expand Down Expand Up @@ -63,6 +63,32 @@ func TestAssertOK(t *testing.T) {
failingAssert.OK(func() error { return errC }, "OK error func should fail and be logged")
}

// TestAssertNotOK tests the NotOK() assertion.
func TestAssertNotOK(t *testing.T) {
successfulAssert := successfulAsserts(t)
failingAssert := failingAsserts(t)

var errA error
var errB withErr = withErr{errA}
var errC = errors.New("ouch")
var errD withErr = withErr{errC}

successfulAssert.NotOK(false, "NotOK false should not fail")
successfulAssert.NotOK(func() bool { return false }, "NotOK false func should not fail")
successfulAssert.NotOK(errC, "NotOK error should not fail")
successfulAssert.NotOK(errD, "NotOK nil Err() should not fail")
successfulAssert.NotOK(-1, "NotOK -1 should not fail")
successfulAssert.NotOK("ouch", "NotOK 'ouch' should not fail")
successfulAssert.NotOK(func() error { return errC }, "NotOK error func should not fail")
failingAssert.NotOK(true, "NotOK true should fail and be logged")
failingAssert.NotOK(func() bool { return true }, "NotOK true func should fail and be logged")
failingAssert.NotOK(errA, "NotOK nil error should fail and be logged")
failingAssert.NotOK(errB, "NotOK nil Err() should fail and be logged")
failingAssert.NotOK(0, "NotOK -1 should fail and be logged")
failingAssert.NotOK("", "NotOK '' should fail and be logged")
failingAssert.NotOK(func() error { return errA }, "NotOK nil error func should fail and be logged")
}

// TestAssertTrue tests the True() assertion.
func TestAssertTrue(t *testing.T) {
successfulAssert := successfulAsserts(t)
Expand Down Expand Up @@ -115,6 +141,22 @@ func TestAssertNoError(t *testing.T) {
failingAssert.NoError(errD, "should fail and be logged")
}

// TestAssertAnyError tests the AnyError() assertion.
func TestAssertAnyError(t *testing.T) {
successfulAssert := successfulAsserts(t)
failingAssert := failingAsserts(t)

var errA error
var errB withErr = withErr{errA}
var errC = errors.New("ouch")
var errD withErr = withErr{errC}

successfulAssert.AnyError(errC, "should not fail")
successfulAssert.AnyError(errD, "should not fail")
failingAssert.AnyError(errA, "should fail and be logged")
failingAssert.AnyError(errB, "should fail and be logged")
}

// TestAssertEqual tests the Equal() assertion.
func TestAssertEqual(t *testing.T) {
successfulAssert := successfulAsserts(t)
Expand Down Expand Up @@ -569,15 +611,15 @@ func TestValidationAssertion(t *testing.T) {
details := failures.Details()
location, fun := details[0].Location()
tt := details[0].Test()
if location != "asserts_test.go:556:0:" || fun != "TestValidationAssertion" {
if location != "asserts_test.go:598:0:" || fun != "TestValidationAssertion" {
t.Errorf("wrong location %q or function %q of first detail", location, fun)
}
if tt != asserts.True {
t.Errorf("wrong test type of first detail: %v", tt)
}
location, fun = details[1].Location()
tt = details[1].Test()
if location != "asserts_test.go:557:0:" || fun != "TestValidationAssertion" {
if location != "asserts_test.go:599:0:" || fun != "TestValidationAssertion" {
t.Errorf("wrong location %q or function %q of second detail", location, fun)
}
if tt != asserts.Equal {
Expand Down
2 changes: 1 addition & 1 deletion asserts/doc.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Tideland Go Audit - Asserts
//
// Copyright (C) 2012-2020 Frank Mueller / Tideland / Oldenburg / Germany
// Copyright (C) 2012-2021 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
Expand Down
8 changes: 6 additions & 2 deletions asserts/failer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Tideland Go Audit - Asserts
//
// Copyright (C) 2012-2020 Frank Mueller / Tideland / Oldenburg / Germany
// Copyright (C) 2012-2021 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
Expand Down Expand Up @@ -381,8 +381,12 @@ func (f *testingFailer) Fail(test Test, obtained, expected interface{}, msgs ...
// package. The *testing.T has to be passed as failable, the argument.
// shallFail controls if a failing assertion also lets fail the Go test.
func NewTesting(f Failable, mode FailMode) *Asserts {
p, ok := f.(Printer)
if !ok {
p = NewStandardPrinter()
}
return New(&testingFailer{
printer: NewStandardPrinter(),
printer: p,
failable: f,
offset: 4,
mode: mode,
Expand Down
5 changes: 4 additions & 1 deletion asserts/printer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Tideland Go Audit - Asserts
//
// Copyright (C) 2012-2020 Frank Mueller / Tideland / Oldenburg / Germany
// Copyright (C) 2012-2021 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
Expand Down Expand Up @@ -32,6 +32,7 @@ const (
Nil
NotNil
NoError
AnyError
Equal
Different
Contains
Expand Down Expand Up @@ -60,6 +61,7 @@ const (
Retry
Fail
OK
NotOK
)

// testNames maps the tests to their descriptive names.
Expand All @@ -70,6 +72,7 @@ var testNames = []string{
Nil: "nil",
NotNil: "not nil",
NoError: "no error",
AnyError: "any error",
Equal: "equal",
Different: "different",
Contains: "contains",
Expand Down
2 changes: 1 addition & 1 deletion asserts/tester.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Tideland Go Audit - Asserts
//
// Copyright (C) 2012-2020 Frank Mueller / Tideland / Oldenburg / Germany
// Copyright (C) 2012-2021 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
Expand Down
2 changes: 1 addition & 1 deletion generators/doc.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Tideland Go Audit - Generators
//
// Copyright (C) 2013-2020 Frank Mueller / Tideland / Oldenburg / Germany
// Copyright (C) 2013-2021 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
Expand Down
11 changes: 10 additions & 1 deletion generators/generators.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Tideland Go Audit - Generators
//
// Copyright (C) 2013-2020 Frank Mueller / Tideland / Oldenburg / Germany
// Copyright (C) 2013-2021 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
Expand Down Expand Up @@ -200,6 +200,15 @@ func (g *Generator) FlipCoin(percent int) bool {
return g.Percent() >= percent
}

// OneOf returns one of the passed empty interfaces (aka values).
func (g *Generator) OneOf(values ...interface{}) interface{} {
if len(values) == 0 {
return 0
}
i := g.Int(0, len(values)-1)
return values[i]
}

// OneByteOf returns one of the passed bytes.
func (g *Generator) OneByteOf(values ...byte) byte {
if len(values) == 0 {
Expand Down
6 changes: 5 additions & 1 deletion generators/generators_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Tideland Go Audit - Generators - Unit Tests
//
// Copyright (C) 2013-2020 Frank Mueller / Tideland / Oldenburg / Germany
// Copyright (C) 2013-2021 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the New BSD license.
Expand Down Expand Up @@ -141,8 +141,12 @@ func TestInts(t *testing.T) {
func TestOneOf(t *testing.T) {
assert := asserts.NewTesting(t, asserts.FailStop)
gen := generators.New(generators.FixedRand())
stuff := []interface{}{1, true, "three", 47.11, []byte{'A', 'B', 'C'}}

for i := 0; i < 10000; i++ {
m := gen.OneOf(stuff...)
assert.Contains(m, stuff)

b := gen.OneByteOf(1, 2, 3, 4, 5)
assert.True(b >= 1 && b <= 5)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module tideland.dev/go/audit

go 1.13
go 1.16

0 comments on commit 3d75fbd

Please sign in to comment.