dawn-cmake/src/tint/ast/case_statement_test.cc
James Price d9f659670d tint: Handle @diagnostic on block statements
Use expect_compound_statement() in all the places that use
compound_statement in the WGSL grammar.

Handle attributes on statements inside Resolver::StatementScope, so
that the logic can be reused for the various places where block
statements are used. This will also make it easier to reuse this logic
when we allow these attributes on other types of statement in the
future.

Add an `EmitBlockHeader()` helper to the WGSL writer to reuse the
logic for emitting attributes on block statements for all the places
that use them.

Bug: tint:1809
Change-Id: Iac3bb01f5031e6134c1798ddafdad080412c8bef
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/118000
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: James Price <jrprice@google.com>
2023-02-01 23:14:10 +00:00

132 lines
4.3 KiB
C++

// Copyright 2020 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/tint/ast/case_statement.h"
#include "gtest/gtest-spi.h"
#include "src/tint/ast/discard_statement.h"
#include "src/tint/ast/if_statement.h"
#include "src/tint/ast/test_helper.h"
using namespace tint::number_suffixes; // NOLINT
namespace tint::ast {
namespace {
using CaseStatementTest = TestHelper;
TEST_F(CaseStatementTest, Creation_i32) {
auto* selector = CaseSelector(2_i);
utils::Vector b{selector};
auto* discard = create<DiscardStatement>();
auto* body = create<BlockStatement>(utils::Vector{discard}, utils::Empty);
auto* c = create<CaseStatement>(b, body);
ASSERT_EQ(c->selectors.Length(), 1u);
EXPECT_EQ(c->selectors[0], selector);
ASSERT_EQ(c->body->statements.Length(), 1u);
EXPECT_EQ(c->body->statements[0], discard);
}
TEST_F(CaseStatementTest, Creation_u32) {
auto* selector = CaseSelector(2_u);
utils::Vector b{selector};
auto* discard = create<DiscardStatement>();
auto* body = create<BlockStatement>(utils::Vector{discard}, utils::Empty);
auto* c = create<CaseStatement>(b, body);
ASSERT_EQ(c->selectors.Length(), 1u);
EXPECT_EQ(c->selectors[0], selector);
ASSERT_EQ(c->body->statements.Length(), 1u);
EXPECT_EQ(c->body->statements[0], discard);
}
TEST_F(CaseStatementTest, ContainsDefault_WithDefault) {
utils::Vector b{CaseSelector(2_u), DefaultCaseSelector()};
auto* c = create<CaseStatement>(b, create<BlockStatement>(utils::Empty, utils::Empty));
EXPECT_TRUE(c->ContainsDefault());
}
TEST_F(CaseStatementTest, ContainsDefault_WithOutDefault) {
utils::Vector b{CaseSelector(2_u), CaseSelector(3_u)};
auto* c = create<CaseStatement>(b, create<BlockStatement>(utils::Empty, utils::Empty));
EXPECT_FALSE(c->ContainsDefault());
}
TEST_F(CaseStatementTest, Creation_WithSource) {
utils::Vector b{CaseSelector(2_i)};
auto* body = create<BlockStatement>(
utils::Vector{
create<DiscardStatement>(),
},
utils::Empty);
auto* c = create<CaseStatement>(Source{Source::Location{20, 2}}, b, body);
auto src = c->source;
EXPECT_EQ(src.range.begin.line, 20u);
EXPECT_EQ(src.range.begin.column, 2u);
}
TEST_F(CaseStatementTest, IsCase) {
auto* c = create<CaseStatement>(utils::Vector{DefaultCaseSelector()},
create<BlockStatement>(utils::Empty, utils::Empty));
EXPECT_TRUE(c->Is<CaseStatement>());
}
TEST_F(CaseStatementTest, Assert_Null_Body) {
EXPECT_FATAL_FAILURE(
{
ProgramBuilder b;
b.create<CaseStatement>(utils::Vector{b.DefaultCaseSelector()}, nullptr);
},
"internal compiler error");
}
TEST_F(CaseStatementTest, Assert_Null_Selector) {
EXPECT_FATAL_FAILURE(
{
ProgramBuilder b;
b.create<CaseStatement>(utils::Vector<const ast::CaseSelector*, 1>{nullptr},
b.create<BlockStatement>(utils::Empty, utils::Empty));
},
"internal compiler error");
}
TEST_F(CaseStatementTest, Assert_DifferentProgramID_Call) {
EXPECT_FATAL_FAILURE(
{
ProgramBuilder b1;
ProgramBuilder b2;
b1.create<CaseStatement>(utils::Vector{b1.DefaultCaseSelector()},
b2.create<BlockStatement>(utils::Empty, utils::Empty));
},
"internal compiler error");
}
TEST_F(CaseStatementTest, Assert_DifferentProgramID_Selector) {
EXPECT_FATAL_FAILURE(
{
ProgramBuilder b1;
ProgramBuilder b2;
b1.create<CaseStatement>(utils::Vector{b2.CaseSelector(b2.Expr(2_i))},
b1.create<BlockStatement>(utils::Empty, utils::Empty));
},
"internal compiler error");
}
} // namespace
} // namespace tint::ast