github.com/sacloud/iaas-api-go@v1.12.0/testutil/assert.go (about) 1 // Copyright 2022-2023 The sacloud/iaas-api-go Authors 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 package testutil 16 17 import ( 18 "errors" 19 "fmt" 20 21 "github.com/stretchr/testify/assert" 22 ) 23 24 // AssertEqualWithExpected 項目ごとに除外設定のできる期待値との比較 25 func AssertEqualWithExpected(testExpect *CRUDTestExpect) func(TestT, *CRUDTestContext, interface{}) error { 26 return func(t TestT, testContext *CRUDTestContext, v interface{}) error { 27 expect, actual := testExpect.Prepare(v) 28 if !assert.Equal(t, expect, actual) { 29 return errors.New("assert.Equal is failed") 30 } 31 return nil 32 } 33 } 34 35 // AssertEqual 値の比較 36 func AssertEqual(t TestT, expected interface{}, actual interface{}, targetName string) error { 37 if !assert.Equal(t, expected, actual) { 38 return fmt.Errorf("assert.Equal is failed: %s", targetName) 39 } 40 return nil 41 } 42 43 // AssertLen lengthのチェック 44 func AssertLen(t TestT, object interface{}, length int, targetName string) error { 45 if !assert.Len(t, object, length) { 46 return fmt.Errorf("assert.Len is failed: %s", targetName) 47 } 48 return nil 49 } 50 51 // AssertNil nilチェック 52 func AssertNil(t TestT, object interface{}, targetName string) error { 53 if !assert.Nil(t, object) { 54 return fmt.Errorf("assert.Nil is failed: %s", targetName) 55 } 56 return nil 57 } 58 59 // AssertNotNil not nilチェック 60 func AssertNotNil(t TestT, object interface{}, targetName string) error { 61 if !assert.NotNil(t, object) { 62 return fmt.Errorf("assert.NotNil is failed: %s", targetName) 63 } 64 return nil 65 } 66 67 // AssertTrue trueチェック 68 func AssertTrue(t TestT, value bool, targetName string) error { 69 if !assert.True(t, value) { 70 return fmt.Errorf("assert.True is failed: %s", targetName) 71 } 72 return nil 73 } 74 75 // AssertFalse falseチェック 76 func AssertFalse(t TestT, value bool, targetName string) error { 77 if !assert.False(t, value) { 78 return fmt.Errorf("assert.False is failed: %s", targetName) 79 } 80 return nil 81 } 82 83 // AssertEmpty emptyチェック 84 func AssertEmpty(t TestT, object interface{}, targetName string) error { 85 if !assert.Empty(t, object) { 86 return fmt.Errorf("assert.Empty is failed: %s", targetName) 87 } 88 return nil 89 } 90 91 // AssertNotEmpty not emptyチェック 92 func AssertNotEmpty(t TestT, object interface{}, targetName string) error { 93 if !assert.NotEmpty(t, object) { 94 return fmt.Errorf("assert.NotEmpty is failed: %s", targetName) 95 } 96 return nil 97 } 98 99 // DoAsserts アサーションを複数適用、最初のエラーを返す 100 func DoAsserts(funcs ...func() error) error { 101 for _, f := range funcs { 102 if err := f(); err != nil { 103 return err 104 } 105 } 106 return nil 107 } 108 109 // AssertEqualFunc 値の比較 110 func AssertEqualFunc(t TestT, expected interface{}, actual interface{}, targetName string) func() error { 111 return func() error { 112 return AssertEqual(t, expected, actual, targetName) 113 } 114 } 115 116 // AssertLenFunc lengthのチェック 117 func AssertLenFunc(t TestT, object interface{}, length int, targetName string) func() error { 118 return func() error { 119 return AssertLen(t, object, length, targetName) 120 } 121 } 122 123 // AssertNilFunc nilチェック 124 func AssertNilFunc(t TestT, object interface{}, targetName string) func() error { 125 return func() error { 126 return AssertNil(t, object, targetName) 127 } 128 } 129 130 // AssertNotNilFunc not nilチェック 131 func AssertNotNilFunc(t TestT, object interface{}, targetName string) func() error { 132 return func() error { 133 return AssertNotNil(t, object, targetName) 134 } 135 } 136 137 // AssertTrueFunc trueチェック 138 func AssertTrueFunc(t TestT, value bool, targetName string) func() error { 139 return func() error { 140 return AssertTrue(t, value, targetName) 141 } 142 } 143 144 // AssertFalseFunc falseチェック 145 func AssertFalseFunc(t TestT, value bool, targetName string) func() error { 146 return func() error { 147 return AssertFalse(t, value, targetName) 148 } 149 } 150 151 // AssertEmptyFunc emptyチェック 152 func AssertEmptyFunc(t TestT, object interface{}, targetName string) func() error { 153 return func() error { 154 return AssertEmpty(t, object, targetName) 155 } 156 } 157 158 // AssertNotEmptyFunc not emptyチェック 159 func AssertNotEmptyFunc(t TestT, object interface{}, targetName string) func() error { 160 return func() error { 161 return AssertNotEmpty(t, object, targetName) 162 } 163 }