gitea源码

glob_test.go 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright 2025 The Gitea Authors. All rights reserved.
  2. // Copyright (c) 2016 Sergey Kamardin
  3. // SPDX-License-Identifier: MIT
  4. //
  5. //nolint:revive // the code is from gobwas/glob
  6. package glob
  7. import (
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. )
  12. // Reference: https://github.com/gobwas/glob/blob/master/glob_test.go
  13. const (
  14. pattern_all = "[a-z][!a-x]*cat*[h][!b]*eyes*"
  15. regexp_all = `^[a-z][^a-x].*cat.*[h][^b].*eyes.*$`
  16. fixture_all_match = "my cat has very bright eyes"
  17. fixture_all_mismatch = "my dog has very bright eyes"
  18. pattern_plain = "google.com"
  19. regexp_plain = `^google\.com$`
  20. fixture_plain_match = "google.com"
  21. fixture_plain_mismatch = "gobwas.com"
  22. pattern_multiple = "https://*.google.*"
  23. regexp_multiple = `^https:\/\/.*\.google\..*$`
  24. fixture_multiple_match = "https://account.google.com"
  25. fixture_multiple_mismatch = "https://google.com"
  26. pattern_alternatives = "{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru}"
  27. regexp_alternatives = `^(https:\/\/.*\.google\..*|.*yandex\..*|.*yahoo\..*|.*mail\.ru)$`
  28. fixture_alternatives_match = "http://yahoo.com"
  29. fixture_alternatives_mismatch = "http://google.com"
  30. pattern_alternatives_suffix = "{https://*gobwas.com,http://exclude.gobwas.com}"
  31. regexp_alternatives_suffix = `^(https:\/\/.*gobwas\.com|http://exclude.gobwas.com)$`
  32. fixture_alternatives_suffix_first_match = "https://safe.gobwas.com"
  33. fixture_alternatives_suffix_first_mismatch = "http://safe.gobwas.com"
  34. fixture_alternatives_suffix_second = "http://exclude.gobwas.com"
  35. pattern_prefix = "abc*"
  36. regexp_prefix = `^abc.*$`
  37. pattern_suffix = "*def"
  38. regexp_suffix = `^.*def$`
  39. pattern_prefix_suffix = "ab*ef"
  40. regexp_prefix_suffix = `^ab.*ef$`
  41. fixture_prefix_suffix_match = "abcdef"
  42. fixture_prefix_suffix_mismatch = "af"
  43. pattern_alternatives_combine_lite = "{abc*def,abc?def,abc[zte]def}"
  44. regexp_alternatives_combine_lite = `^(abc.*def|abc.def|abc[zte]def)$`
  45. fixture_alternatives_combine_lite = "abczdef"
  46. pattern_alternatives_combine_hard = "{abc*[a-c]def,abc?[d-g]def,abc[zte]?def}"
  47. regexp_alternatives_combine_hard = `^(abc.*[a-c]def|abc.[d-g]def|abc[zte].def)$`
  48. fixture_alternatives_combine_hard = "abczqdef"
  49. )
  50. type test struct {
  51. pattern, match string
  52. should bool
  53. delimiters []rune
  54. }
  55. func glob(s bool, p, m string, d ...rune) test {
  56. return test{p, m, s, d}
  57. }
  58. func TestGlob(t *testing.T) {
  59. for _, test := range []test{
  60. glob(true, "* ?at * eyes", "my cat has very bright eyes"),
  61. glob(true, "", ""),
  62. glob(false, "", "b"),
  63. glob(true, "*ä", "åä"),
  64. glob(true, "abc", "abc"),
  65. glob(true, "a*c", "abc"),
  66. glob(true, "a*c", "a12345c"),
  67. glob(true, "a?c", "a1c"),
  68. glob(true, "a.b", "a.b", '.'),
  69. glob(true, "a.*", "a.b", '.'),
  70. glob(true, "a.**", "a.b.c", '.'),
  71. glob(true, "a.?.c", "a.b.c", '.'),
  72. glob(true, "a.?.?", "a.b.c", '.'),
  73. glob(true, "?at", "cat"),
  74. glob(true, "?at", "fat"),
  75. glob(true, "*", "abc"),
  76. glob(true, `\*`, "*"),
  77. glob(true, "**", "a.b.c", '.'),
  78. glob(false, "?at", "at"),
  79. glob(false, "?at", "fat", 'f'),
  80. glob(false, "a.*", "a.b.c", '.'),
  81. glob(false, "a.?.c", "a.bb.c", '.'),
  82. glob(false, "*", "a.b.c", '.'),
  83. glob(true, "*test", "this is a test"),
  84. glob(true, "this*", "this is a test"),
  85. glob(true, "*is *", "this is a test"),
  86. glob(true, "*is*a*", "this is a test"),
  87. glob(true, "**test**", "this is a test"),
  88. glob(true, "**is**a***test*", "this is a test"),
  89. glob(false, "*is", "this is a test"),
  90. glob(false, "*no*", "this is a test"),
  91. glob(true, "[!a]*", "this is a test3"),
  92. glob(true, "*abc", "abcabc"),
  93. glob(true, "**abc", "abcabc"),
  94. glob(true, "???", "abc"),
  95. glob(true, "?*?", "abc"),
  96. glob(true, "?*?", "ac"),
  97. glob(false, "sta", "stagnation"),
  98. glob(true, "sta*", "stagnation"),
  99. glob(false, "sta?", "stagnation"),
  100. glob(false, "sta?n", "stagnation"),
  101. glob(true, "{abc,def}ghi", "defghi"),
  102. glob(true, "{abc,abcd}a", "abcda"),
  103. glob(true, "{a,ab}{bc,f}", "abc"),
  104. glob(true, "{*,**}{a,b}", "ab"),
  105. glob(false, "{*,**}{a,b}", "ac"),
  106. glob(true, "/{rate,[a-z][a-z][a-z]}*", "/rate"),
  107. glob(true, "/{rate,[0-9][0-9][0-9]}*", "/rate"),
  108. glob(true, "/{rate,[a-z][a-z][a-z]}*", "/usd"),
  109. glob(true, "{*.google.*,*.yandex.*}", "www.google.com", '.'),
  110. glob(true, "{*.google.*,*.yandex.*}", "www.yandex.com", '.'),
  111. glob(false, "{*.google.*,*.yandex.*}", "yandex.com", '.'),
  112. glob(false, "{*.google.*,*.yandex.*}", "google.com", '.'),
  113. glob(true, "{*.google.*,yandex.*}", "www.google.com", '.'),
  114. glob(true, "{*.google.*,yandex.*}", "yandex.com", '.'),
  115. glob(false, "{*.google.*,yandex.*}", "www.yandex.com", '.'),
  116. glob(false, "{*.google.*,yandex.*}", "google.com", '.'),
  117. glob(true, "*//{,*.}example.com", "https://www.example.com"),
  118. glob(true, "*//{,*.}example.com", "http://example.com"),
  119. glob(false, "*//{,*.}example.com", "http://example.com.net"),
  120. glob(true, pattern_all, fixture_all_match),
  121. glob(false, pattern_all, fixture_all_mismatch),
  122. glob(true, pattern_plain, fixture_plain_match),
  123. glob(false, pattern_plain, fixture_plain_mismatch),
  124. glob(true, pattern_multiple, fixture_multiple_match),
  125. glob(false, pattern_multiple, fixture_multiple_mismatch),
  126. glob(true, pattern_alternatives, fixture_alternatives_match),
  127. glob(false, pattern_alternatives, fixture_alternatives_mismatch),
  128. glob(true, pattern_alternatives_suffix, fixture_alternatives_suffix_first_match),
  129. glob(false, pattern_alternatives_suffix, fixture_alternatives_suffix_first_mismatch),
  130. glob(true, pattern_alternatives_suffix, fixture_alternatives_suffix_second),
  131. glob(true, pattern_alternatives_combine_hard, fixture_alternatives_combine_hard),
  132. glob(true, pattern_alternatives_combine_lite, fixture_alternatives_combine_lite),
  133. glob(true, pattern_prefix, fixture_prefix_suffix_match),
  134. glob(false, pattern_prefix, fixture_prefix_suffix_mismatch),
  135. glob(true, pattern_suffix, fixture_prefix_suffix_match),
  136. glob(false, pattern_suffix, fixture_prefix_suffix_mismatch),
  137. glob(true, pattern_prefix_suffix, fixture_prefix_suffix_match),
  138. glob(false, pattern_prefix_suffix, fixture_prefix_suffix_mismatch),
  139. } {
  140. g, err := Compile(test.pattern, test.delimiters...)
  141. require.NoError(t, err)
  142. result := g.Match(test.match)
  143. assert.Equal(t, test.should, result, "pattern %q matching %q should be %v but got %v, compiled=%s", test.pattern, test.match, test.should, result, g.(*globCompiler).regexpPattern)
  144. }
  145. }
  146. func TestQuoteMeta(t *testing.T) {
  147. for id, test := range []struct {
  148. in, out string
  149. }{
  150. {
  151. in: `[foo*]`,
  152. out: `\[foo\*\]`,
  153. },
  154. {
  155. in: `{foo*}`,
  156. out: `\{foo\*\}`,
  157. },
  158. {
  159. in: `*?\[]{}`,
  160. out: `\*\?\\\[\]\{\}`,
  161. },
  162. {
  163. in: `some text and *?\[]{}`,
  164. out: `some text and \*\?\\\[\]\{\}`,
  165. },
  166. } {
  167. act := QuoteMeta(test.in)
  168. assert.Equal(t, test.out, act, "QuoteMeta(%q)", test.in)
  169. _, err := Compile(act)
  170. assert.NoError(t, err, "#%d _, err := Compile(QuoteMeta(%q) = %q); err = %q", id, test.in, act, err)
  171. }
  172. }