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

     1  package validator_test
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	. "github.com/onsi/gomega"
     9  
    10  	. "github.com/machinefi/w3bstream/pkg/depends/kit/validator"
    11  	"github.com/machinefi/w3bstream/pkg/depends/x/ptrx"
    12  	"github.com/machinefi/w3bstream/pkg/depends/x/typesx"
    13  )
    14  
    15  func TestMap_New(t *testing.T) {
    16  	cases := []struct {
    17  		rule   string
    18  		expect *Map
    19  		typ    reflect.Type
    20  	}{{
    21  		"@map[1,1000]", &Map{
    22  			MinProperties: 1,
    23  			MaxProperties: ptrx.Uint64(1000),
    24  		}, rtMapStringString,
    25  	}, {
    26  		"@map<,@map[1,2]>[1,]", &Map{
    27  			MinProperties: 1,
    28  			ElemValidator: DefaultFactory.MustCompile(
    29  				bg, []byte("@map[1,2]"), typesx.FromReflectType(rtMapStringString),
    30  			),
    31  		}, rtMapStringMapStringString,
    32  	}, {
    33  		"@map<@string[0,],@map[1,2]>[1,]", &Map{
    34  			MinProperties: 1,
    35  			KeyValidator: DefaultFactory.MustCompile(
    36  				bg, []byte("@string[0,]"), typesx.FromReflectType(rtString),
    37  			),
    38  			ElemValidator: DefaultFactory.MustCompile(
    39  				bg, []byte("@map[1,2]"), typesx.FromReflectType(rtMapStringString),
    40  			),
    41  		}, rtMapStringMapStringString,
    42  	},
    43  	}
    44  
    45  	for i, c := range cases {
    46  		name := fmt.Sprintf("%02d_%s|%s|%s", i+1, c.typ, c.rule, c.expect)
    47  		t.Run(name, func(t *testing.T) {
    48  			v, err := c.expect.New(ctx, MustParseRuleStringByType(
    49  				c.rule, typesx.FromReflectType(c.typ)),
    50  			)
    51  			NewWithT(t).Expect(err).To(BeNil())
    52  			NewWithT(t).Expect(v).To(Equal(c.expect))
    53  		})
    54  	}
    55  }
    56  
    57  func TestMap_NewFailed(t *testing.T) {
    58  	cases := []struct {
    59  		name string
    60  		rule string
    61  		typ  reflect.Type
    62  	}{
    63  		{"01", "@map", rtSliceString},
    64  		{"02", "@map<1,>", rtMapStringString},
    65  		{"03", "@map<,2>", rtMapStringString},
    66  		{"04", "@map<1,2,3>", rtMapStringString},
    67  		{"05", "@map[1,0]", rtMapStringString},
    68  		{"06", "@map[1,-2]", rtMapStringString},
    69  		{"07", "@map[a,]", rtMapStringString},
    70  		{"08", "@map[-1,1]", rtMapStringString},
    71  		{"09", "@map(-1,1)", rtMapStringString},
    72  		{"10", "@map<@unknown,>", rtMapStringString},
    73  		{"11", "@map<,@unknown>", rtMapStringString},
    74  		{"12", "@map<@string[0,],@unknown>", rtMapStringString},
    75  	}
    76  	for i, c := range cases {
    77  		rule := MustParseRuleStringByType(c.rule, typesx.FromReflectType(c.typ))
    78  		name := fmt.Sprintf("%02d_%s|%s", i+1, c.typ, rule.Bytes())
    79  		t.Run(name, func(t *testing.T) {
    80  			v := &Map{}
    81  			_, err := v.New(ctx, rule)
    82  			NewWithT(t).Expect(err).NotTo(BeNil())
    83  			// t.Logf("\n%v", err)
    84  		})
    85  	}
    86  }
    87  
    88  func TestMap_Validate(t *testing.T) {
    89  	cases := []struct {
    90  		values    []interface{}
    91  		validator *Map
    92  		desc      string
    93  	}{
    94  		{
    95  			[]interface{}{
    96  				map[string]string{"1": "", "2": ""},
    97  				map[string]string{"1": "", "2": "", "3": ""},
    98  				map[string]string{"1": "", "2": "", "3": "", "4": ""},
    99  			}, &Map{
   100  				MinProperties: 2,
   101  				MaxProperties: ptrx.Uint64(4),
   102  			}, "InRange",
   103  		}, {
   104  			[]interface{}{
   105  				reflect.ValueOf(map[string]string{"1": "", "2": ""}),
   106  				map[string]string{"1": "", "2": "", "3": ""},
   107  			}, &Map{
   108  				MinProperties: 2,
   109  				MaxProperties: ptrx.Uint64(4),
   110  				KeyValidator: DefaultFactory.MustCompile(
   111  					bg, []byte("@string[1,]"), rttString,
   112  				),
   113  				ElemValidator: DefaultFactory.MustCompile(
   114  					bg, []byte("@string[1,]?"), rttString,
   115  				),
   116  			}, "KeyValueValidate",
   117  		},
   118  	}
   119  
   120  	for ci, c := range cases {
   121  		for vi, v := range c.values {
   122  			t.Run(
   123  				fmt.Sprintf(
   124  					"%02d_%02d_%s|%s|%v",
   125  					ci+1, vi+1, c.desc, c.validator, v,
   126  				),
   127  				func(t *testing.T) {
   128  					NewWithT(t).Expect(c.validator.Validate(v)).To(BeNil())
   129  				},
   130  			)
   131  		}
   132  	}
   133  }
   134  
   135  func TestMap_ValidateFailed(t *testing.T) {
   136  	cases := []struct {
   137  		values    []interface{}
   138  		validator *Map
   139  		desc      string
   140  	}{
   141  		{
   142  			[]interface{}{
   143  				map[string]string{"1": ""},
   144  				map[string]string{"1": "", "2": "", "3": "", "4": "", "5": ""},
   145  				map[string]string{"1": "", "2": "", "3": "", "4": "", "5": "", "6": ""},
   146  			}, &Map{
   147  				MinProperties: 2,
   148  				MaxProperties: ptrx.Uint64(4),
   149  			}, "OutOfRange",
   150  		}, {
   151  			[]interface{}{
   152  				map[string]string{"1": "", "2": ""},
   153  				map[string]string{"1": "", "2": "", "3": ""},
   154  			}, &Map{
   155  				MinProperties: 2,
   156  				MaxProperties: ptrx.Uint64(4),
   157  				KeyValidator:  DefaultFactory.MustCompile(bg, []byte("@string[2,]"), rttString),
   158  				ElemValidator: DefaultFactory.MustCompile(bg, []byte("@string[2,]"), rttString),
   159  			}, "KeyElemValidateFailed",
   160  		},
   161  	}
   162  
   163  	for ci, c := range cases {
   164  		for vi, v := range c.values {
   165  			t.Run(
   166  				fmt.Sprintf("%02d_%02d_%s|%s|%v",
   167  					ci+1, vi+1, c.desc, c.validator, v,
   168  				),
   169  				func(t *testing.T) {
   170  					err := c.validator.Validate(v)
   171  					NewWithT(t).Expect(err).NotTo(BeNil())
   172  					// t.Logf("\n%v", err)
   173  				})
   174  		}
   175  	}
   176  }