Skip to content

Commit

Permalink
Merge pull request #147 from y-yagi/fix-145
Browse files Browse the repository at this point in the history
Fix the month display in case of "line is four"
  • Loading branch information
y-yagi committed Apr 22, 2024
2 parents 4fc4c2e + 98e2c98 commit a75bed8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
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

0 comments on commit a75bed8

Please sign in to comment.