Skip to content

Commit 3d75fbd

Browse files
authored
Release v0.5.0 (#15)
* 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
1 parent eab38ad commit 3d75fbd

File tree

12 files changed

+121
-14
lines changed

12 files changed

+121
-14
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*.so
66
*.dylib
77

8+
# Development environments
9+
.theia
10+
811
# Test binary, built with `go test -c`
912
*.test
1013

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## v0.5.0
4+
5+
* (A) Asserts now contains NotOK() and AnyError()
6+
* (C) Asserts created with NewTesting() now uses the Failable as Printer if
7+
it implements the according interface
8+
* (A) Generator now contains OneOf()
9+
310
## v0.4.0
411

512
* (A) Asserts now contains NotPanics() and PanicsWith()

asserts/asserts.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Tideland Go Audit - Asserts
22
//
3-
// Copyright (C) 2012-2020 Frank Mueller / Tideland / Oldenburg / Germany
3+
// Copyright (C) 2012-2021 Frank Mueller / Tideland / Oldenburg / Germany
44
//
55
// All rights reserved. Use of this source code is governed
66
// by the new BSD license.
@@ -101,6 +101,28 @@ func (a *Asserts) OK(obtained interface{}, msgs ...string) bool {
101101
}
102102
}
103103

104+
// NotOK is a convenient metatest depending in the obtained tyoe. In case
105+
// of a bool it has to be false, a func() bool has to return false, an int
106+
// has to be not 0, a string has to be not empty, and a func() error has to
107+
// return an error. Any else value has to be not nil or in case of an ErrorProne
108+
// its Err() has not to return nil.
109+
func (a *Asserts) NotOK(obtained interface{}, msgs ...string) bool {
110+
switch o := obtained.(type) {
111+
case bool:
112+
return a.False(o, msgs...)
113+
case func() bool:
114+
return a.False(o(), msgs...)
115+
case int:
116+
return a.Different(o, 0, msgs...)
117+
case string:
118+
return a.Different(o, "", msgs...)
119+
case func() error:
120+
return a.AnyError(o(), msgs...)
121+
default:
122+
return a.AnyError(obtained, msgs...)
123+
}
124+
}
125+
104126
// True tests if obtained is true.
105127
func (a *Asserts) True(obtained bool, msgs ...string) bool {
106128
if !isTrue(obtained) {
@@ -158,6 +180,15 @@ func (a *Asserts) NoError(obtained interface{}, msgs ...string) bool {
158180
return true
159181
}
160182

183+
// AnyError tests if the obtained error or ErrorProne.Err() is not nil.
184+
func (a *Asserts) AnyError(obtained interface{}, msgs ...string) bool {
185+
err := ifaceToError(obtained)
186+
if isNil(err) {
187+
return a.failer.Fail(AnyError, err, nil, msgs...)
188+
}
189+
return true
190+
}
191+
161192
// ErrorMatch tests if the obtained error as string matches a
162193
// regular expression.
163194
func (a *Asserts) ErrorMatch(obtained interface{}, regex string, msgs ...string) bool {
@@ -508,6 +539,9 @@ type errable interface {
508539

509540
// ifaceToError converts an interface{} into an error.
510541
func ifaceToError(obtained interface{}) error {
542+
if obtained == nil {
543+
return nil
544+
}
511545
err, ok := obtained.(error)
512546
if ok {
513547
return err
@@ -519,7 +553,8 @@ func ifaceToError(obtained interface{}) error {
519553
}
520554
return able.Err()
521555
}
522-
return err
556+
// No error and not errable, so return panic.
557+
panic("invalid type for error assertion")
523558
}
524559

525560
// lenable describes a type able to return its length

asserts/asserts_test.go

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Tideland Go Audit - Asserts - Unit Tests
22
//
3-
// Copyright (C) 2012-2020 Frank Mueller / Tideland / Oldenburg / Germany
3+
// Copyright (C) 2012-2021 Frank Mueller / Tideland / Oldenburg / Germany
44
//
55
// All rights reserved. Use of this source code is governed
66
// by the new BSD license.
@@ -63,6 +63,32 @@ func TestAssertOK(t *testing.T) {
6363
failingAssert.OK(func() error { return errC }, "OK error func should fail and be logged")
6464
}
6565

66+
// TestAssertNotOK tests the NotOK() assertion.
67+
func TestAssertNotOK(t *testing.T) {
68+
successfulAssert := successfulAsserts(t)
69+
failingAssert := failingAsserts(t)
70+
71+
var errA error
72+
var errB withErr = withErr{errA}
73+
var errC = errors.New("ouch")
74+
var errD withErr = withErr{errC}
75+
76+
successfulAssert.NotOK(false, "NotOK false should not fail")
77+
successfulAssert.NotOK(func() bool { return false }, "NotOK false func should not fail")
78+
successfulAssert.NotOK(errC, "NotOK error should not fail")
79+
successfulAssert.NotOK(errD, "NotOK nil Err() should not fail")
80+
successfulAssert.NotOK(-1, "NotOK -1 should not fail")
81+
successfulAssert.NotOK("ouch", "NotOK 'ouch' should not fail")
82+
successfulAssert.NotOK(func() error { return errC }, "NotOK error func should not fail")
83+
failingAssert.NotOK(true, "NotOK true should fail and be logged")
84+
failingAssert.NotOK(func() bool { return true }, "NotOK true func should fail and be logged")
85+
failingAssert.NotOK(errA, "NotOK nil error should fail and be logged")
86+
failingAssert.NotOK(errB, "NotOK nil Err() should fail and be logged")
87+
failingAssert.NotOK(0, "NotOK -1 should fail and be logged")
88+
failingAssert.NotOK("", "NotOK '' should fail and be logged")
89+
failingAssert.NotOK(func() error { return errA }, "NotOK nil error func should fail and be logged")
90+
}
91+
6692
// TestAssertTrue tests the True() assertion.
6793
func TestAssertTrue(t *testing.T) {
6894
successfulAssert := successfulAsserts(t)
@@ -115,6 +141,22 @@ func TestAssertNoError(t *testing.T) {
115141
failingAssert.NoError(errD, "should fail and be logged")
116142
}
117143

144+
// TestAssertAnyError tests the AnyError() assertion.
145+
func TestAssertAnyError(t *testing.T) {
146+
successfulAssert := successfulAsserts(t)
147+
failingAssert := failingAsserts(t)
148+
149+
var errA error
150+
var errB withErr = withErr{errA}
151+
var errC = errors.New("ouch")
152+
var errD withErr = withErr{errC}
153+
154+
successfulAssert.AnyError(errC, "should not fail")
155+
successfulAssert.AnyError(errD, "should not fail")
156+
failingAssert.AnyError(errA, "should fail and be logged")
157+
failingAssert.AnyError(errB, "should fail and be logged")
158+
}
159+
118160
// TestAssertEqual tests the Equal() assertion.
119161
func TestAssertEqual(t *testing.T) {
120162
successfulAssert := successfulAsserts(t)
@@ -569,15 +611,15 @@ func TestValidationAssertion(t *testing.T) {
569611
details := failures.Details()
570612
location, fun := details[0].Location()
571613
tt := details[0].Test()
572-
if location != "asserts_test.go:556:0:" || fun != "TestValidationAssertion" {
614+
if location != "asserts_test.go:598:0:" || fun != "TestValidationAssertion" {
573615
t.Errorf("wrong location %q or function %q of first detail", location, fun)
574616
}
575617
if tt != asserts.True {
576618
t.Errorf("wrong test type of first detail: %v", tt)
577619
}
578620
location, fun = details[1].Location()
579621
tt = details[1].Test()
580-
if location != "asserts_test.go:557:0:" || fun != "TestValidationAssertion" {
622+
if location != "asserts_test.go:599:0:" || fun != "TestValidationAssertion" {
581623
t.Errorf("wrong location %q or function %q of second detail", location, fun)
582624
}
583625
if tt != asserts.Equal {

asserts/doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Tideland Go Audit - Asserts
22
//
3-
// Copyright (C) 2012-2020 Frank Mueller / Tideland / Oldenburg / Germany
3+
// Copyright (C) 2012-2021 Frank Mueller / Tideland / Oldenburg / Germany
44
//
55
// All rights reserved. Use of this source code is governed
66
// by the new BSD license.

asserts/failer.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Tideland Go Audit - Asserts
22
//
3-
// Copyright (C) 2012-2020 Frank Mueller / Tideland / Oldenburg / Germany
3+
// Copyright (C) 2012-2021 Frank Mueller / Tideland / Oldenburg / Germany
44
//
55
// All rights reserved. Use of this source code is governed
66
// by the new BSD license.
@@ -381,8 +381,12 @@ func (f *testingFailer) Fail(test Test, obtained, expected interface{}, msgs ...
381381
// package. The *testing.T has to be passed as failable, the argument.
382382
// shallFail controls if a failing assertion also lets fail the Go test.
383383
func NewTesting(f Failable, mode FailMode) *Asserts {
384+
p, ok := f.(Printer)
385+
if !ok {
386+
p = NewStandardPrinter()
387+
}
384388
return New(&testingFailer{
385-
printer: NewStandardPrinter(),
389+
printer: p,
386390
failable: f,
387391
offset: 4,
388392
mode: mode,

asserts/printer.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Tideland Go Audit - Asserts
22
//
3-
// Copyright (C) 2012-2020 Frank Mueller / Tideland / Oldenburg / Germany
3+
// Copyright (C) 2012-2021 Frank Mueller / Tideland / Oldenburg / Germany
44
//
55
// All rights reserved. Use of this source code is governed
66
// by the new BSD license.
@@ -32,6 +32,7 @@ const (
3232
Nil
3333
NotNil
3434
NoError
35+
AnyError
3536
Equal
3637
Different
3738
Contains
@@ -60,6 +61,7 @@ const (
6061
Retry
6162
Fail
6263
OK
64+
NotOK
6365
)
6466

6567
// testNames maps the tests to their descriptive names.
@@ -70,6 +72,7 @@ var testNames = []string{
7072
Nil: "nil",
7173
NotNil: "not nil",
7274
NoError: "no error",
75+
AnyError: "any error",
7376
Equal: "equal",
7477
Different: "different",
7578
Contains: "contains",

asserts/tester.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Tideland Go Audit - Asserts
22
//
3-
// Copyright (C) 2012-2020 Frank Mueller / Tideland / Oldenburg / Germany
3+
// Copyright (C) 2012-2021 Frank Mueller / Tideland / Oldenburg / Germany
44
//
55
// All rights reserved. Use of this source code is governed
66
// by the new BSD license.

generators/doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Tideland Go Audit - Generators
22
//
3-
// Copyright (C) 2013-2020 Frank Mueller / Tideland / Oldenburg / Germany
3+
// Copyright (C) 2013-2021 Frank Mueller / Tideland / Oldenburg / Germany
44
//
55
// All rights reserved. Use of this source code is governed
66
// by the new BSD license.

generators/generators.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Tideland Go Audit - Generators
22
//
3-
// Copyright (C) 2013-2020 Frank Mueller / Tideland / Oldenburg / Germany
3+
// Copyright (C) 2013-2021 Frank Mueller / Tideland / Oldenburg / Germany
44
//
55
// All rights reserved. Use of this source code is governed
66
// by the new BSD license.
@@ -200,6 +200,15 @@ func (g *Generator) FlipCoin(percent int) bool {
200200
return g.Percent() >= percent
201201
}
202202

203+
// OneOf returns one of the passed empty interfaces (aka values).
204+
func (g *Generator) OneOf(values ...interface{}) interface{} {
205+
if len(values) == 0 {
206+
return 0
207+
}
208+
i := g.Int(0, len(values)-1)
209+
return values[i]
210+
}
211+
203212
// OneByteOf returns one of the passed bytes.
204213
func (g *Generator) OneByteOf(values ...byte) byte {
205214
if len(values) == 0 {

generators/generators_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Tideland Go Audit - Generators - Unit Tests
22
//
3-
// Copyright (C) 2013-2020 Frank Mueller / Tideland / Oldenburg / Germany
3+
// Copyright (C) 2013-2021 Frank Mueller / Tideland / Oldenburg / Germany
44
//
55
// All rights reserved. Use of this source code is governed
66
// by the New BSD license.
@@ -141,8 +141,12 @@ func TestInts(t *testing.T) {
141141
func TestOneOf(t *testing.T) {
142142
assert := asserts.NewTesting(t, asserts.FailStop)
143143
gen := generators.New(generators.FixedRand())
144+
stuff := []interface{}{1, true, "three", 47.11, []byte{'A', 'B', 'C'}}
144145

145146
for i := 0; i < 10000; i++ {
147+
m := gen.OneOf(stuff...)
148+
assert.Contains(m, stuff)
149+
146150
b := gen.OneByteOf(1, 2, 3, 4, 5)
147151
assert.True(b >= 1 && b <= 5)
148152

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module tideland.dev/go/audit
22

3-
go 1.13
3+
go 1.16

0 commit comments

Comments
 (0)