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.h"
16
17 #include "gtest/gtest.h"
18
19 #include "src/ast/module.h"
20
21 namespace tint {
22 namespace reader {
23 namespace wgsl {
24 namespace {
25
26 using ParserTest = testing::Test;
27
TEST_F(ParserTest,Empty)28 TEST_F(ParserTest, Empty) {
29 Source::File file("test.wgsl", "");
30 auto program = Parse(&file);
31 auto errs = diag::Formatter().format(program.Diagnostics());
32 ASSERT_TRUE(program.IsValid()) << errs;
33 }
34
TEST_F(ParserTest,Parses)35 TEST_F(ParserTest, Parses) {
36 Source::File file("test.wgsl", R"(
37 [[stage(fragment)]]
38 fn main() -> [[location(0)]] vec4<f32> {
39 return vec4<f32>(.4, .2, .3, 1.);
40 }
41 )");
42 auto program = Parse(&file);
43 auto errs = diag::Formatter().format(program.Diagnostics());
44 ASSERT_TRUE(program.IsValid()) << errs;
45
46 ASSERT_EQ(1u, program.AST().Functions().size());
47 }
48
TEST_F(ParserTest,HandlesError)49 TEST_F(ParserTest, HandlesError) {
50 Source::File file("test.wgsl", R"(
51 fn main() -> { // missing return type
52 return;
53 })");
54
55 auto program = Parse(&file);
56 auto errs = diag::Formatter().format(program.Diagnostics());
57 ASSERT_FALSE(program.IsValid()) << errs;
58 EXPECT_EQ(errs,
59 R"(test.wgsl:2:15 error: unable to determine function return type
60 fn main() -> { // missing return type
61 ^
62
63 )");
64 }
65
66 } // namespace
67 } // namespace wgsl
68 } // namespace reader
69 } // namespace tint
70