From 8e2437ee7da233530a2f403f0135c8ce1c871f4e Mon Sep 17 00:00:00 2001 From: nicknickel Date: Wed, 1 Mar 2023 21:04:44 +0000 Subject: [PATCH] move print note line to separate function and add tests --- pkg/cli/cmd/ls/ls.go | 29 +++++++++++++++++------------ pkg/cli/main_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/pkg/cli/cmd/ls/ls.go b/pkg/cli/cmd/ls/ls.go index 265d4516..84332fe5 100644 --- a/pkg/cli/cmd/ls/ls.go +++ b/pkg/cli/cmd/ls/ls.go @@ -168,6 +168,22 @@ func printBooks(ctx context.DnoteCtx, nameOnly bool) error { return nil } +func printNoteLine(info noteInfo, showTimestamp bool) { + body, isExcerpt := formatBody(info.Body) + + rowid := log.ColorYellow.Sprintf("(%d)", info.RowID) + preface := "" + if showTimestamp { + local_time := time.Unix(0, info.AddedOn).Format(time.DateTime) + preface = log.ColorYellow.Sprintf(" [%s]", local_time) + } + if isExcerpt { + body = fmt.Sprintf("%s %s", body, log.ColorYellow.Sprintf("[---More---]")) + } + + log.Plainf("%s%s %s\n", rowid, preface, body) +} + func printNotes(ctx context.DnoteCtx, bookName string, timestamps bool) error { db := ctx.DB @@ -199,18 +215,7 @@ func printNotes(ctx context.DnoteCtx, bookName string, timestamps bool) error { log.Infof("on book %s\n", bookName) for _, info := range infos { - body, isExcerpt := formatBody(info.Body) - - preface := log.ColorYellow.Sprintf("(%d)", info.RowID) - if timestamps { - local_time := time.Unix(0, info.AddedOn).Format(time.DateTime) - preface = log.ColorYellow.Sprintf("(%s)", local_time) - } - if isExcerpt { - body = fmt.Sprintf("%s %s", body, log.ColorYellow.Sprintf("[---More---]")) - } - - log.Plainf("%s %s\n", preface, body) + printNoteLine(info, timestamps) } return nil diff --git a/pkg/cli/main_test.go b/pkg/cli/main_test.go index 4f68e9b3..21382329 100644 --- a/pkg/cli/main_test.go +++ b/pkg/cli/main_test.go @@ -105,6 +105,39 @@ func TestInit(t *testing.T) { assert.NotEqual(t, lastSyncAt, "", "last sync at should not be empty") } +func TestViewNote(t *testing.T) { + // tests successful if command does not return an error + t.Run("using default", func(t *testing.T) { + // Setup + db := database.InitTestDB(t, fmt.Sprintf("%s/%s/%s", testDir, consts.DnoteDirName, consts.DnoteDBFileName), nil) + testutils.Setup3(t, db) + + // Execute + testutils.RunDnoteCmd(t, opts, binaryName, "view", "js") + defer testutils.RemoveDir(t, testDir) + }) + + t.Run("content only", func(t *testing.T) { + // Setup + db := database.InitTestDB(t, fmt.Sprintf("%s/%s/%s", testDir, consts.DnoteDirName, consts.DnoteDBFileName), nil) + testutils.Setup3(t, db) + + // Execute + testutils.RunDnoteCmd(t, opts, binaryName, "view", "js", "--content-only") + defer testutils.RemoveDir(t, testDir) + }) + + t.Run("with timestamps", func(t *testing.T) { + // Setup + db := database.InitTestDB(t, fmt.Sprintf("%s/%s/%s", testDir, consts.DnoteDirName, consts.DnoteDBFileName), nil) + testutils.Setup3(t, db) + + // Execute + testutils.RunDnoteCmd(t, opts, binaryName, "view", "js", "-t") + defer testutils.RemoveDir(t, testDir) + }) +} + func TestAddNote(t *testing.T) { t.Run("new book", func(t *testing.T) { // Set up and execute