Dan Sinclair | 00cf5a4 | 2020-03-05 20:35:57 +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/ast/call_expression.h" |
| 16 | |
| 17 | #include "gtest/gtest.h" |
| 18 | #include "src/ast/identifier_expression.h" |
| 19 | |
| 20 | namespace tint { |
| 21 | namespace ast { |
dan sinclair | 989cee6 | 2020-03-26 15:31:43 +0000 | [diff] [blame] | 22 | namespace { |
Dan Sinclair | 00cf5a4 | 2020-03-05 20:35:57 +0000 | [diff] [blame] | 23 | |
| 24 | using CallExpressionTest = testing::Test; |
| 25 | |
| 26 | TEST_F(CallExpressionTest, Creation) { |
| 27 | auto func = std::make_unique<IdentifierExpression>("func"); |
| 28 | std::vector<std::unique_ptr<Expression>> params; |
| 29 | params.push_back(std::make_unique<IdentifierExpression>("param1")); |
| 30 | params.push_back(std::make_unique<IdentifierExpression>("param2")); |
| 31 | |
| 32 | auto func_ptr = func.get(); |
| 33 | auto param1_ptr = params[0].get(); |
| 34 | auto param2_ptr = params[1].get(); |
| 35 | |
| 36 | CallExpression stmt(std::move(func), std::move(params)); |
| 37 | EXPECT_EQ(stmt.func(), func_ptr); |
| 38 | |
| 39 | const auto& vec = stmt.params(); |
| 40 | ASSERT_EQ(vec.size(), 2); |
| 41 | EXPECT_EQ(vec[0].get(), param1_ptr); |
| 42 | EXPECT_EQ(vec[1].get(), param2_ptr); |
| 43 | } |
| 44 | |
| 45 | TEST_F(CallExpressionTest, Creation_WithSource) { |
| 46 | auto func = std::make_unique<IdentifierExpression>("func"); |
| 47 | CallExpression stmt(Source{20, 2}, std::move(func), {}); |
| 48 | auto src = stmt.source(); |
| 49 | EXPECT_EQ(src.line, 20); |
| 50 | EXPECT_EQ(src.column, 2); |
| 51 | } |
| 52 | |
| 53 | TEST_F(CallExpressionTest, IsCall) { |
| 54 | auto func = std::make_unique<IdentifierExpression>("func"); |
| 55 | CallExpression stmt(std::move(func), {}); |
| 56 | EXPECT_TRUE(stmt.IsCall()); |
| 57 | } |
| 58 | |
| 59 | TEST_F(CallExpressionTest, IsValid) { |
| 60 | auto func = std::make_unique<IdentifierExpression>("func"); |
| 61 | CallExpression stmt(std::move(func), {}); |
| 62 | EXPECT_TRUE(stmt.IsValid()); |
| 63 | } |
| 64 | |
| 65 | TEST_F(CallExpressionTest, IsValid_MissingFunction) { |
| 66 | CallExpression stmt; |
| 67 | EXPECT_FALSE(stmt.IsValid()); |
| 68 | } |
| 69 | |
| 70 | TEST_F(CallExpressionTest, IsValid_NullParam) { |
| 71 | auto func = std::make_unique<IdentifierExpression>("func"); |
| 72 | std::vector<std::unique_ptr<Expression>> params; |
| 73 | params.push_back(std::make_unique<IdentifierExpression>("param1")); |
| 74 | params.push_back(nullptr); |
| 75 | params.push_back(std::make_unique<IdentifierExpression>("param2")); |
| 76 | |
| 77 | CallExpression stmt(std::move(func), std::move(params)); |
| 78 | EXPECT_FALSE(stmt.IsValid()); |
| 79 | } |
| 80 | |
| 81 | TEST_F(CallExpressionTest, IsValid_InvalidFunction) { |
| 82 | auto func = std::make_unique<IdentifierExpression>(""); |
| 83 | std::vector<std::unique_ptr<Expression>> params; |
| 84 | params.push_back(std::make_unique<IdentifierExpression>("param1")); |
| 85 | |
| 86 | CallExpression stmt(std::move(func), std::move(params)); |
| 87 | EXPECT_FALSE(stmt.IsValid()); |
| 88 | } |
| 89 | |
| 90 | TEST_F(CallExpressionTest, IsValid_InvalidParam) { |
| 91 | auto func = std::make_unique<IdentifierExpression>("func"); |
| 92 | std::vector<std::unique_ptr<Expression>> params; |
| 93 | params.push_back(std::make_unique<IdentifierExpression>("")); |
| 94 | |
| 95 | CallExpression stmt(std::move(func), std::move(params)); |
| 96 | EXPECT_FALSE(stmt.IsValid()); |
| 97 | } |
| 98 | |
| 99 | TEST_F(CallExpressionTest, ToStr_NoParams) { |
| 100 | auto func = std::make_unique<IdentifierExpression>("func"); |
| 101 | CallExpression stmt(std::move(func), {}); |
| 102 | std::ostringstream out; |
| 103 | stmt.to_str(out, 2); |
| 104 | EXPECT_EQ(out.str(), R"( Call{ |
| 105 | Identifier{func} |
| 106 | ( |
| 107 | ) |
| 108 | } |
| 109 | )"); |
| 110 | } |
| 111 | |
| 112 | TEST_F(CallExpressionTest, ToStr_WithParams) { |
| 113 | auto func = std::make_unique<IdentifierExpression>("func"); |
| 114 | std::vector<std::unique_ptr<Expression>> params; |
| 115 | params.push_back(std::make_unique<IdentifierExpression>("param1")); |
| 116 | params.push_back(std::make_unique<IdentifierExpression>("param2")); |
| 117 | |
| 118 | CallExpression stmt(std::move(func), std::move(params)); |
| 119 | std::ostringstream out; |
| 120 | stmt.to_str(out, 2); |
| 121 | EXPECT_EQ(out.str(), R"( Call{ |
| 122 | Identifier{func} |
| 123 | ( |
| 124 | Identifier{param1} |
| 125 | Identifier{param2} |
| 126 | ) |
| 127 | } |
| 128 | )"); |
| 129 | } |
| 130 | |
dan sinclair | 989cee6 | 2020-03-26 15:31:43 +0000 | [diff] [blame] | 131 | } // namespace |
Dan Sinclair | 00cf5a4 | 2020-03-05 20:35:57 +0000 | [diff] [blame] | 132 | } // namespace ast |
| 133 | } // namespace tint |