blob: a1d69806dbe1dadfc4610bbe51ed79c861c0f83f [file] [log] [blame]
dan sinclair97bc4ad2020-07-30 22:26:55 +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
dan sinclair97bc4ad2020-07-30 22:26:55 +000015#include "src/ast/else_statement.h"
16#include "src/ast/identifier_expression.h"
17#include "src/ast/if_statement.h"
18#include "src/ast/module.h"
19#include "src/ast/return_statement.h"
dan sinclairdf503f02020-08-26 19:05:46 +000020#include "src/writer/hlsl/test_helper.h"
dan sinclair97bc4ad2020-07-30 22:26:55 +000021
22namespace tint {
23namespace writer {
24namespace hlsl {
25namespace {
26
dan sinclair63b007d2020-08-26 20:02:26 +000027using HlslGeneratorImplTest_If = TestHelper;
dan sinclair97bc4ad2020-07-30 22:26:55 +000028
dan sinclairdf503f02020-08-26 19:05:46 +000029TEST_F(HlslGeneratorImplTest_If, Emit_If) {
Ben Clayton9df857d2020-12-15 14:11:48 +000030 auto* cond = Expr("cond");
31 auto* body = create<ast::BlockStatement>(ast::StatementList{
32 create<ast::ReturnStatement>(),
33 });
34 auto* i = create<ast::IfStatement>(cond, body, ast::ElseStatementList{});
dan sinclair196e0972020-11-13 18:13:24 +000035 gen.increment_indent();
dan sinclair97bc4ad2020-07-30 22:26:55 +000036
Ben Clayton9df857d2020-12-15 14:11:48 +000037 ASSERT_TRUE(gen.EmitStatement(out, i)) << gen.error();
dan sinclairf4bc0e72021-01-11 16:24:32 +000038 EXPECT_EQ(result(), R"( if (test_cond) {
dan sinclair97bc4ad2020-07-30 22:26:55 +000039 return;
40 }
41)");
42}
43
dan sinclairdf503f02020-08-26 19:05:46 +000044TEST_F(HlslGeneratorImplTest_If, Emit_IfWithElseIf) {
Ben Clayton9df857d2020-12-15 14:11:48 +000045 auto* else_cond = Expr("else_cond");
46 auto* else_body = create<ast::BlockStatement>(ast::StatementList{
47 create<ast::ReturnStatement>(),
48 });
dan sinclair97bc4ad2020-07-30 22:26:55 +000049
Ben Clayton9df857d2020-12-15 14:11:48 +000050 auto* cond = Expr("cond");
51 auto* body = create<ast::BlockStatement>(ast::StatementList{
52 create<ast::ReturnStatement>(),
53 });
54 auto* i = create<ast::IfStatement>(
55 cond, body,
56 ast::ElseStatementList{create<ast::ElseStatement>(else_cond, else_body)});
dan sinclair97bc4ad2020-07-30 22:26:55 +000057
dan sinclair196e0972020-11-13 18:13:24 +000058 gen.increment_indent();
dan sinclair97bc4ad2020-07-30 22:26:55 +000059
Ben Clayton9df857d2020-12-15 14:11:48 +000060 ASSERT_TRUE(gen.EmitStatement(out, i)) << gen.error();
dan sinclairf4bc0e72021-01-11 16:24:32 +000061 EXPECT_EQ(result(), R"( if (test_cond) {
dan sinclair97bc4ad2020-07-30 22:26:55 +000062 return;
dan sinclaira0842b52020-09-02 14:29:05 +000063 } else {
dan sinclairf4bc0e72021-01-11 16:24:32 +000064 if (test_else_cond) {
dan sinclaira0842b52020-09-02 14:29:05 +000065 return;
66 }
dan sinclair97bc4ad2020-07-30 22:26:55 +000067 }
68)");
69}
70
dan sinclairdf503f02020-08-26 19:05:46 +000071TEST_F(HlslGeneratorImplTest_If, Emit_IfWithElse) {
Ben Clayton9df857d2020-12-15 14:11:48 +000072 auto* else_body = create<ast::BlockStatement>(ast::StatementList{
73 create<ast::ReturnStatement>(),
74 });
dan sinclair97bc4ad2020-07-30 22:26:55 +000075
Ben Clayton9df857d2020-12-15 14:11:48 +000076 auto* cond = Expr("cond");
77 auto* body = create<ast::BlockStatement>(ast::StatementList{
78 create<ast::ReturnStatement>(),
79 });
80 auto* i = create<ast::IfStatement>(
81 cond, body,
82 ast::ElseStatementList{create<ast::ElseStatement>(nullptr, else_body)});
dan sinclair97bc4ad2020-07-30 22:26:55 +000083
dan sinclair196e0972020-11-13 18:13:24 +000084 gen.increment_indent();
dan sinclair97bc4ad2020-07-30 22:26:55 +000085
Ben Clayton9df857d2020-12-15 14:11:48 +000086 ASSERT_TRUE(gen.EmitStatement(out, i)) << gen.error();
dan sinclairf4bc0e72021-01-11 16:24:32 +000087 EXPECT_EQ(result(), R"( if (test_cond) {
dan sinclair97bc4ad2020-07-30 22:26:55 +000088 return;
89 } else {
90 return;
91 }
92)");
93}
94
dan sinclairdf503f02020-08-26 19:05:46 +000095TEST_F(HlslGeneratorImplTest_If, Emit_IfWithMultiple) {
Ben Clayton9df857d2020-12-15 14:11:48 +000096 auto* else_cond = Expr("else_cond");
dan sinclair97bc4ad2020-07-30 22:26:55 +000097
Ben Clayton9df857d2020-12-15 14:11:48 +000098 auto* else_body = create<ast::BlockStatement>(ast::StatementList{
99 create<ast::ReturnStatement>(),
100 });
dan sinclair97bc4ad2020-07-30 22:26:55 +0000101
Ben Clayton9df857d2020-12-15 14:11:48 +0000102 auto* else_body_2 = create<ast::BlockStatement>(ast::StatementList{
103 create<ast::ReturnStatement>(),
104 });
dan sinclair97bc4ad2020-07-30 22:26:55 +0000105
Ben Clayton9df857d2020-12-15 14:11:48 +0000106 auto* cond = Expr("cond");
107 auto* body = create<ast::BlockStatement>(ast::StatementList{
108 create<ast::ReturnStatement>(),
109 });
110 auto* i = create<ast::IfStatement>(
111 cond, body,
112 ast::ElseStatementList{
113 create<ast::ElseStatement>(else_cond, else_body),
114 create<ast::ElseStatement>(nullptr, else_body_2),
Ben Claytonbbefff62020-12-12 11:58:44 +0000115 });
dan sinclair97bc4ad2020-07-30 22:26:55 +0000116
dan sinclair196e0972020-11-13 18:13:24 +0000117 gen.increment_indent();
dan sinclair97bc4ad2020-07-30 22:26:55 +0000118
Ben Clayton9df857d2020-12-15 14:11:48 +0000119 ASSERT_TRUE(gen.EmitStatement(out, i)) << gen.error();
dan sinclairf4bc0e72021-01-11 16:24:32 +0000120 EXPECT_EQ(result(), R"( if (test_cond) {
dan sinclair97bc4ad2020-07-30 22:26:55 +0000121 return;
dan sinclair97bc4ad2020-07-30 22:26:55 +0000122 } else {
dan sinclairf4bc0e72021-01-11 16:24:32 +0000123 if (test_else_cond) {
dan sinclaira0842b52020-09-02 14:29:05 +0000124 return;
125 } else {
126 return;
127 }
dan sinclair97bc4ad2020-07-30 22:26:55 +0000128 }
129)");
130}
131
132} // namespace
133} // namespace hlsl
134} // namespace writer
135} // namespace tint