github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/matchers/command_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 TestParseCommand(t *testing.T) {
    27  	tests := []struct {
    28  		expectedCommands []*Command
    29  		comment          string
    30  	}{
    31  		{
    32  			expectedCommands: []*Command{},
    33  			comment:          "I have nothing to do with a command",
    34  		},
    35  		{
    36  			expectedCommands: []*Command{},
    37  			comment:          " /COMMAND Must be at the beginning of the line",
    38  		},
    39  		{
    40  			expectedCommands: []*Command{{Name: "COMMAND"}},
    41  			comment:          "/COMMAND",
    42  		},
    43  		{
    44  			expectedCommands: []*Command{{Name: "COMMAND"}},
    45  			comment:          "/COMMAND\r",
    46  		},
    47  		{
    48  			expectedCommands: []*Command{{Name: "COMMAND", Arguments: "Args after tab"}},
    49  			comment:          "/COMMAND\tArgs after tab",
    50  		},
    51  		{
    52  			expectedCommands: []*Command{{Name: "COMMAND", Arguments: "Removes trailing backslash R"}},
    53  			comment:          "/COMMAND Removes trailing backslash R\r\n",
    54  		},
    55  		{
    56  			expectedCommands: []*Command{{Name: "COMMAND", Arguments: "Valid command"}},
    57  			comment:          "/COMMAND Valid command",
    58  		},
    59  		{
    60  			expectedCommands: []*Command{{Name: "COMMAND", Arguments: "Multiple Lines"}},
    61  			comment:          "/COMMAND Multiple Lines\nAnd something else...",
    62  		},
    63  		{
    64  			expectedCommands: []*Command{
    65  				{Name: "COMMAND", Arguments: "Args"},
    66  				{Name: "OTHERCOMMAND", Arguments: "OtherArgs"},
    67  			},
    68  			comment: "/COMMAND Args\n/OTHERCOMMAND OtherArgs",
    69  		},
    70  		{
    71  			expectedCommands: []*Command{{Name: "COMMAND", Arguments: "Command name is upper-cased"}},
    72  			comment:          "/command Command name is upper-cased",
    73  		},
    74  		{
    75  			expectedCommands: []*Command{{Name: "COMMAND", Arguments: "Arguments is trimmed"}},
    76  			comment:          "/COMMAND     Arguments is trimmed   ",
    77  		},
    78  		{
    79  			expectedCommands: []*Command{{Name: "COMMAND"}},
    80  			comment:          "Command not at the beginning:\n/COMMAND\nAnd something else...",
    81  		},
    82  	}
    83  
    84  	for _, test := range tests {
    85  		actualCommand := ParseCommands(&github.IssueComment{Body: &test.comment})
    86  		if !reflect.DeepEqual(actualCommand, test.expectedCommands) {
    87  			t.Error(actualCommand, "doesn't match expected commands:", test.expectedCommands)
    88  		}
    89  	}
    90  }
    91  
    92  func TestStringCommand(t *testing.T) {
    93  	tests := []struct {
    94  		command        *Command
    95  		expectedString string
    96  	}{
    97  		{
    98  			command:        &Command{Name: "COMMAND", Arguments: "Argument"},
    99  			expectedString: "/COMMAND Argument",
   100  		},
   101  		{
   102  			command:        &Command{Name: "command", Arguments: "  Argument  "},
   103  			expectedString: "/COMMAND Argument",
   104  		},
   105  		{
   106  			command:        &Command{Name: "command"},
   107  			expectedString: "/COMMAND",
   108  		},
   109  	}
   110  
   111  	for _, test := range tests {
   112  		actualString := test.command.String()
   113  		if actualString != test.expectedString {
   114  			t.Error(actualString, "doesn't match expected string:", test.expectedString)
   115  		}
   116  	}
   117  }