1 // Copyright 2020 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/reader/wgsl/parser_impl_test_helper.h"
16
17 namespace tint {
18 namespace reader {
19 namespace wgsl {
20 namespace {
21
TEST_F(ParserImplTest,Decoration_Size)22 TEST_F(ParserImplTest, Decoration_Size) {
23 auto p = parser("size(4)");
24 auto deco = p->decoration();
25 EXPECT_TRUE(deco.matched);
26 EXPECT_FALSE(deco.errored);
27 ASSERT_NE(deco.value, nullptr);
28 ASSERT_FALSE(p->has_error());
29
30 auto* member_deco = deco.value->As<ast::Decoration>();
31 ASSERT_NE(member_deco, nullptr);
32 ASSERT_TRUE(member_deco->Is<ast::StructMemberSizeDecoration>());
33
34 auto* o = member_deco->As<ast::StructMemberSizeDecoration>();
35 EXPECT_EQ(o->size, 4u);
36 }
37
TEST_F(ParserImplTest,Decoration_Size_MissingLeftParen)38 TEST_F(ParserImplTest, Decoration_Size_MissingLeftParen) {
39 auto p = parser("size 4)");
40 auto deco = p->decoration();
41 EXPECT_FALSE(deco.matched);
42 EXPECT_TRUE(deco.errored);
43 EXPECT_EQ(deco.value, nullptr);
44 EXPECT_TRUE(p->has_error());
45 EXPECT_EQ(p->error(), "1:6: expected '(' for size decoration");
46 }
47
TEST_F(ParserImplTest,Decoration_Size_MissingRightParen)48 TEST_F(ParserImplTest, Decoration_Size_MissingRightParen) {
49 auto p = parser("size(4");
50 auto deco = p->decoration();
51 EXPECT_FALSE(deco.matched);
52 EXPECT_TRUE(deco.errored);
53 EXPECT_EQ(deco.value, nullptr);
54 EXPECT_TRUE(p->has_error());
55 EXPECT_EQ(p->error(), "1:7: expected ')' for size decoration");
56 }
57
TEST_F(ParserImplTest,Decoration_Size_MissingValue)58 TEST_F(ParserImplTest, Decoration_Size_MissingValue) {
59 auto p = parser("size()");
60 auto deco = p->decoration();
61 EXPECT_FALSE(deco.matched);
62 EXPECT_TRUE(deco.errored);
63 EXPECT_EQ(deco.value, nullptr);
64 EXPECT_TRUE(p->has_error());
65 EXPECT_EQ(p->error(),
66 "1:6: expected signed integer literal for size decoration");
67 }
68
TEST_F(ParserImplTest,Decoration_Size_MissingInvalid)69 TEST_F(ParserImplTest, Decoration_Size_MissingInvalid) {
70 auto p = parser("size(nan)");
71 auto deco = p->decoration();
72 EXPECT_FALSE(deco.matched);
73 EXPECT_TRUE(deco.errored);
74 EXPECT_EQ(deco.value, nullptr);
75 EXPECT_TRUE(p->has_error());
76 EXPECT_EQ(p->error(),
77 "1:6: expected signed integer literal for size decoration");
78 }
79
TEST_F(ParserImplTest,Decoration_Align)80 TEST_F(ParserImplTest, Decoration_Align) {
81 auto p = parser("align(4)");
82 auto deco = p->decoration();
83 EXPECT_TRUE(deco.matched);
84 EXPECT_FALSE(deco.errored);
85 ASSERT_NE(deco.value, nullptr);
86 ASSERT_FALSE(p->has_error());
87
88 auto* member_deco = deco.value->As<ast::Decoration>();
89 ASSERT_NE(member_deco, nullptr);
90 ASSERT_TRUE(member_deco->Is<ast::StructMemberAlignDecoration>());
91
92 auto* o = member_deco->As<ast::StructMemberAlignDecoration>();
93 EXPECT_EQ(o->align, 4u);
94 }
95
TEST_F(ParserImplTest,Decoration_Align_MissingLeftParen)96 TEST_F(ParserImplTest, Decoration_Align_MissingLeftParen) {
97 auto p = parser("align 4)");
98 auto deco = p->decoration();
99 EXPECT_FALSE(deco.matched);
100 EXPECT_TRUE(deco.errored);
101 EXPECT_EQ(deco.value, nullptr);
102 EXPECT_TRUE(p->has_error());
103 EXPECT_EQ(p->error(), "1:7: expected '(' for align decoration");
104 }
105
TEST_F(ParserImplTest,Decoration_Align_MissingRightParen)106 TEST_F(ParserImplTest, Decoration_Align_MissingRightParen) {
107 auto p = parser("align(4");
108 auto deco = p->decoration();
109 EXPECT_FALSE(deco.matched);
110 EXPECT_TRUE(deco.errored);
111 EXPECT_EQ(deco.value, nullptr);
112 EXPECT_TRUE(p->has_error());
113 EXPECT_EQ(p->error(), "1:8: expected ')' for align decoration");
114 }
115
TEST_F(ParserImplTest,Decoration_Align_MissingValue)116 TEST_F(ParserImplTest, Decoration_Align_MissingValue) {
117 auto p = parser("align()");
118 auto deco = p->decoration();
119 EXPECT_FALSE(deco.matched);
120 EXPECT_TRUE(deco.errored);
121 EXPECT_EQ(deco.value, nullptr);
122 EXPECT_TRUE(p->has_error());
123 EXPECT_EQ(p->error(),
124 "1:7: expected signed integer literal for align decoration");
125 }
126
TEST_F(ParserImplTest,Decoration_Align_MissingInvalid)127 TEST_F(ParserImplTest, Decoration_Align_MissingInvalid) {
128 auto p = parser("align(nan)");
129 auto deco = p->decoration();
130 EXPECT_FALSE(deco.matched);
131 EXPECT_TRUE(deco.errored);
132 EXPECT_EQ(deco.value, nullptr);
133 EXPECT_TRUE(p->has_error());
134 EXPECT_EQ(p->error(),
135 "1:7: expected signed integer literal for align decoration");
136 }
137
138 } // namespace
139 } // namespace wgsl
140 } // namespace reader
141 } // namespace tint
142