本站源代码
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

63 行
1.3KB

  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package validation
  5. import (
  6. "testing"
  7. "gitea.com/macaron/binding"
  8. "github.com/gobwas/glob"
  9. )
  10. func getGlobPatternErrorString(pattern string) string {
  11. // It would be unwise to rely on that glob
  12. // compilation errors don't ever change.
  13. if _, err := glob.Compile(pattern); err != nil {
  14. return err.Error()
  15. }
  16. return ""
  17. }
  18. var globValidationTestCases = []validationTestCase{
  19. {
  20. description: "Empty glob pattern",
  21. data: TestForm{
  22. GlobPattern: "",
  23. },
  24. expectedErrors: binding.Errors{},
  25. },
  26. {
  27. description: "Valid glob",
  28. data: TestForm{
  29. GlobPattern: "{master,release*}",
  30. },
  31. expectedErrors: binding.Errors{},
  32. },
  33. {
  34. description: "Invalid glob",
  35. data: TestForm{
  36. GlobPattern: "[a-",
  37. },
  38. expectedErrors: binding.Errors{
  39. binding.Error{
  40. FieldNames: []string{"GlobPattern"},
  41. Classification: ErrGlobPattern,
  42. Message: getGlobPatternErrorString("[a-"),
  43. },
  44. },
  45. },
  46. }
  47. func Test_GlobPatternValidation(t *testing.T) {
  48. AddBindingRules()
  49. for _, testCase := range globValidationTestCases {
  50. t.Run(testCase.description, func(t *testing.T) {
  51. performValidationTest(t, testCase)
  52. })
  53. }
  54. }
上海开阖软件有限公司 沪ICP备12045867号-1