Change module name to go.fd.io/govpp
[govpp.git] / binapigen / binapigen_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 binapigen
16
17 import (
18         "testing"
19
20         . "github.com/onsi/gomega"
21
22         "go.fd.io/govpp/binapigen/vppapi"
23 )
24
25 func TestGenerator(t *testing.T) {
26         tests := []struct {
27                 name          string
28                 file          *vppapi.File
29                 expectPackage string
30         }{
31                 {name: "vpe", file: &vppapi.File{
32                         Name: "vpe",
33                         Path: "/usr/share/vpp/api/core/vpe.api.json",
34                         CRC:  "0x12345678",
35                 },
36                         expectPackage: "vpe",
37                 },
38         }
39         for _, test := range tests {
40                 t.Run(test.name, func(t *testing.T) {
41                         RegisterTestingT(t)
42
43                         apiFiles := []*vppapi.File{test.file}
44
45                         gen, err := New(Options{
46                                 ImportPrefix: "test",
47                         }, apiFiles, nil)
48                         Expect(err).ToNot(HaveOccurred(), "unexpected generator error: %v", err)
49
50                         Expect(gen.Files).To(HaveLen(1))
51                         Expect(gen.Files[0].PackageName).To(BeEquivalentTo(test.expectPackage))
52                         Expect(gen.Files[0].GoImportPath).To(BeEquivalentTo("test/" + test.expectPackage))
53                 })
54         }
55 }
56
57 func TestSanitize(t *testing.T) {
58         tests := []struct {
59                 name     string
60                 expected string
61         }{
62                 {"interface", "interfaces"},
63         }
64         for _, test := range tests {
65                 t.Run(test.name, func(t *testing.T) {
66                         s := sanitizedName(test.name)
67                         if s != test.expected {
68                                 t.Fatalf("expected: %q, got: %q", test.expected, s)
69                         }
70                 })
71         }
72 }