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