github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/mungerutil/util_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package mungerutil 18 19 import ( 20 "fmt" 21 "reflect" 22 "testing" 23 24 "github.com/google/go-github/github" 25 ) 26 27 func makeUser(name string) *github.User { 28 return &github.User{ 29 Login: &name, 30 } 31 } 32 33 func TestGetUsers(t *testing.T) { 34 if len(GetUsers(nil)) != 0 { 35 t.Error("Nil user doesn't make empty Users") 36 } 37 if len(GetUsers(&github.User{})) != 0 { 38 t.Error("Nil user login doesn't make empty Users") 39 } 40 if len(GetUsers(makeUser("John"), makeUser("John"))) != 1 { 41 t.Error("Duplicate users are not removed") 42 } 43 } 44 45 func TestUserSetHas(t *testing.T) { 46 if GetUsers().Has(makeUser("John")) { 47 t.Error("Empty list found someone ...") 48 } 49 if GetUsers(makeUser("John")).Has(makeUser("Jane")) { 50 t.Error("List has found the wrong person") 51 } 52 if !GetUsers(makeUser("John")).Has(makeUser("John")) { 53 t.Error("Failed to find user from the list") 54 } 55 } 56 57 func TestUserSetMention(t *testing.T) { 58 if !reflect.DeepEqual( 59 GetUsers(makeUser("John"), makeUser("Jane")).Mention(), 60 GetUsers(makeUser("@John"), makeUser("@Jane"))) { 61 t.Error("Failed to mention users") 62 } 63 64 if !reflect.DeepEqual( 65 GetUsers(makeUser("@John")).Mention().List(), 66 GetUsers(makeUser("@John")).List()) { 67 fmt.Println(GetUsers(makeUser("@John")).List()) 68 t.Error("Failed to re-mention users") 69 } 70 } 71 72 func TestUserSetJoin(t *testing.T) { 73 if GetUsers().Join() != "" { 74 t.Error("Empty UserSet doesn't return empty string") 75 } 76 if GetUsers(makeUser("John")).Join() != "John" { 77 t.Error("Single user should be unmodified") 78 } 79 if GetUsers(makeUser("John"), makeUser("Jane")).Join() != "Jane John" { 80 t.Error("UserSet join doesn't join properly") 81 } 82 } 83 84 func TestGetIssueUsers(t *testing.T) { 85 users := GetIssueUsers(&github.Issue{ 86 Assignees: []*github.User{makeUser("Jane")}, 87 Assignee: makeUser("John"), 88 User: makeUser("Bob"), 89 }) 90 91 expectedAssignees := []string{"Jane", "John"} 92 if !reflect.DeepEqual(users.Assignees.List(), expectedAssignees) { 93 t.Errorf("Assignees (%s) doesn't match expected: %s", users.Assignees.List(), expectedAssignees) 94 } 95 96 expectedAuthor := []string{"Bob"} 97 if !reflect.DeepEqual(users.Author.List(), expectedAuthor) { 98 t.Errorf("Author (%s) doesn't match expected: %s", users.Author.List(), expectedAuthor) 99 } 100 } 101 102 func TestIssueUsersAll(t *testing.T) { 103 users := GetIssueUsers(&github.Issue{ 104 Assignees: []*github.User{makeUser("Jane")}, 105 Assignee: makeUser("John"), 106 User: makeUser("Bob"), 107 }) 108 109 expected := []string{"Bob", "Jane", "John"} 110 if !reflect.DeepEqual(users.AllUsers().List(), expected) { 111 t.Errorf("AllUsers (%s) doesn't match expected list: %s", users.AllUsers().List(), expected) 112 } 113 }