-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatter_test.go
More file actions
93 lines (74 loc) · 2.15 KB
/
formatter_test.go
File metadata and controls
93 lines (74 loc) · 2.15 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
package jsonstruct_test
import (
"fmt"
"log/slog"
"os"
"path"
"path/filepath"
"strings"
"testing"
"github.com/cneill/jsonstruct"
"github.com/stretchr/testify/assert"
)
func TestFormatString(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input *jsonstruct.JSONStruct
expected string
}{
{
name: "simple",
input: jsonstruct.New().AddFields(
jsonstruct.NewField().SetName("a").SetValue(int64(1)),
),
expected: "\ntype Simple struct {\n\tA int64 `json:\"a\"`\n}\n",
},
}
formatter, err := jsonstruct.NewFormatter(&jsonstruct.FormatterOptions{})
assert.Nil(t, err)
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
test.input.SetName(jsonstruct.GetGoName(test.name))
output, err := formatter.FormatStructs(test.input)
assert.Nil(t, err)
assert.Equal(t, test.expected, output)
})
}
}
func TestFormatStringFiles(t *testing.T) {
t.Parallel()
testFilePaths, err := filepath.Glob("test/*.json")
assert.Nil(t, err)
for _, testFilePath := range testFilePaths {
testFilePath := testFilePath
t.Run(testFilePath, func(t *testing.T) {
t.Parallel()
testFileDir, testFileName := path.Split(testFilePath)
expectedFileName := fmt.Sprintf("%s_result.txt", strings.TrimSuffix(testFileName, path.Ext(testFileName)))
expectedFilePath := path.Join(testFileDir, expectedFileName)
testFile, err := os.Open(testFilePath)
assert.Nil(t, err)
defer testFile.Close()
expectedContents, err := os.ReadFile(expectedFilePath)
assert.Nil(t, err)
parser := jsonstruct.NewParser(testFile, slog.Default())
jStruct, err := parser.Start()
assert.Nil(t, err)
formatterOpts := &jsonstruct.FormatterOptions{}
if strings.Contains(testFileName, "comment") {
formatterOpts.ValueComments = true
}
if strings.Contains(testFileName, "inline") {
formatterOpts.InlineStructs = true
}
formatter, err := jsonstruct.NewFormatter(formatterOpts)
assert.Nil(t, err)
output, err := formatter.FormatStructs(jStruct...)
assert.Nil(t, err)
assert.Equal(t, strings.TrimSpace(string(expectedContents)), strings.TrimSpace(output))
})
}
}