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

     1  package rules_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "github.com/onsi/gomega"
     7  
     8  	"github.com/machinefi/w3bstream/pkg/depends/kit/validator/rules"
     9  )
    10  
    11  func TestSlashAndUnSlash(t *testing.T) {
    12  	cases := []struct {
    13  		name     string
    14  		inAndOut [2]string
    15  	}{
    16  		{"1", [2]string{`/\w+\/test/`, `\w+/test`}},
    17  		{"2", [2]string{`/a/`, `a`}},
    18  		{"3", [2]string{`/abc/`, `abc`}},
    19  		{"4", [2]string{`/☺/`, `☺`}},
    20  		{"5", [2]string{`/\xFF/`, `\xFF`}},
    21  		{"6", [2]string{`/\377/`, `\377`}},
    22  		{"7", [2]string{`/\u1234/`, `\u1234`}},
    23  		{"8", [2]string{`/\U00010111/`, `\U00010111`}},
    24  		{"9", [2]string{`/\U0001011111/`, `\U0001011111`}},
    25  		{"A", [2]string{`/\a\b\f\n\r\t\v\\\"/`, `\a\b\f\n\r\t\v\\\"`}},
    26  		{"B", [2]string{`/\//`, `/`}},
    27  		{"C", [2]string{`/`, ``}},
    28  		{"D", [2]string{`/adfadf`, ``}},
    29  	}
    30  
    31  	for i := range cases {
    32  		c := cases[i]
    33  		t.Run("UnSlash:"+c.name, func(t *testing.T) {
    34  			r, err := rules.UnSlash([]byte(c.inAndOut[0]))
    35  			if c.inAndOut[1] == "" {
    36  				NewWithT(t).Expect(err).NotTo(BeNil())
    37  				return
    38  			}
    39  			NewWithT(t).Expect(err).To(BeNil())
    40  			NewWithT(t).Expect(string(r)).To(Equal(c.inAndOut[1]))
    41  		})
    42  		t.Run("Slash:"+c.name, func(t *testing.T) {
    43  			if c.inAndOut[1] == "" {
    44  				return
    45  			}
    46  			NewWithT(t).Expect(string(rules.Slash([]byte(c.inAndOut[1])))).
    47  				To(Equal(c.inAndOut[0]))
    48  		})
    49  	}
    50  }
    51  
    52  func TestParseRule(t *testing.T) {
    53  	cases := [][]string{
    54  		// simple
    55  		{`@email`, `@email`},
    56  
    57  		// with parameters
    58  		{`@map<@email,         @url>`, `@map<@email,@url>`},
    59  		{`@map<@string,>`, `@map<@string,>`},
    60  		{`@map<,@string>`, `@map<,@string>`},
    61  		{`@float32<10,6>`, `@float32<10,6>`},
    62  		{`@float32<10,-1>`, `@float32<10,-1>`},
    63  		{`@slice<@string>`, `@slice<@string>`},
    64  
    65  		// with range
    66  		{`@slice[0,   10]`, `@slice[0,10]`},
    67  		{`@array[10]`, `@array[10]`},
    68  		{`@string[0,)`, `@string[0,)`},
    69  		{`@string[0,)`, `@string[0,)`},
    70  		{`@int(0,)`, `@int(0,)`},
    71  		{`@int(,1)`, `@int(,1)`},
    72  		{`@float32(1.10,)`, `@float32(1.10,)`},
    73  
    74  		// with values
    75  		{`@string{A, B,    C}`, `@string{A,B,C}`},
    76  		{`@string{, B,    C}`, `@string{,B,C}`},
    77  		{`@uint{%2}`, `@uint{%2}`},
    78  
    79  		// with value matrix
    80  		{`@string{A, B,    C}{a,b}`, `@string{A,B,C}{a,b}`},
    81  
    82  		// with not required mark or default value
    83  		{`@string?`, `@string?`},
    84  		{`@string ?`, `@string?`},
    85  		{`@string = `, `@string = ''`},
    86  		{`@string = '\''`, `@string = '\''`},
    87  		{`@string = 'default value'`, `@string = 'default value'`},
    88  		{`@string = 'defa\'ult\ value'`, `@string = 'defa\'ult\ value'`},
    89  		{`@string = 13123`, `@string = '13123'`},
    90  		{`@string = 1.1`, `@string = '1.1'`},
    91  
    92  		// with regexp
    93  		{`@string/\w+/`, `@string/\w+/`},
    94  		{`@string/\w+     $/`, `@string/\w+     $/`},
    95  		{`@string/\w+\/abc/`, `@string/\w+\/abc/`},
    96  		{`@string/\w+\/\/abc/`, `@string/\w+\/\/abc/`},
    97  		{`@string/^\w+\/test/`, `@string/^\w+\/test/`},
    98  
    99  		// composes
   100  		{`@string = 's'/\w+/`, `@string/\w+/ = 's'`},
   101  		{`@map<,@string[1,]>`, `@map<,@string[1,]>`},
   102  		{`@map<@string,>[1,2]`, `@map<@string,>[1,2]`},
   103  		{`@map<@string = 's',>[1,2]`, `@map<@string = 's',>[1,2]`},
   104  		{`@slice<@float64<10,4>[-1.000,100.000]?>`, `@slice<@float64<10,4>[-1.000,100.000]?>`},
   105  	}
   106  
   107  	for i := range cases {
   108  		c := cases[i]
   109  		t.Run("rule:"+c[0], func(t *testing.T) {
   110  			r, err := rules.Parse(c[0])
   111  			NewWithT(t).Expect(err).To(BeNil())
   112  			NewWithT(t).Expect(c[1]).To(Equal(string(r.Bytes())))
   113  		})
   114  	}
   115  }
   116  
   117  func TestParseRuleFailed(t *testing.T) {
   118  	cases := []string{
   119  		`@`,
   120  		`@unsupportted-name`,
   121  		`@name<`,
   122  		`@name[`,
   123  		`@name(`,
   124  		`@name{`,
   125  		`@name/`,
   126  		`@name)`,
   127  		`@name<@sub[>`,
   128  		`@name</>`,
   129  		`@/`,
   130  		`@name?=`,
   131  	}
   132  
   133  	for _, c := range cases {
   134  		_, err := rules.Parse(c)
   135  		NewWithT(t).Expect(err).NotTo(BeNil())
   136  		// t.Logf("\n%v\n%v", c, err)
   137  	}
   138  }
   139  
   140  //
   141  // func TestRule(t *testing.T) {
   142  // 	r, _ := ParseRuleString("@string{A,B,C}{a,b}{1,2}")
   143  // 	spew.Dump(r.ComputedValues())
   144  // }
   145  //