-
Notifications
You must be signed in to change notification settings - Fork 0
/
tellFile_test.go
98 lines (91 loc) · 2.1 KB
/
tellFile_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package tell_test
import (
"bufio"
"encoding/json"
"io/fs"
"reflect"
"strings"
"testing"
"github.com/ionous/tell"
"github.com/ionous/tell/note"
"github.com/ionous/tell/testdata"
)
// helper for debugging specific tests
var focus string
func TestFiles(t *testing.T) {
// focus = "smallCatalogComments"
if files, e := testdata.Tell.ReadDir("."); e != nil {
t.Fatal(e)
} else {
for _, info := range files {
tellName := info.Name()
jsonName := tellName[:len(tellName)-4] + "json"
if (len(focus) > 0 && !strings.Contains(tellName, focus)) ||
strings.HasPrefix(tellName, "x_") {
// t.Log("skipping", tellName)
continue
}
//
t.Log("trying", tellName)
if got, e := readTell(tellName); e != nil {
t.Log("error", e)
t.Fail()
} else if want, e := readJson(jsonName); e != nil {
t.Log(stringify(got))
t.Fail()
} else {
if !reflect.DeepEqual(got, want) {
t.Log("ng: ", jsonName)
t.Log("got:", stringify(got))
t.Fail()
} else {
t.Log("ok: ", jsonName)
}
}
}
}
}
func stringify(got any) (ret string) {
if a, e := json.MarshalIndent(got, "", " "); e != nil {
panic(e)
} else {
ret = string(a)
}
return
}
func readTell(filePath string) (ret any, err error) {
if fp, e := testdata.Tell.Open(filePath); e != nil {
err = e
} else {
var res any
var docComments note.Book
dec := tell.NewDecoder(bufio.NewReader(fp))
dec.UseFloats() // because json does
if keepComments := strings.Contains(strings.ToLower(filePath), "comment"); keepComments {
dec.UseNotes(&docComments)
}
if e := dec.Decode(&res); e != nil {
err = e
} else if str, _ := docComments.Resolve(); len(str) > 0 {
ret = map[string]any{
"content": res,
"comment": str,
}
} else {
ret = map[string]any{
"content": res,
}
}
}
return
}
// given one of the json files in the tesdata directory;
// read its contents and return the result
func readJson(filePath string) (ret any, err error) {
if b, e := fs.ReadFile(testdata.Json, filePath); e != nil {
err = e
} else if e := json.Unmarshal(b, &ret); e != nil {
err = e
}
return
}