-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexamples_test.go
More file actions
146 lines (120 loc) · 3.48 KB
/
examples_test.go
File metadata and controls
146 lines (120 loc) · 3.48 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package goblhtml_test
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/invopop/gobl"
goblhtml "github.com/invopop/gobl.html"
"github.com/invopop/gobl.html/internal/gallery"
"github.com/pmezard/go-difflib/difflib"
"github.com/stretchr/testify/require"
"github.com/yosssi/gohtml"
)
const (
examplesPath = "./examples"
examplesOutPath = "./examples/out"
)
var updateOut = flag.Bool("update", false, "Update the HTML files in examples/out and regenerate examples/index.html")
func TestGOBLRenderExamples(t *testing.T) {
examples, err := findExamples()
require.NoError(t, err)
for _, example := range examples {
name := fmt.Sprintf("should convert %s example file successfully", example)
t.Run(name, func(t *testing.T) {
data, err := convertExample(example)
require.NoError(t, err)
outPath := filepath.Join(examplesOutPath, strings.TrimSuffix(example, ".json")+".html")
// Make the output pretty!
out := gohtml.Format(string(data))
if *updateOut {
err = os.WriteFile(outPath, []byte(out), 0644)
require.NoError(t, err)
return
}
expected, err := os.ReadFile(outPath)
require.False(t, os.IsNotExist(err), "output file %s missing, run tests with `--update` flag to create", filepath.Base(outPath))
require.NoError(t, err)
if out != string(expected) {
diff := difflib.UnifiedDiff{
A: difflib.SplitLines(string(expected)),
B: difflib.SplitLines(out),
FromFile: "Expected",
ToFile: "Got",
Context: 2,
}
text, _ := difflib.GetUnifiedDiffString(diff)
t.Errorf("output file %s does not match:\n%s", filepath.Base(outPath), text)
}
// assert.Equal(t, string(expected), out, "output file %s does not match, run tests with `--update` flag to update", filepath.Base(outPath))
})
}
if *updateOut {
err := gallery.WriteIndexHTML(gallery.ExamplesDir, gallery.IndexHTMLPath())
require.NoError(t, err)
}
}
func findExamples() ([]string, error) {
examples, err := filepath.Glob(filepath.Join(examplesPath, "*.json"))
if err != nil {
return nil, err
}
for i, example := range examples {
examples[i] = filepath.Base(example)
}
return examples, nil
}
func convertExample(name string) ([]byte, error) {
env, err := loadExample(name)
if err != nil {
return nil, err
}
// Extract the state from the name
var opts []goblhtml.Option
if strings.Contains(name, ".void.") {
opts = append(opts, goblhtml.WithVoid(true))
}
if strings.Contains(name, ".sandbox.") {
opts = append(opts, goblhtml.WithSandbox(true))
}
if strings.Contains(name, ".adjustment.") {
opts = append(opts, goblhtml.EnableAdjustmentMode)
}
if strings.Contains(name, ".es.") {
opts = append(opts, goblhtml.WithLocale("es"))
}
ctx := context.Background()
return goblhtml.Render(ctx, env, opts...)
}
func loadExample(name string) (*gobl.Envelope, error) {
src, _ := os.Open(filepath.Join(examplesPath, name))
buf := new(bytes.Buffer)
if _, err := buf.ReadFrom(src); err != nil {
return nil, err
}
env := new(gobl.Envelope)
if err := json.Unmarshal(buf.Bytes(), env); err != nil {
return nil, err
}
if err := env.Calculate(); err != nil {
return nil, err
}
if err := env.Validate(); err != nil {
return nil, err
}
if *updateOut {
data, err := json.MarshalIndent(env, "", "\t")
if err != nil {
return nil, err
}
if err := os.WriteFile(filepath.Join(examplesPath, name), data, 0644); err != nil {
return nil, err
}
}
return env, nil
}