From 94620e85f0bdbb054af07ce3670fadc1f76cfdf0 Mon Sep 17 00:00:00 2001 From: Ondrej Fabry Date: Thu, 18 Jun 2020 08:22:13 +0200 Subject: Refactored binapi generator with message encoding Change-Id: I5a6abb68b9d058866f94818169300e5c2fc43895 Signed-off-by: Ondrej Fabry --- binapigen/vppapi/parser_test.go | 114 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 binapigen/vppapi/parser_test.go (limited to 'binapigen/vppapi/parser_test.go') diff --git a/binapigen/vppapi/parser_test.go b/binapigen/vppapi/parser_test.go new file mode 100644 index 0000000..2dc82e4 --- /dev/null +++ b/binapigen/vppapi/parser_test.go @@ -0,0 +1,114 @@ +// Copyright (c) 2020 Cisco and/or its affiliates. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package vppapi + +import ( + "encoding/json" + "io/ioutil" + "testing" + + . "github.com/onsi/gomega" +) + +func TestGetInputFiles(t *testing.T) { + RegisterTestingT(t) + + result, err := FindFiles("testdata", 1) + Expect(err).ShouldNot(HaveOccurred()) + Expect(result).To(HaveLen(5)) + for _, file := range result { + Expect(file).To(BeAnExistingFile()) + } +} + +func TestGetInputFilesError(t *testing.T) { + RegisterTestingT(t) + + result, err := FindFiles("nonexisting_directory", 1) + Expect(err).Should(HaveOccurred()) + Expect(result).To(BeNil()) +} + +func TestReadJson(t *testing.T) { + RegisterTestingT(t) + + inputData, err := ioutil.ReadFile("testdata/af_packet.api.json") + Expect(err).ShouldNot(HaveOccurred()) + result, err := parseJSON(inputData) + Expect(err).ShouldNot(HaveOccurred()) + Expect(result).ToNot(BeNil()) + Expect(result.EnumTypes).To(HaveLen(0)) + Expect(result.StructTypes).To(HaveLen(0)) + Expect(result.Messages).To(HaveLen(6)) + Expect(result.Service.RPCs).To(HaveLen(3)) +} + +func TestReadJsonError(t *testing.T) { + RegisterTestingT(t) + + inputData, err := ioutil.ReadFile("testdata/input-read-json-error.json") + Expect(err).ShouldNot(HaveOccurred()) + result, err := parseJSON(inputData) + Expect(err).Should(HaveOccurred()) + Expect(result).To(BeNil()) +} + +func TestParseFile(t *testing.T) { + module, err := ParseFile("testdata/vpe.api.json") + if err != nil { + t.Fatal("unexpected error:", err) + } + + b, err := json.MarshalIndent(module, "", " ") + if err != nil { + t.Fatal(err) + } + t.Logf("parsed module: %s", b) + + if module.Name != "vpe" { + t.Errorf("expected Name=%s, got %v", "vpe", module.Name) + } + if module.CRC != "0xbd2c94f4" { + t.Errorf("expected CRC=%s, got %v", "0xbd2c94f4", module.CRC) + } + if module.Version() != "1.6.1" { + t.Errorf("expected Version=%s, got %v", "1.6.1", module.Version()) + } + if len(module.Imports) == 0 { + t.Errorf("expected imports, got none") + } + if len(module.Options) == 0 { + t.Errorf("expected options, got none") + } + if len(module.AliasTypes) == 0 { + t.Errorf("expected aliases, got none") + } + if len(module.StructTypes) == 0 { + t.Errorf("expected types, got none") + } + if len(module.Service.RPCs) == 0 { + t.Errorf("expected service method, got none") + } + if len(module.Messages) == 0 { + t.Errorf("expected messages, got none") + } +} + +func TestParseFileUnsupported(t *testing.T) { + _, err := ParseFile("testdata/input.txt") + if err == nil { + t.Fatal("expected error") + } +} -- cgit 1.2.3-korg