compile_test.go 2.9 KB

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