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 
14 namespace {
15 
Compiles(const std::string & source_code)16 static bool Compiles(const std::string& source_code) {
17     return TestLibrary("test.fidl", source_code).Compile();
18 }
19 
compiling(void)20 static bool compiling(void) {
21     BEGIN_TEST;
22 
23     // Populated fields.
24     EXPECT_TRUE(Compiles(R"FIDL(
25 library fidl.test.tables;
26 
27 table Foo {
28     1: int64 x;
29 };
30 )FIDL"));
31 
32     // Reserved fields.
33     EXPECT_TRUE(Compiles(R"FIDL(
34 library fidl.test.tables;
35 
36 table Foo {
37     1: reserved;
38 };
39 )FIDL"));
40 
41     // Reserved and populated fields.
42     EXPECT_TRUE(Compiles(R"FIDL(
43 library fidl.test.tables;
44 
45 table Foo {
46     1: reserved;
47     2: int64 x;
48 };
49 )FIDL"));
50 
51     EXPECT_TRUE(Compiles(R"FIDL(
52 library fidl.test.tables;
53 
54 table Foo {
55     1: int64 x;
56     2: reserved;
57 };
58 )FIDL"));
59 
60     // Many reserved fields.
61     EXPECT_TRUE(Compiles(R"FIDL(
62 library fidl.test.tables;
63 
64 table Foo {
65     1: reserved;
66     2: reserved;
67     3: reserved;
68 };
69 )FIDL"));
70 
71     // Out of order fields.
72     EXPECT_TRUE(Compiles(R"FIDL(
73 library fidl.test.tables;
74 
75 table Foo {
76     3: reserved;
77     1: reserved;
78     2: reserved;
79 };
80 )FIDL"));
81 
82     // Duplicate ordinals.
83     EXPECT_FALSE(Compiles(R"FIDL(
84 library fidl.test.tables;
85 
86 table Foo {
87     1: reserved;
88     1: reserved;
89 };
90 )FIDL"));
91 
92     // Missing ordinals.
93     EXPECT_FALSE(Compiles(R"FIDL(
94 library fidl.test.tables;
95 
96 table Foo {
97     1: reserved;
98     3: reserved;
99 };
100 )FIDL"));
101 
102     // Empty tables not allowed.
103     EXPECT_FALSE(Compiles(R"FIDL(
104 library fidl.test.tables;
105 
106 table Foo {
107 };
108 )FIDL"));
109 
110     // Ordinals required.
111     EXPECT_FALSE(Compiles(R"FIDL(
112 library fidl.test.tables;
113 
114 table Foo {
115     int64 x;
116 };
117 )FIDL"));
118 
119     // Attributes on fields.
120     EXPECT_TRUE(Compiles(R"FIDL(
121 library fidl.test.tables;
122 
123 table Foo {
124     [FooAttr="bar"]
125     1: int64 x;
126     [BarAttr]
127     2: bool bar;
128 };
129 )FIDL"));
130 
131     // Attributes on tables.
132     EXPECT_TRUE(Compiles(R"FIDL(
133 library fidl.test.tables;
134 
135 [FooAttr="bar"]
136 table Foo {
137     1: int64 x;
138     2: bool please;
139 };
140 )FIDL"));
141 
142     // Attributes on reserved.
143     EXPECT_FALSE(Compiles(R"FIDL(
144 library fidl.test.tables;
145 
146 table Foo {
147     [Foo]
148     1: reserved;
149 };
150 )FIDL"));
151 
152     END_TEST;
153 }
154 
155 } // namespace
156 
157 BEGIN_TEST_CASE(table_tests);
158 RUN_TEST(compiling);
159 END_TEST_CASE(table_tests);
160