blob: 6a5ae67df917ad11c06ed6b127eee9bbe0721f61 [file] [log] [blame]
James Priceebe97412022-04-07 13:42:45 +00001// 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
20namespace tint::ast {
21namespace {
22
23using IncrementDecrementStatementTest = TestHelper;
24
25TEST_F(IncrementDecrementStatementTest, Creation) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000026 auto* expr = Expr("expr");
James Priceebe97412022-04-07 13:42:45 +000027
dan sinclair41e4d9a2022-05-01 14:40:55 +000028 auto* i = create<IncrementDecrementStatement>(expr, true);
29 EXPECT_EQ(i->lhs, expr);
30 EXPECT_TRUE(i->increment);
James Priceebe97412022-04-07 13:42:45 +000031}
32
33TEST_F(IncrementDecrementStatementTest, Creation_WithSource) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000034 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 Priceebe97412022-04-07 13:42:45 +000041}
42
43TEST_F(IncrementDecrementStatementTest, IsIncrementDecrement) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000044 auto* expr = Expr("expr");
45 auto* i = create<IncrementDecrementStatement>(expr, true);
46 EXPECT_TRUE(i->Is<IncrementDecrementStatement>());
James Priceebe97412022-04-07 13:42:45 +000047}
48
49TEST_F(IncrementDecrementStatementTest, Decrement) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000050 auto* expr = Expr("expr");
51 auto* i = create<IncrementDecrementStatement>(expr, false);
52 EXPECT_EQ(i->lhs, expr);
53 EXPECT_FALSE(i->increment);
James Priceebe97412022-04-07 13:42:45 +000054}
55
56TEST_F(IncrementDecrementStatementTest, Assert_DifferentProgramID_Expr) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000057 EXPECT_FATAL_FAILURE(
58 {
59 ProgramBuilder b1;
60 ProgramBuilder b2;
61 b1.create<IncrementDecrementStatement>(b2.Expr(true), true);
62 },
63 "internal compiler error");
James Priceebe97412022-04-07 13:42:45 +000064}
65
66} // namespace
67} // namespace tint::ast