Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the month display in case of "line is four" #147

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion calendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down
42 changes: 42 additions & 0 deletions calendar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -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

Expand Down
Loading