-
Notifications
You must be signed in to change notification settings - Fork 2
/
examples_test.go
62 lines (49 loc) · 1.56 KB
/
examples_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
package fatturapa_test
import (
"encoding/xml"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/invopop/gobl.fatturapa/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestExamples(t *testing.T) {
schema, err := test.LoadSchema()
require.NoError(t, err)
var files []string
err = filepath.Walk(test.GetDataPath(), func(path string, _ os.FileInfo, _ error) error {
if filepath.Ext(path) == ".json" {
files = append(files, filepath.Base(path))
}
return nil
})
require.NoError(t, err)
for _, file := range files {
fmt.Printf("processing file: %v\n", file)
env := test.LoadTestFile(file)
doc, err := test.ConvertFromGOBL(env, test.NewConverter())
require.NoError(t, err)
data, err := xml.MarshalIndent(doc, "", "\t")
require.NoError(t, err)
np := strings.TrimSuffix(file, filepath.Ext(file)) + ".xml"
outPath := filepath.Join(test.GetDataPath(), "out", np)
if *test.UpdateOut {
errs := test.ValidateXML(schema, data)
for _, e := range errs {
assert.NoError(t, e)
}
if len(errs) > 0 {
assert.Fail(t, "Invalid XML:\n"+string(data))
}
err = os.WriteFile(outPath, data, 0644)
require.NoError(t, err, "writing file")
}
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)
require.Equal(t, string(expected), string(data), "output file %s does not match, run tests with `--update` flag to update", filepath.Base(outPath))
}
}