github.com/wangyougui/gf/v2@v2.6.5/test/gtest/gtest_z_unit_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package gtest_test 8 9 import ( 10 "errors" 11 "path/filepath" 12 "strconv" 13 "testing" 14 15 "github.com/wangyougui/gf/v2/test/gtest" 16 ) 17 18 var ( 19 map1 = map[string]string{"k1": "v1"} 20 map1Expect = map[string]string{"k1": "v1"} 21 map2 = map[string]string{"k2": "v2"} 22 mapLong1 = map[string]string{"k1": "v1", "k2": "v2"} 23 mapLong1Expect = map[string]string{"k2": "v2", "k1": "v1"} 24 ) 25 26 func TestC(t *testing.T) { 27 gtest.C(t, func(t *gtest.T) { 28 t.Assert(1, 1) 29 t.AssertNE(1, 0) 30 t.AssertEQ(float32(123.456), float32(123.456)) 31 t.AssertEQ(float32(123.456), float32(123.456)) 32 t.Assert(map[string]string{"1": "1"}, map[string]string{"1": "1"}) 33 }) 34 35 gtest.C(t, func(t *gtest.T) { 36 defer func() { 37 if err := recover(); err != nil { 38 t.Assert(err, "[ASSERT] EXPECT 1 == 0") 39 } 40 }() 41 t.Assert(1, 0) 42 }) 43 } 44 45 func TestCase(t *testing.T) { 46 gtest.C(t, func(t *gtest.T) { 47 t.Assert(1, 1) 48 t.AssertNE(1, 0) 49 t.AssertEQ(float32(123.456), float32(123.456)) 50 t.AssertEQ(float32(123.456), float32(123.456)) 51 }) 52 } 53 54 func TestAssert(t *testing.T) { 55 56 gtest.C(t, func(t *gtest.T) { 57 var ( 58 nilChan chan struct{} 59 ) 60 t.Assert(1, 1) 61 t.Assert(nilChan, nil) 62 t.Assert(map1, map1Expect) 63 }) 64 65 gtest.C(t, func(t *gtest.T) { 66 defer func() { 67 if err := recover(); err != nil { 68 t.Assert(err, `[ASSERT] EXPECT VALUE map["k2"]: == map["k2"]:v2 69 GIVEN : map[k1:v1] 70 EXPECT: map[k2:v2]`) 71 } 72 }() 73 t.Assert(map1, map2) 74 }) 75 76 gtest.C(t, func(t *gtest.T) { 77 defer func() { 78 if err := recover(); err != nil { 79 t.Assert(err, `[ASSERT] EXPECT MAP LENGTH 2 == 1`) 80 } 81 }() 82 t.Assert(mapLong1, map2) 83 }) 84 85 gtest.C(t, func(t *gtest.T) { 86 defer func() { 87 if err := recover(); err != nil { 88 t.Assert(err, `[ASSERT] EXPECT VALUE TO BE A MAP, BUT GIVEN "int"`) 89 } 90 }() 91 t.Assert(0, map1) 92 }) 93 } 94 95 func TestAssertEQ(t *testing.T) { 96 97 gtest.C(t, func(t *gtest.T) { 98 var ( 99 nilChan chan struct{} 100 ) 101 t.AssertEQ(nilChan, nil) 102 t.AssertEQ("0", "0") 103 t.AssertEQ(float32(123.456), float32(123.456)) 104 t.AssertEQ(mapLong1, mapLong1Expect) 105 }) 106 107 gtest.C(t, func(t *gtest.T) { 108 defer func() { 109 if err := recover(); err != nil { 110 t.Assert(err, "[ASSERT] EXPECT 1 == 0") 111 } 112 }() 113 t.AssertEQ(1, 0) 114 }) 115 116 gtest.C(t, func(t *gtest.T) { 117 defer func() { 118 if err := recover(); err != nil { 119 t.Assert(err, "[ASSERT] EXPECT TYPE 1[int] == 1[string]") 120 } 121 }() 122 t.AssertEQ(1, "1") 123 }) 124 125 gtest.C(t, func(t *gtest.T) { 126 defer func() { 127 if err := recover(); err != nil { 128 t.Assert(err, `[ASSERT] EXPECT VALUE map["k2"]: == map["k2"]:v2 129 GIVEN : map[k1:v1] 130 EXPECT: map[k2:v2]`) 131 } 132 }() 133 t.AssertEQ(map1, map2) 134 }) 135 136 gtest.C(t, func(t *gtest.T) { 137 defer func() { 138 if err := recover(); err != nil { 139 t.Assert(err, `[ASSERT] EXPECT MAP LENGTH 2 == 1`) 140 } 141 }() 142 t.AssertEQ(mapLong1, map2) 143 }) 144 145 gtest.C(t, func(t *gtest.T) { 146 defer func() { 147 if err := recover(); err != nil { 148 t.Assert(err, `[ASSERT] EXPECT VALUE TO BE A MAP, BUT GIVEN "int"`) 149 } 150 }() 151 t.AssertEQ(0, map1) 152 }) 153 } 154 155 func TestAssertNE(t *testing.T) { 156 gtest.C(t, func(t *gtest.T) { 157 var ( 158 c = make(chan struct{}, 1) 159 ) 160 t.AssertNE(nil, c) 161 t.AssertNE("0", "1") 162 t.AssertNE(float32(123.456), float32(123.4567)) 163 t.AssertNE(map1, map2) 164 }) 165 166 gtest.C(t, func(t *gtest.T) { 167 defer func() { 168 if err := recover(); err != nil { 169 t.Assert(err, "[ASSERT] EXPECT 1 != 1") 170 } 171 }() 172 t.AssertNE(1, 1) 173 }) 174 175 gtest.C(t, func(t *gtest.T) { 176 defer func() { 177 if err := recover(); err != nil { 178 t.Assert(err, `[ASSERT] EXPECT map[k1:v1] != map[k1:v1]`) 179 } 180 }() 181 t.AssertNE(map1, map1Expect) 182 }) 183 } 184 185 func TestAssertNQ(t *testing.T) { 186 gtest.C(t, func(t *gtest.T) { 187 t.AssertNQ(1, "0") 188 t.AssertNQ(float32(123.456), float64(123.4567)) 189 }) 190 191 gtest.C(t, func(t *gtest.T) { 192 defer func() { 193 if err := recover(); err != nil { 194 t.Assert(err, "[ASSERT] EXPECT 1 != 1") 195 } 196 }() 197 t.AssertNQ(1, "1") 198 }) 199 200 gtest.C(t, func(t *gtest.T) { 201 defer func() { 202 if err := recover(); err != nil { 203 t.Assert(err, "[ASSERT] EXPECT TYPE 1[int] != 1[int]") 204 } 205 }() 206 t.AssertNQ(1, 1) 207 }) 208 } 209 210 func TestAssertGT(t *testing.T) { 211 gtest.C(t, func(t *gtest.T) { 212 t.AssertGT("b", "a") 213 t.AssertGT(1, -1) 214 t.AssertGT(uint(1), uint(0)) 215 t.AssertGT(float32(123.45678), float32(123.4567)) 216 }) 217 218 gtest.C(t, func(t *gtest.T) { 219 defer func() { 220 if err := recover(); err != nil { 221 t.Assert(err, "[ASSERT] EXPECT -1 > 1") 222 } 223 }() 224 t.AssertGT(-1, 1) 225 }) 226 } 227 228 func TestAssertGE(t *testing.T) { 229 gtest.C(t, func(t *gtest.T) { 230 t.AssertGE("b", "a") 231 t.AssertGE("a", "a") 232 t.AssertGE(1, -1) 233 t.AssertGE(1, 1) 234 t.AssertGE(uint(1), uint(0)) 235 t.AssertGE(uint(0), uint(0)) 236 t.AssertGE(float32(123.45678), float32(123.4567)) 237 t.AssertGE(float32(123.456), float32(123.456)) 238 }) 239 240 gtest.C(t, func(t *gtest.T) { 241 defer func() { 242 if err := recover(); err != nil { 243 t.Assert(err, "[ASSERT] EXPECT -1(int) >= 1(int)") 244 } 245 }() 246 t.AssertGE(-1, 1) 247 }) 248 } 249 250 func TestAssertLT(t *testing.T) { 251 gtest.C(t, func(t *gtest.T) { 252 t.AssertLT("a", "b") 253 t.AssertLT(-1, 1) 254 t.AssertLT(uint(0), uint(1)) 255 t.AssertLT(float32(123.456), float32(123.4567)) 256 }) 257 258 gtest.C(t, func(t *gtest.T) { 259 defer func() { 260 if err := recover(); err != nil { 261 t.Assert(err, "[ASSERT] EXPECT 1 < -1") 262 } 263 }() 264 t.AssertLT(1, -1) 265 }) 266 } 267 268 func TestAssertLE(t *testing.T) { 269 gtest.C(t, func(t *gtest.T) { 270 t.AssertLE("a", "b") 271 t.AssertLE("a", "a") 272 t.AssertLE(-1, 1) 273 t.AssertLE(1, 1) 274 t.AssertLE(uint(0), uint(1)) 275 t.AssertLE(uint(0), uint(0)) 276 t.AssertLE(float32(123.456), float32(123.4567)) 277 t.AssertLE(float32(123.456), float32(123.456)) 278 }) 279 280 gtest.C(t, func(t *gtest.T) { 281 defer func() { 282 if err := recover(); err != nil { 283 t.Assert(err, "[ASSERT] EXPECT 1 <= -1") 284 } 285 }() 286 t.AssertLE(1, -1) 287 }) 288 } 289 290 func TestAssertIN(t *testing.T) { 291 gtest.C(t, func(t *gtest.T) { 292 t.AssertIN("a", []string{"a", "b", "c"}) 293 t.AssertIN(1, []int{1, 2, 3}) 294 }) 295 296 gtest.C(t, func(t *gtest.T) { 297 defer func() { 298 if err := recover(); err != nil { 299 t.Assert(err, "[ASSERT] INVALID EXPECT VALUE TYPE: int") 300 } 301 }() 302 t.AssertIN(0, 0) 303 }) 304 305 gtest.C(t, func(t *gtest.T) { 306 defer func() { 307 if err := recover(); err != nil { 308 t.Assert(err, "[ASSERT] EXPECT 4 IN [1 2 3]") 309 } 310 }() 311 // t.AssertIN(0, []int{0, 1, 2, 3}) 312 // t.AssertIN(0, []int{ 1, 2, 3}) 313 t.AssertIN(4, []int{1, 2, 3}) 314 }) 315 } 316 317 func TestAssertNI(t *testing.T) { 318 gtest.C(t, func(t *gtest.T) { 319 t.AssertNI("d", []string{"a", "b", "c"}) 320 t.AssertNI(4, []int{1, 2, 3}) 321 }) 322 323 gtest.C(t, func(t *gtest.T) { 324 defer func() { 325 if err := recover(); err != nil { 326 t.Assert(err, "[ASSERT] INVALID EXPECT VALUE TYPE: int") 327 } 328 }() 329 t.AssertNI(0, 0) 330 }) 331 332 gtest.C(t, func(t *gtest.T) { 333 defer func() { 334 if err := recover(); err != nil { 335 t.Assert(err, "[ASSERT] EXPECT 1 NOT IN [1 2 3]") 336 } 337 }() 338 t.AssertNI(1, []int{1, 2, 3}) 339 }) 340 } 341 342 func TestAssertNil(t *testing.T) { 343 gtest.C(t, func(t *gtest.T) { 344 var ( 345 nilChan chan struct{} 346 ) 347 t.AssertNil(nilChan) 348 _, err := strconv.ParseInt("123", 10, 64) 349 t.AssertNil(err) 350 }) 351 352 gtest.C(t, func(t *gtest.T) { 353 defer func() { 354 if err := recover(); err != nil { 355 t.Assert(err, "error") 356 } 357 }() 358 t.AssertNil(errors.New("error")) 359 }) 360 361 gtest.C(t, func(t *gtest.T) { 362 defer func() { 363 if err := recover(); err != nil { 364 t.AssertNE(err, nil) 365 } 366 }() 367 t.AssertNil(1) 368 }) 369 } 370 371 func TestAssertError(t *testing.T) { 372 gtest.C(t, func(t *gtest.T) { 373 defer func() { 374 if err := recover(); err != nil { 375 t.Assert(err, "[ERROR] this is an error") 376 } 377 }() 378 t.Error("this is an error") 379 }) 380 } 381 382 func TestDataPath(t *testing.T) { 383 gtest.C(t, func(t *gtest.T) { 384 t.Assert(filepath.ToSlash(gtest.DataPath("testdata.txt")), `./testdata/testdata.txt`) 385 }) 386 } 387 388 func TestDataContent(t *testing.T) { 389 gtest.C(t, func(t *gtest.T) { 390 t.Assert(gtest.DataContent("testdata.txt"), `hello`) 391 t.Assert(gtest.DataContent(""), "") 392 }) 393 }