James Price | ebe9741 | 2022-04-07 13:42:45 +0000 | [diff] [blame] | 1 | // Copyright 2022 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/increment_decrement_statement.h" |
| 16 | |
| 17 | #include "gtest/gtest-spi.h" |
| 18 | #include "src/tint/ast/test_helper.h" |
| 19 | |
| 20 | namespace tint::ast { |
| 21 | namespace { |
| 22 | |
| 23 | using IncrementDecrementStatementTest = TestHelper; |
| 24 | |
| 25 | TEST_F(IncrementDecrementStatementTest, Creation) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 26 | auto* expr = Expr("expr"); |
James Price | ebe9741 | 2022-04-07 13:42:45 +0000 | [diff] [blame] | 27 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 28 | auto* i = create<IncrementDecrementStatement>(expr, true); |
| 29 | EXPECT_EQ(i->lhs, expr); |
| 30 | EXPECT_TRUE(i->increment); |
James Price | ebe9741 | 2022-04-07 13:42:45 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | TEST_F(IncrementDecrementStatementTest, Creation_WithSource) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 34 | auto* expr = Expr("expr"); |
| 35 | auto* i = create<IncrementDecrementStatement>(Source{Source::Location{20, 2}}, expr, true); |
| 36 | auto src = i->source; |
| 37 | EXPECT_EQ(i->lhs, expr); |
| 38 | EXPECT_TRUE(i->increment); |
| 39 | EXPECT_EQ(src.range.begin.line, 20u); |
| 40 | EXPECT_EQ(src.range.begin.column, 2u); |
James Price | ebe9741 | 2022-04-07 13:42:45 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | TEST_F(IncrementDecrementStatementTest, IsIncrementDecrement) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 44 | auto* expr = Expr("expr"); |
| 45 | auto* i = create<IncrementDecrementStatement>(expr, true); |
| 46 | EXPECT_TRUE(i->Is<IncrementDecrementStatement>()); |
James Price | ebe9741 | 2022-04-07 13:42:45 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | TEST_F(IncrementDecrementStatementTest, Decrement) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 50 | auto* expr = Expr("expr"); |
| 51 | auto* i = create<IncrementDecrementStatement>(expr, false); |
| 52 | EXPECT_EQ(i->lhs, expr); |
| 53 | EXPECT_FALSE(i->increment); |
James Price | ebe9741 | 2022-04-07 13:42:45 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | TEST_F(IncrementDecrementStatementTest, Assert_DifferentProgramID_Expr) { |
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<IncrementDecrementStatement>(b2.Expr(true), true); |
| 62 | }, |
| 63 | "internal compiler error"); |
James Price | ebe9741 | 2022-04-07 13:42:45 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | } // namespace |
| 67 | } // namespace tint::ast |