helper_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package pagebuilder
  2. import (
  3. "path"
  4. "testing"
  5. )
  6. func TestSlugReg(t *testing.T) {
  7. cases := []string{
  8. "/apple",
  9. "/Apple",
  10. "/fruit/Apple",
  11. "/fruit/Apple/Red",
  12. "/fruit/bAnAnA/yElLoW",
  13. "/vegetable/Carrot/Orange",
  14. "/animal/Dog/Brown",
  15. "/animal/Cat/Gray",
  16. "/vehicle/Car/Red",
  17. "/City/New-York",
  18. "/Country/United-States/New-York",
  19. "/fruit/apple",
  20. "/fruit/apple/red",
  21. "/fruit/banana/yellow",
  22. "/vegetable/carrot/orange",
  23. "/animal/dog/brown",
  24. "/animal/cat/gray",
  25. "/vehicle/car/red",
  26. "/city/new-york",
  27. "/country/united-states/new-york",
  28. "/user/john_doe",
  29. "/product/12345",
  30. "/page/home",
  31. "/file/file-name",
  32. "/images/image_01",
  33. "/folder/sub_folder",
  34. "/route/route-1",
  35. "/data/data123",
  36. "/user/user12345/profile",
  37. }
  38. casesNotMatch := []string{
  39. "apple",
  40. "*apple",
  41. "$fruit/apple",
  42. "/fruit/apple&banana",
  43. "#vegetable/carrot",
  44. "/animal/dog:",
  45. "/animal/dog#cat",
  46. "%20images/image01",
  47. `/user\john`,
  48. "/product?item=123",
  49. `/my$folder`,
  50. `/usr#bin`,
  51. `/home/user&docs`,
  52. `/var/www/my folder`,
  53. `/home/user/documents/with space`,
  54. `/home/user*docs`,
  55. `/usr?bin`,
  56. `/var/www/my\tfolder`,
  57. `/home/user/documents\nwithnewline`,
  58. }
  59. for _, c := range cases {
  60. if !directoryRe.MatchString(path.Clean(c)) {
  61. t.Errorf("directoryRe.MatchString(%q) = false, want true", c)
  62. }
  63. }
  64. for _, c := range casesNotMatch {
  65. if directoryRe.MatchString(path.Clean(c)) {
  66. t.Errorf("directoryRe.MatchString(%q) = true, want false", c)
  67. }
  68. }
  69. }