Skip to content

Commit

Permalink
add calls to assert pkg to cover almost all
Browse files Browse the repository at this point in the history
  • Loading branch information
lainio committed Oct 6, 2024
1 parent 5cc1710 commit ef35568
Showing 1 changed file with 111 additions and 30 deletions.
141 changes: 111 additions & 30 deletions assert/assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,64 +25,71 @@ func ExampleNotNil() {
sample := func(b *byte) (err error) {
defer err2.Handle(&err, "sample")

assert.NotNil(b)
assert.Nil(b) // OK
assert.NotNil(b) // Not OK
return err
}
var b *byte
err := sample(b)
fmt.Printf("%v", err)
// Output: sample: assert_test.go:28: ExampleNotNil.func1(): assertion failure: pointer should not be nil
// Output: sample: assert_test.go:29: ExampleNotNil.func1(): assertion failure: pointer should not be nil
}

func ExampleMNotNil() {
sample := func(b map[string]byte) (err error) {
defer err2.Handle(&err, "sample")

assert.MNotNil(b)
assert.MEmpty(b) // OK
assert.MNil(b) // OK
assert.MNotNil(b) // Not OK
return err
}
var b map[string]byte
err := sample(b)
fmt.Printf("%v", err)
// Output: sample: assert_test.go:41: ExampleMNotNil.func1(): assertion failure: map should not be nil
// Output: sample: assert_test.go:44: ExampleMNotNil.func1(): assertion failure: map should not be nil
}

func ExampleCNotNil() {
sample := func(c chan byte) (err error) {
defer err2.Handle(&err, "sample")

assert.CNotNil(c)
assert.CNil(c) // OK
assert.CNotNil(c) // Not OK
return err
}
var c chan byte
err := sample(c)
fmt.Printf("%v", err)
// Output: sample: assert_test.go:54: ExampleCNotNil.func1(): assertion failure: channel should not be nil
// Output: sample: assert_test.go:58: ExampleCNotNil.func1(): assertion failure: channel should not be nil
}

func ExampleSNotNil() {
sample := func(b []byte) (err error) {
defer err2.Handle(&err, "sample")

assert.SNotNil(b)
assert.SEmpty(b) // OK
assert.SNil(b) // OK
assert.SNotNil(b) // Not OK
return err
}
var b []byte
err := sample(b)
fmt.Printf("%v", err)
// Output: sample: assert_test.go:67: ExampleSNotNil.func1(): assertion failure: slice should not be nil
// Output: sample: assert_test.go:73: ExampleSNotNil.func1(): assertion failure: slice should not be nil
}

func ExampleEqual() {
sample := func(b []byte) (err error) {
defer err2.Handle(&err, "sample")

assert.Equal(len(b), 3)
assert.NotEqual(b[0], 3) // OK, b[0] != 3; (b[0] == 1)
assert.Equal(b[1], 1) // Not OK, b[1] == 2
return err
}
err := sample([]byte{1, 2})
fmt.Printf("%v", err)
// Output: sample: assert_test.go:80: ExampleEqual.func1(): assertion failure: equal: got '2', want '3'
// Output: sample: assert_test.go:87: ExampleEqual.func1(): assertion failure: equal: got '2', want '1'
}

func ExampleSLen() {
Expand All @@ -94,7 +101,7 @@ func ExampleSLen() {
}
err := sample([]byte{1, 2})
fmt.Printf("%v", err)
// Output: sample: assert_test.go:92: ExampleSLen.func1(): assertion failure: length: got '2', want '3'
// Output: sample: assert_test.go:99: ExampleSLen.func1(): assertion failure: length: got '2', want '3'
}

func ExampleSNotEmpty() {
Expand All @@ -106,7 +113,7 @@ func ExampleSNotEmpty() {
}
err := sample([]byte{})
fmt.Printf("%v", err)
// Output: sample: assert_test.go:104: ExampleSNotEmpty.func1(): assertion failure: slice should not be empty
// Output: sample: assert_test.go:111: ExampleSNotEmpty.func1(): assertion failure: slice should not be empty
}

func ExampleNotEmpty() {
Expand All @@ -119,7 +126,7 @@ func ExampleNotEmpty() {
}
err := sample("")
fmt.Printf("%v", err)
// Output: sample: assert_test.go:117: ExampleNotEmpty.func1(): assertion failure: string should not be empty
// Output: sample: assert_test.go:124: ExampleNotEmpty.func1(): assertion failure: string should not be empty
}

func ExampleMKeyExists() {
Expand All @@ -136,7 +143,7 @@ func ExampleMKeyExists() {
}
err := sample("2")
fmt.Printf("%v", err)
// Output: sample: assert_test.go:134: ExampleMKeyExists.func1(): assertion failure: key '2' doesn't exist
// Output: sample: assert_test.go:141: ExampleMKeyExists.func1(): assertion failure: key '2' doesn't exist
}

func ExampleZero() {
Expand All @@ -149,33 +156,34 @@ func ExampleZero() {
var b int8 = 1 // we want sample to assert the violation.
err := sample(b)
fmt.Printf("%v", err)
// Output: sample: assert_test.go:146: ExampleZero.func1(): assertion failure: got '1', want (== '0')
// Output: sample: assert_test.go:153: ExampleZero.func1(): assertion failure: got '1', want (== '0')
}

func ExampleSLonger() {
sample := func(b []byte) (err error) {
defer err2.Handle(&err, "sample")

assert.SLonger(b, 0) // ok
assert.SLonger(b, 1) // not ok
assert.SLonger(b, 0) // OK
assert.SLonger(b, 1) // Not OK
return err
}
err := sample([]byte{01}) // len = 1
fmt.Printf("%v", err)
// Output: sample: assert_test.go:160: ExampleSLonger.func1(): assertion failure: got '1', should be longer than '1'
// Output: sample: assert_test.go:167: ExampleSLonger.func1(): assertion failure: got '1', should be longer than '1'
}

func ExampleMShorter() {
sample := func(b map[byte]byte) (err error) {
defer err2.Handle(&err, "sample")

assert.MShorter(b, 1) // ok
assert.MShorter(b, 0) // not ok
assert.MNotEmpty(b) // OK
assert.MShorter(b, 1) // OK
assert.MShorter(b, 0) // Not OK
return err
}
err := sample(map[byte]byte{01: 01}) // len = 1
fmt.Printf("%v", err)
// Output: sample: assert_test.go:172: ExampleMShorter.func1(): assertion failure: got '1', should be shorter than '1'
// Output: sample: assert_test.go:180: ExampleMShorter.func1(): assertion failure: got '1', should be shorter than '1'
}

func ExampleSShorter() {
Expand All @@ -188,7 +196,7 @@ func ExampleSShorter() {
}
err := sample([]byte{01}) // len = 1
fmt.Printf("%v", err)
// Output: sample: assert_test.go:186: ExampleSShorter.func1(): assertion failure: got '1', should be shorter than '0': optional message (test_str)
// Output: sample: assert_test.go:194: ExampleSShorter.func1(): assertion failure: got '1', should be shorter than '0': optional message (test_str)
}

func ExampleLess() {
Expand All @@ -203,7 +211,7 @@ func ExampleLess() {
var b int8 = 1
err := sample(b)
fmt.Printf("%v", err)
// Output: sample: assert_test.go:200: ExampleLess.func1(): assertion failure: got '1', want >= '1'
// Output: sample: assert_test.go:208: ExampleLess.func1(): assertion failure: got '1', want >= '1'
}

func ExampleGreater() {
Expand All @@ -218,7 +226,7 @@ func ExampleGreater() {
var b int8 = 2
err := sample(b)
fmt.Printf("%v", err)
// Output: sample: assert_test.go:215: ExampleGreater.func1(): assertion failure: got '2', want <= '2'
// Output: sample: assert_test.go:223: ExampleGreater.func1(): assertion failure: got '2', want <= '2'
}

func ExampleNotZero() {
Expand All @@ -231,34 +239,38 @@ func ExampleNotZero() {
var b int8
err := sample(b)
fmt.Printf("%v", err)
// Output: sample: assert_test.go:228: ExampleNotZero.func1(): assertion failure: got '0', want (!= 0)
// Output: sample: assert_test.go:236: ExampleNotZero.func1(): assertion failure: got '0', want (!= 0)
}

func ExampleMLen() {
sample := func(b map[int]byte) (err error) {
defer err2.Handle(&err, "sample")

assert.MLen(b, 3)
assert.MLonger(b, 1) // OK
assert.MShorter(b, 3) // OK
assert.MLen(b, 3) // Not OK
return err
}
err := sample(map[int]byte{1: 1, 2: 2})
fmt.Printf("%v", err)
// Output: sample: assert_test.go:241: ExampleMLen.func1(): assertion failure: length: got '2', want '3'
// Output: sample: assert_test.go:251: ExampleMLen.func1(): assertion failure: length: got '2', want '3'
}

func ExampleCLen() {
sample := func(b chan int) (err error) {
defer err2.Handle(&err, "sample")

assert.CLen(b, 3)
assert.CLonger(b, 1) // OK
assert.CShorter(b, 3) // OK
assert.CLen(b, 3) // Not OK
return err
}
d := make(chan int, 2)
d <- int(1)
d <- int(1)
err := sample(d)
fmt.Printf("%v", err)
// Output: sample: assert_test.go:253: ExampleCLen.func1(): assertion failure: length: got '2', want '3'
// Output: sample: assert_test.go:265: ExampleCLen.func1(): assertion failure: length: got '2', want '3'
}

func ExampleThatNot() {
Expand All @@ -269,14 +281,83 @@ func ExampleThatNot() {
return err
}

// set asserter for this thread/goroutine only, we want plain errors
// Set a specific asserter for this goroutine only, we want plain errors
defer assert.SetAsserter(assert.Plain)()

err := sample()
fmt.Printf("%v", err)
// Output: testing: run example: overrides if Plain asserter
}

func ExampleINotNil() {
sample := func(b error) (err error) {
defer err2.Handle(&err, "sample")

assert.INotNil(b) // OK
assert.INil(b) // Not OK
return err
}
var b = fmt.Errorf("test")
err := sample(b)
fmt.Printf("%v", err)
// Output: sample: assert_test.go:297: ExampleINotNil.func1(): assertion failure: interface should be nil
}

func ExampleLen() {
sample := func(b string) (err error) {
defer err2.Handle(&err, "sample")

assert.Shorter(b, 3) // OK
assert.Longer(b, 1) // OK
assert.Len(b, 3) // Not OK
return err
}
err := sample("12")
fmt.Printf("%v", err)
// Output: sample: assert_test.go:312: ExampleLen.func1(): assertion failure: length: got '2', want '3'
}

func ExampleDeepEqual() {
sample := func(b []byte) (err error) {
defer err2.Handle(&err, "sample")

assert.NoError(err)
assert.NotDeepEqual(len(b), 3) // OK, correct size is 2
assert.DeepEqual(len(b), 3) // Not OK, size is still 2
return err
}
err := sample([]byte{1, 2})
fmt.Printf("%v", err)
// Output: sample: assert_test.go:326: ExampleDeepEqual.func1(): assertion failure: got '2', want '3'
}

func ExampleError() {
sample := func(b error) (err error) {
defer err2.Handle(&err, "sample")

assert.Error(b) // OK
assert.NoError(b) // Not OK
return err
}
var b = fmt.Errorf("test")
err := sample(b)
fmt.Printf("%v", err)
// Output: sample: assert_test.go:339: ExampleError.func1(): assertion failure: test
}

func ExampleNotImplemented() {
sample := func(_ error) (err error) {
defer err2.Handle(&err, "sample")

assert.NotImplemented() // Not OK
return err
}
var b = fmt.Errorf("test")
err := sample(b)
fmt.Printf("%v", err)
// Output: sample: assert_test.go:352: ExampleNotImplemented.func1(): assertion failure: not implemented
}

func BenchmarkMKeyExists(b *testing.B) {
bs := map[int]int{0: 0, 1: 1}
for n := 0; n < b.N; n++ {
Expand Down

0 comments on commit ef35568

Please sign in to comment.