初始化
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package drama
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
const MaxEpisodeCharacters = 5000
|
||||
|
||||
func ValidateEpisodeContent(content string) error {
|
||||
count := utf8.RuneCountInString(strings.TrimSpace(content))
|
||||
if count == 0 {
|
||||
return fmt.Errorf("剧集原文不能为空")
|
||||
}
|
||||
if count > MaxEpisodeCharacters {
|
||||
return fmt.Errorf("每集原文不能超过%d字,当前%d字", MaxEpisodeCharacters, count)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package drama
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestValidateEpisodeContent(t *testing.T) {
|
||||
if err := ValidateEpisodeContent(strings.Repeat("字", MaxEpisodeCharacters)); err != nil {
|
||||
t.Fatalf("expected content at limit to pass: %v", err)
|
||||
}
|
||||
if err := ValidateEpisodeContent(strings.Repeat("字", MaxEpisodeCharacters+1)); err == nil {
|
||||
t.Fatal("expected over-limit content to fail")
|
||||
}
|
||||
if err := ValidateEpisodeContent(" \n\t"); err == nil {
|
||||
t.Fatal("expected blank content to fail")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user