1 Star 0 Fork 0

CNCF/devstatscode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
gha_test.go 24.08 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
package devstatscode
import (
"reflect"
"regexp"
"testing"
lib "github.com/cncf/devstatscode"
)
func TestMakeUniqueSort(t *testing.T) {
var testCases = []struct {
input []string
expected []string
}{
{
input: []string{},
expected: []string{},
},
{
input: []string{"a", "b", "cde"},
expected: []string{"a", "b", "cde"},
},
{
input: []string{"cde", "a", "b"},
expected: []string{"a", "b", "cde"},
},
{
input: []string{"a", "a", "b", "cde"},
expected: []string{"a", "b", "cde"},
},
{
input: []string{"a", "b", "b", "a", "cde", "a", "cde", "b"},
expected: []string{"a", "b", "cde"},
},
{
input: []string{"a", "a", "b", "b", "b", "cde", "cde"},
expected: []string{"a", "b", "cde"},
},
}
// Execute test cases
for index, test := range testCases {
expected := test.expected
got := lib.MakeUniqueSort(test.input)
if (len(got) > 0 || len(expected) > 0) && !reflect.DeepEqual(got, expected) {
t.Errorf(
"test number %d, expected '%v', got '%v', test case: %+v",
index+1, expected, got, test,
)
}
}
}
func TestExcludedForProject(t *testing.T) {
var testCases = []struct {
currentProject string
metricProject string
expected bool
}{
{
currentProject: "",
metricProject: "",
expected: false,
},
{
currentProject: "X",
metricProject: "",
expected: false,
},
{
currentProject: "",
metricProject: "X",
expected: false,
},
{
currentProject: "X",
metricProject: "X",
expected: false,
},
{
currentProject: "X",
metricProject: "!X",
expected: true,
},
{
currentProject: "Y",
metricProject: "X",
expected: true,
},
{
currentProject: "Y",
metricProject: "!X",
expected: false,
},
{
currentProject: "",
metricProject: "!X",
expected: false,
},
{
currentProject: "",
metricProject: "!",
expected: false,
},
}
for index, test := range testCases {
expected := test.expected
got := lib.ExcludedForProject(test.currentProject, test.metricProject)
if got != expected {
t.Errorf(
"test number %d, expected '%v', got '%v', test case: %+v",
index+1, expected, got, test,
)
}
}
}
func TestIsProjectDisabled(t *testing.T) {
var ctx lib.Ctx
var testCases = []struct {
overrides map[string]bool
proj string
yamlDisabled bool
expected bool
}{
{
proj: "pro1",
overrides: map[string]bool{},
yamlDisabled: false,
expected: false,
},
{
proj: "pro1",
overrides: map[string]bool{},
yamlDisabled: true,
expected: true,
},
{
proj: "pro1",
overrides: map[string]bool{"pro1": true},
yamlDisabled: true,
expected: false,
},
{
proj: "pro1",
overrides: map[string]bool{"pro1": false},
yamlDisabled: true,
expected: true,
},
{
proj: "pro1",
overrides: map[string]bool{"pro1": true},
yamlDisabled: false,
expected: false,
},
{
proj: "pro1",
overrides: map[string]bool{"pro1": false},
yamlDisabled: false,
expected: true,
},
}
// Execute test cases
for index, test := range testCases {
expected := test.expected
ctx.ProjectsOverride = test.overrides
got := lib.IsProjectDisabled(&ctx, test.proj, test.yamlDisabled)
if got != expected {
t.Errorf(
"test number %d, expected '%v', got '%v', test case: %+v",
index+1, expected, got, test,
)
}
}
}
func TestActorHit(t *testing.T) {
// Variables
var (
ctx lib.Ctx
nilRegexp *regexp.Regexp
)
// Test cases
var testCases = []struct {
actorsFilter bool
actorsAllow *regexp.Regexp
actorsForbid *regexp.Regexp
actorName string
hit bool
}{
{
actorsFilter: false,
actorsAllow: nilRegexp,
actorsForbid: nilRegexp,
actorName: "actor",
hit: true,
},
{
actorsFilter: false,
actorsAllow: regexp.MustCompile(`^a`),
actorsForbid: regexp.MustCompile(`z$`),
actorName: "actor",
hit: true,
},
{
actorsFilter: true,
actorsAllow: nilRegexp,
actorsForbid: nilRegexp,
actorName: "",
hit: true,
},
{
actorsFilter: true,
actorsAllow: nilRegexp,
actorsForbid: nilRegexp,
actorName: "arbuz",
hit: true,
},
{
actorsFilter: true,
actorsAllow: regexp.MustCompile(`^a`),
actorsForbid: nilRegexp,
actorName: "arbuz",
hit: true,
},
{
actorsFilter: true,
actorsAllow: regexp.MustCompile(`^a`),
actorsForbid: nilRegexp,
actorName: "rbuz",
hit: false,
},
{
actorsFilter: true,
actorsAllow: nilRegexp,
actorsForbid: regexp.MustCompile(`z$`),
actorName: "arbuz",
hit: false,
},
{
actorsFilter: true,
actorsAllow: nilRegexp,
actorsForbid: regexp.MustCompile(`z$`),
actorName: "arbu",
hit: true,
},
{
actorsFilter: true,
actorsAllow: regexp.MustCompile(`^a`),
actorsForbid: regexp.MustCompile(`z$`),
actorName: "arbuz",
hit: false,
},
{
actorsFilter: true,
actorsAllow: regexp.MustCompile(`^a`),
actorsForbid: regexp.MustCompile(`z$`),
actorName: "rbuz",
hit: false,
},
{
actorsFilter: true,
actorsAllow: regexp.MustCompile(`^a`),
actorsForbid: regexp.MustCompile(`z$`),
actorName: "arbu",
hit: true,
},
{
actorsFilter: true,
actorsAllow: regexp.MustCompile(`^a`),
actorsForbid: regexp.MustCompile(`z$`),
actorName: "rbu",
hit: false,
},
}
// Execute test cases
for index, test := range testCases {
expected := test.hit
ctx.ActorsFilter = test.actorsFilter
ctx.ActorsAllow = test.actorsAllow
ctx.ActorsForbid = test.actorsForbid
got := lib.ActorHit(&ctx, test.actorName)
if got != expected {
t.Errorf(
"test number %d, expected '%v', got '%v', test case: %+v",
index+1, expected, got, test,
)
}
}
}
func TestRepoHit(t *testing.T) {
// Test cases
var ctx lib.Ctx
var testCases = []struct {
excludes map[string]bool
exact bool
fullName string
forg map[string]struct{}
frepo map[string]struct{}
orgRE *regexp.Regexp
repoRE *regexp.Regexp
hit bool
}{
{
exact: true,
fullName: "abc/def",
forg: map[string]struct{}{"a/b": {}, "abc/def": {}, "x/y/z": {}},
hit: true,
},
{
exact: true,
fullName: "a/b",
forg: map[string]struct{}{"a/b": {}, "abc/def": {}, "x/y/z": {}},
hit: true,
},
{
fullName: "abc/def",
forg: map[string]struct{}{"a/b": {}, "abc/def": {}, "x/y/z": {}},
hit: true,
},
{
fullName: "abc/def",
forg: map[string]struct{}{"abc": {}},
frepo: map[string]struct{}{"def": {}},
hit: true,
},
{
fullName: "",
forg: map[string]struct{}{"abc": {}},
frepo: map[string]struct{}{"def": {}},
},
{
fullName: "abc",
forg: map[string]struct{}{"abc": {}},
},
{
fullName: "abc",
frepo: map[string]struct{}{"abc": {}},
hit: true,
},
{
fullName: "abcd",
forg: map[string]struct{}{"abc": {}},
},
{
fullName: "abcd",
frepo: map[string]struct{}{"abc": {}},
},
{
fullName: "abc",
forg: map[string]struct{}{"abcd": {}},
},
{
fullName: "abc",
frepo: map[string]struct{}{"abcd": {}},
},
{
fullName: "abc/def",
forg: map[string]struct{}{"abc": {}},
frepo: map[string]struct{}{"def": {}},
hit: true,
},
{
fullName: "abc/def",
forg: map[string]struct{}{"abc": {}},
hit: true,
},
{
fullName: "abc/def",
frepo: map[string]struct{}{"def": {}},
hit: true,
},
{
fullName: "abc/def",
hit: true,
},
{
fullName: "abc/xyz",
forg: map[string]struct{}{"abc": {}, "def/ghi": {}, "j/l": {}},
hit: true,
},
{
fullName: "abc/ghi",
forg: map[string]struct{}{"abc": {}, "def/ghi": {}, "j/l": {}},
hit: true,
},
{
fullName: "j/l",
forg: map[string]struct{}{"abc": {}, "def/ghi": {}, "j/l": {}},
hit: true,
},
{
fullName: "j/l",
forg: map[string]struct{}{"abc": {}, "def/ghi": {}, "j/l": {}},
frepo: map[string]struct{}{"l": {}, "klm": {}},
hit: true,
},
{
fullName: "def/ghi",
forg: map[string]struct{}{"abc": {}, "def/ghi": {}, "j/l": {}},
frepo: map[string]struct{}{"l": {}, "klm": {}},
hit: true,
},
{
fullName: "abc",
forg: map[string]struct{}{"abc": {}, "def/ghi": {}, "j/l": {}},
frepo: map[string]struct{}{"l": {}, "klm": {}},
},
{
exact: true,
fullName: "abc",
forg: map[string]struct{}{"abc": {}, "def/ghi": {}, "j/l": {}},
frepo: map[string]struct{}{"l": {}, "klm": {}},
hit: true,
},
{
exact: true,
fullName: "j/l",
forg: map[string]struct{}{"abc": {}, "def/ghi": {}, "j/l": {}},
frepo: map[string]struct{}{"l": {}, "klm": {}},
hit: true,
},
{
fullName: "abc/def",
forg: map[string]struct{}{"abc": {}},
frepo: map[string]struct{}{"def": {}},
excludes: map[string]bool{"abc/def": true},
hit: false,
},
{
fullName: "abc/def",
forg: map[string]struct{}{"abc": {}},
frepo: map[string]struct{}{"def": {}},
excludes: map[string]bool{"abc/ghi": true},
hit: true,
},
{
fullName: "abc/def",
forg: map[string]struct{}{"abc": {}},
frepo: map[string]struct{}{},
excludes: map[string]bool{"abc/def": true},
hit: false,
},
{
fullName: "abc/ghi",
forg: map[string]struct{}{"abc": {}},
excludes: map[string]bool{"abc/def": true},
hit: true,
},
{
exact: true,
fullName: "abc/def",
orgRE: regexp.MustCompile(`^(a\/b|abc\/def|x\/y\/z)$`),
hit: true,
},
{
exact: true,
fullName: "a/b",
orgRE: regexp.MustCompile(`^(a\/b|abc\/def|x\/y\/z)$`),
hit: true,
},
{
fullName: "abc/def",
orgRE: regexp.MustCompile(`^(a\/b|abc\/def|x\/y\/z)$`),
hit: true,
},
{
fullName: "XabcX/XdefX",
orgRE: regexp.MustCompile("abc"),
repoRE: regexp.MustCompile("def"),
hit: true,
},
{
fullName: "XabcX/XdefX",
orgRE: regexp.MustCompile(`^abc`),
repoRE: regexp.MustCompile(`^def`),
},
{
fullName: "abc/def",
orgRE: regexp.MustCompile("^abc$"),
repoRE: regexp.MustCompile("^def$"),
hit: true,
},
{
fullName: "",
orgRE: regexp.MustCompile("abc"),
repoRE: regexp.MustCompile("def"),
},
{
fullName: "abc",
orgRE: regexp.MustCompile("abc"),
},
{
fullName: "abc",
repoRE: regexp.MustCompile("abc"),
hit: true,
},
{
fullName: "abcd",
orgRE: regexp.MustCompile("abc"),
},
{
fullName: "abcd",
repoRE: regexp.MustCompile("abc"),
hit: true,
},
{
fullName: "abcd",
repoRE: regexp.MustCompile("abc$"),
},
{
fullName: "abc",
orgRE: regexp.MustCompile("abcd"),
},
{
fullName: "abc",
repoRE: regexp.MustCompile("abcd"),
},
{
fullName: "abc/def",
orgRE: regexp.MustCompile("^abc$"),
repoRE: regexp.MustCompile("^def$"),
hit: true,
},
{
fullName: "abc/def",
orgRE: regexp.MustCompile("abc"),
hit: true,
},
{
fullName: "abc/def",
repoRE: regexp.MustCompile("def"),
hit: true,
},
{
fullName: "abc/xyz",
orgRE: regexp.MustCompile(`^(abc|def\/ghi|j\/l)$`),
hit: true,
},
{
fullName: "abc/ghi",
orgRE: regexp.MustCompile(`^(abc|def\/ghi|j\/l)$`),
hit: true,
},
{
fullName: "j/l",
orgRE: regexp.MustCompile(`^(abc|def\/ghi|j\/l)$`),
hit: true,
},
{
fullName: "j/l",
orgRE: regexp.MustCompile(`^(abc|def\/ghi|j\/l)$`),
hit: true,
},
{
fullName: "def/ghi",
orgRE: regexp.MustCompile(`^(abc|def\/ghi|j\/l)$`),
hit: true,
},
{
fullName: "abc",
orgRE: regexp.MustCompile(`^(abc|def\/ghi|j\/l)$`),
},
{
exact: true,
fullName: "abc",
orgRE: regexp.MustCompile(`^(abc|def\/ghi|j\/l)$`),
hit: true,
},
{
exact: true,
fullName: "j/l",
orgRE: regexp.MustCompile(`^(abc|def\/ghi|j\/l)$`),
hit: true,
},
{
fullName: "abc/def",
orgRE: regexp.MustCompile("abc"),
repoRE: regexp.MustCompile("def"),
excludes: map[string]bool{"abc/def": true},
hit: false,
},
{
fullName: "abc/def",
orgRE: regexp.MustCompile("abc"),
repoRE: regexp.MustCompile("def"),
excludes: map[string]bool{"abc/ghi": true},
hit: true,
},
{
fullName: "abc/def",
orgRE: regexp.MustCompile("abc"),
excludes: map[string]bool{"abc/def": true},
hit: false,
},
{
fullName: "abc/ghi",
orgRE: regexp.MustCompile("abc"),
excludes: map[string]bool{"abc/def": true},
hit: true,
},
{
fullName: "unknown/some-fluentd-plugin-v2",
repoRE: regexp.MustCompile("fluentd"),
excludes: map[string]bool{"abc/def": true},
hit: true,
},
{
fullName: "Fluentd-Org/some-FLuentd-plugin-v2",
orgRE: regexp.MustCompile("(?i)fluentd"),
repoRE: regexp.MustCompile("(?i)fluentd"),
excludes: map[string]bool{"abc/def": true},
hit: true,
},
{
fullName: "fluent-plugins-nursery/fluent-plugin-cloudwatch-logs",
orgRE: regexp.MustCompile(`^(fluent|fluent-plugins-nursery\/.*fluent.*|.+\/fluentd?-plugin-.+)$`),
excludes: map[string]bool{"fluent-plugins-nursery/this-fluent-is-excluded": true},
hit: true,
},
{
fullName: "fluent-plugins-nursery/api",
orgRE: regexp.MustCompile(`^(fluent|fluent-plugins-nursery\/.*fluent.*|.+\/fluentd?-plugin-.+)$`),
excludes: map[string]bool{"fluent-plugins-nursery/this-fluent-is-excluded": true},
},
{
fullName: "fluent-plugins-nursery/this-fluent-is-excluded",
orgRE: regexp.MustCompile(`^(fluent|fluent-plugins-nursery\/.*fluent.*|.+\/fluentd?-plugin-.+)$`),
excludes: map[string]bool{"fluent-plugins-nursery/this-fluent-is-excluded": true},
},
{
fullName: "fluent/client",
orgRE: regexp.MustCompile(`^(fluent|fluent-plugins-nursery\/.*fluent.*|.+\/fluentd?-plugin-.+)$`),
excludes: map[string]bool{"fluent-plugins-nursery/this-fluent-is-excluded": true},
hit: true,
},
{
fullName: "fluent-plugins-nursery/excluded",
orgRE: regexp.MustCompile(`^(fluent|fluent-plugins-nursery\/.*fluent.*|.+\/fluentd?-plugin-.+)$`),
excludes: map[string]bool{"fluent-plugins-nursery/excluded": true},
},
{
fullName: "excluded/fluent-plugin-a",
orgRE: regexp.MustCompile(`^(fluent|fluent-plugins-nursery\/.*fluent.*|.+\/fluentd?-plugin-.+)$`),
excludes: map[string]bool{"excluded/fluent-plugin-a": true, "excluded2/fluentd-plugin-b": true},
},
{
fullName: "excluded2/fluentd-plugin-b",
orgRE: regexp.MustCompile(`^(fluent|fluent-plugins-nursery\/.*fluent.*|.+\/fluentd?-plugin-.+)$`),
excludes: map[string]bool{"excluded/fluent-plugin-a": true, "excluded2/fluentd-plugin-b": true},
},
{
fullName: "any-org/fluent-plugin-",
orgRE: regexp.MustCompile(`^(fluent|fluent-plugins-nursery\/.*fluent.*|.+\/fluentd?-plugin-.+)$`),
excludes: map[string]bool{"excluded/fluent-plugin-a": true, "excluded2/fluentd-plugin-b": true},
},
{
fullName: "any-org/fluentd-plugin-",
orgRE: regexp.MustCompile(`^(fluent|fluent-plugins-nursery\/.*fluent.*|.+\/fluentd?-plugin-.+)$`),
excludes: map[string]bool{"excluded/fluent-plugin-a": true, "excluded2/fluentd-plugin-b": true},
},
{
fullName: "any-org/fluentd-plugin-x",
orgRE: regexp.MustCompile(`^(fluent|fluent-plugins-nursery\/.*fluent.*|.+\/fluentd?-plugin-.+)$`),
excludes: map[string]bool{"excluded/fluent-plugin-a": true, "excluded2/fluentd-plugin-b": true},
hit: true,
},
{
fullName: "x/a-fluentd-plugin-x",
orgRE: regexp.MustCompile(`^(fluent|fluent-plugins-nursery\/.*fluent.*|.+\/fluentd?-plugin-.+)$`),
excludes: map[string]bool{"excluded/fluent-plugin-a": true, "excluded2/fluentd-plugin-b": true},
},
{
fullName: "WallyNegima/scenario-manager-plugin",
orgRE: regexp.MustCompile(`(?i)^(fluent|fluent-plugins-nursery\/.*fluent.*|.+\/fluentd?-plugin-.+|wallynegima\/scenario-manager-plugin)$`),
hit: true,
},
{
fullName: "WallyNegima/scenario-manager-plugin",
orgRE: regexp.MustCompile(`(?i)^(fluent|fluent-plugins-nursery\/.*fluent.*|.+\/fluentd?-plugin-.+|baritolog\/barito-fluent-plugin|blacknight95\/aws-fluent-plugin-kinesis|sumologic\/fluentd-kubernetes-sumologic|sumologic\/fluentd-output-sumologic|wallynegima\/scenario-manager-plugin|aliyun\/aliyun-odps-fluentd-plugin|awslabs\/aws-fluent-plugin-kinesis|campanja\/fluent-output-router|grafana\/loki\/|jdoconnor\/fluentd_https_out|newrelic\/newrelic-fluentd-output|roma42427\/filter_wms_auth|scalyr\/scalyr-fluentd|sebryu\/fluent_plugin_in_websocket|tagomoris\/fluent-helper-plugin-spec|y-ken\/fluent-mixin-rewrite-tag-name|y-ken\/fluent-mixin-type-converter)$`),
hit: true,
},
}
// Execute test cases
for index, test := range testCases {
expected := test.hit
ctx.ExcludeRepos = test.excludes
ctx.Exact = test.exact
got := lib.RepoHit(&ctx, test.fullName, test.forg, test.frepo, test.orgRE, test.repoRE)
if got != expected {
t.Errorf(
"test number %d, expected '%v', got '%v', test case: %+v",
index+1, expected, got, test,
)
}
}
}
func TestOrgIDOrNil(t *testing.T) {
result := lib.OrgIDOrNil(nil)
if result != nil {
t.Errorf("test nil case: expected <nil>, got %v", result)
}
result = lib.OrgIDOrNil(&lib.Org{ID: 2})
if result != 2 {
t.Errorf("test ID=2 case: expected 2, got %v", result)
}
}
func TestRepoIDOrNil(t *testing.T) {
result := lib.RepoIDOrNil(nil)
if result != nil {
t.Errorf("test nil case: expected <nil>, got %v", result)
}
result = lib.RepoIDOrNil(&lib.Repo{ID: 2})
if result != 2 {
t.Errorf("test ID=2 case: expected 2, got %v", result)
}
}
func TestRepoNameOrNil(t *testing.T) {
result := lib.RepoNameOrNil(nil)
if result != nil {
t.Errorf("test nil case: expected <nil>, got %v", result)
}
expected := "kubernetes"
result = lib.RepoNameOrNil(&lib.Repo{Name: expected})
if result != expected {
t.Errorf("test Name=%s case: expected %s, got %v", expected, expected, result)
}
}
func TestIssueIDOrNil(t *testing.T) {
result := lib.IssueIDOrNil(nil)
if result != nil {
t.Errorf("test nil case: expected <nil>, got %v", result)
}
result = lib.IssueIDOrNil(&lib.Issue{ID: 2})
if result != 2 {
t.Errorf("test ID=2 case: expected 2, got %v", result)
}
}
func TestPullRequestIDOrNil(t *testing.T) {
result := lib.PullRequestIDOrNil(nil)
if result != nil {
t.Errorf("test nil case: expected <nil>, got %v", result)
}
result = lib.PullRequestIDOrNil(&lib.PullRequest{ID: 2})
if result != 2 {
t.Errorf("test ID=2 case: expected 2, got %v", result)
}
}
func TestCommentIDOrNil(t *testing.T) {
result := lib.CommentIDOrNil(nil)
if result != nil {
t.Errorf("test nil case: expected <nil>, got %v", result)
}
result = lib.CommentIDOrNil(&lib.Comment{ID: 2})
if result != 2 {
t.Errorf("test ID=2 case: expected 2, got %v", result)
}
}
func TestForkeeIDOrNil(t *testing.T) {
result := lib.ForkeeIDOrNil(nil)
if result != nil {
t.Errorf("test nil case: expected <nil>, got %v", result)
}
result = lib.ForkeeIDOrNil(&lib.Forkee{ID: 2})
if result != 2 {
t.Errorf("test ID=2 case: expected 2, got %v", result)
}
}
func TestForkeeOldIDOrNil(t *testing.T) {
result := lib.ForkeeOldIDOrNil(nil)
if result != nil {
t.Errorf("test nil case: expected <nil>, got %v", result)
}
result = lib.ForkeeOldIDOrNil(&lib.ForkeeOld{ID: 2})
if result != 2 {
t.Errorf("test ID=2 case: expected 2, got %v", result)
}
}
func TestForkeeNameOrNil(t *testing.T) {
result := lib.ForkeeNameOrNil(nil)
if result != nil {
t.Errorf("test nil case: expected <nil>, got %v", result)
}
expected := "kubernetes"
result = lib.ForkeeNameOrNil(&lib.Forkee{Name: expected})
if result != expected {
t.Errorf("test Name=%s case: expected %s, got %v", expected, expected, result)
}
}
func TestActorIDOrNil(t *testing.T) {
result := lib.ActorIDOrNil(nil)
if result != nil {
t.Errorf("test nil case: expected <nil>, got %v", result)
}
result = lib.ActorIDOrNil(&lib.Actor{ID: 2})
if result != 2 {
t.Errorf("test ID=2 case: expected 2, got %v", result)
}
}
func TestActorLoginOrNil(t *testing.T) {
result := lib.ActorLoginOrNil(nil, func(a string) string { return a })
if result != nil {
t.Errorf("test nil case: expected <nil>, got %v", result)
}
expected := "lukaszgryglicki"
result = lib.ActorLoginOrNil(&lib.Actor{Login: expected}, func(a string) string { return a })
if result != expected {
t.Errorf("test Login=%s case: expected %s, got %v", expected, expected, result)
}
login := "forbidden"
expected = "anon-1"
result = lib.ActorLoginOrNil(&lib.Actor{Login: login}, func(a string) string { return "anon-1" })
if result != expected {
t.Errorf("test Login=%s case: expected %s, got %v", login, expected, result)
}
}
func TestReleaseIDOrNil(t *testing.T) {
result := lib.ReleaseIDOrNil(nil)
if result != nil {
t.Errorf("test nil case: expected <nil>, got %v", result)
}
result = lib.ReleaseIDOrNil(&lib.Release{ID: 2})
if result != 2 {
t.Errorf("test ID=2 case: expected 2, got %v", result)
}
}
func TestMilestoneIDOrNil(t *testing.T) {
result := lib.MilestoneIDOrNil(nil)
if result != nil {
t.Errorf("test nil case: expected <nil>, got %v", result)
}
result = lib.MilestoneIDOrNil(&lib.Milestone{ID: 2})
if result != 2 {
t.Errorf("test ID=2 case: expected 2, got %v", result)
}
}
func TestCompareStringPtr(t *testing.T) {
s1 := "string1"
s2 := "string2"
s3 := "string1"
result := lib.CompareStringPtr(nil, nil)
if result != true {
t.Errorf("test nil, nil case: expected true, got %v", result)
}
result = lib.CompareStringPtr(nil, &s1)
if result != false {
t.Errorf("test nil, &s1 case: expected false, got %v", result)
}
result = lib.CompareStringPtr(&s2, nil)
if result != false {
t.Errorf("test &s2, nil case: expected false, got %v", result)
}
result = lib.CompareStringPtr(&s1, &s2)
if result != false {
t.Errorf("test &s1, &s2 case: expected false, got %v", result)
}
result = lib.CompareStringPtr(&s1, &s1)
if result != true {
t.Errorf("test &s1, &s1 case: expected true, got %v", result)
}
result = lib.CompareStringPtr(&s1, &s3)
if result != true {
t.Errorf("test &s1, &s1 case: expected true, got %v", result)
}
}
func TestCompareIntPtr(t *testing.T) {
i1 := 1
i2 := 2
i3 := 1
result := lib.CompareIntPtr(nil, nil)
if result != true {
t.Errorf("test nil, nil case: expected true, got %v", result)
}
result = lib.CompareIntPtr(nil, &i1)
if result != false {
t.Errorf("test nil, &i1 case: expected false, got %v", result)
}
result = lib.CompareIntPtr(&i2, nil)
if result != false {
t.Errorf("test &i2, nil case: expected false, got %v", result)
}
result = lib.CompareIntPtr(&i1, &i2)
if result != false {
t.Errorf("test &i1, &i2 case: expected false, got %v", result)
}
result = lib.CompareIntPtr(&i1, &i1)
if result != true {
t.Errorf("test &i1, &i1 case: expected true, got %v", result)
}
result = lib.CompareIntPtr(&i1, &i3)
if result != true {
t.Errorf("test &i1, &i1 case: expected true, got %v", result)
}
}
func TestCompareFloat64Ptr(t *testing.T) {
f1 := 1.1
f2 := 1.2
f3 := 1.1
f4 := 1.10000000001
result := lib.CompareFloat64Ptr(nil, nil)
if result != true {
t.Errorf("test nil, nil case: expected true, got %v", result)
}
result = lib.CompareFloat64Ptr(nil, &f1)
if result != false {
t.Errorf("test nil, &f1 case: expected false, got %v", result)
}
result = lib.CompareFloat64Ptr(&f2, nil)
if result != false {
t.Errorf("test &f2, nil case: expected false, got %v", result)
}
result = lib.CompareFloat64Ptr(&f1, &f2)
if result != false {
t.Errorf("test &f1, &f2 case: expected false, got %v", result)
}
result = lib.CompareFloat64Ptr(&f1, &f1)
if result != true {
t.Errorf("test &f1, &f1 case: expected true, got %v", result)
}
result = lib.CompareFloat64Ptr(&f1, &f3)
if result != true {
t.Errorf("test &f1, &f1 case: expected true, got %v", result)
}
result = lib.CompareFloat64Ptr(&f1, &f4)
if result != true {
t.Errorf("test &f1, &f4 case: expected true, got %v", result)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/cncf/devstatscode.git
git@gitee.com:cncf/devstatscode.git
cncf
devstatscode
devstatscode
master

搜索帮助