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