Dan Sinclair | 45d585f | 2020-03-17 14:59:46 +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 | |
dan sinclair | e49bd5e | 2020-03-30 22:46:48 +0000 | [diff] [blame] | 15 | #include "src/ast/variable_decl_statement.h" |
Dan Sinclair | 45d585f | 2020-03-17 14:59:46 +0000 | [diff] [blame] | 16 | |
Ben Clayton | 10d5c6a | 2020-11-13 21:58:28 +0000 | [diff] [blame] | 17 | #include "src/ast/test_helper.h" |
Dan Sinclair | 45d585f | 2020-03-17 14:59:46 +0000 | [diff] [blame] | 18 | #include "src/ast/type/f32_type.h" |
| 19 | #include "src/ast/variable.h" |
| 20 | |
| 21 | namespace tint { |
| 22 | namespace ast { |
dan sinclair | 989cee6 | 2020-03-26 15:31:43 +0000 | [diff] [blame] | 23 | namespace { |
Dan Sinclair | 45d585f | 2020-03-17 14:59:46 +0000 | [diff] [blame] | 24 | |
Ben Clayton | 10d5c6a | 2020-11-13 21:58:28 +0000 | [diff] [blame] | 25 | using VariableDeclStatementTest = TestHelper; |
Dan Sinclair | 45d585f | 2020-03-17 14:59:46 +0000 | [diff] [blame] | 26 | |
dan sinclair | e49bd5e | 2020-03-30 22:46:48 +0000 | [diff] [blame] | 27 | TEST_F(VariableDeclStatementTest, Creation) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 28 | type::F32 f32; |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 29 | auto* var = create<Variable>("a", StorageClass::kNone, &f32); |
Dan Sinclair | 45d585f | 2020-03-17 14:59:46 +0000 | [diff] [blame] | 30 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 31 | VariableDeclStatement stmt(var); |
| 32 | EXPECT_EQ(stmt.variable(), var); |
Dan Sinclair | 45d585f | 2020-03-17 14:59:46 +0000 | [diff] [blame] | 33 | } |
| 34 | |
dan sinclair | e49bd5e | 2020-03-30 22:46:48 +0000 | [diff] [blame] | 35 | TEST_F(VariableDeclStatementTest, Creation_WithSource) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 36 | type::F32 f32; |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 37 | auto* var = create<Variable>("a", StorageClass::kNone, &f32); |
Dan Sinclair | 45d585f | 2020-03-17 14:59:46 +0000 | [diff] [blame] | 38 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 39 | VariableDeclStatement stmt(Source{Source::Location{20, 2}}, var); |
Dan Sinclair | 45d585f | 2020-03-17 14:59:46 +0000 | [diff] [blame] | 40 | auto src = stmt.source(); |
Ben Clayton | 5bee67f | 2020-10-30 20:44:53 +0000 | [diff] [blame] | 41 | EXPECT_EQ(src.range.begin.line, 20u); |
| 42 | EXPECT_EQ(src.range.begin.column, 2u); |
Dan Sinclair | 45d585f | 2020-03-17 14:59:46 +0000 | [diff] [blame] | 43 | } |
| 44 | |
dan sinclair | e49bd5e | 2020-03-30 22:46:48 +0000 | [diff] [blame] | 45 | TEST_F(VariableDeclStatementTest, IsVariableDecl) { |
dan sinclair | 5792783 | 2020-12-02 15:18:59 +0000 | [diff] [blame] | 46 | VariableDeclStatement s; |
Ben Clayton | 1d8098a | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 47 | EXPECT_TRUE(s.Is<VariableDeclStatement>()); |
Dan Sinclair | 45d585f | 2020-03-17 14:59:46 +0000 | [diff] [blame] | 48 | } |
| 49 | |
dan sinclair | e49bd5e | 2020-03-30 22:46:48 +0000 | [diff] [blame] | 50 | TEST_F(VariableDeclStatementTest, IsValid) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 51 | type::F32 f32; |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 52 | auto* var = create<Variable>("a", StorageClass::kNone, &f32); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 53 | VariableDeclStatement stmt(var); |
Dan Sinclair | 45d585f | 2020-03-17 14:59:46 +0000 | [diff] [blame] | 54 | EXPECT_TRUE(stmt.IsValid()); |
| 55 | } |
| 56 | |
dan sinclair | e49bd5e | 2020-03-30 22:46:48 +0000 | [diff] [blame] | 57 | TEST_F(VariableDeclStatementTest, IsValid_InvalidVariable) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 58 | type::F32 f32; |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 59 | auto* var = create<Variable>("", StorageClass::kNone, &f32); |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 60 | VariableDeclStatement stmt(var); |
Dan Sinclair | 45d585f | 2020-03-17 14:59:46 +0000 | [diff] [blame] | 61 | EXPECT_FALSE(stmt.IsValid()); |
| 62 | } |
| 63 | |
dan sinclair | e49bd5e | 2020-03-30 22:46:48 +0000 | [diff] [blame] | 64 | TEST_F(VariableDeclStatementTest, IsValid_NullVariable) { |
dan sinclair | 5792783 | 2020-12-02 15:18:59 +0000 | [diff] [blame] | 65 | VariableDeclStatement stmt; |
Dan Sinclair | 45d585f | 2020-03-17 14:59:46 +0000 | [diff] [blame] | 66 | EXPECT_FALSE(stmt.IsValid()); |
| 67 | } |
| 68 | |
dan sinclair | e49bd5e | 2020-03-30 22:46:48 +0000 | [diff] [blame] | 69 | TEST_F(VariableDeclStatementTest, ToStr) { |
Ben Clayton | f1b0e1e | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 70 | type::F32 f32; |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 71 | auto* var = create<Variable>("a", StorageClass::kNone, &f32); |
Dan Sinclair | 45d585f | 2020-03-17 14:59:46 +0000 | [diff] [blame] | 72 | |
Ben Clayton | 4bfe461 | 2020-11-16 16:41:47 +0000 | [diff] [blame] | 73 | VariableDeclStatement stmt(Source{Source::Location{20, 2}}, var); |
Dan Sinclair | 45d585f | 2020-03-17 14:59:46 +0000 | [diff] [blame] | 74 | std::ostringstream out; |
| 75 | stmt.to_str(out, 2); |
dan sinclair | e49bd5e | 2020-03-30 22:46:48 +0000 | [diff] [blame] | 76 | EXPECT_EQ(out.str(), R"( VariableDeclStatement{ |
Dan Sinclair | 45d585f | 2020-03-17 14:59:46 +0000 | [diff] [blame] | 77 | Variable{ |
| 78 | a |
| 79 | none |
| 80 | __f32 |
| 81 | } |
| 82 | } |
| 83 | )"); |
| 84 | } |
| 85 | |
dan sinclair | 989cee6 | 2020-03-26 15:31:43 +0000 | [diff] [blame] | 86 | } // namespace |
Dan Sinclair | 45d585f | 2020-03-17 14:59:46 +0000 | [diff] [blame] | 87 | } // namespace ast |
| 88 | } // namespace tint |