github.com/Trim21/git-chglog@v0.0.0-20200414013904-db796966b373/commit_extractor_test.go (about)

     1  package chglog
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestCommitExtractor(t *testing.T) {
    10  	assert := assert.New(t)
    11  
    12  	extractor := newCommitExtractor(&Options{
    13  		CommitSortBy:      "Scope",
    14  		CommitGroupBy:     "Type",
    15  		CommitGroupSortBy: "Title",
    16  		CommitGroupTitleMaps: map[string]string{
    17  			"bar": "BAR",
    18  		},
    19  	})
    20  
    21  	fixtures := []*Commit{
    22  		// [0]
    23  		&Commit{
    24  			Type:   "foo",
    25  			Scope:  "c",
    26  			Header: "1",
    27  			Notes:  []*Note{},
    28  		},
    29  		// [1]
    30  		&Commit{
    31  			Type:   "foo",
    32  			Scope:  "b",
    33  			Header: "2",
    34  			Notes: []*Note{
    35  				{"note1-title", "note1-body"},
    36  				{"note2-title", "note2-body"},
    37  			},
    38  		},
    39  		// [2]
    40  		&Commit{
    41  			Type:   "bar",
    42  			Scope:  "d",
    43  			Header: "3",
    44  			Notes: []*Note{
    45  				{"note1-title", "note1-body"},
    46  				{"note3-title", "note3-body"},
    47  			},
    48  		},
    49  		// [3]
    50  		&Commit{
    51  			Type:   "foo",
    52  			Scope:  "a",
    53  			Header: "4",
    54  			Notes: []*Note{
    55  				{"note4-title", "note4-body"},
    56  			},
    57  		},
    58  		// [4]
    59  		&Commit{
    60  			Type:   "",
    61  			Scope:  "",
    62  			Header: "Merge1",
    63  			Notes:  []*Note{},
    64  			Merge: &Merge{
    65  				Ref:    "123",
    66  				Source: "merges/merge1",
    67  			},
    68  		},
    69  		// [5]
    70  		&Commit{
    71  			Type:   "",
    72  			Scope:  "",
    73  			Header: "Revert1",
    74  			Notes:  []*Note{},
    75  			Revert: &Revert{
    76  				Header: "REVERT1",
    77  			},
    78  		},
    79  	}
    80  
    81  	commitGroups, mergeCommits, revertCommits, noteGroups := extractor.Extract(fixtures)
    82  
    83  	assert.Equal([]*CommitGroup{
    84  		&CommitGroup{
    85  			RawTitle: "bar",
    86  			Title:    "BAR",
    87  			Commits: []*Commit{
    88  				fixtures[2],
    89  			},
    90  		},
    91  		&CommitGroup{
    92  			RawTitle: "foo",
    93  			Title:    "Foo",
    94  			Commits: []*Commit{
    95  				fixtures[3],
    96  				fixtures[1],
    97  				fixtures[0],
    98  			},
    99  		},
   100  	}, commitGroups)
   101  
   102  	assert.Equal([]*Commit{
   103  		fixtures[4],
   104  	}, mergeCommits)
   105  
   106  	assert.Equal([]*Commit{
   107  		fixtures[5],
   108  	}, revertCommits)
   109  
   110  	assert.Equal([]*NoteGroup{
   111  		&NoteGroup{
   112  			Title: "note1-title",
   113  			Notes: []*Note{
   114  				fixtures[1].Notes[0],
   115  				fixtures[2].Notes[0],
   116  			},
   117  		},
   118  		&NoteGroup{
   119  			Title: "note2-title",
   120  			Notes: []*Note{
   121  				fixtures[1].Notes[1],
   122  			},
   123  		},
   124  		&NoteGroup{
   125  			Title: "note3-title",
   126  			Notes: []*Note{
   127  				fixtures[2].Notes[1],
   128  			},
   129  		},
   130  		&NoteGroup{
   131  			Title: "note4-title",
   132  			Notes: []*Note{
   133  				fixtures[3].Notes[0],
   134  			},
   135  		},
   136  	}, noteGroups)
   137  }