github.com/zhongdalu/gf@v1.0.0/g/util/gvalid/gvalid_unit_customerror_test.go (about)

     1  // Copyright 2019 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/gf.
     6  
     7  package gvalid_test
     8  
     9  import (
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/zhongdalu/gf/g/test/gtest"
    14  	"github.com/zhongdalu/gf/g/util/gvalid"
    15  )
    16  
    17  func Test_Map(t *testing.T) {
    18  	rule := "ipv4"
    19  	val := "0.0.0"
    20  	msg := map[string]string{
    21  		"ipv4": "IPv4地址格式不正确",
    22  	}
    23  
    24  	err := gvalid.Check(val, rule, nil)
    25  	gtest.Assert(err.Map(), msg)
    26  }
    27  
    28  func Test_FirstString(t *testing.T) {
    29  	rule := "ipv4"
    30  	val := "0.0.0"
    31  
    32  	err := gvalid.Check(val, rule, nil)
    33  	n := err.FirstString()
    34  	gtest.Assert(n, "IPv4地址格式不正确")
    35  }
    36  
    37  func Test_SetDefaultErrorMsgs(t *testing.T) {
    38  	rule := "integer|length:6,16"
    39  	msgs := map[string]string{
    40  		"integer": "请输入一个整数",
    41  		"length":  "参数长度不对啊老铁",
    42  	}
    43  	gvalid.SetDefaultErrorMsgs(msgs)
    44  	e := gvalid.Check("6.66", rule, nil)
    45  	if e == nil || len(e.Map()) != 2 {
    46  		t.Error("规则校验失败")
    47  	} else {
    48  		if v, ok := e.Map()["integer"]; ok {
    49  			if strings.Compare(v, msgs["integer"]) != 0 {
    50  				t.Error("错误信息不匹配")
    51  			}
    52  		}
    53  		if v, ok := e.Map()["length"]; ok {
    54  			if strings.Compare(v, msgs["length"]) != 0 {
    55  				t.Error("错误信息不匹配")
    56  			}
    57  		}
    58  	}
    59  }
    60  
    61  func Test_CustomError1(t *testing.T) {
    62  	rule := "integer|length:6,16"
    63  	msgs := map[string]string{
    64  		"integer": "请输入一个整数",
    65  		"length":  "参数长度不对啊老铁",
    66  	}
    67  	e := gvalid.Check("6.66", rule, msgs)
    68  	if e == nil || len(e.Map()) != 2 {
    69  		t.Error("规则校验失败")
    70  	} else {
    71  		if v, ok := e.Map()["integer"]; ok {
    72  			if strings.Compare(v, msgs["integer"]) != 0 {
    73  				t.Error("错误信息不匹配")
    74  			}
    75  		}
    76  		if v, ok := e.Map()["length"]; ok {
    77  			if strings.Compare(v, msgs["length"]) != 0 {
    78  				t.Error("错误信息不匹配")
    79  			}
    80  		}
    81  	}
    82  }
    83  
    84  func Test_CustomError2(t *testing.T) {
    85  	rule := "integer|length:6,16"
    86  	msgs := "请输入一个整数|参数长度不对啊老铁"
    87  	e := gvalid.Check("6.66", rule, msgs)
    88  	if e == nil || len(e.Map()) != 2 {
    89  		t.Error("规则校验失败")
    90  	} else {
    91  		if v, ok := e.Map()["integer"]; ok {
    92  			if strings.Compare(v, "请输入一个整数") != 0 {
    93  				t.Error("错误信息不匹配")
    94  			}
    95  		}
    96  		if v, ok := e.Map()["length"]; ok {
    97  			if strings.Compare(v, "参数长度不对啊老铁") != 0 {
    98  				t.Error("错误信息不匹配")
    99  			}
   100  		}
   101  	}
   102  }