1 package httprule
2
3 import (
4 "reflect"
5 "testing"
6
7 "github.com/grpc-ecosystem/grpc-gateway/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 wildcard{},
27 },
28 ops: []int{int(utilities.OpPush), operandFiller},
29 },
30 {
31 segs: []segment{
32 deepWildcard{},
33 },
34 ops: []int{int(utilities.OpPushM), operandFiller},
35 },
36 {
37 segs: []segment{
38 literal("v1"),
39 },
40 ops: []int{int(utilities.OpLitPush), 0},
41 pool: []string{"v1"},
42 },
43 {
44 segs: []segment{
45 literal("v1"),
46 },
47 verb: "LOCK",
48 ops: []int{int(utilities.OpLitPush), 0},
49 pool: []string{"v1"},
50 },
51 {
52 segs: []segment{
53 variable{
54 path: "name.nested",
55 segments: []segment{
56 wildcard{},
57 },
58 },
59 },
60 ops: []int{
61 int(utilities.OpPush), operandFiller,
62 int(utilities.OpConcatN), 1,
63 int(utilities.OpCapture), 0,
64 },
65 pool: []string{"name.nested"},
66 fields: []string{"name.nested"},
67 },
68 {
69 segs: []segment{
70 literal("obj"),
71 variable{
72 path: "name.nested",
73 segments: []segment{
74 literal("a"),
75 wildcard{},
76 literal("b"),
77 },
78 },
79 variable{
80 path: "obj",
81 segments: []segment{
82 deepWildcard{},
83 },
84 },
85 },
86 ops: []int{
87 int(utilities.OpLitPush), 0,
88 int(utilities.OpLitPush), 1,
89 int(utilities.OpPush), operandFiller,
90 int(utilities.OpLitPush), 2,
91 int(utilities.OpConcatN), 3,
92 int(utilities.OpCapture), 3,
93 int(utilities.OpPushM), operandFiller,
94 int(utilities.OpConcatN), 1,
95 int(utilities.OpCapture), 0,
96 },
97 pool: []string{"obj", "a", "b", "name.nested"},
98 fields: []string{"name.nested", "obj"},
99 },
100 } {
101 tmpl := template{
102 segments: spec.segs,
103 verb: spec.verb,
104 }
105 compiled := tmpl.Compile()
106 if got, want := compiled.Version, opcodeVersion; got != want {
107 t.Errorf("tmpl.Compile().Version = %d; want %d; segs=%#v, verb=%q", got, want, spec.segs, spec.verb)
108 }
109 if got, want := compiled.OpCodes, spec.ops; !reflect.DeepEqual(got, want) {
110 t.Errorf("tmpl.Compile().OpCodes = %v; want %v; segs=%#v, verb=%q", got, want, spec.segs, spec.verb)
111 }
112 if got, want := compiled.Pool, spec.pool; !reflect.DeepEqual(got, want) {
113 t.Errorf("tmpl.Compile().Pool = %q; want %q; segs=%#v, verb=%q", got, want, spec.segs, spec.verb)
114 }
115 if got, want := compiled.Verb, spec.verb; got != want {
116 t.Errorf("tmpl.Compile().Verb = %q; want %q; segs=%#v, verb=%q", got, want, spec.segs, spec.verb)
117 }
118 if got, want := compiled.Fields, spec.fields; !reflect.DeepEqual(got, want) {
119 t.Errorf("tmpl.Compile().Fields = %q; want %q; segs=%#v, verb=%q", got, want, spec.segs, spec.verb)
120 }
121 }
122 }
123
View as plain text