From 3e27a20f31288980326b9e6794ef50f4093a06bc Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Tue, 13 Jul 2021 22:13:11 +0000 Subject: [PATCH] writer: Fix CaseStatment tests Case statements cannot be used as regular statements. They must exist inside a switch. Change-Id: Ica2e0573ad926c3e257f02c95397a8666dfcd547 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/57705 Kokoro: Kokoro Reviewed-by: James Price Commit-Queue: James Price Auto-Submit: Ben Clayton --- src/writer/hlsl/generator_impl_case_test.cc | 48 +++++++++------------ src/writer/msl/generator_impl_case_test.cc | 48 +++++++++------------ src/writer/wgsl/generator_impl_case_test.cc | 29 ++++++------- 3 files changed, 52 insertions(+), 73 deletions(-) diff --git a/src/writer/hlsl/generator_impl_case_test.cc b/src/writer/hlsl/generator_impl_case_test.cc index 93e80a69e2..6ecd75552e 100644 --- a/src/writer/hlsl/generator_impl_case_test.cc +++ b/src/writer/hlsl/generator_impl_case_test.cc @@ -23,17 +23,15 @@ namespace { using HlslGeneratorImplTest_Case = TestHelper; TEST_F(HlslGeneratorImplTest_Case, Emit_Case) { - auto* body = Block(create()); - ast::CaseSelectorList lit; - lit.push_back(Literal(5)); - auto* c = create(lit, body); - WrapInFunction(c); + auto* s = Switch(1, Case(Literal(5), Block(create())), + DefaultCase()); + WrapInFunction(s); GeneratorImpl& gen = Build(); gen.increment_indent(); - ASSERT_TRUE(gen.EmitCase(c)) << gen.error(); + ASSERT_TRUE(gen.EmitCase(s->body()[0])) << gen.error(); EXPECT_EQ(gen.result(), R"( case 5: { break; } @@ -41,16 +39,14 @@ TEST_F(HlslGeneratorImplTest_Case, Emit_Case) { } TEST_F(HlslGeneratorImplTest_Case, Emit_Case_BreaksByDefault) { - ast::CaseSelectorList lit; - lit.push_back(Literal(5)); - auto* c = create(lit, Block()); - WrapInFunction(c); + auto* s = Switch(1, Case(Literal(5), Block()), DefaultCase()); + WrapInFunction(s); GeneratorImpl& gen = Build(); gen.increment_indent(); - ASSERT_TRUE(gen.EmitCase(c)) << gen.error(); + ASSERT_TRUE(gen.EmitCase(s->body()[0])) << gen.error(); EXPECT_EQ(gen.result(), R"( case 5: { break; } @@ -58,17 +54,16 @@ TEST_F(HlslGeneratorImplTest_Case, Emit_Case_BreaksByDefault) { } TEST_F(HlslGeneratorImplTest_Case, Emit_Case_WithFallthrough) { - auto* body = Block(create()); - ast::CaseSelectorList lit; - lit.push_back(Literal(5)); - auto* c = create(lit, body); - WrapInFunction(c); + auto* s = + Switch(1, Case(Literal(5), Block(create())), + DefaultCase()); + WrapInFunction(s); GeneratorImpl& gen = Build(); gen.increment_indent(); - ASSERT_TRUE(gen.EmitCase(c)) << gen.error(); + ASSERT_TRUE(gen.EmitCase(s->body()[0])) << gen.error(); EXPECT_EQ(gen.result(), R"( case 5: { /* fallthrough */ } @@ -76,18 +71,16 @@ TEST_F(HlslGeneratorImplTest_Case, Emit_Case_WithFallthrough) { } TEST_F(HlslGeneratorImplTest_Case, Emit_Case_MultipleSelectors) { - auto* body = Block(create()); - ast::CaseSelectorList lit; - lit.push_back(Literal(5)); - lit.push_back(Literal(6)); - auto* c = create(lit, body); - WrapInFunction(c); + auto* s = Switch( + 1, Case({Literal(5), Literal(6)}, Block(create())), + DefaultCase()); + WrapInFunction(s); GeneratorImpl& gen = Build(); gen.increment_indent(); - ASSERT_TRUE(gen.EmitCase(c)) << gen.error(); + ASSERT_TRUE(gen.EmitCase(s->body()[0])) << gen.error(); EXPECT_EQ(gen.result(), R"( case 5: case 6: { break; @@ -96,15 +89,14 @@ TEST_F(HlslGeneratorImplTest_Case, Emit_Case_MultipleSelectors) { } TEST_F(HlslGeneratorImplTest_Case, Emit_Case_Default) { - auto* body = Block(create()); - auto* c = create(ast::CaseSelectorList{}, body); - WrapInFunction(c); + auto* s = Switch(1, DefaultCase(Block(create()))); + WrapInFunction(s); GeneratorImpl& gen = Build(); gen.increment_indent(); - ASSERT_TRUE(gen.EmitCase(c)) << gen.error(); + ASSERT_TRUE(gen.EmitCase(s->body()[0])) << gen.error(); EXPECT_EQ(gen.result(), R"( default: { break; } diff --git a/src/writer/msl/generator_impl_case_test.cc b/src/writer/msl/generator_impl_case_test.cc index 4b8cd3461e..42b94826fd 100644 --- a/src/writer/msl/generator_impl_case_test.cc +++ b/src/writer/msl/generator_impl_case_test.cc @@ -23,17 +23,15 @@ namespace { using MslGeneratorImplTest = TestHelper; TEST_F(MslGeneratorImplTest, Emit_Case) { - auto* body = Block(create()); - ast::CaseSelectorList lit; - lit.push_back(Literal(5)); - auto* c = create(lit, body); - WrapInFunction(c); + auto* s = Switch(1, Case(Literal(5), Block(create())), + DefaultCase()); + WrapInFunction(s); GeneratorImpl& gen = Build(); gen.increment_indent(); - ASSERT_TRUE(gen.EmitCase(c)) << gen.error(); + ASSERT_TRUE(gen.EmitCase(s->body()[0])) << gen.error(); EXPECT_EQ(gen.result(), R"( case 5: { break; } @@ -41,16 +39,14 @@ TEST_F(MslGeneratorImplTest, Emit_Case) { } TEST_F(MslGeneratorImplTest, Emit_Case_BreaksByDefault) { - ast::CaseSelectorList lit; - lit.push_back(Literal(5)); - auto* c = create(lit, Block()); - WrapInFunction(c); + auto* s = Switch(1, Case(Literal(5), Block()), DefaultCase()); + WrapInFunction(s); GeneratorImpl& gen = Build(); gen.increment_indent(); - ASSERT_TRUE(gen.EmitCase(c)) << gen.error(); + ASSERT_TRUE(gen.EmitCase(s->body()[0])) << gen.error(); EXPECT_EQ(gen.result(), R"( case 5: { break; } @@ -58,17 +54,16 @@ TEST_F(MslGeneratorImplTest, Emit_Case_BreaksByDefault) { } TEST_F(MslGeneratorImplTest, Emit_Case_WithFallthrough) { - auto* body = Block(create()); - ast::CaseSelectorList lit; - lit.push_back(Literal(5)); - auto* c = create(lit, body); - WrapInFunction(c); + auto* s = + Switch(1, Case(Literal(5), Block(create())), + DefaultCase()); + WrapInFunction(s); GeneratorImpl& gen = Build(); gen.increment_indent(); - ASSERT_TRUE(gen.EmitCase(c)) << gen.error(); + ASSERT_TRUE(gen.EmitCase(s->body()[0])) << gen.error(); EXPECT_EQ(gen.result(), R"( case 5: { /* fallthrough */ } @@ -76,18 +71,16 @@ TEST_F(MslGeneratorImplTest, Emit_Case_WithFallthrough) { } TEST_F(MslGeneratorImplTest, Emit_Case_MultipleSelectors) { - auto* body = Block(create()); - ast::CaseSelectorList lit; - lit.push_back(Literal(5)); - lit.push_back(Literal(6)); - auto* c = create(lit, body); - WrapInFunction(c); + auto* s = Switch( + 1, Case({Literal(5), Literal(6)}, Block(create())), + DefaultCase()); + WrapInFunction(s); GeneratorImpl& gen = Build(); gen.increment_indent(); - ASSERT_TRUE(gen.EmitCase(c)) << gen.error(); + ASSERT_TRUE(gen.EmitCase(s->body()[0])) << gen.error(); EXPECT_EQ(gen.result(), R"( case 5: case 6: { break; @@ -96,15 +89,14 @@ TEST_F(MslGeneratorImplTest, Emit_Case_MultipleSelectors) { } TEST_F(MslGeneratorImplTest, Emit_Case_Default) { - auto* body = Block(create()); - auto* c = create(ast::CaseSelectorList{}, body); - WrapInFunction(c); + auto* s = Switch(1, DefaultCase(Block(create()))); + WrapInFunction(s); GeneratorImpl& gen = Build(); gen.increment_indent(); - ASSERT_TRUE(gen.EmitCase(c)) << gen.error(); + ASSERT_TRUE(gen.EmitCase(s->body()[0])) << gen.error(); EXPECT_EQ(gen.result(), R"( default: { break; } diff --git a/src/writer/wgsl/generator_impl_case_test.cc b/src/writer/wgsl/generator_impl_case_test.cc index 04538ccb77..e03ee56e5c 100644 --- a/src/writer/wgsl/generator_impl_case_test.cc +++ b/src/writer/wgsl/generator_impl_case_test.cc @@ -22,17 +22,15 @@ namespace { using WgslGeneratorImplTest = TestHelper; TEST_F(WgslGeneratorImplTest, Emit_Case) { - auto* body = Block(create()); - ast::CaseSelectorList lit; - lit.push_back(Literal(5)); - auto* c = create(lit, body); - WrapInFunction(c); + auto* s = Switch(1, Case(Literal(5), Block(create())), + DefaultCase()); + WrapInFunction(s); GeneratorImpl& gen = Build(); gen.increment_indent(); - ASSERT_TRUE(gen.EmitCase(c)) << gen.error(); + ASSERT_TRUE(gen.EmitCase(s->body()[0])) << gen.error(); EXPECT_EQ(gen.result(), R"( case 5: { break; } @@ -40,18 +38,16 @@ TEST_F(WgslGeneratorImplTest, Emit_Case) { } TEST_F(WgslGeneratorImplTest, Emit_Case_MultipleSelectors) { - auto* body = Block(create()); - ast::CaseSelectorList lit; - lit.push_back(Literal(5)); - lit.push_back(Literal(6)); - auto* c = create(lit, body); - WrapInFunction(c); + auto* s = Switch( + 1, Case({Literal(5), Literal(6)}, Block(create())), + DefaultCase()); + WrapInFunction(s); GeneratorImpl& gen = Build(); gen.increment_indent(); - ASSERT_TRUE(gen.EmitCase(c)) << gen.error(); + ASSERT_TRUE(gen.EmitCase(s->body()[0])) << gen.error(); EXPECT_EQ(gen.result(), R"( case 5, 6: { break; } @@ -59,15 +55,14 @@ TEST_F(WgslGeneratorImplTest, Emit_Case_MultipleSelectors) { } TEST_F(WgslGeneratorImplTest, Emit_Case_Default) { - auto* body = Block(create()); - auto* c = create(ast::CaseSelectorList{}, body); - WrapInFunction(c); + auto* s = Switch(1, DefaultCase(Block(create()))); + WrapInFunction(s); GeneratorImpl& gen = Build(); gen.increment_indent(); - ASSERT_TRUE(gen.EmitCase(c)) << gen.error(); + ASSERT_TRUE(gen.EmitCase(s->body()[0])) << gen.error(); EXPECT_EQ(gen.result(), R"( default: { break; }