summaryrefslogtreecommitdiffstats
path: root/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'cmd')
-rw-r--r--cmd/binapi-generator/generate_test.go21
-rw-r--r--cmd/binapi-generator/main.go14
2 files changed, 22 insertions, 13 deletions
diff --git a/cmd/binapi-generator/generate_test.go b/cmd/binapi-generator/generate_test.go
index e68b54a..bff5406 100644
--- a/cmd/binapi-generator/generate_test.go
+++ b/cmd/binapi-generator/generate_test.go
@@ -16,6 +16,7 @@ package main
import (
"bufio"
+ "io/ioutil"
"os"
"testing"
@@ -24,7 +25,7 @@ import (
func TestGetInputFiles(t *testing.T) {
RegisterTestingT(t)
- result, err := getInputFiles("testdata")
+ result, err := getInputFiles("testdata", 1)
Expect(err).ShouldNot(HaveOccurred())
Expect(result).To(HaveLen(3))
for _, file := range result {
@@ -34,7 +35,7 @@ func TestGetInputFiles(t *testing.T) {
func TestGetInputFilesError(t *testing.T) {
RegisterTestingT(t)
- result, err := getInputFiles("nonexisting_directory")
+ result, err := getInputFiles("nonexisting_directory", 1)
Expect(err).Should(HaveOccurred())
Expect(result).To(BeNil())
}
@@ -112,9 +113,9 @@ func TestGetContextInterfaceJson(t *testing.T) {
func TestReadJson(t *testing.T) {
RegisterTestingT(t)
- inputData, err := readFile("testdata/af_packet.api.json")
+ inputData, err := ioutil.ReadFile("testdata/af_packet.api.json")
Expect(err).ShouldNot(HaveOccurred())
- result, err := parseJSON(inputData)
+ result, err := parseInputJSON(inputData)
Expect(err).ShouldNot(HaveOccurred())
Expect(result).ToNot(BeNil())
Expect(result.Len()).To(BeEquivalentTo(5))
@@ -122,9 +123,9 @@ func TestReadJson(t *testing.T) {
func TestReadJsonError(t *testing.T) {
RegisterTestingT(t)
- inputData, err := readFile("testdata/input-read-json-error.json")
+ inputData, err := ioutil.ReadFile("testdata/input-read-json-error.json")
Expect(err).ShouldNot(HaveOccurred())
- result, err := parseJSON(inputData)
+ result, err := parseInputJSON(inputData)
Expect(err).Should(HaveOccurred())
Expect(result).To(BeNil())
}
@@ -136,9 +137,9 @@ func TestGeneratePackage(t *testing.T) {
testCtx.packageName = "test-package-name"
// prepare input/output output files
- inputData, err := readFile("testdata/ip.api.json")
+ inputData, err := ioutil.ReadFile("testdata/ip.api.json")
Expect(err).ShouldNot(HaveOccurred())
- jsonRoot, err := parseJSON(inputData)
+ jsonRoot, err := parseInputJSON(inputData)
Expect(err).ShouldNot(HaveOccurred())
testCtx.packageData, err = parsePackage(testCtx, jsonRoot)
Expect(err).ShouldNot(HaveOccurred())
@@ -161,9 +162,9 @@ func TestGenerateMessageType(t *testing.T) {
testCtx.packageName = "test-package-name"
// prepare input/output output files
- inputData, err := readFile("testdata/ip.api.json")
+ inputData, err := ioutil.ReadFile("testdata/ip.api.json")
Expect(err).ShouldNot(HaveOccurred())
- jsonRoot, err := parseJSON(inputData)
+ jsonRoot, err := parseInputJSON(inputData)
Expect(err).ShouldNot(HaveOccurred())
outDir := "test_output_directory"
outFile, err := os.Create(outDir)
diff --git a/cmd/binapi-generator/main.go b/cmd/binapi-generator/main.go
index 89a2b2d..d46001d 100644
--- a/cmd/binapi-generator/main.go
+++ b/cmd/binapi-generator/main.go
@@ -116,6 +116,14 @@ func getInputFiles(inputDir string, deep int) (files []string, err error) {
return files, nil
}
+func parseInputJSON(inputData []byte) (*jsongo.JSONNode, error) {
+ jsonRoot := new(jsongo.JSONNode)
+ if err := json.Unmarshal(inputData, jsonRoot); err != nil {
+ return nil, fmt.Errorf("unmarshalling JSON failed: %v", err)
+ }
+ return jsonRoot, nil
+}
+
// generateFromFile generates Go package from one input JSON file
func generateFromFile(inputFile, outputDir string) error {
// create generator context
@@ -142,9 +150,9 @@ func generateFromFile(inputFile, outputDir string) error {
return fmt.Errorf("reading input file %s failed: %v", ctx.inputFile, err)
}
// parse JSON data into objects
- jsonRoot := new(jsongo.JSONNode)
- if err := json.Unmarshal(ctx.inputData, jsonRoot); err != nil {
- return fmt.Errorf("unmarshalling JSON failed: %v", err)
+ jsonRoot, err := parseInputJSON(ctx.inputData)
+ if err != nil {
+ return fmt.Errorf("parsing JSON input failed: %v", err)
}
ctx.packageData, err = parsePackage(ctx, jsonRoot)
if err != nil {