github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/validator/validator_z_string_test.go (about)

     1  package validator_test
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"regexp"
     7  	"testing"
     8  
     9  	. "github.com/onsi/gomega"
    10  
    11  	. "github.com/machinefi/w3bstream/pkg/depends/kit/validator"
    12  	"github.com/machinefi/w3bstream/pkg/depends/x/ptrx"
    13  	"github.com/machinefi/w3bstream/pkg/depends/x/typesx"
    14  )
    15  
    16  func TestString_New(t *testing.T) {
    17  	cases := []struct {
    18  		rule   string
    19  		expect *String
    20  	}{
    21  		{
    22  			"@string[1,1000]", &String{
    23  				MinLength: 1,
    24  				MaxLength: ptrx.Uint64(1000),
    25  			},
    26  		}, {
    27  			"@string[1,]", &String{
    28  				MinLength: 1,
    29  			},
    30  		}, {
    31  			"@string<length>[1]", &String{
    32  				MinLength: 1,
    33  				MaxLength: ptrx.Uint64(1),
    34  			},
    35  		}, {
    36  			"@char[1,]", &String{
    37  				LenMode:   STR_LEN_MODE__RUNE_COUNT,
    38  				MinLength: 1,
    39  			},
    40  		}, {
    41  			"@string<rune_count>[1,]", &String{
    42  				LenMode:   STR_LEN_MODE__RUNE_COUNT,
    43  				MinLength: 1,
    44  			},
    45  		}, {
    46  			"@string{KEY1,KEY2}", &String{
    47  				Enums: map[string]string{
    48  					"KEY1": "KEY1",
    49  					"KEY2": "KEY2",
    50  				},
    51  			},
    52  		}, {
    53  			`@string/^\w+/`, &String{
    54  				Pattern: regexp.MustCompile(`^\w+`),
    55  			},
    56  		}, {
    57  			`@string/^\w+\/test/`, &String{
    58  				Pattern: regexp.MustCompile(`^\w+/test`),
    59  			},
    60  		},
    61  	}
    62  
    63  	for i, c := range cases {
    64  		name := fmt.Sprintf("%02d_%s|%s|%s", i, rtString, c.rule, c.expect)
    65  		t.Run(name, func(t *testing.T) {
    66  			v, err := c.expect.New(ctx, MustParseRuleStringByType(c.rule, rttString))
    67  			NewWithT(t).Expect(err).To(BeNil())
    68  			NewWithT(t).Expect(v).To(Equal(c.expect))
    69  		})
    70  	}
    71  }
    72  
    73  func TestString_NewFailed(t *testing.T) {
    74  	cases := []struct {
    75  		rule string
    76  		rtyp typesx.Type
    77  	}{
    78  
    79  		{"@string", rttInt},
    80  		{"@string<length, 1>", rttString},
    81  		{"@string<unsupported>", rttString},
    82  		{"@string[1,0]", rttString},
    83  		{"@string[1,-2]", rttString},
    84  		{"@string[a,]", rttString},
    85  		{"@string[-1,1]", rttString},
    86  		{"@string(-1,1)", rttString},
    87  	}
    88  
    89  	vldt := &String{}
    90  
    91  	for ci, c := range cases {
    92  		rule := MustParseRuleStringByType(c.rule, c.rtyp)
    93  		t.Run(
    94  			fmt.Sprintf("%02d_%s|%s", ci, c.rtyp, c.rule),
    95  			func(t *testing.T) {
    96  				_, err := vldt.New(ctx, rule)
    97  				NewWithT(t).Expect(err).NotTo(BeNil())
    98  				// t.Logf("\n%v", err)
    99  			},
   100  		)
   101  	}
   102  }
   103  
   104  func TestString_Validate(t *testing.T) {
   105  	cases := []struct {
   106  		values []interface{}
   107  		vldt   *String
   108  		desc   string
   109  	}{
   110  		{
   111  			[]interface{}{reflect.ValueOf("a"), StringType("aa"), "aaa", "aaaa", "aaaaa"}, &String{
   112  				MaxLength: ptrx.Uint64(5),
   113  			}, "LessThan",
   114  		}, {
   115  			[]interface{}{"一", "一一", "一一一"}, &String{
   116  				LenMode:   STR_LEN_MODE__RUNE_COUNT,
   117  				MaxLength: ptrx.Uint64(3),
   118  			}, "CharCountLessThan",
   119  		}, {
   120  			[]interface{}{"A", "B"}, &String{
   121  				Enums: map[string]string{
   122  					"A": "A",
   123  					"B": "B",
   124  				},
   125  			}, "InEnum",
   126  		}, {
   127  			[]interface{}{"word", "word1"}, &String{
   128  				Pattern: regexp.MustCompile(`^\w+`),
   129  			}, "RegexpMatched",
   130  		},
   131  	}
   132  
   133  	for ci, c := range cases {
   134  		for vi, v := range c.values {
   135  			name := fmt.Sprintf("%02d_%02d_%s|%s|%v", ci, vi, c.desc, c.vldt, v)
   136  			t.Run(name, func(t *testing.T) {
   137  				err := c.vldt.Validate(v)
   138  				NewWithT(t).Expect(err).To(BeNil())
   139  			})
   140  		}
   141  	}
   142  }
   143  
   144  func TestString_ValidateFailed(t *testing.T) {
   145  	cases := []struct {
   146  		values []interface{}
   147  		vldt   *String
   148  		desc   string
   149  	}{
   150  		{
   151  			[]interface{}{"C", "D", "E"}, &String{
   152  				Enums: map[string]string{"A": "A", "B": "B"},
   153  			}, "NotInEnum",
   154  		},
   155  		{
   156  			[]interface{}{"-word", "-word1"}, &String{
   157  				Pattern: regexp.MustCompile(`^\w+`),
   158  			}, "RegexpNotMatched",
   159  		}, {
   160  			[]interface{}{1.1, reflect.ValueOf(1.1)}, &String{
   161  				MinLength: 5,
   162  			}, "UnsupportedTypes",
   163  		}, {
   164  			[]interface{}{"a", "aa", StringType("aaa"), []byte("aaaa")}, &String{
   165  				MinLength: 5,
   166  			}, "TooShort",
   167  		}, {
   168  			[]interface{}{"aa", "aaa", "aaaa", "aaaaa"}, &String{
   169  				MaxLength: ptrx.Uint64(1),
   170  			}, "TooLong",
   171  		}, {
   172  			[]interface{}{"字符太多"}, &String{
   173  				LenMode:   STR_LEN_MODE__RUNE_COUNT,
   174  				MaxLength: ptrx.Uint64(3),
   175  			}, "TooManyChars",
   176  		},
   177  	}
   178  
   179  	for ci, c := range cases {
   180  		for vi, v := range c.values {
   181  			name := fmt.Sprintf("%02d_%02d_%s|%s|%v", ci, vi, c.desc, c.vldt, v)
   182  			t.Run(name, func(t *testing.T) {
   183  				err := c.vldt.Validate(v)
   184  				NewWithT(t).Expect(err).NotTo(BeNil())
   185  				// t.Logf("\n%v", err)
   186  			})
   187  		}
   188  	}
   189  }