136 lines
3.4 KiB
Go
136 lines
3.4 KiB
Go
package validator
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestValidateEnterpriseName(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
expectError bool
|
|
errorMsg string
|
|
}{
|
|
{
|
|
name: "有效的有限公司名称",
|
|
input: "北京天远数据科技有限公司",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "有效的工作室名称",
|
|
input: "张三设计工作室",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "有效的英文公司名称",
|
|
input: "Apple Inc",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "有效的英文有限公司",
|
|
input: "Google LLC",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "有效的股份有限公司",
|
|
input: "中国移动股份有限公司",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "有效的个体工商户",
|
|
input: "李四个体工商户",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "空字符串",
|
|
input: "",
|
|
expectError: true,
|
|
errorMsg: "企业名称不能为空",
|
|
},
|
|
{
|
|
name: "只有空格",
|
|
input: " ",
|
|
expectError: true,
|
|
errorMsg: "企业名称不能为空",
|
|
},
|
|
{
|
|
name: "长度过短",
|
|
input: "A",
|
|
expectError: true,
|
|
errorMsg: "企业名称长度不能少于2个字符",
|
|
},
|
|
{
|
|
name: "包含非法字符",
|
|
input: "测试公司@#$",
|
|
expectError: true,
|
|
errorMsg: "企业名称不能包含特殊字符",
|
|
},
|
|
{
|
|
name: "包含公司关键词但后缀不合法",
|
|
input: "测试公司123",
|
|
expectError: true,
|
|
errorMsg: "企业名称格式不正确,请使用标准的企业类型后缀",
|
|
},
|
|
{
|
|
name: "包含工作室关键词但后缀不合法",
|
|
input: "设计工作室ABC",
|
|
expectError: true,
|
|
errorMsg: "企业名称格式不正确,请使用标准的企业类型后缀",
|
|
},
|
|
{
|
|
name: "只包含数字和特殊字符",
|
|
input: "12345-67890",
|
|
expectError: true,
|
|
errorMsg: "企业名称必须包含中文字符或英文字母",
|
|
},
|
|
{
|
|
name: "没有企业类型关键词的个人名称",
|
|
input: "张三理发店",
|
|
expectError: false, // 没有企业关键词,所以不强制要求后缀
|
|
},
|
|
{
|
|
name: "包含括号的企业名称",
|
|
input: "北京天远数据科技有限公司(分公司)",
|
|
expectError: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := ValidateEnterpriseName(tt.input)
|
|
|
|
if tt.expectError {
|
|
if err == nil {
|
|
t.Errorf("期望有错误,但没有返回错误")
|
|
} else if tt.errorMsg != "" && !containsString(err.Error(), tt.errorMsg) {
|
|
t.Errorf("期望错误消息包含 '%s',但得到 '%s'", tt.errorMsg, err.Error())
|
|
}
|
|
} else {
|
|
if err != nil {
|
|
t.Errorf("期望没有错误,但得到错误: %v", err)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// containsString 检查字符串是否包含子字符串
|
|
func containsString(s, substr string) bool {
|
|
return len(s) >= len(substr) &&
|
|
(s == substr ||
|
|
len(s) > len(substr) &&
|
|
(s[:len(substr)] == substr ||
|
|
s[len(s)-len(substr):] == substr ||
|
|
containsSubstring(s, substr)))
|
|
}
|
|
|
|
// containsSubstring 辅助函数检查子字符串
|
|
func containsSubstring(s, substr string) bool {
|
|
for i := 0; i <= len(s)-len(substr); i++ {
|
|
if s[i:i+len(substr)] == substr {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|