github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/matchers/comment/notification_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 comment 18 19 import ( 20 "reflect" 21 "testing" 22 ) 23 24 func TestParseNotification(t *testing.T) { 25 tests := []struct { 26 notif *Notification 27 comment string 28 }{ 29 { 30 notif: nil, 31 comment: "I have nothing to do with a notification", 32 }, 33 { 34 notif: nil, 35 comment: " [NOTIF] Line can't start with space", 36 }, 37 { 38 notif: nil, 39 comment: "[NOTIF SOMETHING] Notif name can't have space", 40 }, 41 { 42 notif: &Notification{Name: "NOTIF"}, 43 comment: "[NOTIF]", 44 }, 45 { 46 notif: nil, 47 comment: "Notif must be at the very beginning:\n[NOTIF]\nAnd something else...", 48 }, 49 { 50 notif: &Notification{Name: "NOTIF", Arguments: "Valid notification"}, 51 comment: "[NOTIF] Valid notification", 52 }, 53 { 54 notif: &Notification{Name: "NOTIF", Arguments: "Multiple Lines"}, 55 comment: "[NOTIF] Multiple Lines \nAnd something else...", 56 }, 57 { 58 notif: &Notification{Name: "NOTIF", Arguments: "Notif name is upper-cased"}, 59 comment: "[notif] Notif name is upper-cased", 60 }, 61 { 62 notif: &Notification{Name: "NOTIF", Arguments: "Arguments is trimmed"}, 63 comment: "[notif] Arguments is trimmed ", 64 }, 65 { 66 notif: &Notification{Name: "NOTIF", Arguments: "Context is read", Context: "What a lovely context"}, 67 comment: "[notif] Context is read \n\n What a lovely context ", 68 }, 69 } 70 71 for _, test := range tests { 72 actualNotif := ParseNotification(&Comment{Body: &test.comment}) 73 if !reflect.DeepEqual(actualNotif, test.notif) { 74 t.Errorf("%#v doesn't match expected notif: %#v", actualNotif, test.notif) 75 } 76 } 77 } 78 79 func TestStringNotification(t *testing.T) { 80 tests := []struct { 81 notif *Notification 82 str string 83 }{ 84 { 85 notif: &Notification{Name: "NOTIF"}, 86 str: "[NOTIF]", 87 }, 88 { 89 notif: &Notification{Name: "NOTIF", Arguments: "Argument"}, 90 str: "[NOTIF] Argument", 91 }, 92 { 93 notif: &Notification{Name: "NOTIF", Arguments: "Argument", Context: "Context"}, 94 str: "[NOTIF] Argument\n\nContext", 95 }, 96 { 97 notif: &Notification{Name: "notif", Arguments: " Argument ", Context: "Context"}, 98 str: "[NOTIF] Argument\n\nContext", 99 }, 100 } 101 102 for _, test := range tests { 103 actualString := test.notif.String() 104 if actualString != test.str { 105 t.Error(actualString, "doesn't match expected string:", test.str) 106 } 107 } 108 }