cat / 012
clone

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

main
012 / internal / ui / nbview / stmts_test.go
stmts_test.go 2.1K
1package nbview23import (4 "context"5 "fmt"6 "strings"7 "testing"89 "src.rest/cat/012/internal/notebook"10)1112// A cell of several statements: each name assigned shows as a variable,13// a comment's text as a comment, whatever it holds, and # in a string14// or a raw string is the string's.15func TestHighlightsStatements(t *testing.T) {16 src := "files = ls # all\n# big = $files\nbig = $files | where name != \"a#b\" | append r#'c # d'#"17 var got []string18 for _, s := range (Tokens{}).Highlight(context.Background(), src) {19 got = append(got, fmt.Sprintf("%s:%d", src[s.From:s.To], s.Kind))20 }21 want := fmt.Sprint([]string{"files:2", "=:5", "ls:0", "# all:6", "# big = $files:6", "big:2", "=:5", "$files:2", "|:5", "where:0",22 "!=:5", `"a#b":1`, "|:5", "append:0", "r#'c # d'#:1"})23 if fmt.Sprint(got) != want {24 t.Errorf("spans %v\nwant %v", got, want)25 }26}2728// nu is asked about a cell of several statements with each `name =`29// blanked, every byte in its place, and the names its lines assign for30// the lines after declared.31func TestPrepareStatements(t *testing.T) {32 src := "files = ls # x = 1\nbig = $files | first"33 p := prepare(src, []string{"sales"})34 if body := p.text[p.at:]; body != " ls # x = 1\n $files | first" {35 t.Errorf("masked %q", body)36 }37 pre := p.text[:p.at]38 if !strings.Contains(pre, "let files: any = []\n") || strings.Contains(pre, "let big") || strings.Contains(pre, "let x") {39 t.Errorf("prelude %q", pre)40 }41}4243// The context line says what a cell of several statements reads and44// sets: not its own variables.45func TestHeadStatements(t *testing.T) {46 h := newHost("files = ls | where type == file\n$files | where size > 1kb | sort-by size --reverse", "$files | length")47 v := newView(h, 60, 12)48 v.Select(0, false)49 if name, text := v.Head(); name != "cell 1" || text != "a nushell pipeline; sets $files" {50 t.Errorf("head %q %q", name, text)51 }52 v.Select(1, false)53 if _, text := v.Head(); text != "reads $files" {54 t.Errorf("head of the reader %q", text)55 }56 if (notebook.Cell{Source: h.cells[0].Source}).Name() != "" {57 t.Error("the cell is named")58 }59}