cat / 012
clone

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

main
012 / internal / sheet / file_doc_test.go
file_doc_test.go 1.5K
1package sheet23import (4 "os"5 "reflect"6 "strings"7 "testing"8)910// TestFileDocNamesFields checks docs/files/format.md names every field the .01211// format has, and docs/reference/macro-api.md a macro's, so a new field comes with its12// documentation.13func TestFileDocNamesFields(t *testing.T) {14 files := readDoc(t, "../../docs/files/format.md")15 macros := readDoc(t, "../../docs/reference/macro-api.md")16 seen := map[reflect.Type]bool{reflect.TypeFor[fileMacro](): true}17 text := files18 var walk func(reflect.Type, string)19 walk = func(ty reflect.Type, path string) {20 for ty.Kind() == reflect.Pointer || ty.Kind() == reflect.Slice || ty.Kind() == reflect.Map {21 ty = ty.Elem()22 }23 if ty.Kind() != reflect.Struct || seen[ty] {24 return25 }26 seen[ty] = true27 for f := range ty.Fields() {28 if f.Anonymous {29 walk(f.Type, path)30 continue31 }32 name, _, _ := strings.Cut(f.Tag.Get("json"), ",")33 if name == "" || name == "-" {34 continue35 }36 if !strings.Contains(text, "`"+name+"`") && !strings.Contains(text, `"`+name+`"`) {37 t.Errorf("the docs don't name the field %s.%s", path, name)38 }39 walk(f.Type, path+"."+name)40 }41 }42 for _, v := range []any{fileFormat{}, fileCell{}, fileChart{}} {43 walk(reflect.TypeOf(v), reflect.TypeOf(v).Name())44 }45 text, seen = macros, map[reflect.Type]bool{}46 walk(reflect.TypeFor[fileMacro](), "fileMacro")47}4849func readDoc(t *testing.T, path string) string {50 t.Helper()51 data, err := os.ReadFile(path)52 if err != nil {53 t.Fatal(err)54 }55 return string(data)56}