59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
package formatter
|
|
|
|
import (
|
|
"bytes"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"royalroadupdates/internal/royalroad"
|
|
)
|
|
|
|
func TestPrintTableTruncation(t *testing.T) {
|
|
stories := []royalroad.Story{
|
|
{
|
|
Name: "Short Name",
|
|
DateModified: time.Now(),
|
|
},
|
|
{
|
|
Name: "This is a very long story name that should definitely be truncated if the column width is restricted",
|
|
DateModified: time.Now(),
|
|
},
|
|
{
|
|
Name: "Unicode Story 🌟 Sparkle",
|
|
DateModified: time.Now(),
|
|
},
|
|
}
|
|
|
|
t.Run("Natural width (no terminal)", func(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
PrintTable(&buf, stories)
|
|
out := buf.String()
|
|
|
|
expectedLongName := "This is a very long story name that should definitely be truncated if the column width is restricted"
|
|
if !strings.Contains(out, expectedLongName) {
|
|
t.Errorf("Expected output to contain full long name when not truncated, but it didn't. Output:\n%s", out)
|
|
}
|
|
})
|
|
|
|
t.Run("Check relative time alignment", func(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
PrintTable(&buf, stories)
|
|
out := buf.String()
|
|
|
|
lines := strings.Split(out, "\n")
|
|
if len(lines) < 2 {
|
|
t.Fatalf("Expected at least 2 lines of output, got %d", len(lines))
|
|
}
|
|
separatorLine := lines[1]
|
|
parts := strings.Split(separatorLine, " ")
|
|
if len(parts) < 2 {
|
|
t.Fatalf("Separator line does not contain expected gutter: %s", separatorLine)
|
|
}
|
|
updateDashes := parts[1]
|
|
if len(updateDashes) < len("Last Update") {
|
|
t.Errorf("Update column separator too short: %d < 11", len(updateDashes))
|
|
}
|
|
})
|
|
}
|