1 // Copyright (c) 2020 Cisco and/or its affiliates.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
22 . "github.com/onsi/gomega"
25 func TestGetInputFiles(t *testing.T) {
28 result, err := FindFiles("testdata", 1)
29 Expect(err).ShouldNot(HaveOccurred())
30 Expect(result).To(HaveLen(6))
31 for _, file := range result {
32 Expect(file).To(BeAnExistingFile())
36 func TestGetInputFilesError(t *testing.T) {
39 result, err := FindFiles("nonexisting_directory", 1)
40 Expect(err).Should(HaveOccurred())
41 Expect(result).To(BeNil())
44 func TestReadJson(t *testing.T) {
47 inputData, err := ioutil.ReadFile("testdata/af_packet.api.json")
48 Expect(err).ShouldNot(HaveOccurred())
49 result, err := ParseRaw(inputData)
50 Expect(err).ShouldNot(HaveOccurred())
51 Expect(result).ToNot(BeNil())
52 Expect(result.EnumTypes).To(HaveLen(0))
53 Expect(result.StructTypes).To(HaveLen(0))
54 Expect(result.Messages).To(HaveLen(6))
55 Expect(result.Service.RPCs).To(HaveLen(3))
58 func TestReadJsonError(t *testing.T) {
61 inputData, err := ioutil.ReadFile("testdata/input-read-json-error.json")
62 Expect(err).ShouldNot(HaveOccurred())
63 result, err := ParseRaw(inputData)
64 Expect(err).Should(HaveOccurred())
65 Expect(result).To(BeNil())
68 func TestParseFile(t *testing.T) {
69 module, err := ParseFile("testdata/vpe.api.json")
71 t.Fatal("unexpected error:", err)
74 b, err := json.MarshalIndent(module, "", " ")
78 t.Logf("parsed module: %s", b)
80 if module.Name != "vpe" {
81 t.Errorf("expected Name=%s, got %v", "vpe", module.Name)
83 if module.Path != "testdata/vpe.api.json" {
84 t.Errorf("expected Path=%s, got %v", "testdata/vpe.api.json", module.Path)
86 if module.CRC != "0xbd2c94f4" {
87 t.Errorf("expected CRC=%s, got %v", "0xbd2c94f4", module.CRC)
90 if version := module.Options["version"]; version != "1.6.1" {
91 t.Errorf("expected option[version]=%s, got %v", "1.6.1", version)
93 if len(module.Imports) == 0 {
94 t.Errorf("expected imports, got none")
96 if len(module.EnumTypes) == 0 {
97 t.Errorf("expected enums, got none")
99 if len(module.AliasTypes) == 0 {
100 t.Errorf("expected aliases, got none")
102 if len(module.StructTypes) == 0 {
103 t.Errorf("expected types, got none")
105 if len(module.Messages) == 0 {
106 t.Errorf("expected messages, got none")
108 if len(module.Service.RPCs) == 0 {
109 t.Errorf("expected service RPCs, got none")
113 func TestParseFileUnsupported(t *testing.T) {
114 _, err := ParseFile("testdata/input.txt")
116 t.Fatal("expected error")