blob: df5098801bc5dde3bc2fc15c1a464211cde2db66 [file] [log] [blame]
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001// Copyright 2021 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/fallthrough_statement.h"
16#include "src/tint/writer/glsl/test_helper.h"
17
dan sinclair5d590592022-04-07 14:40:24 +000018namespace tint::writer::glsl {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000019namespace {
20
21using GlslGeneratorImplTest_Case = TestHelper;
22
23TEST_F(GlslGeneratorImplTest_Case, Emit_Case) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000024 auto* s = Switch(1, Case(Expr(5), Block(create<ast::BreakStatement>())), DefaultCase());
25 WrapInFunction(s);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000026
dan sinclair41e4d9a2022-05-01 14:40:55 +000027 GeneratorImpl& gen = Build();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000028
dan sinclair41e4d9a2022-05-01 14:40:55 +000029 gen.increment_indent();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000030
dan sinclair41e4d9a2022-05-01 14:40:55 +000031 ASSERT_TRUE(gen.EmitCase(s->body[0])) << gen.error();
32 EXPECT_EQ(gen.result(), R"( case 5: {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000033 break;
34 }
35)");
36}
37
38TEST_F(GlslGeneratorImplTest_Case, Emit_Case_BreaksByDefault) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000039 auto* s = Switch(1, Case(Expr(5), Block()), DefaultCase());
40 WrapInFunction(s);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000041
dan sinclair41e4d9a2022-05-01 14:40:55 +000042 GeneratorImpl& gen = Build();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000043
dan sinclair41e4d9a2022-05-01 14:40:55 +000044 gen.increment_indent();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000045
dan sinclair41e4d9a2022-05-01 14:40:55 +000046 ASSERT_TRUE(gen.EmitCase(s->body[0])) << gen.error();
47 EXPECT_EQ(gen.result(), R"( case 5: {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000048 break;
49 }
50)");
51}
52
53TEST_F(GlslGeneratorImplTest_Case, Emit_Case_WithFallthrough) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000054 auto* s = Switch(1, Case(Expr(5), Block(create<ast::FallthroughStatement>())), DefaultCase());
55 WrapInFunction(s);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000056
dan sinclair41e4d9a2022-05-01 14:40:55 +000057 GeneratorImpl& gen = Build();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000058
dan sinclair41e4d9a2022-05-01 14:40:55 +000059 gen.increment_indent();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000060
dan sinclair41e4d9a2022-05-01 14:40:55 +000061 ASSERT_TRUE(gen.EmitCase(s->body[0])) << gen.error();
62 EXPECT_EQ(gen.result(), R"( case 5: {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000063 /* fallthrough */
64 }
65)");
66}
67
68TEST_F(GlslGeneratorImplTest_Case, Emit_Case_MultipleSelectors) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000069 auto* s =
70 Switch(1, Case({Expr(5), Expr(6)}, Block(create<ast::BreakStatement>())), DefaultCase());
71 WrapInFunction(s);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000072
dan sinclair41e4d9a2022-05-01 14:40:55 +000073 GeneratorImpl& gen = Build();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000074
dan sinclair41e4d9a2022-05-01 14:40:55 +000075 gen.increment_indent();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000076
dan sinclair41e4d9a2022-05-01 14:40:55 +000077 ASSERT_TRUE(gen.EmitCase(s->body[0])) << gen.error();
78 EXPECT_EQ(gen.result(), R"( case 5:
Ryan Harrisondbc13af2022-02-21 15:19:07 +000079 case 6: {
80 break;
81 }
82)");
83}
84
85TEST_F(GlslGeneratorImplTest_Case, Emit_Case_Default) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000086 auto* s = Switch(1, DefaultCase(Block(create<ast::BreakStatement>())));
87 WrapInFunction(s);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000088
dan sinclair41e4d9a2022-05-01 14:40:55 +000089 GeneratorImpl& gen = Build();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000090
dan sinclair41e4d9a2022-05-01 14:40:55 +000091 gen.increment_indent();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000092
dan sinclair41e4d9a2022-05-01 14:40:55 +000093 ASSERT_TRUE(gen.EmitCase(s->body[0])) << gen.error();
94 EXPECT_EQ(gen.result(), R"( default: {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000095 break;
96 }
97)");
98}
99
100} // namespace
dan sinclair5d590592022-04-07 14:40:24 +0000101} // namespace tint::writer::glsl