-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathparser_test.go
138 lines (115 loc) · 4.45 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
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
// Copyright 2020 - 2024 The xgen Authors. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.
//
// Package xgen written in pure Go providing a set of functions that allow you
// to parse XSD (XML schema files). This library needs Go version 1.10 or
// later.
package xgen
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
testFixtureDir = "test"
// externalFixtureDir is where one copy their own XSDs to run validation on them. For a set
// of XSDs to run tests on, see https://github.com/xuri/xsd. Note that external tests leave the
// generated output for inspection to support use-cases of manual review of generated code
externalFixtureDir = "data"
)
func TestParseGo(t *testing.T) {
testParseForSource(t, "Go", "go", "go", testFixtureDir, false)
}
// TestParseGoExternal runs tests on any external XSDs within the externalFixtureDir
func TestParseGoExternal(t *testing.T) {
testParseForSource(t, "Go", "go", "go", externalFixtureDir, true)
}
// testParseForSource runs parsing tests for a given language. The sourceDirectory specifies the root of the
// input for the tests. The expected structure of the sourceDirectory is as follows:
//
// source
// ├── xsd (with the input xsd files to run through the parser)
// └── <langDirName> (with the expected generated code named <xsd-file>.<fileExt>
//
// The test cleans up files it generates unless leaveOutput is set to true. In which case, the generate file is left
// on disk for manual inspection under <sourceDirectory>/<langDirName>/output.
func testParseForSource(t *testing.T, lang string, fileExt string, langDirName string, sourceDirectory string, leaveOutput bool) {
codeDir := filepath.Join(sourceDirectory, langDirName)
outputDir := filepath.Join(codeDir, "output")
if leaveOutput {
err := PrepareOutputDir(outputDir)
require.NoError(t, err)
} else {
tempDir, err := ioutil.TempDir(codeDir, "output-*")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
outputDir = tempDir
}
inputDir := filepath.Join(sourceDirectory, "xsd")
files, err := GetFileList(inputDir)
// Abort testing if the source directory doesn't include a xsd directory with inputs
if os.IsNotExist(err) {
return
}
require.NoError(t, err)
for _, file := range files {
if filepath.Ext(file) == ".xsd" {
xsdName, err := filepath.Rel(inputDir, file)
require.NoError(t, err)
t.Run(xsdName, func(t *testing.T) {
parser := NewParser(&Options{
FilePath: file,
InputDir: inputDir,
OutputDir: outputDir,
Lang: lang,
IncludeMap: make(map[string]bool),
LocalNameNSMap: make(map[string]string),
NSSchemaLocationMap: make(map[string]string),
ParseFileList: make(map[string]bool),
ParseFileMap: make(map[string][]interface{}),
ProtoTree: make([]interface{}, 0),
})
err = parser.Parse()
assert.NoError(t, err, file)
generatedFileName := strings.TrimPrefix(file, inputDir) + "." + fileExt
actualFilename := filepath.Join(outputDir, generatedFileName)
actualGenerated, err := ioutil.ReadFile(actualFilename)
assert.NoError(t, err)
expectedFilename := filepath.Join(codeDir, generatedFileName)
expectedGenerated, err := ioutil.ReadFile(expectedFilename)
assert.NoError(t, err)
assert.Equal(t, string(expectedGenerated), string(actualGenerated), fmt.Sprintf("error in generated code for %s", file))
})
}
}
}
func TestParseTypeScript(t *testing.T) {
testParseForSource(t, "TypeScript", "ts", "ts", testFixtureDir, false)
}
func TestParseTypeScriptExternal(t *testing.T) {
testParseForSource(t, "TypeScript", "ts", "ts", externalFixtureDir, true)
}
func TestParseC(t *testing.T) {
testParseForSource(t, "C", "h", "c", testFixtureDir, false)
}
func TestParseCExternal(t *testing.T) {
testParseForSource(t, "C", "h", "c", externalFixtureDir, true)
}
func TestParseJava(t *testing.T) {
testParseForSource(t, "Java", "java", "java", testFixtureDir, false)
}
func TestParseJavaExternal(t *testing.T) {
testParseForSource(t, "Java", "java", "java", externalFixtureDir, true)
}
func TestParseRust(t *testing.T) {
testParseForSource(t, "Rust", "rs", "rs", testFixtureDir, false)
}
func TestParseRustExternal(t *testing.T) {
testParseForSource(t, "Rust", "rs", "rs", externalFixtureDir, true)
}