Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 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/tint/reader/wgsl/parser.h" |
| 16 | |
| 17 | #include "gtest/gtest.h" |
| 18 | |
| 19 | #include "src/tint/ast/module.h" |
| 20 | |
dan sinclair | 30a03d9 | 2022-04-07 17:55:04 +0000 | [diff] [blame] | 21 | namespace tint::reader::wgsl { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 22 | namespace { |
| 23 | |
| 24 | using ParserTest = testing::Test; |
| 25 | |
| 26 | TEST_F(ParserTest, Empty) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 27 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | TEST_F(ParserTest, Parses) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 34 | Source::File file("test.wgsl", R"( |
dan sinclair | b29892b | 2022-06-07 13:55:34 +0000 | [diff] [blame] | 35 | @fragment |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 36 | fn main() -> @location(0) vec4<f32> { |
| 37 | return vec4<f32>(.4, .2, .3, 1.); |
| 38 | } |
| 39 | )"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 40 | auto program = Parse(&file); |
| 41 | auto errs = diag::Formatter().format(program.Diagnostics()); |
| 42 | ASSERT_TRUE(program.IsValid()) << errs; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 43 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 44 | ASSERT_EQ(1u, program.AST().Functions().size()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | TEST_F(ParserTest, HandlesError) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 48 | Source::File file("test.wgsl", R"( |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 49 | fn main() -> { // missing return type |
| 50 | return; |
| 51 | })"); |
| 52 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 53 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 58 | fn main() -> { // missing return type |
| 59 | ^ |
| 60 | |
| 61 | )"); |
| 62 | } |
| 63 | |
| 64 | } // namespace |
dan sinclair | 30a03d9 | 2022-04-07 17:55:04 +0000 | [diff] [blame] | 65 | } // namespace tint::reader::wgsl |