cat / 012
clone

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

main
012 / internal / macro / answer_test.go
answer_test.go 1.7K
1package macro23import (4 "strings"5 "testing"6)78func TestJSONAnswersAreWrittenAsDicts(t *testing.T) {9 a := Call("run", "data.sort_range").With("answer", JSON(`{"by":[{"column":"B","order":"desc"},{"column":"A"}],"header":true,"min":-1.5,"x":null,"q":"it's \"so\""}`))10 want := `run("data.sort_range", answer={"by": [{"column": "B", "order": "desc"}, {"column": "A"}], "header": True, "min": -1.5, "x": None, "q": "it's \"so\""})`11 if got := a.String(); got != want {12 t.Fatalf("got %s\nwant %s", got, want)13 }14 // The script hands the answer back as the JSON it came from, keys in15 // the order written.16 f := newFake()17 if _, err := New("m", Source("x", []Action{a}), Env{Host: f}).Exec(); err != nil {18 t.Fatal(err)19 }20 wantCall := `run data.sort_range "{\"by\":[{\"column\":\"B\",\"order\":\"desc\"},{\"column\":\"A\"}],\"header\":true,\"min\":-1.5,\"x\":null,\"q\":\"it's \\\"so\\\"\"}"`21 if len(f.calls) != 1 || f.calls[0] != wantCall {22 t.Fatalf("calls %q\nwant %q", f.calls, wantCall)23 }24}2526func TestListAnswersAndBadOnes(t *testing.T) {27 f := newFake()28 if _, err := New("m", `run("x", answer=[1, (2, 3), {"k": [True]}])`, Env{Host: f}).Exec(); err != nil {29 t.Fatal(err)30 }31 if f.calls[0] != `run x "[1,[2,3],{\"k\":[true]}]"` {32 t.Fatalf("calls %q", f.calls)33 }34 for src, want := range map[string]string{35 `run("x", answer={1: 2})`: "keys are strings, not int",36 `run("x", answer={"f": print})`: "can't hold a builtin_function_or_method",37 `run("x", answer=[float("nan")])`: "unsupported value",38 } {39 _, err := New("m", src, Env{Host: newFake()}).Exec()40 if err == nil || !strings.Contains(err.Error(), want) {41 t.Errorf("%s: %v, want %q", src, err, want)42 }43 }44}