You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

glob_test.go 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright (c) 2020 Shivaram Lingamneni <slingamn@cs.stanford.edu>
  2. // released under the MIT license
  3. package utils
  4. import (
  5. "regexp"
  6. "testing"
  7. )
  8. func globMustCompile(glob string) *regexp.Regexp {
  9. re, err := CompileGlob(glob)
  10. if err != nil {
  11. panic(err)
  12. }
  13. return re
  14. }
  15. func assertMatches(glob, str string, match bool, t *testing.T) {
  16. re := globMustCompile(glob)
  17. if re.MatchString(str) != match {
  18. t.Errorf("should %s match %s? %t, but got %t instead", glob, str, match, !match)
  19. }
  20. }
  21. func TestGlob(t *testing.T) {
  22. assertMatches("https://testnet.oragono.io", "https://testnet.oragono.io", true, t)
  23. assertMatches("https://*.oragono.io", "https://testnet.oragono.io", true, t)
  24. assertMatches("*://*.oragono.io", "https://testnet.oragono.io", true, t)
  25. assertMatches("*://*.oragono.io", "https://oragono.io", false, t)
  26. assertMatches("*://*.oragono.io", "https://githubusercontent.com", false, t)
  27. assertMatches("*://*.oragono.io", "https://testnet.oragono.io.example.com", false, t)
  28. assertMatches("", "", true, t)
  29. assertMatches("", "x", false, t)
  30. assertMatches("*", "", true, t)
  31. assertMatches("*", "x", true, t)
  32. assertMatches("c?b", "cab", true, t)
  33. assertMatches("c?b", "cub", true, t)
  34. assertMatches("c?b", "cb", false, t)
  35. assertMatches("c?b", "cube", false, t)
  36. assertMatches("?*", "cube", true, t)
  37. assertMatches("?*", "", false, t)
  38. assertMatches("S*e", "Skåne", true, t)
  39. assertMatches("Sk?ne", "Skåne", true, t)
  40. }