github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/matchers/interactions_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  	"regexp"
    21  	"testing"
    22  
    23  	"github.com/google/go-github/github"
    24  )
    25  
    26  func makeCommentWithBody(body string) *github.IssueComment {
    27  	return &github.IssueComment{
    28  		Body: &body,
    29  	}
    30  }
    31  
    32  func TestNotificationName(t *testing.T) {
    33  	if NotificationName("MESSAGE").MatchComment(&github.IssueComment{}) {
    34  		t.Error("Shouldn't match nil body")
    35  	}
    36  	if NotificationName("MESSAGE").MatchComment(makeCommentWithBody("MESSAGE WRONG FORMAT")) {
    37  		t.Error("Shouldn't match invalid match")
    38  	}
    39  	if !NotificationName("MESSAGE").MatchComment(makeCommentWithBody("[MESSAGE] Valid format")) {
    40  		t.Error("Should match valid format")
    41  	}
    42  	if !NotificationName("MESSAGE").MatchComment(makeCommentWithBody("[MESSAGE]")) {
    43  		t.Error("Should match with no arguments")
    44  	}
    45  	if !NotificationName("MESSage").MatchComment(makeCommentWithBody("[meSSAGE]")) {
    46  		t.Error("Should match with different case")
    47  	}
    48  }
    49  
    50  func TestCommandName(t *testing.T) {
    51  	if CommandName("COMMAND").MatchComment(&github.IssueComment{}) {
    52  		t.Error("Shouldn't match nil body")
    53  	}
    54  	if CommandName("COMMAND").MatchComment(makeCommentWithBody("COMMAND WRONG FORMAT")) {
    55  		t.Error("Shouldn't match invalid format")
    56  	}
    57  	if !CommandName("COMMAND").MatchComment(makeCommentWithBody("/COMMAND Valid format")) {
    58  		t.Error("Should match valid format")
    59  	}
    60  	if !CommandName("COMMAND").MatchComment(makeCommentWithBody("/COMMAND")) {
    61  		t.Error("Should match with no arguments")
    62  	}
    63  	if !CommandName("COMmand").MatchComment(makeCommentWithBody("/ComMAND")) {
    64  		t.Error("Should match with different case")
    65  	}
    66  }
    67  
    68  func TestCommandArgmuents(t *testing.T) {
    69  	var testcases = []struct {
    70  		name        string
    71  		re          string
    72  		comment     *github.IssueComment
    73  		shouldMatch bool
    74  	}{
    75  		{
    76  			name:        "shouldn't match nil body",
    77  			re:          ".*",
    78  			comment:     &github.IssueComment{},
    79  			shouldMatch: false,
    80  		},
    81  		{
    82  			name:        "shouldn't match non-command",
    83  			re:          ".*",
    84  			comment:     makeCommentWithBody("COMMAND WRONG FORMAT"),
    85  			shouldMatch: false,
    86  		},
    87  		{
    88  			name:        "should match from the beginning of arguments",
    89  			re:          "^carret",
    90  			comment:     makeCommentWithBody("/command carret is the beginning of argument"),
    91  			shouldMatch: true,
    92  		},
    93  		{
    94  			name:        "shouldn't match command name",
    95  			re:          "command",
    96  			comment:     makeCommentWithBody("/command name is not part of match"),
    97  			shouldMatch: false,
    98  		},
    99  	}
   100  	for _, tc := range testcases {
   101  		ca := CommandArguments(*regexp.MustCompile(tc.re))
   102  		if tc.shouldMatch != ca.MatchComment(tc.comment) {
   103  			t.Error(tc.name)
   104  		}
   105  	}
   106  }