github.com/quickfeed/quickfeed@v0.0.0-20240507093252-ed8ca812a09c/web/hooks/pull_request_test.go (about)

     1  package hooks
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/quickfeed/quickfeed/qf"
     7  )
     8  
     9  const (
    10  	moreThanOneHash = "more than one '#' character in pull request body"
    11  	noIssueFound    = "no issue found in pull request body"
    12  )
    13  
    14  func TestFindIssue(t *testing.T) {
    15  	wantIssue := &qf.Issue{ScmIssueNumber: 30}
    16  	issues := []*qf.Issue{
    17  		{ScmIssueNumber: 10},
    18  		{ScmIssueNumber: 20},
    19  		wantIssue,
    20  	}
    21  	tests := map[string]struct {
    22  		body   string
    23  		errStr string
    24  	}{
    25  		"Fixes":            {body: "Fixes #30", errStr: ""},
    26  		"fixes":            {body: "fixes #30", errStr: ""},
    27  		"Closes":           {body: "Closes #30", errStr: ""},
    28  		"closes":           {body: "closes #30", errStr: ""},
    29  		"Resolves":         {body: "Resolves #30", errStr: ""},
    30  		"resolves":         {body: "resolves #30", errStr: ""},
    31  		"Not a number":     {body: "Fixes #30nan", errStr: noIssueFound},
    32  		"Unexpected issue": {body: "Fixes #40", errStr: "unknown issue #40"},
    33  		"Multiple issues":  {body: "Fixes #30 Fixes #20", errStr: moreThanOneHash},
    34  		"Duplicate issues": {body: "Fixes #30 Fixes #30", errStr: moreThanOneHash},
    35  		"Invalid body":     {body: "Fixes #30nan #", errStr: moreThanOneHash},
    36  		"Invalid body 2":   {body: "Fixes", errStr: noIssueFound},
    37  		"Invalid body 3":   {body: "Fixes #", errStr: noIssueFound},
    38  		"Invalid body 4":   {body: "Fixes #30 task-hello_world", errStr: noIssueFound},
    39  	}
    40  	for name, tt := range tests {
    41  		t.Run(name, func(t *testing.T) {
    42  			gotIssue, err := findIssue(tt.body, issues)
    43  			if tt.errStr != "" && err == nil {
    44  				t.Errorf("findIssue() = %v, expected %q", gotIssue, tt.errStr)
    45  				return
    46  			}
    47  			if err != nil {
    48  				if tt.errStr == err.Error() {
    49  					return
    50  				}
    51  				t.Errorf("findIssue() = %q, expected %q", err, tt.errStr)
    52  				return
    53  			}
    54  			if gotIssue != wantIssue {
    55  				t.Errorf("findIssue() = %v, expected %v", gotIssue, wantIssue)
    56  			}
    57  		})
    58  	}
    59  }