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