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

     1  package main
     2  
     3  import (
     4  	"github.com/isyscore/isc-gobase/validate"
     5  	"testing"
     6  )
     7  
     8  type AcceptEntity1 struct {
     9  	Name string `match:"value=zhou" accept:"false"`
    10  	Age  int
    11  }
    12  
    13  type AcceptEntity2 struct {
    14  	Name string `match:"isBlank=false" accept:"false"`
    15  	Age  int
    16  }
    17  
    18  type AcceptEntity3 struct {
    19  	Name string `match:"isBlank=true value=zhou" accept:"true"`
    20  	Age  int
    21  }
    22  
    23  type AcceptEntity4 struct {
    24  	Name string `match:"isBlank=true value=zhou" accept:"false"`
    25  	Age  int
    26  }
    27  
    28  func TestAccept1(t *testing.T) {
    29  	var value AcceptEntity1
    30  	var result bool
    31  	var err string
    32  
    33  	//测试 正常情况
    34  	value = AcceptEntity1{Name: "宋江"}
    35  	result, err = validate.Check(value, "name")
    36  	TrueErr(t, result, err)
    37  
    38  	// 测试 正常情况
    39  	value = AcceptEntity1{Name: "zhou"}
    40  	result, err = validate.Check(value, "name")
    41  	Equal(t, err, "属性 Name 的值 zhou 位于禁用值 [zhou] 中", result, false)
    42  }
    43  
    44  func TestAccept2(t *testing.T) {
    45  	var value AcceptEntity2
    46  	var result bool
    47  	var err string
    48  
    49  	//测试 正常情况
    50  	value = AcceptEntity2{Name: ""}
    51  	result, err = validate.Check(value, "name")
    52  	TrueErr(t, result, err)
    53  
    54  	// 测试 正常情况
    55  	value = AcceptEntity2{Name: "zhou"}
    56  	result, err = validate.Check(value, "name")
    57  	Equal(t, err, "属性 Name 的值不为空", result, false)
    58  }
    59  
    60  func TestAccept3(t *testing.T) {
    61  	var value AcceptEntity3
    62  	var result bool
    63  	var err string
    64  
    65  	//测试 正常情况
    66  	value = AcceptEntity3{Name: ""}
    67  	result, err = validate.Check(value, "name")
    68  	TrueErr(t, result, err)
    69  
    70  	// 测试 正常情况
    71  	value = AcceptEntity3{Name: "zhou"}
    72  	result, err = validate.Check(value, "name")
    73  	TrueErr(t, result, err)
    74  
    75  	// 测试 异常情况
    76  	value = AcceptEntity3{Name: "宋江"}
    77  	result, err = validate.Check(value, "name")
    78  	Equal(t, err, "[\"属性 Name 的值为非空字符\",\"属性 Name 的值 宋江 不在只可用列表 [zhou] 中\"]", result, false)
    79  }
    80  
    81  func TestAccept4(t *testing.T) {
    82  	var value AcceptEntity4
    83  	var result bool
    84  	var err string
    85  
    86  	//测试 正常情况
    87  	value = AcceptEntity4{Name: "宋江"}
    88  	result, err = validate.Check(value, "name")
    89  	TrueErr(t, result, err)
    90  
    91  	// 测试 异常情况
    92  	value = AcceptEntity4{Name: ""}
    93  	result, err = validate.Check(value, "name")
    94  	Equal(t, err, "属性 Name 的值为空字符", result, false)
    95  
    96  	// 测试 异常情况
    97  	value = AcceptEntity4{Name: "zhou"}
    98  	result, err = validate.Check(value, "name")
    99  	Equal(t, err, "属性 Name 的值 zhou 位于禁用值 [zhou] 中", result, false)
   100  }