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/ast/unary_op_expression.h" |
| 16 | |
| 17 | #include "gtest/gtest-spi.h" |
| 18 | #include "src/tint/ast/test_helper.h" |
| 19 | |
dan sinclair | 34323ac | 2022-04-07 18:39:35 +0000 | [diff] [blame] | 20 | namespace tint::ast { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | using UnaryOpExpressionTest = TestHelper; |
| 24 | |
| 25 | TEST_F(UnaryOpExpressionTest, Creation) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 26 | auto* ident = Expr("ident"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 27 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 28 | auto* u = create<UnaryOpExpression>(UnaryOp::kNot, ident); |
| 29 | EXPECT_EQ(u->op, UnaryOp::kNot); |
| 30 | EXPECT_EQ(u->expr, ident); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | TEST_F(UnaryOpExpressionTest, Creation_WithSource) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 34 | auto* ident = Expr("ident"); |
| 35 | auto* u = create<UnaryOpExpression>(Source{Source::Location{20, 2}}, UnaryOp::kNot, ident); |
| 36 | auto src = u->source; |
| 37 | EXPECT_EQ(src.range.begin.line, 20u); |
| 38 | EXPECT_EQ(src.range.begin.column, 2u); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | TEST_F(UnaryOpExpressionTest, IsUnaryOp) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 42 | auto* ident = Expr("ident"); |
| 43 | auto* u = create<UnaryOpExpression>(UnaryOp::kNot, ident); |
| 44 | EXPECT_TRUE(u->Is<UnaryOpExpression>()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | TEST_F(UnaryOpExpressionTest, Assert_Null_Expression) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 48 | EXPECT_FATAL_FAILURE( |
| 49 | { |
| 50 | ProgramBuilder b; |
| 51 | b.create<UnaryOpExpression>(UnaryOp::kNot, nullptr); |
| 52 | }, |
| 53 | "internal compiler error"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | TEST_F(UnaryOpExpressionTest, Assert_DifferentProgramID_Expression) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 57 | EXPECT_FATAL_FAILURE( |
| 58 | { |
| 59 | ProgramBuilder b1; |
| 60 | ProgramBuilder b2; |
| 61 | b1.create<UnaryOpExpression>(UnaryOp::kNot, b2.Expr(true)); |
| 62 | }, |
| 63 | "internal compiler error"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | } // namespace |
dan sinclair | 34323ac | 2022-04-07 18:39:35 +0000 | [diff] [blame] | 67 | } // namespace tint::ast |