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/writer/hlsl/test_helper.h"
16
17 namespace tint {
18 namespace writer {
19 namespace hlsl {
20 namespace {
21
22 using HlslGeneratorImplTest_If = TestHelper;
23
TEST_F(HlslGeneratorImplTest_If,Emit_If)24 TEST_F(HlslGeneratorImplTest_If, Emit_If) {
25 Global("cond", ty.bool_(), ast::StorageClass::kPrivate);
26
27 auto* cond = Expr("cond");
28 auto* body = Block(Return());
29 auto* i = If(cond, body);
30 WrapInFunction(i);
31
32 GeneratorImpl& gen = Build();
33
34 gen.increment_indent();
35 ASSERT_TRUE(gen.EmitStatement(i)) << gen.error();
36 EXPECT_EQ(gen.result(), R"( if (cond) {
37 return;
38 }
39 )");
40 }
41
TEST_F(HlslGeneratorImplTest_If,Emit_IfWithElseIf)42 TEST_F(HlslGeneratorImplTest_If, Emit_IfWithElseIf) {
43 Global("cond", ty.bool_(), ast::StorageClass::kPrivate);
44 Global("else_cond", ty.bool_(), ast::StorageClass::kPrivate);
45
46 auto* else_cond = Expr("else_cond");
47 auto* else_body = Block(Return());
48
49 auto* cond = Expr("cond");
50 auto* body = Block(Return());
51 auto* i = If(
52 cond, body,
53 ast::ElseStatementList{create<ast::ElseStatement>(else_cond, else_body)});
54 WrapInFunction(i);
55
56 GeneratorImpl& gen = Build();
57
58 gen.increment_indent();
59
60 ASSERT_TRUE(gen.EmitStatement(i)) << gen.error();
61 EXPECT_EQ(gen.result(), R"( if (cond) {
62 return;
63 } else {
64 if (else_cond) {
65 return;
66 }
67 }
68 )");
69 }
70
TEST_F(HlslGeneratorImplTest_If,Emit_IfWithElse)71 TEST_F(HlslGeneratorImplTest_If, Emit_IfWithElse) {
72 Global("cond", ty.bool_(), ast::StorageClass::kPrivate);
73
74 auto* else_body = Block(Return());
75
76 auto* cond = Expr("cond");
77 auto* body = Block(Return());
78 auto* i = If(
79 cond, body,
80 ast::ElseStatementList{create<ast::ElseStatement>(nullptr, else_body)});
81 WrapInFunction(i);
82
83 GeneratorImpl& gen = Build();
84
85 gen.increment_indent();
86
87 ASSERT_TRUE(gen.EmitStatement(i)) << gen.error();
88 EXPECT_EQ(gen.result(), R"( if (cond) {
89 return;
90 } else {
91 return;
92 }
93 )");
94 }
95
TEST_F(HlslGeneratorImplTest_If,Emit_IfWithMultiple)96 TEST_F(HlslGeneratorImplTest_If, Emit_IfWithMultiple) {
97 Global("cond", ty.bool_(), ast::StorageClass::kPrivate);
98 Global("else_cond", ty.bool_(), ast::StorageClass::kPrivate);
99
100 auto* else_cond = Expr("else_cond");
101
102 auto* else_body = Block(Return());
103
104 auto* else_body_2 = Block(Return());
105
106 auto* cond = Expr("cond");
107 auto* body = Block(Return());
108 auto* i = If(cond, body,
109 ast::ElseStatementList{
110 create<ast::ElseStatement>(else_cond, else_body),
111 create<ast::ElseStatement>(nullptr, else_body_2),
112 });
113 WrapInFunction(i);
114
115 GeneratorImpl& gen = Build();
116
117 gen.increment_indent();
118
119 ASSERT_TRUE(gen.EmitStatement(i)) << gen.error();
120 EXPECT_EQ(gen.result(), R"( if (cond) {
121 return;
122 } else {
123 if (else_cond) {
124 return;
125 } else {
126 return;
127 }
128 }
129 )");
130 }
131
132 } // namespace
133 } // namespace hlsl
134 } // namespace writer
135 } // namespace tint
136