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 "test_library.h"
8
9 namespace {
10
GoodEnumTestSimple()11 bool GoodEnumTestSimple() {
12 BEGIN_TEST;
13
14 TestLibrary library(R"FIDL(
15 library example;
16
17 enum Fruit : uint64 {
18 ORANGE = 1;
19 APPLE = 2;
20 BANANA = 3;
21 };
22 )FIDL");
23 ASSERT_TRUE(library.Compile());
24
25 END_TEST;
26 }
27
BadEnumTestWithNonUniqueValues()28 bool BadEnumTestWithNonUniqueValues() {
29 BEGIN_TEST;
30
31 TestLibrary library(R"FIDL(
32 library example;
33
34 enum Fruit : uint64 {
35 ORANGE = 1;
36 APPLE = 1;
37 };
38 )FIDL");
39 ASSERT_FALSE(library.Compile());
40 auto errors = library.errors();
41 ASSERT_EQ(errors.size(), 1);
42 ASSERT_STR_STR(errors[0].c_str(), "value of member APPLE conflicts with previously declared "
43 "member ORANGE in the enum Fruit");
44
45 END_TEST;
46 }
47
BadEnumTestWithNonUniqueValuesOutOfLine()48 bool BadEnumTestWithNonUniqueValuesOutOfLine() {
49 BEGIN_TEST;
50
51 TestLibrary library(R"FIDL(
52 library example;
53
54 enum Fruit {
55 ORANGE = FOUR;
56 APPLE = TWO_SQUARED;
57 };
58
59 const uint32 FOUR = 4;
60 const uint32 TWO_SQUARED = 4;
61 )FIDL");
62 ASSERT_FALSE(library.Compile());
63 auto errors = library.errors();
64 ASSERT_EQ(errors.size(), 1);
65 ASSERT_STR_STR(errors[0].c_str(), "value of member APPLE conflicts with previously declared "
66 "member ORANGE in the enum Fruit");
67
68 END_TEST;
69 }
70
BadEnumTestUnsignedWithNegativeMember()71 bool BadEnumTestUnsignedWithNegativeMember() {
72 BEGIN_TEST;
73
74 TestLibrary library(R"FIDL(
75 library example;
76
77 enum Fruit : uint64 {
78 ORANGE = 1;
79 APPLE = -2;
80 };
81 )FIDL");
82 ASSERT_FALSE(library.Compile());
83 auto errors = library.errors();
84 ASSERT_GE(errors.size(), 1);
85 ASSERT_STR_STR(errors[0].c_str(), "-2 cannot be interpreted as type uint64");
86
87 END_TEST;
88 }
89
BadEnumTestInferredUnsignedWithNegativeMember()90 bool BadEnumTestInferredUnsignedWithNegativeMember() {
91 BEGIN_TEST;
92
93 TestLibrary library(R"FIDL(
94 library example;
95
96 enum Fruit {
97 ORANGE = 1;
98 APPLE = -2;
99 };
100 )FIDL");
101 ASSERT_FALSE(library.Compile());
102 auto errors = library.errors();
103 ASSERT_GE(errors.size(), 1);
104 ASSERT_STR_STR(errors[0].c_str(), "-2 cannot be interpreted as type uint32");
105
106 END_TEST;
107 }
108
BadEnumTestMemberOverflow()109 bool BadEnumTestMemberOverflow() {
110 BEGIN_TEST;
111
112 TestLibrary library(R"FIDL(
113 library example;
114
115 enum Fruit : uint8 {
116 ORANGE = 1;
117 APPLE = 256;
118 };
119 )FIDL");
120 ASSERT_FALSE(library.Compile());
121 auto errors = library.errors();
122 ASSERT_GE(errors.size(), 1);
123 ASSERT_STR_STR(errors[0].c_str(), "256 cannot be interpreted as type uint8");
124
125 END_TEST;
126 }
127
BadEnumTestDuplicateMember()128 bool BadEnumTestDuplicateMember() {
129 BEGIN_TEST;
130
131 TestLibrary library(R"FIDL(
132 library example;
133
134 enum Fruit : uint64 {
135 ORANGE = 1;
136 APPLE = 2;
137 ORANGE = 3;
138 };
139 )FIDL");
140 ASSERT_FALSE(library.Compile());
141 auto errors = library.errors();
142 ASSERT_GE(errors.size(), 1);
143 ASSERT_STR_STR(errors[0].c_str(), "name of member ORANGE conflicts with previously declared "
144 "member in the enum Fruit");
145
146 END_TEST;
147 }
148
149 } // namespace
150
151 BEGIN_TEST_CASE(enums_tests);
152
153 RUN_TEST(GoodEnumTestSimple);
154 RUN_TEST(BadEnumTestWithNonUniqueValues);
155 RUN_TEST(BadEnumTestWithNonUniqueValuesOutOfLine);
156 RUN_TEST(BadEnumTestUnsignedWithNegativeMember);
157 RUN_TEST(BadEnumTestInferredUnsignedWithNegativeMember);
158 RUN_TEST(BadEnumTestMemberOverflow);
159 RUN_TEST(BadEnumTestDuplicateMember);
160
161 END_TEST_CASE(enums_tests);
162