Skip to content

Commit

Permalink
main_test.go: Use stable test data, fix “military time” test (help #56)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnd-au authored and oz committed Oct 25, 2024
1 parent df2f11e commit b8d3ffc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
52 changes: 36 additions & 16 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
package main

import (
"os"
"regexp"
"strings"
"testing"
"time"

tea "github.com/charmbracelet/bubbletea"
"github.com/muesli/termenv"
"golang.org/x/tools/txtar"
)

var (
Expand All @@ -47,9 +49,6 @@ var (
)

func getTimestampWithHour(hour int) time.Time {
if hour == -1 {
hour = time.Now().Hour()
}
return time.Date(
time.Now().Year(),
time.Now().Month(),
Expand Down Expand Up @@ -98,14 +97,14 @@ func TestUpdateIncHour(t *testing.T) {
}

db := m.clock.Time().Day()
nextState, cmd := m.Update(msg)
_, cmd := m.Update(msg)
da := m.clock.Time().Day()

if cmd != nil {
t.Errorf("Expected nil Cmd, but got %v", cmd)
return
}
h := nextState.(*model).clock.t.Hour()
h := m.clock.t.Hour()
if h != test.nextHour {
t.Errorf("Expected %d, but got %d", test.nextHour, h)
}
Expand Down Expand Up @@ -139,12 +138,12 @@ func TestUpdateDecHour(t *testing.T) {
keymaps: NewDefaultConfig().Keymaps,
clock: *NewClockTime(getTimestampWithHour(test.startHour)),
}
nextState, cmd := m.Update(msg)
_, cmd := m.Update(msg)
if cmd != nil {
t.Errorf("Expected nil Cmd, but got %v", cmd)
return
}
h := nextState.(*model).clock.t.Hour()
h := m.clock.t.Hour()
if h != test.nextHour {
t.Errorf("Expected %d, but got %d", test.nextHour, h)
}
Expand All @@ -162,7 +161,7 @@ func TestUpdateQuitMsg(t *testing.T) {
m := model{
zones: DefaultZones,
keymaps: NewDefaultConfig().Keymaps,
clock: *NewClock(),
clock: *NewClockTime(getTimestampWithHour(10)),
}
_, cmd := m.Update(msg)
if cmd == nil {
Expand All @@ -174,14 +173,35 @@ func TestUpdateQuitMsg(t *testing.T) {
}

func TestMilitaryTime(t *testing.T) {
m := model{
zones: DefaultZones,
clock: *NewClock(),
isMilitary: true,
showDates: true,
testDataFile := "testdata/main/test-military-time.txt"
testData, err := txtar.ParseFile(testDataFile)
if err != nil {
t.Fatal(err)
}

formatted := utcMinuteAfterMidnightTime.Format(" 15:04, Mon Jan 02, 2006")
expected := stripAnsiControlSequencesAndNewline(testData.Files[0].Data)
observed := stripAnsiControlSequences(utcMinuteAfterMidnightModel.View())

archive := txtar.Archive{
Comment: testData.Comment,
Files: []txtar.File{
{
Name: "expected",
Data: []byte(expected),
},
{
Name: "observed",
Data: []byte(observed),
},
},
}
os.WriteFile(testDataFile, txtar.Format(&archive), 0666)

if formatted != expected {
t.Errorf("Expected military time of %s, but got %s", expected, formatted)
}
s := m.View()
if !strings.Contains(s, m.clock.t.Format("15:04")) {
t.Errorf("Expected military time of %s, but got %s", m.clock.t.Format("15:04"), s)
if !strings.Contains(observed, expected) {
t.Errorf("Expected military time of %s, but got %s", expected, observed)
}
}
12 changes: 12 additions & 0 deletions testdata/main/test-military-time.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Military time is (a) 24-hour time (b) padded with leading zeros (c) without an AM/PM suffix.
For example, one minute past midnight is “0001” (neither unpadded 24-hour “0:01” nor 12-hour suffixed “12:01AM”).
However for clarity, we include a colon delimiter between hours and minutes anyway:
-- expected --
00:01, Sun Nov 05, 2017
-- observed --

What time is it?

🕛 UTC 00:01, Sun Nov 05, 2017
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
📆 Sun 05

0 comments on commit b8d3ffc

Please sign in to comment.