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