1package ui23import (4 "strconv"5 "testing"67 "src.rest/cat/012/internal/notebook"8 "src.rest/cat/012/internal/sheet"9)1011// withRegion adds a sheet after the one shown holding, at B2, the12// output of a notebook cell named name: a header row and two rows of13// numbers. It returns the sheet and the table.14func withRegion(t *testing.T, m *Model, name string) (*sheet.Sheet, sheet.Rect) {15 t.Helper()16 w := m.book()17 nb, err := w.AddNotebook("", m.sheet)18 if err != nil {19 t.Fatal(err)20 }21 nb.SetNotebookCells("add cell", []notebook.Cell{{Source: name + " = ls"}})22 w.SetOutput(nb.NotebookCells()[0].ID, ¬ebook.Output{NUON: []byte("[[name, size]; [a, 1], [b, 2]]")})23 s, err := w.AddSheet("Shell", w.Index(m.sheet)+1)24 if err != nil {25 t.Fatal(err)26 }27 if err := s.AddRegion(sheet.Region{Name: name, At: sheet.Addr{Col: 1, Row: 1}, Output: true}); err != nil {28 t.Fatal(err)29 }30 m.feedOutput(name)31 table, ok := s.RegionTable(name)32 if !ok {33 t.Fatal("no table")34 }35 return s, table36}3738// Go to takes a region's name as formulas write it and selects its39// table, header row included, on whichever sheet holds it.40func TestGotoRegion(t *testing.T) {41 m := newModel()42 nb, want := withRegion(t, m, "totals")43 press(t, m, "<ctrl+g>", "NU.Totals", "<enter>")44 if m.mode != modeReady || m.sheet != nb {45 t.Fatalf("mode %v, sheet %q, err %q", m.mode, m.sheet.Name(), m.errMsg)46 }47 if got := m.selection(); got != want {48 t.Errorf("selected %v, want the table %v", got, want)49 }50 press(t, m, "<f5>", "nu.nothing", "<enter>")51 if m.mode != modeError {52 t.Errorf("an unknown region: mode %v", m.mode)53 }54}5556// From a cell of a region, the table around it is the region's table,57// not the block of data around the cell, which may run into cells next58// to it; Insert > Chart and a pivot table read it.59func TestTableAroundRegion(t *testing.T) {60 m := newModel()61 nb, table := withRegion(t, m, "r1")62 m.showSheet(nb)63 nb.Set(sheet.Addr{Col: 3, Row: 1}, "next to it")64 for _, at := range []sheet.Addr{table.To, table.From} {65 m.clearSelection()66 m.cur = at67 if got := m.dataRange(); got != table {68 t.Errorf("from %v: %v, want the table %v", at, got, table)69 }70 if got, ok := m.pivotData(); !ok || got != table {71 t.Errorf("from %v: pivot reads %v %v, want %v", at, got, ok, table)72 }73 }74 m.cur = table.To75 m.runCommand("insert.chart")76 press(t, m, "<enter>")77 if cs := nb.Charts(); len(cs) != 1 || cs[0].Data != table {78 t.Fatalf("charts %+v, want one of %v", cs, table)79 }80 // Ordinary data is still the block around the cell.81 m.showSheet(m.book().Sheets()[0])82 for r := range 3 {83 m.sheet.Set(sheet.Addr{Row: r}, strconv.Itoa(r))84 }85 m.cur = sheet.Addr{Row: 1}86 if got := m.dataRange(); got != (sheet.Rect{To: sheet.Addr{Row: 2}}) {87 t.Errorf("ordinary data: %v", got)88 }89}