Some checks failed
cache-seeder / prune (push) Has been skipped
cache-seeder / frontend (push) Failing after 3s
cache-seeder / python (push) Failing after 3s
cache-seeder / gobuild (push) Failing after 4s
cache-seeder / lint (bindata, lint-backend) (push) Failing after 3s
giteabot backport / giteabot (push) Has been skipped
giteabot / giteabot (push) Has been skipped
cache-prune / prune (push) Has been skipped
release-nightly-snapcraft / build-and-publish (ubuntu-24.04) (push) Has been cancelled
release-nightly-snapcraft / build-and-publish (ubuntu-24.04-arm) (push) Has been cancelled
release-nightly / nightly-binary (push) Has been cancelled
release-nightly / nightly-container (push) Has been cancelled
cron-renovate / cron-renovate (push) Has been skipped
54 lines
1.0 KiB
Go
54 lines
1.0 KiB
Go
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package validation
|
|
|
|
import (
|
|
"regexp"
|
|
"testing"
|
|
)
|
|
|
|
func getRegexPatternErrorString(pattern string) string {
|
|
if _, err := regexp.Compile(pattern); err != nil {
|
|
return err.Error()
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func Test_RegexPatternValidation(t *testing.T) {
|
|
regexValidationTestCases := []validationTestCase{
|
|
{
|
|
description: "Empty regex pattern",
|
|
data: &TestForm{
|
|
RegexPattern: "",
|
|
},
|
|
},
|
|
{
|
|
description: "Valid regex",
|
|
data: &TestForm{
|
|
RegexPattern: `(\d{1,3})+`,
|
|
},
|
|
},
|
|
|
|
{
|
|
description: "Invalid regex",
|
|
data: &TestForm{
|
|
RegexPattern: "[a-",
|
|
},
|
|
expectedErrors: BindingErrors{
|
|
BindingError{
|
|
FieldNames: []string{"RegexPattern"},
|
|
Classification: ErrRegexPattern,
|
|
Message: getRegexPatternErrorString("[a-"),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, testCase := range regexValidationTestCases {
|
|
t.Run(testCase.description, func(t *testing.T) {
|
|
performValidationTest(t, testCase)
|
|
})
|
|
}
|
|
}
|