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