-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
74 lines (64 loc) · 1.94 KB
/
parser_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
package betterjson
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
)
type Config struct {
Name string `json:"name"`
Value int `json:"value"`
}
func TestUnmarshalWithComments(t *testing.T) {
testFilesDir := "./test_files/"
// Map to store the content of the expected files
expectedFiles := make(map[string][]byte)
// Read all files in the test_files directory
files, err := os.ReadDir(testFilesDir)
if err != nil {
t.Fatalf("Failed to read directory: %v", err)
}
// Separate the files into expected and input
for _, file := range files {
filename := file.Name()
if strings.Contains(filename, "_expected.json") {
content, err := os.ReadFile(filepath.Join(testFilesDir, filename))
if err != nil {
t.Errorf("Failed to read file %s: %v", filename, err)
continue
}
testName := strings.TrimSuffix(filename, "_expected.json")
expectedFiles[testName] = content
}
}
// Process each input file and compare with the expected content
for _, file := range files {
filename := file.Name()
if strings.Contains(filename, "_input.json") {
content, err := os.ReadFile(filepath.Join(testFilesDir, filename))
if err != nil {
t.Errorf("Failed to read file %s: %v", filename, err)
continue
}
testName := strings.TrimSuffix(filename, "_input.json")
expectedContent, exists := expectedFiles[testName]
if !exists {
t.Errorf("No expected file found for %s", testName)
continue
}
var inputConfig, expectedConfig Config
if err := Unmarshal(content, &inputConfig); err != nil {
t.Errorf("Failed to unmarshal input from %s: %v", filename, err)
continue
}
if err := json.Unmarshal(expectedContent, &expectedConfig); err != nil {
t.Errorf("Failed to unmarshal expected content for %s: %v", testName, err)
continue
}
if inputConfig != expectedConfig {
t.Errorf("Mismatch in %s. Expected: %+v, Got: %+v", testName, expectedConfig, inputConfig)
}
}
}
}