From 98e2c982394d26cebd739202a77f31a5739fa552 Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Sun, 21 Apr 2024 17:02:50 +0900 Subject: [PATCH] Fix the month display in case of "line is four" We need to fill in the 5th line. Fixes #145. --- calendar.go | 6 +++++- calendar_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/calendar.go b/calendar.go index bb5ae71..b469f2a 100644 --- a/calendar.go +++ b/calendar.go @@ -87,7 +87,11 @@ func (calendar *Calendar) Generate(date time.Time) { } wday = int(lastDate.Weekday()) - calendar.Body[line] += strings.Repeat(daySpace, 6-wday) + if wday == 6 && line == 4 { + calendar.Body[line] += strings.Repeat(daySpace, 7) + } else { + calendar.Body[line] += strings.Repeat(daySpace, 6-wday) + } calendar.adjustSpace(line) for line++; line < len(calendar.Body); line++ { diff --git a/calendar_test.go b/calendar_test.go index 32d5876..216ea05 100644 --- a/calendar_test.go +++ b/calendar_test.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "strings" "testing" "time" ) @@ -48,6 +49,47 @@ func TestGenerate(t *testing.T) { } } +func TestGenerate_fitsOnFourLines(t *testing.T) { + var calendar Calendar + date, _ := time.Parse("2006-01-02", "2026-02-01") + calendar.Generate(date) + + expect := " 2026年 02月 " + if calendar.DateHeader != expect { + t.Errorf("Expect calendar.DateHeader is %s, but %s", expect, calendar.DateHeader) + } + + expect = fmt.Sprintf("%s %s %s %s %s %s %s ", red("日"), "月", "火", "水", "木", "金", blue("土")) + if calendar.WeekHeader != expect { + t.Errorf("Expect calendar.WeekHeader is %s, but %s", expect, calendar.WeekHeader) + } + + expect = fmt.Sprintf(" %s %s %s %s %s %s %s ", red("1"), "2", "3", "4", "5", "6", blue("7")) + if calendar.Body[0] != expect { + t.Errorf("Expect calendar.Body[0] is %s, but %s", expect, calendar.Body[0]) + } + + expect = fmt.Sprintf(" %s %s %s %s %s %s %s ", red("8"), "9", "10", red("11"), "12", "13", blue("14")) + if calendar.Body[1] != expect { + t.Errorf("Expect calendar.Body[1] is %s, but %s", expect, calendar.Body[1]) + } + + expect = fmt.Sprintf("%s %s %s %s %s %s %s ", red("15"), "16", "17", "18", "19", "20", blue("21")) + if calendar.Body[2] != expect { + t.Errorf("Expect calendar.Body[2] is %s, but %s", expect, calendar.Body[2]) + } + + expect = fmt.Sprintf("%s %s %s %s %s %s %s ", red("22"), "23", "24", "25", "26", "27", blue("28")) + if calendar.Body[3] != expect { + t.Errorf("Expect calendar.Body[3] is %s, but %s", expect, calendar.Body[3]) + } + + expect = strings.Repeat(" ", 23) + if calendar.Body[4] != expect { + t.Errorf("Expect calendar.Body[4] is %s, but %s", expect, calendar.Body[4]) + } +} + func TestClear(t *testing.T) { var calendar Calendar