socket adapter: don't bother sending sockclnt_delete messages
[govpp.git] / binapigen / vppapi / parser_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 := parseJSON(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 := parseJSON(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.CRC != "0xbd2c94f4" {
84                 t.Errorf("expected CRC=%s, got %v", "0xbd2c94f4", module.CRC)
85         }
86         if module.Version() != "1.6.1" {
87                 t.Errorf("expected Version=%s, got %v", "1.6.1", module.Version())
88         }
89         if len(module.Imports) == 0 {
90                 t.Errorf("expected imports, got none")
91         }
92         if len(module.Options) == 0 {
93                 t.Errorf("expected options, got none")
94         }
95         if len(module.AliasTypes) == 0 {
96                 t.Errorf("expected aliases, got none")
97         }
98         if len(module.StructTypes) == 0 {
99                 t.Errorf("expected types, got none")
100         }
101         if len(module.Service.RPCs) == 0 {
102                 t.Errorf("expected service method, got none")
103         }
104         if len(module.Messages) == 0 {
105                 t.Errorf("expected messages, got none")
106         }
107 }
108
109 func TestParseFileUnsupported(t *testing.T) {
110         _, err := ParseFile("testdata/input.txt")
111         if err == nil {
112                 t.Fatal("expected error")
113         }
114 }