github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/d/try_test.go (about) 1 // Copyright 2019 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // This file incorporates work covered by the following copyright and 16 // permission notice: 17 // 18 // Copyright 2016 Attic Labs, Inc. All rights reserved. 19 // Licensed under the Apache License, version 2.0: 20 // http://www.apache.org/licenses/LICENSE-2.0 21 22 package d 23 24 import ( 25 "errors" 26 "testing" 27 28 "github.com/stretchr/testify/assert" 29 ) 30 31 var ( 32 te = testError{"te"} 33 te2 = testError2{"te2"} 34 ) 35 36 type testError struct { 37 s string 38 } 39 40 func (e testError) Error() string { return e.s } 41 42 type testError2 struct { 43 s string 44 } 45 46 func (e testError2) Error() string { return e.s } 47 48 func TestUnwrap(t *testing.T) { 49 assert := assert.New(t) 50 51 err := errors.New("test") 52 we := wrappedError{"test msg", err} 53 assert.Equal(err, Unwrap(err)) 54 assert.Equal(err, Unwrap(we)) 55 } 56 57 func TestPanicIfTrue(t *testing.T) { 58 assert := assert.New(t) 59 60 assert.Panics(func() { 61 PanicIfTrue(true) 62 }) 63 64 assert.Panics(func() { 65 PanicIfTrue(true) 66 }) 67 68 assert.NotPanics(func() { 69 PanicIfTrue(false) 70 }) 71 } 72 73 func TestPanicIfFalse(t *testing.T) { 74 assert := assert.New(t) 75 76 assert.Panics(func() { 77 PanicIfFalse(false) 78 }) 79 80 assert.Panics(func() { 81 PanicIfFalse(false) 82 }) 83 84 assert.NotPanics(func() { 85 PanicIfFalse(true) 86 }) 87 } 88 89 func TestPanicIfNotType(t *testing.T) { 90 assert := assert.New(t) 91 92 te := testError{"te"} 93 te2 := testError2{"te2"} 94 95 assert.Panics(func() { 96 PanicIfNotType(te, te2) 97 }) 98 99 assert.Equal(te, PanicIfNotType(te, te)) 100 assert.Equal(te2, PanicIfNotType(te2, te, te2)) 101 } 102 103 func TestCauseInTypes(t *testing.T) { 104 assert := assert.New(t) 105 106 te := testError{"te"} 107 te2 := testError2{"te2"} 108 109 assert.True(causeInTypes(te, te)) 110 assert.True(causeInTypes(te, te2, te)) 111 assert.False(causeInTypes(te, te2)) 112 assert.False(causeInTypes(te)) 113 } 114 115 func TestWrap(t *testing.T) { 116 assert := assert.New(t) 117 118 te := testError{"te"} 119 we := Wrap(te) 120 assert.Equal(te, we.Cause()) 121 assert.IsType(wrappedError{}, we) 122 assert.Equal(we, Wrap(we)) 123 //fmt.Printf("st: %s, cause: %s\n", we.Error(), we.Cause()) 124 assert.Nil(Wrap(nil)) 125 }