github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/validate/test/isBlank_test.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/isyscore/isc-gobase/validate"
     5  	"testing"
     6  )
     7  
     8  type IsBlankEntity1 struct {
     9  	Name string `match:"isBlank=false"`
    10  	Age  int
    11  }
    12  
    13  // 默认为true
    14  type IsBlankEntity2 struct {
    15  	Name string `match:"isBlank"`
    16  	Age  int
    17  }
    18  
    19  type IsBlankEntity3 struct {
    20  	Name string `match:"isBlank=true"`
    21  	Age  int
    22  }
    23  
    24  // 测试基本类型
    25  func TestIsBlank1(t *testing.T) {
    26  	var value IsBlankEntity1
    27  	var result bool
    28  	var err string
    29  
    30  	//测试 正常情况
    31  	value = IsBlankEntity1{Name: "zhou"}
    32  	result, err = validate.Check(value, "name")
    33  	TrueErr(t, result, err)
    34  
    35  	// 测试 正常情况
    36  	value = IsBlankEntity1{Age: 13}
    37  	result, err = validate.Check(value, "name")
    38  	Equal(t, err, "属性 Name 的值为空字符", result, false)
    39  }
    40  
    41  // 测试基本类型:简化版
    42  func TestIsBlank2(t *testing.T) {
    43  	var value IsBlankEntity2
    44  	var result bool
    45  	var err string
    46  
    47  	//测试 正常情况
    48  	value = IsBlankEntity2{Name: ""}
    49  	result, err = validate.Check(value, "name")
    50  	TrueErr(t, result, err)
    51  
    52  	// 测试 正常情况
    53  	value = IsBlankEntity2{Name: "zhou"}
    54  	result, err = validate.Check(value, "name")
    55  	Equal(t, err, "属性 Name 的值为非空字符", result, false)
    56  }
    57  
    58  func TestIsBlank3(t *testing.T) {
    59  	var value IsBlankEntity3
    60  	var result bool
    61  	var err string
    62  
    63  	//测试 正常情况
    64  	value = IsBlankEntity3{Name: ""}
    65  	result, err = validate.Check(value, "name")
    66  	TrueErr(t, result, err)
    67  
    68  	// 测试 正常情况
    69  	value = IsBlankEntity3{Name: "zhou"}
    70  	result, err = validate.Check(value, "name")
    71  	Equal(t, err, "属性 Name 的值为非空字符", result, false)
    72  }
    73  
    74  // isBlank的基准测试
    75  func Benchmark_IsBlank(b *testing.B) {
    76  	var value IsBlankEntity2
    77  	for i := 0; i < b.N; i++ {
    78  		value = IsBlankEntity2{Name: "zhou"}
    79  		validate.Check(value, "name")
    80  	}
    81  }