From 729cfb5f4941a6a22f82390c072248a04b048524 Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Sun, 17 Nov 2019 14:43:01 -0300 Subject: [PATCH] test: unit tests for releasenotes.go --- sv/helpers_test.go | 35 +++++++++++++++++++++++++++++++++++ sv/releasenotes_test.go | 38 ++++++++++++++++++++++++++++++++++++++ sv/semver_test.go | 14 -------------- 3 files changed, 73 insertions(+), 14 deletions(-) create mode 100644 sv/helpers_test.go create mode 100644 sv/releasenotes_test.go diff --git a/sv/helpers_test.go b/sv/helpers_test.go new file mode 100644 index 0000000..e6d2451 --- /dev/null +++ b/sv/helpers_test.go @@ -0,0 +1,35 @@ +package sv + +import ( + "time" + + "github.com/Masterminds/semver" +) + +func version(v string) semver.Version { + r, _ := semver.NewVersion(v) + return *r +} + +func commitlog(t string, metadata map[string]string) GitCommitLog { + return GitCommitLog{ + Type: t, + Subject: "subject text", + Metadata: metadata, + } +} + +func releaseNote(sections map[string]ReleaseNoteSection, breakingChanges []string) ReleaseNote { + return ReleaseNote{ + Date: time.Now().Truncate(time.Minute), + Sections: sections, + BreakingChanges: breakingChanges, + } +} + +func rnSection(name string, items []GitCommitLog) ReleaseNoteSection { + return ReleaseNoteSection{ + Name: name, + Items: items, + } +} diff --git a/sv/releasenotes_test.go b/sv/releasenotes_test.go new file mode 100644 index 0000000..b264b26 --- /dev/null +++ b/sv/releasenotes_test.go @@ -0,0 +1,38 @@ +package sv + +import ( + "reflect" + "testing" +) + +func TestReleaseNoteProcessorImpl_Get(t *testing.T) { + tests := []struct { + name string + commits []GitCommitLog + want ReleaseNote + }{ + { + name: "mapped tag", + commits: []GitCommitLog{commitlog("t1", map[string]string{})}, + want: releaseNote(map[string]ReleaseNoteSection{"t1": rnSection("Tag 1", []GitCommitLog{commitlog("t1", map[string]string{})})}, nil), + }, + { + name: "unmapped tag", + commits: []GitCommitLog{commitlog("t1", map[string]string{}), commitlog("unmapped", map[string]string{})}, + want: releaseNote(map[string]ReleaseNoteSection{"t1": rnSection("Tag 1", []GitCommitLog{commitlog("t1", map[string]string{})})}, nil), + }, + { + name: "breaking changes tag", + commits: []GitCommitLog{commitlog("t1", map[string]string{}), commitlog("unmapped", map[string]string{"breakingchange": "breaks"})}, + want: releaseNote(map[string]ReleaseNoteSection{"t1": rnSection("Tag 1", []GitCommitLog{commitlog("t1", map[string]string{})})}, []string{"breaks"}), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := NewReleaseNoteProcessor(map[string]string{"t1": "Tag 1", "t2": "Tag 2"}) + if got := p.Get(tt.commits); !reflect.DeepEqual(got, tt.want) { + t.Errorf("ReleaseNoteProcessorImpl.Get() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/sv/semver_test.go b/sv/semver_test.go index cb34610..8943d9b 100644 --- a/sv/semver_test.go +++ b/sv/semver_test.go @@ -57,17 +57,3 @@ func TestToVersion(t *testing.T) { }) } } - -// helpers - -func version(v string) semver.Version { - r, _ := semver.NewVersion(v) - return *r -} - -func commitlog(t string, metadata map[string]string) GitCommitLog { - return GitCommitLog{ - Type: t, - Metadata: metadata, - } -}