1// Copyright 2018 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <unittest/unittest.h>
6
7#include <fidl/flat_ast.h>
8#include <fidl/lexer.h>
9#include <fidl/parser.h>
10#include <fidl/source_file.h>
11
12#include "test_library.h"
13
14namespace {
15
16class ValidSuperinterfaces : public TestLibrary {
17public:
18    ValidSuperinterfaces() : TestLibrary("superinterfaces.fidl", R"FIDL(
19library fidl.test.superinterfaces;
20
21interface A {
22    1: MethodA();
23};
24
25interface B : A {
26    2: MethodB();
27};
28
29interface C : A {
30    3: MethodC();
31};
32
33interface D: B, C {
34    4: MethodD();
35};
36
37)FIDL") {}
38};
39
40class InvalidNameSuperinterfaces : public TestLibrary {
41public:
42    InvalidNameSuperinterfaces() : TestLibrary("superinterfaces.fidl", R"FIDL(
43library fidl.test.superinterfaces;
44
45interface A {
46    1: MethodA();
47};
48
49interface B : A {
50    2: MethodB();
51};
52
53interface C : A {
54    3: MethodC();
55};
56
57interface D: B, C {
58    4: MethodD();
59    5: MethodA();
60};
61
62)FIDL") {}
63};
64
65class InvalidOrdinalSuperinterfaces : public TestLibrary {
66public:
67    InvalidOrdinalSuperinterfaces() : TestLibrary("superinterfaces.fidl", R"FIDL(
68library fidl.test.superinterfaces;
69
70interface A {
71    1: MethodA();
72};
73
74interface B : A {
75    2: MethodB();
76};
77
78interface C : A {
79    3: MethodC();
80};
81
82interface D: B, C {
83    4: MethodD();
84    1: MethodE();
85};
86
87)FIDL") {}
88};
89
90class InvalidSimpleSuperinterfaces : public TestLibrary {
91public:
92    InvalidSimpleSuperinterfaces() : TestLibrary("superinterfaces.fidl", R"FIDL(
93library fidl.test.superinterfaces;
94
95interface A {
96    1: MethodA(vector<uint64>);
97};
98
99interface B : A {
100    2: MethodB();
101};
102
103interface C : A {
104    3: MethodC();
105};
106
107[Layout="Simple"]
108interface D: B, C {
109    4: MethodD();
110};
111
112)FIDL") {}
113};
114
115// Test that an interface with a valid diamond dependency has the
116// correct number of methods.
117bool valid_superinterface_test() {
118    BEGIN_TEST;
119
120    ValidSuperinterfaces library;
121    EXPECT_TRUE(library.Compile());
122
123    auto interface_d = library.LookupInterface("D");
124    EXPECT_NONNULL(interface_d);
125
126    EXPECT_EQ(interface_d->all_methods.size(), 4);
127
128    END_TEST;
129}
130
131// Test that an interface with a name collision with a superinterface
132// fails to compile.
133bool invalid_name_superinterface_test() {
134    BEGIN_TEST;
135
136    InvalidNameSuperinterfaces library;
137    EXPECT_FALSE(library.Compile());
138
139    END_TEST;
140}
141
142// Test that an interface with a ordinal collision with a superinterface
143// fails to compile.
144bool invalid_ordinal_superinterface_test() {
145    BEGIN_TEST;
146
147    InvalidOrdinalSuperinterfaces library;
148    EXPECT_FALSE(library.Compile());
149
150    END_TEST;
151}
152
153// Test that an interface with a Simple layout constraint violation in
154// a superinterface's method fails to compile.
155bool invalid_simple_superinterface_test() {
156    BEGIN_TEST;
157
158    InvalidSimpleSuperinterfaces library;
159    EXPECT_FALSE(library.Compile());
160
161    END_TEST;
162}
163
164} // namespace
165
166BEGIN_TEST_CASE(superinterface_tests);
167RUN_TEST(valid_superinterface_test);
168RUN_TEST(invalid_name_superinterface_test);
169RUN_TEST(invalid_ordinal_superinterface_test);
170RUN_TEST(invalid_simple_superinterface_test);
171END_TEST_CASE(superinterface_tests);
172