regex_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. *
  3. * Copyright 2021 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 grpcutil
  19. import (
  20. "regexp"
  21. "testing"
  22. )
  23. func TestFullMatchWithRegex(t *testing.T) {
  24. tests := []struct {
  25. name string
  26. regexStr string
  27. string string
  28. want bool
  29. }{
  30. {
  31. name: "not match because only partial",
  32. regexStr: "^a+$",
  33. string: "ab",
  34. want: false,
  35. },
  36. {
  37. name: "match because fully match",
  38. regexStr: "^a+$",
  39. string: "aa",
  40. want: true,
  41. },
  42. {
  43. name: "longest",
  44. regexStr: "a(|b)",
  45. string: "ab",
  46. want: true,
  47. },
  48. {
  49. name: "match all",
  50. regexStr: ".*",
  51. string: "",
  52. want: true,
  53. },
  54. {
  55. name: "matches non-empty strings",
  56. regexStr: ".+",
  57. string: "",
  58. want: false,
  59. },
  60. }
  61. for _, tt := range tests {
  62. t.Run(tt.name, func(t *testing.T) {
  63. hrm := regexp.MustCompile(tt.regexStr)
  64. if got := FullMatchWithRegex(hrm, tt.string); got != tt.want {
  65. t.Errorf("match() = %v, want %v", got, tt.want)
  66. }
  67. })
  68. }
  69. }