html_image_size_test.go

 1package view
 2
 3import (
 4	"os"
 5	"runtime"
 6	"testing"
 7)
 8
 9func TestMaxImageCellHeight(t *testing.T) {
10	old := terminalSize
11	defer func() { terminalSize = old }()
12
13	terminalSize = struct {
14		cols, rows int
15		ok         bool
16	}{cols: 100, rows: 40, ok: true}
17
18	if got := maxImageCellHeight(); got != 32 {
19		t.Fatalf("expected maxImageCellHeight=32 for 40-row terminal, got %d", got)
20	}
21}
22
23func TestMaxImageCellWidth(t *testing.T) {
24	old := terminalSize
25	defer func() { terminalSize = old }()
26
27	terminalSize = struct {
28		cols, rows int
29		ok         bool
30	}{cols: 100, rows: 40, ok: true}
31
32	if got := maxImageCellWidth(); got != 96 {
33		t.Fatalf("expected maxImageCellWidth=96 for 100-col terminal, got %d", got)
34	}
35}
36
37func TestMaxImageCellDefaultsWhenNoTerminal(t *testing.T) {
38	old := terminalSize
39	defer func() { terminalSize = old }()
40
41	terminalSize = struct {
42		cols, rows int
43		ok         bool
44	}{}
45
46	if got := maxImageCellHeight(); got != 25 {
47		t.Fatalf("expected default height 25, got %d", got)
48	}
49	if got := maxImageCellWidth(); got != 80 {
50		t.Fatalf("expected default width 80, got %d", got)
51	}
52}
53
54func TestTerminalSizeFrom(t *testing.T) {
55	if runtime.GOOS == "windows" {
56		t.Skip("TIOCGWINSZ is unavailable on Windows")
57	}
58	// The current process stdin/stdout may or may not be a terminal. We just
59	// verify the helper doesn't panic and returns sensible values when given a
60	// valid file.
61	ts, ok := terminalSizeFrom(os.Stdout)
62	if ok {
63		if ts.cols < 1 || ts.rows < 1 {
64			t.Fatalf("unexpected terminal size: %+v", ts)
65		}
66	}
67}