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