027cc1f583bce85205a957fbf2b956bb4bdb4e64
[govpp.git] / binapigen / vppapi / vppapi_test.go
1 //  Copyright (c) 2020 Cisco and/or its affiliates.
2 //
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:
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 package vppapi
16
17 import (
18         "encoding/json"
19         "io/ioutil"
20         "testing"
21
22         . "github.com/onsi/gomega"
23 )
24
25 func TestGetInputFiles(t *testing.T) {
26         RegisterTestingT(t)
27
28         result, err := FindFiles("testdata", 1)
29         Expect(err).ShouldNot(HaveOccurred())
30         Expect(result).To(HaveLen(5))
31         for _, file := range result {
32                 Expect(file).To(BeAnExistingFile())
33         }
34 }
35
36 func TestGetInputFilesError(t *testing.T) {
37         RegisterTestingT(t)
38
39         result, err := FindFiles("nonexisting_directory", 1)
40         Expect(err).Should(HaveOccurred())
41         Expect(result).To(BeNil())
42 }
43
44 func TestReadJson(t *testing.T) {
45         RegisterTestingT(t)
46
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))
56 }
57
58 func TestReadJsonError(t *testing.T) {
59         RegisterTestingT(t)
60
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())
66 }
67
68 func TestParseFile(t *testing.T) {
69         module, err := ParseFile("testdata/vpe.api.json")
70         if err != nil {
71                 t.Fatal("unexpected error:", err)
72         }
73
74         b, err := json.MarshalIndent(module, "", "  ")
75         if err != nil {
76                 t.Fatal(err)
77         }
78         t.Logf("parsed module: %s", b)
79
80         if module.Name != "vpe" {
81                 t.Errorf("expected Name=%s, got %v", "vpe", module.Name)
82         }
83         if module.Path != "testdata/vpe.api.json" {
84                 t.Errorf("expected Path=%s, got %v", "testdata/vpe.api.json", module.Path)
85         }
86         if module.CRC != "0xbd2c94f4" {
87                 t.Errorf("expected CRC=%s, got %v", "0xbd2c94f4", module.CRC)
88         }
89
90         if version := module.Options["version"]; version != "1.6.1" {
91                 t.Errorf("expected option[version]=%s, got %v", "1.6.1", version)
92         }
93         if len(module.Imports) == 0 {
94                 t.Errorf("expected imports, got none")
95         }
96         if len(module.EnumTypes) == 0 {
97                 t.Errorf("expected enums, got none")
98         }
99         if len(module.AliasTypes) == 0 {
100                 t.Errorf("expected aliases, got none")
101         }
102         if len(module.StructTypes) == 0 {
103                 t.Errorf("expected types, got none")
104         }
105         if len(module.Messages) == 0 {
106                 t.Errorf("expected messages, got none")
107         }
108         if len(module.Service.RPCs) == 0 {
109                 t.Errorf("expected service RPCs, got none")
110         }
111 }
112
113 func TestParseFileUnsupported(t *testing.T) {
114         _, err := ParseFile("testdata/input.txt")
115         if err == nil {
116                 t.Fatal("expected error")
117         }
118 }