regexp_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. *
  3. * Copyright 2018 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package binarylog
  19. import (
  20. "reflect"
  21. "testing"
  22. )
  23. func TestLongMethodConfigRegexp(t *testing.T) {
  24. testCases := []struct {
  25. in string
  26. out []string
  27. }{
  28. {in: "", out: nil},
  29. {in: "*/m", out: nil},
  30. {
  31. in: "p.s/m{}",
  32. out: []string{"p.s/m{}", "p.s", "m", "{}"},
  33. },
  34. {
  35. in: "p.s/m",
  36. out: []string{"p.s/m", "p.s", "m", ""},
  37. },
  38. {
  39. in: "p.s/m{h}",
  40. out: []string{"p.s/m{h}", "p.s", "m", "{h}"},
  41. },
  42. {
  43. in: "p.s/m{m}",
  44. out: []string{"p.s/m{m}", "p.s", "m", "{m}"},
  45. },
  46. {
  47. in: "p.s/m{h:123}",
  48. out: []string{"p.s/m{h:123}", "p.s", "m", "{h:123}"},
  49. },
  50. {
  51. in: "p.s/m{m:123}",
  52. out: []string{"p.s/m{m:123}", "p.s", "m", "{m:123}"},
  53. },
  54. {
  55. in: "p.s/m{h:123,m:123}",
  56. out: []string{"p.s/m{h:123,m:123}", "p.s", "m", "{h:123,m:123}"},
  57. },
  58. {
  59. in: "p.s/*",
  60. out: []string{"p.s/*", "p.s", "*", ""},
  61. },
  62. {
  63. in: "p.s/*{h}",
  64. out: []string{"p.s/*{h}", "p.s", "*", "{h}"},
  65. },
  66. {
  67. in: "s/m*",
  68. out: []string{"s/m*", "s", "m", "*"},
  69. },
  70. {
  71. in: "s/**",
  72. out: []string{"s/**", "s", "*", "*"},
  73. },
  74. }
  75. for _, tc := range testCases {
  76. match := longMethodConfigRegexp.FindStringSubmatch(tc.in)
  77. if !reflect.DeepEqual(match, tc.out) {
  78. t.Errorf("in: %q, out: %q, want: %q", tc.in, match, tc.out)
  79. }
  80. }
  81. }
  82. func TestHeaderConfigRegexp(t *testing.T) {
  83. testCases := []struct {
  84. in string
  85. out []string
  86. }{
  87. {in: "{}", out: nil},
  88. {in: "{a:b}", out: nil},
  89. {in: "{m:123}", out: nil},
  90. {in: "{h:123;m:123}", out: nil},
  91. {
  92. in: "{h}",
  93. out: []string{"{h}", ""},
  94. },
  95. {
  96. in: "{h:123}",
  97. out: []string{"{h:123}", "123"},
  98. },
  99. }
  100. for _, tc := range testCases {
  101. match := headerConfigRegexp.FindStringSubmatch(tc.in)
  102. if !reflect.DeepEqual(match, tc.out) {
  103. t.Errorf("in: %q, out: %q, want: %q", tc.in, match, tc.out)
  104. }
  105. }
  106. }
  107. func TestMessageConfigRegexp(t *testing.T) {
  108. testCases := []struct {
  109. in string
  110. out []string
  111. }{
  112. {in: "{}", out: nil},
  113. {in: "{a:b}", out: nil},
  114. {in: "{h:123}", out: nil},
  115. {in: "{h:123;m:123}", out: nil},
  116. {
  117. in: "{m}",
  118. out: []string{"{m}", ""},
  119. },
  120. {
  121. in: "{m:123}",
  122. out: []string{"{m:123}", "123"},
  123. },
  124. }
  125. for _, tc := range testCases {
  126. match := messageConfigRegexp.FindStringSubmatch(tc.in)
  127. if !reflect.DeepEqual(match, tc.out) {
  128. t.Errorf("in: %q, out: %q, want: %q", tc.in, match, tc.out)
  129. }
  130. }
  131. }
  132. func TestHeaderMessageConfigRegexp(t *testing.T) {
  133. testCases := []struct {
  134. in string
  135. out []string
  136. }{
  137. {in: "{}", out: nil},
  138. {in: "{a:b}", out: nil},
  139. {in: "{h}", out: nil},
  140. {in: "{h:123}", out: nil},
  141. {in: "{m}", out: nil},
  142. {in: "{m:123}", out: nil},
  143. {
  144. in: "{h;m}",
  145. out: []string{"{h;m}", "", ""},
  146. },
  147. {
  148. in: "{h:123;m}",
  149. out: []string{"{h:123;m}", "123", ""},
  150. },
  151. {
  152. in: "{h;m:123}",
  153. out: []string{"{h;m:123}", "", "123"},
  154. },
  155. {
  156. in: "{h:123;m:123}",
  157. out: []string{"{h:123;m:123}", "123", "123"},
  158. },
  159. }
  160. for _, tc := range testCases {
  161. match := headerMessageConfigRegexp.FindStringSubmatch(tc.in)
  162. if !reflect.DeepEqual(match, tc.out) {
  163. t.Errorf("in: %q, out: %q, want: %q", tc.in, match, tc.out)
  164. }
  165. }
  166. }