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 <noreply+kokoro@google.com>
Reviewed-by: James Price <jrprice@google.com>
Commit-Queue: James Price <jrprice@google.com>
Auto-Submit: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2021-07-13 22:13:11 +00:00 committed by Tint LUCI CQ
parent 8751227258
commit 3e27a20f31
3 changed files with 52 additions and 73 deletions

View File

@ -23,17 +23,15 @@ namespace {
using HlslGeneratorImplTest_Case = TestHelper; using HlslGeneratorImplTest_Case = TestHelper;
TEST_F(HlslGeneratorImplTest_Case, Emit_Case) { TEST_F(HlslGeneratorImplTest_Case, Emit_Case) {
auto* body = Block(create<ast::BreakStatement>()); auto* s = Switch(1, Case(Literal(5), Block(create<ast::BreakStatement>())),
ast::CaseSelectorList lit; DefaultCase());
lit.push_back(Literal(5)); WrapInFunction(s);
auto* c = create<ast::CaseStatement>(lit, body);
WrapInFunction(c);
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
gen.increment_indent(); 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: { EXPECT_EQ(gen.result(), R"( case 5: {
break; break;
} }
@ -41,16 +39,14 @@ TEST_F(HlslGeneratorImplTest_Case, Emit_Case) {
} }
TEST_F(HlslGeneratorImplTest_Case, Emit_Case_BreaksByDefault) { TEST_F(HlslGeneratorImplTest_Case, Emit_Case_BreaksByDefault) {
ast::CaseSelectorList lit; auto* s = Switch(1, Case(Literal(5), Block()), DefaultCase());
lit.push_back(Literal(5)); WrapInFunction(s);
auto* c = create<ast::CaseStatement>(lit, Block());
WrapInFunction(c);
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
gen.increment_indent(); 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: { EXPECT_EQ(gen.result(), R"( case 5: {
break; break;
} }
@ -58,17 +54,16 @@ TEST_F(HlslGeneratorImplTest_Case, Emit_Case_BreaksByDefault) {
} }
TEST_F(HlslGeneratorImplTest_Case, Emit_Case_WithFallthrough) { TEST_F(HlslGeneratorImplTest_Case, Emit_Case_WithFallthrough) {
auto* body = Block(create<ast::FallthroughStatement>()); auto* s =
ast::CaseSelectorList lit; Switch(1, Case(Literal(5), Block(create<ast::FallthroughStatement>())),
lit.push_back(Literal(5)); DefaultCase());
auto* c = create<ast::CaseStatement>(lit, body); WrapInFunction(s);
WrapInFunction(c);
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
gen.increment_indent(); 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: { EXPECT_EQ(gen.result(), R"( case 5: {
/* fallthrough */ /* fallthrough */
} }
@ -76,18 +71,16 @@ TEST_F(HlslGeneratorImplTest_Case, Emit_Case_WithFallthrough) {
} }
TEST_F(HlslGeneratorImplTest_Case, Emit_Case_MultipleSelectors) { TEST_F(HlslGeneratorImplTest_Case, Emit_Case_MultipleSelectors) {
auto* body = Block(create<ast::BreakStatement>()); auto* s = Switch(
ast::CaseSelectorList lit; 1, Case({Literal(5), Literal(6)}, Block(create<ast::BreakStatement>())),
lit.push_back(Literal(5)); DefaultCase());
lit.push_back(Literal(6)); WrapInFunction(s);
auto* c = create<ast::CaseStatement>(lit, body);
WrapInFunction(c);
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
gen.increment_indent(); 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: EXPECT_EQ(gen.result(), R"( case 5:
case 6: { case 6: {
break; break;
@ -96,15 +89,14 @@ TEST_F(HlslGeneratorImplTest_Case, Emit_Case_MultipleSelectors) {
} }
TEST_F(HlslGeneratorImplTest_Case, Emit_Case_Default) { TEST_F(HlslGeneratorImplTest_Case, Emit_Case_Default) {
auto* body = Block(create<ast::BreakStatement>()); auto* s = Switch(1, DefaultCase(Block(create<ast::BreakStatement>())));
auto* c = create<ast::CaseStatement>(ast::CaseSelectorList{}, body); WrapInFunction(s);
WrapInFunction(c);
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
gen.increment_indent(); 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: { EXPECT_EQ(gen.result(), R"( default: {
break; break;
} }

View File

@ -23,17 +23,15 @@ namespace {
using MslGeneratorImplTest = TestHelper; using MslGeneratorImplTest = TestHelper;
TEST_F(MslGeneratorImplTest, Emit_Case) { TEST_F(MslGeneratorImplTest, Emit_Case) {
auto* body = Block(create<ast::BreakStatement>()); auto* s = Switch(1, Case(Literal(5), Block(create<ast::BreakStatement>())),
ast::CaseSelectorList lit; DefaultCase());
lit.push_back(Literal(5)); WrapInFunction(s);
auto* c = create<ast::CaseStatement>(lit, body);
WrapInFunction(c);
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
gen.increment_indent(); 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: { EXPECT_EQ(gen.result(), R"( case 5: {
break; break;
} }
@ -41,16 +39,14 @@ TEST_F(MslGeneratorImplTest, Emit_Case) {
} }
TEST_F(MslGeneratorImplTest, Emit_Case_BreaksByDefault) { TEST_F(MslGeneratorImplTest, Emit_Case_BreaksByDefault) {
ast::CaseSelectorList lit; auto* s = Switch(1, Case(Literal(5), Block()), DefaultCase());
lit.push_back(Literal(5)); WrapInFunction(s);
auto* c = create<ast::CaseStatement>(lit, Block());
WrapInFunction(c);
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
gen.increment_indent(); 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: { EXPECT_EQ(gen.result(), R"( case 5: {
break; break;
} }
@ -58,17 +54,16 @@ TEST_F(MslGeneratorImplTest, Emit_Case_BreaksByDefault) {
} }
TEST_F(MslGeneratorImplTest, Emit_Case_WithFallthrough) { TEST_F(MslGeneratorImplTest, Emit_Case_WithFallthrough) {
auto* body = Block(create<ast::FallthroughStatement>()); auto* s =
ast::CaseSelectorList lit; Switch(1, Case(Literal(5), Block(create<ast::FallthroughStatement>())),
lit.push_back(Literal(5)); DefaultCase());
auto* c = create<ast::CaseStatement>(lit, body); WrapInFunction(s);
WrapInFunction(c);
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
gen.increment_indent(); 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: { EXPECT_EQ(gen.result(), R"( case 5: {
/* fallthrough */ /* fallthrough */
} }
@ -76,18 +71,16 @@ TEST_F(MslGeneratorImplTest, Emit_Case_WithFallthrough) {
} }
TEST_F(MslGeneratorImplTest, Emit_Case_MultipleSelectors) { TEST_F(MslGeneratorImplTest, Emit_Case_MultipleSelectors) {
auto* body = Block(create<ast::BreakStatement>()); auto* s = Switch(
ast::CaseSelectorList lit; 1, Case({Literal(5), Literal(6)}, Block(create<ast::BreakStatement>())),
lit.push_back(Literal(5)); DefaultCase());
lit.push_back(Literal(6)); WrapInFunction(s);
auto* c = create<ast::CaseStatement>(lit, body);
WrapInFunction(c);
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
gen.increment_indent(); 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: EXPECT_EQ(gen.result(), R"( case 5:
case 6: { case 6: {
break; break;
@ -96,15 +89,14 @@ TEST_F(MslGeneratorImplTest, Emit_Case_MultipleSelectors) {
} }
TEST_F(MslGeneratorImplTest, Emit_Case_Default) { TEST_F(MslGeneratorImplTest, Emit_Case_Default) {
auto* body = Block(create<ast::BreakStatement>()); auto* s = Switch(1, DefaultCase(Block(create<ast::BreakStatement>())));
auto* c = create<ast::CaseStatement>(ast::CaseSelectorList{}, body); WrapInFunction(s);
WrapInFunction(c);
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
gen.increment_indent(); 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: { EXPECT_EQ(gen.result(), R"( default: {
break; break;
} }

View File

@ -22,17 +22,15 @@ namespace {
using WgslGeneratorImplTest = TestHelper; using WgslGeneratorImplTest = TestHelper;
TEST_F(WgslGeneratorImplTest, Emit_Case) { TEST_F(WgslGeneratorImplTest, Emit_Case) {
auto* body = Block(create<ast::BreakStatement>()); auto* s = Switch(1, Case(Literal(5), Block(create<ast::BreakStatement>())),
ast::CaseSelectorList lit; DefaultCase());
lit.push_back(Literal(5)); WrapInFunction(s);
auto* c = create<ast::CaseStatement>(lit, body);
WrapInFunction(c);
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
gen.increment_indent(); 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: { EXPECT_EQ(gen.result(), R"( case 5: {
break; break;
} }
@ -40,18 +38,16 @@ TEST_F(WgslGeneratorImplTest, Emit_Case) {
} }
TEST_F(WgslGeneratorImplTest, Emit_Case_MultipleSelectors) { TEST_F(WgslGeneratorImplTest, Emit_Case_MultipleSelectors) {
auto* body = Block(create<ast::BreakStatement>()); auto* s = Switch(
ast::CaseSelectorList lit; 1, Case({Literal(5), Literal(6)}, Block(create<ast::BreakStatement>())),
lit.push_back(Literal(5)); DefaultCase());
lit.push_back(Literal(6)); WrapInFunction(s);
auto* c = create<ast::CaseStatement>(lit, body);
WrapInFunction(c);
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
gen.increment_indent(); 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: { EXPECT_EQ(gen.result(), R"( case 5, 6: {
break; break;
} }
@ -59,15 +55,14 @@ TEST_F(WgslGeneratorImplTest, Emit_Case_MultipleSelectors) {
} }
TEST_F(WgslGeneratorImplTest, Emit_Case_Default) { TEST_F(WgslGeneratorImplTest, Emit_Case_Default) {
auto* body = Block(create<ast::BreakStatement>()); auto* s = Switch(1, DefaultCase(Block(create<ast::BreakStatement>())));
auto* c = create<ast::CaseStatement>(ast::CaseSelectorList{}, body); WrapInFunction(s);
WrapInFunction(c);
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
gen.increment_indent(); 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: { EXPECT_EQ(gen.result(), R"( default: {
break; break;
} }