github.com/emmahsax/go-git-helper@v0.0.8-0.20240519163017-907b9de0fa52/cmd/codeRequest/codeRequest_test.go (about)

     1  package codeRequest
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  type MockExecutor struct {
     8  	Args    []string
     9  	Command string
    10  	Debug   bool
    11  	Output  []byte
    12  }
    13  
    14  func (me *MockExecutor) Exec(execType string, command string, args ...string) ([]byte, error) {
    15  	me.Command = command
    16  	me.Args = args
    17  	return me.Output, nil
    18  }
    19  
    20  func Test_autogeneratedTitle(t *testing.T) {
    21  	tests := []struct {
    22  		name           string
    23  		executorOutput []byte
    24  		expected       string
    25  	}{
    26  		{
    27  			name:           "regular branch",
    28  			executorOutput: []byte("* feature-branch"),
    29  			expected:       "Feature branch",
    30  		},
    31  		{
    32  			name:           "single word branch",
    33  			executorOutput: []byte("* feature"),
    34  			expected:       "Feature",
    35  		},
    36  		{
    37  			name:           "Jira branch with underscores",
    38  			executorOutput: []byte("* jira_123_something"),
    39  			expected:       "JIRA-123 Something",
    40  		},
    41  		{
    42  			name:           "Jira branch with dashes",
    43  			executorOutput: []byte("* jira-123-something"),
    44  			expected:       "JIRA-123 Something",
    45  		},
    46  		{
    47  			name:           "Jira branch with combo 1",
    48  			executorOutput: []byte("* jira-123_something"),
    49  			expected:       "JIRA-123 Something",
    50  		},
    51  		{
    52  			name:           "Jira branch with combo 2",
    53  			executorOutput: []byte("* jira_123-something-else"),
    54  			expected:       "JIRA-123 Something else",
    55  		},
    56  		{
    57  			name:           "Jira branch with combo 3",
    58  			executorOutput: []byte("* jira-29142"),
    59  			expected:       "JIRA-29142 ",
    60  		},
    61  	}
    62  
    63  	for _, test := range tests {
    64  		executor := &MockExecutor{
    65  			Debug:  true,
    66  			Output: test.executorOutput,
    67  		}
    68  
    69  		cr := newCodeRequest(true, executor)
    70  		resp := cr.autogeneratedTitle()
    71  
    72  		if resp != test.expected {
    73  			t.Fatalf(`expected %v, but got %v`, test.expected, resp)
    74  		}
    75  	}
    76  }
    77  
    78  func Test_checkAllLetters(t *testing.T) {
    79  	executor := &MockExecutor{Debug: true}
    80  	cr := newCodeRequest(true, executor)
    81  	resp := cr.checkAllLetters("iekslkjasd")
    82  
    83  	if resp == false {
    84  		t.Fatalf(`string %v should be all letters`, resp)
    85  	}
    86  
    87  	resp = cr.checkAllLetters("iekslkjasd321")
    88  
    89  	if resp == true {
    90  		t.Fatalf(`string %v should not be all letters`, resp)
    91  	}
    92  }
    93  
    94  func Test_checkAllNumbers(t *testing.T) {
    95  	executor := &MockExecutor{Debug: true}
    96  	cr := newCodeRequest(true, executor)
    97  	resp := cr.checkAllNumbers("284161")
    98  
    99  	if resp == false {
   100  		t.Fatalf(`string %v should be all numbers`, resp)
   101  	}
   102  
   103  	resp = cr.checkAllNumbers("39812k3jiksd9z")
   104  
   105  	if resp == true {
   106  		t.Fatalf(`string %v should not be all numbers`, resp)
   107  	}
   108  }
   109  
   110  func Test_matchesFullJiraPattern(t *testing.T) {
   111  	executor := &MockExecutor{Debug: true}
   112  	cr := newCodeRequest(true, executor)
   113  	resp := cr.matchesFullJiraPattern("jira-29142")
   114  
   115  	if resp == false {
   116  		t.Fatalf(`string %v should match Jira pattern (e.g. jira-123)`, resp)
   117  	}
   118  
   119  	resp = cr.matchesFullJiraPattern("jIra*3291")
   120  
   121  	if resp == true {
   122  		t.Fatalf(`string %v should not match Jira pattern (e.g. jira-123)`, resp)
   123  	}
   124  }
   125  
   126  func Test_titleize(t *testing.T) {
   127  	executor := &MockExecutor{Debug: true}
   128  	cr := newCodeRequest(true, executor)
   129  	resp := cr.titleize("mysTrInG")
   130  
   131  	if resp != "MysTrInG" {
   132  		t.Fatalf(`string %v was not properly titleized`, resp)
   133  	}
   134  }
   135  
   136  func Test_isGitHub(t *testing.T) {
   137  	tests := []struct {
   138  		remotes  string
   139  		expected bool
   140  	}{
   141  		{
   142  			remotes: `origin  git@github.com:emmahsax/go-git-helper.git (fetch)
   143  origin  git@github.com:emmahsax/go-git-helper.git (push)`,
   144  			expected: true,
   145  		},
   146  		{
   147  			remotes: `origin  git@gitlab.com:emmahsax/github-project.git (fetch)
   148  origin  git@gitlab.com:emmahsax/github-project.git (push)`,
   149  			expected: false,
   150  		},
   151  	}
   152  
   153  	for _, test := range tests {
   154  		executor := &MockExecutor{
   155  			Debug:  true,
   156  			Output: []byte(test.remotes),
   157  		}
   158  		cr := newCodeRequest(true, executor)
   159  		resp := cr.isGitHub()
   160  
   161  		if resp != test.expected {
   162  			t.Fatalf(`should have been %v, but was %v`, test.expected, resp)
   163  		}
   164  	}
   165  }
   166  
   167  func Test_isGitLab(t *testing.T) {
   168  	tests := []struct {
   169  		remotes  string
   170  		expected bool
   171  	}{
   172  		{
   173  			remotes: `origin  git@gitlab.com:emmahsax/go-git-helper.git (fetch)
   174  origin  git@gitlab.com:emmahsax/go-git-helper.git (push)`,
   175  			expected: true,
   176  		},
   177  		{
   178  			remotes: `origin  git@github.com:emmahsax/gitlab-project.git (fetch)
   179  origin  git@github.com:emmahsax/gitlab-project.git (push)`,
   180  			expected: false,
   181  		},
   182  	}
   183  
   184  	for _, test := range tests {
   185  		executor := &MockExecutor{
   186  			Debug:  true,
   187  			Output: []byte(test.remotes),
   188  		}
   189  		cr := newCodeRequest(true, executor)
   190  		resp := cr.isGitLab()
   191  
   192  		if resp != test.expected {
   193  			t.Fatalf(`should have been %v, but was %v`, test.expected, resp)
   194  		}
   195  	}
   196  }
   197  
   198  func Test_containsSubstring(t *testing.T) {
   199  	executor := &MockExecutor{Debug: true}
   200  	cr := newCodeRequest(true, executor)
   201  	strs := []string{"string1", "string3", "string18"}
   202  	resp := cr.containsSubstring(strs, "string3")
   203  
   204  	if resp == false {
   205  		t.Fatalf(`string %v should be present in %v`, "string3", strs)
   206  	}
   207  
   208  	resp = cr.containsSubstring(strs, "string2")
   209  
   210  	if resp == true {
   211  		t.Fatalf(`string %v should not be present in %v`, "string2", strs)
   212  	}
   213  }