cat / 012
clone

a spreadsheet for the terminal: lotus 1-2-3 looks, google sheets keys and formulas, one go binary

cat/module-path
012 / internal / sheet / layoutstyle_test.go
layoutstyle_test.go 3.5K
1package sheet23import (4 "strings"5 "testing"6 "unsafe"7)89// A Cell stays in its allocation size class with a style of twelve10// bytes, which fits in what the flags after it would pad.11func TestCellSize(t *testing.T) {12 if n := unsafe.Sizeof(Cell{}); n > 256 {13 t.Errorf("Cell is %d bytes", n)14 }15 if n := unsafe.Sizeof(Style{}); n > 12 {16 t.Errorf("Style is %d bytes", n)17 }18}1920// Vertical alignment and border colors are saved, undone and cleared21// with the rest of a cell's style.22func TestVAlignAndBorderColors(t *testing.T) {23 s := New()24 s.Set(at("B2"), "x")25 s.SetStyle(rng("B2"), func(st *Style) { st.VAlign = VAlignTop })26 s.SetBorderStroke(rng("B2:C3"), BorderOuter, Stroke{Line: LineThick, Color: ColorRed})27 s.SetBorderStroke(rng("B2:C3"), BorderInner, Stroke{Line: LineThin})28 b := s.CellStyle(at("B2")).Borders29 if b.Stroke(EdgeTop) != (Stroke{LineThick, ColorRed}) || b.Stroke(EdgeBottom) != (Stroke{Line: LineThin}) {30 t.Errorf("B2 borders %x", uint32(b))31 }32 if st := s.StrokeAbove(at("B4")); st != (Stroke{LineThick, ColorRed}) {33 t.Errorf("below the outline %+v", st)34 }35 got := roundTrip(t, s)36 if st := got.CellStyle(at("B2")); st.VAlign != VAlignTop || st.Borders != b {37 t.Errorf("read back %+v", st)38 }39 var buf strings.Builder40 if err := s.Write(&buf); err != nil {41 t.Fatal(err)42 }43 if !strings.Contains(buf.String(), `"valign":"top"`) || !strings.Contains(buf.String(), `"topColor":"red"`) {44 t.Errorf("file:\n%s", buf.String())45 }46 s.Book().Undo()47 s.Book().Undo()48 if s.CellStyle(at("B2")).Borders != 0 {49 t.Error("undo kept the borders")50 }51 // A line removed takes its color with it.52 if b := Borders(0).WithStroke(EdgeLeft, Stroke{LineThin, ColorBlue}).With(EdgeLeft, LineNone); b != 0 {53 t.Errorf("no line keeps color %x", uint32(b))54 }55 for v, want := range map[VAlign][3]int{VAlignAuto: {3, 3, 1}, VAlignTop: {0, 0, 0}, VAlignMiddle: {1, 0, 1}, VAlignBottom: {3, 3, 3}} {56 if got := [3]int{v.Offset(1, 4, false), v.Offset(3, 4, false) * 3, v.Offset(1, 4, true)}; got != want {57 t.Errorf("%v offsets %v, want %v", v, got, want)58 }59 }60 for _, name := range []string{"", "top", "middle", "bottom"} {61 if v, ok := ParseVAlign(name); !ok || v.String() != name {62 t.Errorf("valign %q", name)63 }64 }65}6667// The outline of whole columns or rows draws the sheet's first and last68// edges, as lines where it covers the whole sheet.69func TestSheetEdgeBorders(t *testing.T) {70 s := New()71 cols := Rect{From: Addr{Col: 1}, To: Addr{Col: 2, Row: MaxRows - 1}}72 s.SetBorders(cols, BorderOuter, LineThick)73 if s.EdgeAbove(Addr{Col: 1}) != LineThick || s.EdgeAbove(Addr{Col: 2}) != LineThick || s.EdgeAbove(Addr{Col: 3}) != LineNone {74 t.Error("no top edge over whole columns")75 }76 if s.CellStyle(Addr{Col: 2, Row: MaxRows - 1}).Borders.Bottom() != LineThick || s.EdgeAbove(Addr{Col: 1, Row: 5}) != LineNone {77 t.Error("bottom edge of whole columns")78 }79 if s.EdgeLeft(Addr{Col: 1, Row: 7}) != LineThick || s.EdgeLeft(Addr{Col: 3, Row: 7}) != LineThick {80 t.Error("sides of whole columns")81 }82 rows := Rect{From: Addr{Row: 4}, To: Addr{Col: MaxCols - 1, Row: 5}}83 s.SetBorders(rows, BorderLeft, LineDouble)84 if s.EdgeLeft(Addr{Row: 4}) != LineDouble || s.EdgeLeft(Addr{Row: 6}) != LineNone {85 t.Error("left edge of whole rows")86 }87 all := Rect{To: Addr{Col: MaxCols - 1, Row: MaxRows - 1}}88 s.SetBorders(all, BorderOuter, LineThin)89 if s.EdgeAbove(Addr{Col: 900}) != LineThin || s.EdgeLeft(Addr{Row: 900}) != LineThin {90 t.Error("the whole sheet's outline")91 }92 if n := s.Len(); n > 16 {93 t.Errorf("%d cells for the outlines", n)94 }95}