...

Source file src/github.com/grpc-ecosystem/grpc-gateway/v2/internal/httprule/compile_test.go

Documentation: github.com/grpc-ecosystem/grpc-gateway/v2/internal/httprule

     1  package httprule
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/grpc-ecosystem/grpc-gateway/v2/utilities"
     8  )
     9  
    10  const (
    11  	operandFiller = 0
    12  )
    13  
    14  func TestCompile(t *testing.T) {
    15  	for _, spec := range []struct {
    16  		segs []segment
    17  		verb string
    18  
    19  		ops    []int
    20  		pool   []string
    21  		fields []string
    22  	}{
    23  		{},
    24  		{
    25  			segs: []segment{
    26  				literal(eof),
    27  			},
    28  			ops:  []int{int(utilities.OpLitPush), 0},
    29  			pool: []string{""},
    30  		},
    31  		{
    32  			segs: []segment{
    33  				wildcard{},
    34  			},
    35  			ops: []int{int(utilities.OpPush), operandFiller},
    36  		},
    37  		{
    38  			segs: []segment{
    39  				deepWildcard{},
    40  			},
    41  			ops: []int{int(utilities.OpPushM), operandFiller},
    42  		},
    43  		{
    44  			segs: []segment{
    45  				literal("v1"),
    46  			},
    47  			ops:  []int{int(utilities.OpLitPush), 0},
    48  			pool: []string{"v1"},
    49  		},
    50  		{
    51  			segs: []segment{
    52  				literal("v1"),
    53  			},
    54  			verb: "LOCK",
    55  			ops:  []int{int(utilities.OpLitPush), 0},
    56  			pool: []string{"v1"},
    57  		},
    58  		{
    59  			segs: []segment{
    60  				variable{
    61  					path: "name.nested",
    62  					segments: []segment{
    63  						wildcard{},
    64  					},
    65  				},
    66  			},
    67  			ops: []int{
    68  				int(utilities.OpPush), operandFiller,
    69  				int(utilities.OpConcatN), 1,
    70  				int(utilities.OpCapture), 0,
    71  			},
    72  			pool:   []string{"name.nested"},
    73  			fields: []string{"name.nested"},
    74  		},
    75  		{
    76  			segs: []segment{
    77  				literal("obj"),
    78  				variable{
    79  					path: "name.nested",
    80  					segments: []segment{
    81  						literal("a"),
    82  						wildcard{},
    83  						literal("b"),
    84  					},
    85  				},
    86  				variable{
    87  					path: "obj",
    88  					segments: []segment{
    89  						deepWildcard{},
    90  					},
    91  				},
    92  			},
    93  			ops: []int{
    94  				int(utilities.OpLitPush), 0,
    95  				int(utilities.OpLitPush), 1,
    96  				int(utilities.OpPush), operandFiller,
    97  				int(utilities.OpLitPush), 2,
    98  				int(utilities.OpConcatN), 3,
    99  				int(utilities.OpCapture), 3,
   100  				int(utilities.OpPushM), operandFiller,
   101  				int(utilities.OpConcatN), 1,
   102  				int(utilities.OpCapture), 0,
   103  			},
   104  			pool:   []string{"obj", "a", "b", "name.nested"},
   105  			fields: []string{"name.nested", "obj"},
   106  		},
   107  	} {
   108  		tmpl := template{
   109  			segments: spec.segs,
   110  			verb:     spec.verb,
   111  		}
   112  		compiled := tmpl.Compile()
   113  		if got, want := compiled.Version, opcodeVersion; got != want {
   114  			t.Errorf("tmpl.Compile().Version = %d; want %d; segs=%#v, verb=%q", got, want, spec.segs, spec.verb)
   115  		}
   116  		if got, want := compiled.OpCodes, spec.ops; !reflect.DeepEqual(got, want) {
   117  			t.Errorf("tmpl.Compile().OpCodes = %v; want %v; segs=%#v, verb=%q", got, want, spec.segs, spec.verb)
   118  		}
   119  		if got, want := compiled.Pool, spec.pool; !reflect.DeepEqual(got, want) {
   120  			t.Errorf("tmpl.Compile().Pool = %q; want %q; segs=%#v, verb=%q", got, want, spec.segs, spec.verb)
   121  		}
   122  		if got, want := compiled.Verb, spec.verb; got != want {
   123  			t.Errorf("tmpl.Compile().Verb = %q; want %q; segs=%#v, verb=%q", got, want, spec.segs, spec.verb)
   124  		}
   125  		if got, want := compiled.Fields, spec.fields; !reflect.DeepEqual(got, want) {
   126  			t.Errorf("tmpl.Compile().Fields = %q; want %q; segs=%#v, verb=%q", got, want, spec.segs, spec.verb)
   127  		}
   128  	}
   129  }
   130  

View as plain text