summaryrefslogtreecommitdiffstats
path: root/binapigen/vppapi/vppapi_test.go
blob: a555d9fe120b157a5cac5abd491924d0c506913e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//  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(6))
	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 := ParseRaw(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 := ParseRaw(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.Path != "testdata/vpe.api.json" {
		t.Errorf("expected Path=%s, got %v", "testdata/vpe.api.json", module.Path)
	}
	if module.CRC != "0xbd2c94f4" {
		t.Errorf("expected CRC=%s, got %v", "0xbd2c94f4", module.CRC)
	}

	if version := module.Options["version"]; version != "1.6.1" {
		t.Errorf("expected option[version]=%s, got %v", "1.6.1", version)
	}
	if len(module.Imports) == 0 {
		t.Errorf("expected imports, got none")
	}
	if len(module.EnumTypes) == 0 {
		t.Errorf("expected enums, 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.Messages) == 0 {
		t.Errorf("expected messages, got none")
	}
	if len(module.Service.RPCs) == 0 {
		t.Errorf("expected service RPCs, got none")
	}
}

func TestParseFileUnsupported(t *testing.T) {
	_, err := ParseFile("testdata/input.txt")
	if err == nil {
		t.Fatal("expected error")
	}
}