blob: 1f0e206c744f4439cc4acf3a319307966e1a1df0 [file] [log] [blame]
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001// 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/tint/reader/wgsl/parser.h"
16
17#include "gtest/gtest.h"
18
19#include "src/tint/ast/module.h"
20
dan sinclair30a03d92022-04-07 17:55:04 +000021namespace tint::reader::wgsl {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000022namespace {
23
24using ParserTest = testing::Test;
25
26TEST_F(ParserTest, Empty) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000027 Source::File file("test.wgsl", "");
28 auto program = Parse(&file);
29 auto errs = diag::Formatter().format(program.Diagnostics());
30 ASSERT_TRUE(program.IsValid()) << errs;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000031}
32
33TEST_F(ParserTest, Parses) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000034 Source::File file("test.wgsl", R"(
dan sinclairb29892b2022-06-07 13:55:34 +000035@fragment
Ryan Harrisondbc13af2022-02-21 15:19:07 +000036fn main() -> @location(0) vec4<f32> {
37 return vec4<f32>(.4, .2, .3, 1.);
38}
39)");
dan sinclair41e4d9a2022-05-01 14:40:55 +000040 auto program = Parse(&file);
41 auto errs = diag::Formatter().format(program.Diagnostics());
42 ASSERT_TRUE(program.IsValid()) << errs;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000043
dan sinclair41e4d9a2022-05-01 14:40:55 +000044 ASSERT_EQ(1u, program.AST().Functions().size());
Ryan Harrisondbc13af2022-02-21 15:19:07 +000045}
46
47TEST_F(ParserTest, HandlesError) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000048 Source::File file("test.wgsl", R"(
Ryan Harrisondbc13af2022-02-21 15:19:07 +000049fn main() -> { // missing return type
50 return;
51})");
52
dan sinclair41e4d9a2022-05-01 14:40:55 +000053 auto program = Parse(&file);
54 auto errs = diag::Formatter().format(program.Diagnostics());
55 ASSERT_FALSE(program.IsValid()) << errs;
56 EXPECT_EQ(errs,
57 R"(test.wgsl:2:15 error: unable to determine function return type
Ryan Harrisondbc13af2022-02-21 15:19:07 +000058fn main() -> { // missing return type
59 ^
60
61)");
62}
63
64} // namespace
dan sinclair30a03d92022-04-07 17:55:04 +000065} // namespace tint::reader::wgsl