Skip to content

Commit

Permalink
misc: add ut
Browse files Browse the repository at this point in the history
  • Loading branch information
voidint committed Jun 30, 2024
1 parent de073fa commit 9a4ea04
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 3 deletions.
5 changes: 5 additions & 0 deletions collector/autoindex/autoindex_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func NewCollector(downloadPageURL string) (*Collector, error) {
return &c, nil
}

// Name Collector name
func (c *Collector) Name() string {
return Name
}

func (c *Collector) loadDocument() (err error) {
resp, err := http.Get(c.url)
if err != nil {
Expand Down
10 changes: 7 additions & 3 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (

// official collector
const (
// OfficialDownloadPageURL Golang official site URL
// OriginalOfficialDownloadPageURL Golang official site download page URL
OriginalOfficialDownloadPageURL = "https://golang.org/dl/"
// OfficialDownloadPageURL Golang official site download page URL
OfficialDownloadPageURL = "https://go.dev/dl/"
// CNDownloadPageURL China mirror site URL
// CNDownloadPageURL China mirror site download page URL
CNDownloadPageURL = "https://golang.google.cn/dl/"
)

Expand All @@ -36,6 +38,8 @@ const (

// Collector Version information collector
type Collector interface {
// Name Collector name
Name() string
// StableVersions Return all stable versions
StableVersions() (items []*version.Version, err error)
// UnstableVersions Return all stable versions
Expand Down Expand Up @@ -81,7 +85,7 @@ func NewCollector(urls ...string) (c Collector, err error) {
}

switch urls[i] {
case OfficialDownloadPageURL, CNDownloadPageURL:
case OfficialDownloadPageURL, OriginalOfficialDownloadPageURL, CNDownloadPageURL:
return official.NewCollector(urls[i])

case AliYunDownloadPageURL, HUSTDownloadPageURL, NJUDownloadPageURL:
Expand Down
110 changes: 110 additions & 0 deletions collector/collector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package collector

import (
"reflect"
"testing"

"github.com/voidint/g/collector/autoindex"
"github.com/voidint/g/collector/fancyindex"
"github.com/voidint/g/collector/official"
"github.com/voidint/g/pkg/errs"
)

func TestNewCollector(t *testing.T) {
type args struct {
urls []string
}
tests := []struct {
name string
args args
wantCollectorName string
wantErr error
}{
{
name: "nil parameter",
args: args{urls: nil},
wantCollectorName: official.Name,
},
{
name: "A slice containing an empty string",
args: args{urls: []string{""}},
wantCollectorName: official.Name,
},
{
name: "The parameter is a URL slice without a trailing backslash",
args: args{urls: []string{"https://mirrors.aliyun.com/golang"}},
wantCollectorName: fancyindex.Name,
},
{
name: "A slice containing the name of the official collector",
args: args{urls: []string{"official|https://golang.google.cn/dl/"}},
wantCollectorName: official.Name,
},
{
name: "A slice containing the name of the fancyindex collector",
args: args{urls: []string{"fancyindex|https://mirrors.hust.edu.cn/golang/"}},
wantCollectorName: fancyindex.Name,
},
{
name: "A slice containing the name of the autoindex collector",
args: args{urls: []string{"autoindex|https://mirrors.ustc.edu.cn/golang/"}},
wantCollectorName: autoindex.Name,
},
{
name: "A slice containing only official collector URLs",
args: args{urls: []string{OfficialDownloadPageURL}},
wantCollectorName: official.Name,
},
{
name: "A slice containing only original official collector URLs",
args: args{urls: []string{OriginalOfficialDownloadPageURL}},
wantCollectorName: official.Name,
},
{
name: "A slice containing only china official mirror site collector URLs",
args: args{urls: []string{CNDownloadPageURL}},
wantCollectorName: official.Name,
},
{
name: "A slice containing only Alibaba cloud mirror site collector URLs",
args: args{urls: []string{AliYunDownloadPageURL}},
wantCollectorName: fancyindex.Name,
},
{
name: "A slice containing only HUST mirror site collector URLs",
args: args{urls: []string{HUSTDownloadPageURL}},
wantCollectorName: fancyindex.Name,
},
{
name: "A slice containing only NJU mirror site collector URLs",
args: args{urls: []string{NJUDownloadPageURL}},
wantCollectorName: fancyindex.Name,
},
{
name: "A slice containing only USTC mirror site collector URLs",
args: args{urls: []string{USTCDownloadPageURL}},
wantCollectorName: autoindex.Name,
},
{
name: "Collector not found",
args: args{urls: []string{"hello world"}},
wantCollectorName: "",
wantErr: errs.ErrCollectorNotFound,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotC, err := NewCollector(tt.args.urls...)
if err != tt.wantErr {
t.Errorf("NewCollector() error = %v, wantErr %v", err, tt.wantErr)
return
}

if gotC != nil {
if !reflect.DeepEqual(gotC.Name(), tt.wantCollectorName) {
t.Errorf("NewCollector() = %v, want %v", gotC.Name(), tt.wantCollectorName)
}
}
})
}
}
5 changes: 5 additions & 0 deletions collector/fancyindex/fancyindex_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func NewCollector(downloadPageURL string) (*Collector, error) {
return &c, nil
}

// Name Collector name
func (c *Collector) Name() string {
return Name
}

func (c *Collector) loadDocument() (err error) {
resp, err := http.Get(c.url)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions collector/official/official_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func NewCollector(downloadPageURL string) (*Collector, error) {
return &c, nil
}

// Name Collector name
func (c *Collector) Name() string {
return Name
}

func (c *Collector) loadDocument() (err error) {
resp, err := http.Get(c.url)
if err != nil {
Expand Down

0 comments on commit 9a4ea04

Please sign in to comment.