From 07d9ad5531edcbecf1a725c2be06ba8b1980d55c Mon Sep 17 00:00:00 2001 From: Robson Cruz Date: Thu, 9 Nov 2023 20:12:08 -0300 Subject: [PATCH] test(journal): add tests for each component of `String` output --- internal/journal_test.go | 43 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/internal/journal_test.go b/internal/journal_test.go index 5729cba..ee9aedf 100644 --- a/internal/journal_test.go +++ b/internal/journal_test.go @@ -3,6 +3,7 @@ package journal import ( "slices" "strconv" + "strings" "testing" "time" ) @@ -147,6 +148,48 @@ func TestJournalEntryStringer(t *testing.T) { }) } +func TestJournalEntryStringerComponents(t *testing.T) { + startTime := time.Date(2021, time.January, 1, 12, 0, 0, 0, time.UTC) + endTime := time.Date(2021, time.January, 1, 13, 0, 0, 0, time.UTC) + notes := []string{"Note 1", "Note 2"} + + journalEntry := &JournalEntry{ + StartTime: startTime, + EndTime: endTime, + Notes: notes, + } + + expectedHeader := "Date: 2021-01-01" + expectedTime := "Start: 12:00:00 | End: 13:00:00 | Time: 1h0m0s" + expectedNotes := []string{"- Note 1", "- Note 2"} + + result := journalEntry.String() + + // Split the result into lines + lines := strings.Split(strings.TrimSpace(result), "\n") + + t.Run("Date header is properly formatted", func(t *testing.T) { + if lines[0] != expectedHeader { + t.Errorf("Expected header: %s, but got: %s", expectedHeader, lines[0]) + } + }) + + t.Run("Time header is properly formatted", func(t *testing.T) { + if lines[1] != expectedTime { + t.Errorf("Expected time: %s, but got: %s", expectedTime, lines[1]) + } + }) + + t.Run("Notes components is properly formatted", func(t *testing.T) { + for i, note := range expectedNotes { + // Add 3 because the notes start on the 4th line + if lines[i+3] != note { + t.Errorf("Expected note: %s, but got: %s", note, lines[i+3]) + } + } + }) +} + func BenchmarkFetchEntryByID(b *testing.B) { // Setup entries for benchmark entries := make([]JournalEntry, 1e6)