github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/matchers/event/event_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 event 18 19 import ( 20 "testing" 21 "time" 22 23 "github.com/google/go-github/github" 24 ) 25 26 func getDate(year int, month time.Month, day, hour, min, sec int) *time.Time { 27 date := time.Date(year, month, day, hour, min, sec, 0, time.UTC) 28 return &date 29 } 30 31 func TestCreationBefore(t *testing.T) { 32 if CreatedBefore( 33 time.Date(2000, 1, 1, 12, 0, 0, 0, time.UTC), 34 ).Match(nil) { 35 t.Error("Shouldn't match nil event") 36 } 37 if CreatedBefore( 38 time.Date(2000, 1, 1, 12, 0, 0, 0, time.UTC), 39 ).Match(&github.IssueEvent{}) { 40 t.Error("Shouldn't match nil CreatedAt") 41 } 42 if CreatedBefore( 43 time.Date(2000, 1, 1, 12, 0, 0, 0, time.UTC), 44 ).Match(&github.IssueEvent{ 45 CreatedAt: getDate(2000, 1, 1, 12, 0, 1), 46 }) { 47 t.Error("Shouldn't match later event") 48 } 49 if !CreatedBefore( 50 time.Date(2000, 1, 1, 12, 0, 0, 0, time.UTC), 51 ).Match(&github.IssueEvent{ 52 CreatedAt: getDate(2000, 1, 1, 11, 0, 0), 53 }) { 54 t.Error("Should match earlier event") 55 } 56 } 57 58 func TestCreationAfter(t *testing.T) { 59 if CreatedAfter( 60 time.Date(2000, 1, 1, 12, 0, 0, 0, time.UTC), 61 ).Match(nil) { 62 t.Error("Shouldn't match nil event") 63 } 64 if CreatedAfter( 65 time.Date(2000, 1, 1, 12, 0, 0, 0, time.UTC), 66 ).Match(&github.IssueEvent{}) { 67 t.Error("Shouldn't match nil CreatedAt") 68 } 69 if !CreatedAfter( 70 time.Date(2000, 1, 1, 12, 0, 0, 0, time.UTC), 71 ).Match(&github.IssueEvent{ 72 CreatedAt: getDate(2000, 1, 1, 12, 0, 1), 73 }) { 74 t.Error("Should match later event") 75 } 76 if CreatedAfter( 77 time.Date(2000, 1, 1, 12, 0, 0, 0, time.UTC), 78 ).Match(&github.IssueEvent{ 79 CreatedAt: getDate(2000, 1, 1, 11, 0, 0), 80 }) { 81 t.Error("Shouldn't match earlier event") 82 } 83 } 84 85 func makeEventWithActor(actor string) *github.IssueEvent { 86 return &github.IssueEvent{ 87 Actor: &github.User{Login: &actor}, 88 } 89 } 90 91 func TestActor(t *testing.T) { 92 if Actor("actor").Match(nil) { 93 t.Error("Shouldn't match nil event") 94 } 95 if Actor("actor").Match(&github.IssueEvent{}) { 96 t.Error("Shouldn't match nil Actor") 97 } 98 if Actor("actor").Match(&github.IssueEvent{Actor: &github.User{}}) { 99 t.Error("Shouldn't match nil Login") 100 } 101 if Actor("actor").Match(makeEventWithActor("user")) { 102 t.Error("Shouldn't match actor with different names") 103 } 104 if !Actor("actor").Match(makeEventWithActor("actor")) { 105 t.Error("Should match actor with similar name") 106 } 107 if !Actor("actoR").Match(makeEventWithActor("Actor")) { 108 t.Error("Should match actor with similar name, but different case") 109 } 110 }