blob: 968b03a6dc8542e3e9d1df6906886dd22b6df66e [file] [log] [blame]
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001// 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/writer/wgsl/test_helper.h"
16
dan sinclair27c77222023-04-13 02:57:27 +000017#include "gmock/gmock.h"
18
dan sinclair9f54a512022-04-07 11:08:24 +000019namespace tint::writer::wgsl {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000020namespace {
21
22using WgslGeneratorImplTest = TestHelper;
23
24TEST_F(WgslGeneratorImplTest, Emit_If) {
dan sinclair2a651632023-02-19 04:03:55 +000025 GlobalVar("cond", ty.bool_(), builtin::AddressSpace::kPrivate);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000026
dan sinclair41e4d9a2022-05-01 14:40:55 +000027 auto* cond = Expr("cond");
28 auto* body = Block(Return());
29 auto* i = If(cond, body);
30 WrapInFunction(i);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000031
dan sinclair41e4d9a2022-05-01 14:40:55 +000032 GeneratorImpl& gen = Build();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000033
dan sinclair41e4d9a2022-05-01 14:40:55 +000034 gen.increment_indent();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000035
dan sinclair27c77222023-04-13 02:57:27 +000036 gen.EmitStatement(i);
37 EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
dan sinclair41e4d9a2022-05-01 14:40:55 +000038 EXPECT_EQ(gen.result(), R"( if (cond) {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000039 return;
40 }
41)");
42}
43
44TEST_F(WgslGeneratorImplTest, Emit_IfWithElseIf) {
dan sinclair2a651632023-02-19 04:03:55 +000045 GlobalVar("cond", ty.bool_(), builtin::AddressSpace::kPrivate);
46 GlobalVar("else_cond", ty.bool_(), builtin::AddressSpace::kPrivate);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000047
dan sinclair41e4d9a2022-05-01 14:40:55 +000048 auto* else_cond = Expr("else_cond");
49 auto* else_body = Block(Return());
Ryan Harrisondbc13af2022-02-21 15:19:07 +000050
dan sinclair41e4d9a2022-05-01 14:40:55 +000051 auto* cond = Expr("cond");
52 auto* body = Block(Return());
James Price8aff0ed2022-05-02 14:53:36 +000053 auto* i = If(cond, body, Else(If(else_cond, else_body)));
dan sinclair41e4d9a2022-05-01 14:40:55 +000054 WrapInFunction(i);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000055
dan sinclair41e4d9a2022-05-01 14:40:55 +000056 GeneratorImpl& gen = Build();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000057
dan sinclair41e4d9a2022-05-01 14:40:55 +000058 gen.increment_indent();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000059
dan sinclair27c77222023-04-13 02:57:27 +000060 gen.EmitStatement(i);
61 EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
dan sinclair41e4d9a2022-05-01 14:40:55 +000062 EXPECT_EQ(gen.result(), R"( if (cond) {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000063 return;
64 } else if (else_cond) {
65 return;
66 }
67)");
68}
69
70TEST_F(WgslGeneratorImplTest, Emit_IfWithElse) {
dan sinclair2a651632023-02-19 04:03:55 +000071 GlobalVar("cond", ty.bool_(), builtin::AddressSpace::kPrivate);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000072
dan sinclair41e4d9a2022-05-01 14:40:55 +000073 auto* else_body = Block(Return());
Ryan Harrisondbc13af2022-02-21 15:19:07 +000074
dan sinclair41e4d9a2022-05-01 14:40:55 +000075 auto* cond = Expr("cond");
76 auto* body = Block(Return());
James Price8aff0ed2022-05-02 14:53:36 +000077 auto* i = If(cond, body, Else(else_body));
dan sinclair41e4d9a2022-05-01 14:40:55 +000078 WrapInFunction(i);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000079
dan sinclair41e4d9a2022-05-01 14:40:55 +000080 GeneratorImpl& gen = Build();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000081
dan sinclair41e4d9a2022-05-01 14:40:55 +000082 gen.increment_indent();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000083
dan sinclair27c77222023-04-13 02:57:27 +000084 gen.EmitStatement(i);
85 EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
dan sinclair41e4d9a2022-05-01 14:40:55 +000086 EXPECT_EQ(gen.result(), R"( if (cond) {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000087 return;
88 } else {
89 return;
90 }
91)");
92}
93
94TEST_F(WgslGeneratorImplTest, Emit_IfWithMultiple) {
dan sinclair2a651632023-02-19 04:03:55 +000095 GlobalVar("cond", ty.bool_(), builtin::AddressSpace::kPrivate);
96 GlobalVar("else_cond", ty.bool_(), builtin::AddressSpace::kPrivate);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000097
dan sinclair41e4d9a2022-05-01 14:40:55 +000098 auto* else_cond = Expr("else_cond");
Ryan Harrisondbc13af2022-02-21 15:19:07 +000099
dan sinclair41e4d9a2022-05-01 14:40:55 +0000100 auto* else_body = Block(Return());
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000101
dan sinclair41e4d9a2022-05-01 14:40:55 +0000102 auto* else_body_2 = Block(Return());
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000103
dan sinclair41e4d9a2022-05-01 14:40:55 +0000104 auto* cond = Expr("cond");
105 auto* body = Block(Return());
James Price8aff0ed2022-05-02 14:53:36 +0000106 auto* i = If(cond, body, Else(If(else_cond, else_body, Else(else_body_2))));
dan sinclair41e4d9a2022-05-01 14:40:55 +0000107 WrapInFunction(i);
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000108
dan sinclair41e4d9a2022-05-01 14:40:55 +0000109 GeneratorImpl& gen = Build();
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000110
dan sinclair41e4d9a2022-05-01 14:40:55 +0000111 gen.increment_indent();
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000112
dan sinclair27c77222023-04-13 02:57:27 +0000113 gen.EmitStatement(i);
114 EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
dan sinclair41e4d9a2022-05-01 14:40:55 +0000115 EXPECT_EQ(gen.result(), R"( if (cond) {
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000116 return;
117 } else if (else_cond) {
118 return;
119 } else {
120 return;
121 }
122)");
123}
124
125} // namespace
dan sinclair9f54a512022-04-07 11:08:24 +0000126} // namespace tint::writer::wgsl