dan sinclair | 8347687 | 2020-03-18 20:10:44 +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 <memory> |
| 16 | #include <vector> |
| 17 | |
| 18 | #include "gtest/gtest.h" |
| 19 | #include "src/ast/binding_decoration.h" |
| 20 | #include "src/ast/builtin_decoration.h" |
| 21 | #include "src/ast/decorated_variable.h" |
| 22 | #include "src/ast/location_decoration.h" |
| 23 | #include "src/ast/set_decoration.h" |
| 24 | #include "src/ast/type/f32_type.h" |
| 25 | #include "src/ast/variable_decoration.h" |
| 26 | #include "src/writer/wgsl/generator_impl.h" |
| 27 | |
| 28 | namespace tint { |
| 29 | namespace writer { |
| 30 | namespace wgsl { |
| 31 | namespace { |
| 32 | |
| 33 | using GeneratorImplTest = testing::Test; |
| 34 | |
| 35 | TEST_F(GeneratorImplTest, EmitVariable) { |
| 36 | ast::type::F32Type f32; |
| 37 | ast::Variable v("a", ast::StorageClass::kNone, &f32); |
| 38 | |
| 39 | GeneratorImpl g; |
| 40 | ASSERT_TRUE(g.EmitVariable(&v)) << g.error(); |
| 41 | EXPECT_EQ(g.result(), R"(var a : f32; |
| 42 | )"); |
| 43 | } |
| 44 | |
| 45 | TEST_F(GeneratorImplTest, EmitVariable_StorageClass) { |
| 46 | ast::type::F32Type f32; |
| 47 | ast::Variable v("a", ast::StorageClass::kInput, &f32); |
| 48 | |
| 49 | GeneratorImpl g; |
| 50 | ASSERT_TRUE(g.EmitVariable(&v)) << g.error(); |
| 51 | EXPECT_EQ(g.result(), R"(var<in> a : f32; |
| 52 | )"); |
| 53 | } |
| 54 | |
| 55 | TEST_F(GeneratorImplTest, EmitVariable_Decorated) { |
| 56 | ast::type::F32Type f32; |
| 57 | |
| 58 | std::vector<std::unique_ptr<ast::VariableDecoration>> decos; |
| 59 | decos.push_back(std::make_unique<ast::LocationDecoration>(2)); |
| 60 | |
| 61 | ast::DecoratedVariable dv; |
| 62 | dv.set_name("a"); |
| 63 | dv.set_type(&f32); |
| 64 | dv.set_decorations(std::move(decos)); |
| 65 | |
| 66 | GeneratorImpl g; |
| 67 | ASSERT_TRUE(g.EmitVariable(&dv)) << g.error(); |
| 68 | EXPECT_EQ(g.result(), R"([[location 2]] var a : f32; |
| 69 | )"); |
| 70 | } |
| 71 | |
| 72 | TEST_F(GeneratorImplTest, EmitVariable_Decorated_Multiple) { |
| 73 | ast::type::F32Type f32; |
| 74 | |
| 75 | std::vector<std::unique_ptr<ast::VariableDecoration>> decos; |
| 76 | decos.push_back( |
| 77 | std::make_unique<ast::BuiltinDecoration>(ast::Builtin::kPosition)); |
| 78 | decos.push_back(std::make_unique<ast::BindingDecoration>(0)); |
| 79 | decos.push_back(std::make_unique<ast::SetDecoration>(1)); |
| 80 | decos.push_back(std::make_unique<ast::LocationDecoration>(2)); |
| 81 | |
| 82 | ast::DecoratedVariable dv; |
| 83 | dv.set_name("a"); |
| 84 | dv.set_type(&f32); |
| 85 | dv.set_decorations(std::move(decos)); |
| 86 | |
| 87 | GeneratorImpl g; |
| 88 | ASSERT_TRUE(g.EmitVariable(&dv)) << g.error(); |
| 89 | EXPECT_EQ(g.result(), |
| 90 | R"([[builtin position, binding 0, set 1, location 2]] var a : f32; |
| 91 | )"); |
| 92 | } |
| 93 | |
dan sinclair | a322f5d | 2020-03-30 22:46:06 +0000 | [diff] [blame^] | 94 | TEST_F(GeneratorImplTest, EmitVariable_Constructor) { |
dan sinclair | 8347687 | 2020-03-18 20:10:44 +0000 | [diff] [blame] | 95 | auto ident = std::make_unique<ast::IdentifierExpression>("initializer"); |
| 96 | |
| 97 | ast::type::F32Type f32; |
| 98 | ast::Variable v("a", ast::StorageClass::kNone, &f32); |
dan sinclair | a322f5d | 2020-03-30 22:46:06 +0000 | [diff] [blame^] | 99 | v.set_constructor(std::move(ident)); |
dan sinclair | 8347687 | 2020-03-18 20:10:44 +0000 | [diff] [blame] | 100 | |
| 101 | GeneratorImpl g; |
| 102 | ASSERT_TRUE(g.EmitVariable(&v)) << g.error(); |
| 103 | EXPECT_EQ(g.result(), R"(var a : f32 = initializer; |
| 104 | )"); |
| 105 | } |
| 106 | |
| 107 | TEST_F(GeneratorImplTest, EmitVariable_Const) { |
| 108 | auto ident = std::make_unique<ast::IdentifierExpression>("initializer"); |
| 109 | |
| 110 | ast::type::F32Type f32; |
| 111 | ast::Variable v("a", ast::StorageClass::kNone, &f32); |
dan sinclair | a322f5d | 2020-03-30 22:46:06 +0000 | [diff] [blame^] | 112 | v.set_constructor(std::move(ident)); |
dan sinclair | 8347687 | 2020-03-18 20:10:44 +0000 | [diff] [blame] | 113 | v.set_is_const(true); |
| 114 | |
| 115 | GeneratorImpl g; |
| 116 | ASSERT_TRUE(g.EmitVariable(&v)) << g.error(); |
| 117 | EXPECT_EQ(g.result(), R"(const a : f32 = initializer; |
| 118 | )"); |
| 119 | } |
| 120 | |
| 121 | } // namespace |
| 122 | } // namespace wgsl |
| 123 | } // namespace writer |
| 124 | } // namespace tint |