Added test file for search ParseCategories
Cette révision appartient à :
Parent
13a15b3b2c
révision
61e9c9ea33
2 fichiers modifiés avec 51 ajouts et 6 suppressions
|
@ -7,6 +7,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/NyaaPantsu/nyaa/config"
|
||||
catUtil "github.com/NyaaPantsu/nyaa/util/categories"
|
||||
)
|
||||
|
||||
type Status uint8
|
||||
|
@ -157,10 +158,9 @@ func (c Category) IsSubSet() bool {
|
|||
func ParseCategories(s string) []*Category {
|
||||
if s != "" {
|
||||
parts := strings.Split(s, ",")
|
||||
categories := make([]*Category, len(parts))
|
||||
for key, val := range parts {
|
||||
var categories []*Category
|
||||
for _, val := range parts {
|
||||
partsCat := strings.Split(val, "_")
|
||||
|
||||
if len(partsCat) == 2 {
|
||||
tmp, err := strconv.ParseUint(partsCat[0], 10, 8)
|
||||
if err == nil {
|
||||
|
@ -170,9 +170,11 @@ func ParseCategories(s string) []*Category {
|
|||
if err == nil {
|
||||
sub = uint8(tmp)
|
||||
}
|
||||
categories[key] = &Category{
|
||||
Main: c,
|
||||
Sub: sub,
|
||||
if catUtil.CategoryExists(partsCat[0] + "_" + partsCat[1]) {
|
||||
categories = append(categories, &Category{
|
||||
Main: c,
|
||||
Sub: sub,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
43
common/search_test.go
Fichier normal
43
common/search_test.go
Fichier normal
|
@ -0,0 +1,43 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
"github.com/NyaaPantsu/nyaa/config"
|
||||
)
|
||||
|
||||
// run before config/parse.go:init()
|
||||
var _ = func() (_ struct{}) {
|
||||
config.ConfigPath = path.Join("..", config.ConfigPath)
|
||||
config.DefaultConfigPath = path.Join("..", config.DefaultConfigPath)
|
||||
config.Parse()
|
||||
return
|
||||
}()
|
||||
|
||||
func TestParseCategories(t *testing.T) {
|
||||
cat := ParseCategories("")
|
||||
if len(cat) > 0 {
|
||||
t.Fatal("ParseCategories with empty arg doesn't return an empty array")
|
||||
}
|
||||
cat = ParseCategories("5050")
|
||||
if len(cat) > 0 {
|
||||
t.Fatal("ParseCategories with wrong arg doesn't return an empty array")
|
||||
}
|
||||
cat = ParseCategories("50_50")
|
||||
if len(cat) > 0 {
|
||||
t.Fatal("ParseCategories with wrong arg doesn't return an empty array")
|
||||
}
|
||||
cat = ParseCategories("3_13")
|
||||
if len(cat) == 0 {
|
||||
t.Fatal("ParseCategories with good arg return an empty array")
|
||||
}
|
||||
cat = ParseCategories("3_13,3_5")
|
||||
if len(cat) != 2 {
|
||||
t.Fatal("ParseCategories with good arg doesn't return the right array")
|
||||
}
|
||||
cat = ParseCategories("3_13,3_5,5_50")
|
||||
if len(cat) != 2 {
|
||||
t.Fatal("ParseCategories doesn't filter the wrong categories")
|
||||
}
|
||||
}
|
Référencer dans un nouveau ticket