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 test_no_optional_on_primitive()16bool test_no_optional_on_primitive() { 17 BEGIN_TEST; 18 19 TestLibrary library(R"FIDL( 20 library test.optionals; 21 22 struct Bad { 23 int64? opt_num; 24 }; 25 26 )FIDL"); 27 ASSERT_FALSE(library.Compile()); 28 const auto& errors = library.errors(); 29 ASSERT_EQ(1, errors.size()); 30 ASSERT_STR_STR(errors[0].c_str(), 31 "primitives cannot be nullable"); 32 33 END_TEST; 34 } 35 test_no_optional_on_aliased_primitive()36bool test_no_optional_on_aliased_primitive() { 37 BEGIN_TEST; 38 39 TestLibrary library(R"FIDL( 40 library test.optionals; 41 42 using alias = int64; 43 44 struct Bad { 45 alias? opt_num; 46 }; 47 48 )FIDL"); 49 ASSERT_FALSE(library.Compile()); 50 const auto& errors = library.errors(); 51 ASSERT_EQ(1, errors.size()); 52 ASSERT_STR_STR(errors[0].c_str(), 53 "type aliases cannot be nullable"); 54 55 END_TEST; 56 } 57 58 } // namespace 59 60 BEGIN_TEST_CASE(optionals_test); 61 RUN_TEST(test_no_optional_on_primitive); 62 RUN_TEST(test_no_optional_on_aliased_primitive); 63 END_TEST_CASE(optionals_test); 64