Cleanup WGSL writer.

This Cl removes the boolean return values from most of the WGSL writer
methods. The diagnostics are used to determine if the generation was
successful. The writer itself just continues until complete.

Change-Id: Iff33128c1df286cab799f08dab8c3defc5651d76
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/124680
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
dan sinclair 2023-04-13 02:57:27 +00:00 committed by Dawn LUCI CQ
parent 818865925d
commit 27c7722620
34 changed files with 605 additions and 798 deletions

View File

@ -17,6 +17,8 @@
#include "src/tint/utils/string_stream.h"
#include "src/tint/writer/wgsl/generator_impl.h"
#include "gmock/gmock.h"
namespace tint::reader::spirv::test {
// Default to not dumping the SPIR-V assembly.
@ -35,7 +37,9 @@ ParserImplWrapperForTest::~ParserImplWrapperForTest() {
std::string ToString(const Program& program) {
writer::wgsl::GeneratorImpl writer(&program);
if (!writer.Generate()) {
writer.Generate();
if (!writer.Diagnostics().empty()) {
return "WGSL writer error: " + writer.Diagnostics().str();
}
return writer.result();
@ -44,9 +48,10 @@ std::string ToString(const Program& program) {
std::string ToString(const Program& program, utils::VectorRef<const ast::Statement*> stmts) {
writer::wgsl::GeneratorImpl writer(&program);
for (const auto* stmt : stmts) {
if (!writer.EmitStatement(stmt)) {
return "WGSL writer error: " + writer.Diagnostics().str();
}
writer.EmitStatement(stmt);
}
if (!writer.Diagnostics().empty()) {
return "WGSL writer error: " + writer.Diagnostics().str();
}
return writer.result();
}
@ -57,13 +62,15 @@ std::string ToString(const Program& program, const ast::Node* node) {
node,
[&](const ast::Expression* expr) {
utils::StringStream out;
if (!writer.EmitExpression(out, expr)) {
writer.EmitExpression(out, expr);
if (!writer.Diagnostics().empty()) {
return "WGSL writer error: " + writer.Diagnostics().str();
}
return out.str();
},
[&](const ast::Statement* stmt) {
if (!writer.EmitStatement(stmt)) {
writer.EmitStatement(stmt);
if (!writer.Diagnostics().empty()) {
return "WGSL writer error: " + writer.Diagnostics().str();
}
return writer.result();

View File

@ -26,7 +26,8 @@ Result Generate(const Program* program, const Options&) {
// Generate the WGSL code.
auto impl = std::make_unique<GeneratorImpl>(program);
result.success = impl->Generate();
impl->Generate();
result.success = impl->Diagnostics().empty();
result.error = impl->Diagnostics().str();
result.wgsl = impl->result();

File diff suppressed because it is too large Load Diff

View File

@ -49,181 +49,141 @@ class GeneratorImpl : public TextGenerator {
~GeneratorImpl();
/// Generates the result data
/// @returns true on successful generation; false otherwise
bool Generate();
void Generate();
/// Handles generating a diagnostic control
/// @param out the output stream
/// @param diagnostic the diagnostic control node
/// @returns true if the diagnostic control was emitted
bool EmitDiagnosticControl(utils::StringStream& out, const ast::DiagnosticControl& diagnostic);
void EmitDiagnosticControl(utils::StringStream& out, const ast::DiagnosticControl& diagnostic);
/// Handles generating an enable directive
/// @param enable the enable node
/// @returns true if the enable directive was emitted
bool EmitEnable(const ast::Enable* enable);
void EmitEnable(const ast::Enable* enable);
/// Handles generating a declared type
/// @param ty the declared type to generate
/// @returns true if the declared type was emitted
bool EmitTypeDecl(const ast::TypeDecl* ty);
void EmitTypeDecl(const ast::TypeDecl* ty);
/// Handles an index accessor expression
/// @param out the output stream
/// @param expr the expression to emit
/// @returns true if the index accessor was emitted
bool EmitIndexAccessor(utils::StringStream& out, const ast::IndexAccessorExpression* expr);
void EmitIndexAccessor(utils::StringStream& out, const ast::IndexAccessorExpression* expr);
/// Handles an assignment statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully
bool EmitAssign(const ast::AssignmentStatement* stmt);
void EmitAssign(const ast::AssignmentStatement* stmt);
/// Handles generating a binary expression
/// @param out the output stream
/// @param expr the binary expression
/// @returns true if the expression was emitted, false otherwise
bool EmitBinary(utils::StringStream& out, const ast::BinaryExpression* expr);
void EmitBinary(utils::StringStream& out, const ast::BinaryExpression* expr);
/// Handles generating a binary operator
/// @param out the output stream
/// @param op the binary operator
/// @returns true if the operator was emitted, false otherwise
bool EmitBinaryOp(utils::StringStream& out, const ast::BinaryOp op);
void EmitBinaryOp(utils::StringStream& out, const ast::BinaryOp op);
/// Handles generating a bitcast expression
/// @param out the output stream
/// @param expr the bitcast expression
/// @returns true if the bitcast was emitted
bool EmitBitcast(utils::StringStream& out, const ast::BitcastExpression* expr);
void EmitBitcast(utils::StringStream& out, const ast::BitcastExpression* expr);
/// Handles a block statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully
bool EmitBlock(const ast::BlockStatement* stmt);
void EmitBlock(const ast::BlockStatement* stmt);
/// Handles emitting the start of a block statement (including attributes)
/// @param out the output stream to write the header to
/// @param stmt the block statement to emit the header for
/// @returns true if the statement was emitted successfully
bool EmitBlockHeader(utils::StringStream& out, const ast::BlockStatement* stmt);
void EmitBlockHeader(utils::StringStream& out, const ast::BlockStatement* stmt);
/// Handles a break statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully
bool EmitBreak(const ast::BreakStatement* stmt);
void EmitBreak(const ast::BreakStatement* stmt);
/// Handles a break-if statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully
bool EmitBreakIf(const ast::BreakIfStatement* stmt);
void EmitBreakIf(const ast::BreakIfStatement* stmt);
/// Handles generating a call expression
/// @param out the output stream
/// @param expr the call expression
/// @returns true if the call expression is emitted
bool EmitCall(utils::StringStream& out, const ast::CallExpression* expr);
void EmitCall(utils::StringStream& out, const ast::CallExpression* expr);
/// Handles a case statement
/// @param stmt the statement
/// @returns true if the statment was emitted successfully
bool EmitCase(const ast::CaseStatement* stmt);
void EmitCase(const ast::CaseStatement* stmt);
/// Handles a compound assignment statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully
bool EmitCompoundAssign(const ast::CompoundAssignmentStatement* stmt);
void EmitCompoundAssign(const ast::CompoundAssignmentStatement* stmt);
/// Handles generating a literal expression
/// @param out the output stream
/// @param expr the literal expression expression
/// @returns true if the literal expression is emitted
bool EmitLiteral(utils::StringStream& out, const ast::LiteralExpression* expr);
void EmitLiteral(utils::StringStream& out, const ast::LiteralExpression* expr);
/// Handles a continue statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully
bool EmitContinue(const ast::ContinueStatement* stmt);
void EmitContinue(const ast::ContinueStatement* stmt);
/// Handles generate an Expression
/// @param out the output stream
/// @param expr the expression
/// @returns true if the expression was emitted
bool EmitExpression(utils::StringStream& out, const ast::Expression* expr);
void EmitExpression(utils::StringStream& out, const ast::Expression* expr);
/// Handles generating a function
/// @param func the function to generate
/// @returns true if the function was emitted
bool EmitFunction(const ast::Function* func);
void EmitFunction(const ast::Function* func);
/// Handles generating an identifier expression
/// @param out the output stream
/// @param expr the identifier expression
/// @returns true if the identifier was emitted
bool EmitIdentifier(utils::StringStream& out, const ast::IdentifierExpression* expr);
void EmitIdentifier(utils::StringStream& out, const ast::IdentifierExpression* expr);
/// Handles generating an identifier
/// @param out the output of the expression stream
/// @param ident the identifier
/// @returns true if the identifier was emitted
bool EmitIdentifier(utils::StringStream& out, const ast::Identifier* ident);
void EmitIdentifier(utils::StringStream& out, const ast::Identifier* ident);
/// Handles an if statement
/// @param stmt the statement to emit
/// @returns true if the statement was successfully emitted
bool EmitIf(const ast::IfStatement* stmt);
void EmitIf(const ast::IfStatement* stmt);
/// Handles an increment/decrement statement
/// @param stmt the statement to emit
/// @returns true if the statement was successfully emitted
bool EmitIncrementDecrement(const ast::IncrementDecrementStatement* stmt);
void EmitIncrementDecrement(const ast::IncrementDecrementStatement* stmt);
/// Handles generating a discard statement
/// @param stmt the discard statement
/// @returns true if the statement was successfully emitted
bool EmitDiscard(const ast::DiscardStatement* stmt);
void EmitDiscard(const ast::DiscardStatement* stmt);
/// Handles a loop statement
/// @param stmt the statement to emit
/// @returns true if the statement was emtited
bool EmitLoop(const ast::LoopStatement* stmt);
void EmitLoop(const ast::LoopStatement* stmt);
/// Handles a for-loop statement
/// @param stmt the statement to emit
/// @returns true if the statement was emtited
bool EmitForLoop(const ast::ForLoopStatement* stmt);
void EmitForLoop(const ast::ForLoopStatement* stmt);
/// Handles a while statement
/// @param stmt the statement to emit
/// @returns true if the statement was emtited
bool EmitWhile(const ast::WhileStatement* stmt);
void EmitWhile(const ast::WhileStatement* stmt);
/// Handles a member accessor expression
/// @param out the output stream
/// @param expr the member accessor expression
/// @returns true if the member accessor was emitted
bool EmitMemberAccessor(utils::StringStream& out, const ast::MemberAccessorExpression* expr);
void EmitMemberAccessor(utils::StringStream& out, const ast::MemberAccessorExpression* expr);
/// Handles return statements
/// @param stmt the statement to emit
/// @returns true if the statement was successfully emitted
bool EmitReturn(const ast::ReturnStatement* stmt);
void EmitReturn(const ast::ReturnStatement* stmt);
/// Handles const assertion statements
/// @param stmt the statement to emit
/// @returns true if the statement was successfully emitted
bool EmitConstAssert(const ast::ConstAssert* stmt);
void EmitConstAssert(const ast::ConstAssert* stmt);
/// Handles statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted
bool EmitStatement(const ast::Statement* stmt);
void EmitStatement(const ast::Statement* stmt);
/// Handles a statement list
/// @param stmts the statements to emit
/// @returns true if the statements were emitted
bool EmitStatements(utils::VectorRef<const ast::Statement*> stmts);
void EmitStatements(utils::VectorRef<const ast::Statement*> stmts);
/// Handles a statement list with an increased indentation
/// @param stmts the statements to emit
/// @returns true if the statements were emitted
bool EmitStatementsWithIndent(utils::VectorRef<const ast::Statement*> stmts);
void EmitStatementsWithIndent(utils::VectorRef<const ast::Statement*> stmts);
/// Handles generating a switch statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted
bool EmitSwitch(const ast::SwitchStatement* stmt);
void EmitSwitch(const ast::SwitchStatement* stmt);
/// Handles generating a struct declaration
/// @param str the struct
/// @returns true if the struct is emitted
bool EmitStructType(const ast::Struct* str);
void EmitStructType(const ast::Struct* str);
/// Handles emitting an image format
/// @param out the output stream
/// @param fmt the format to generate
/// @returns true if the format is emitted
bool EmitImageFormat(utils::StringStream& out, const builtin::TexelFormat fmt);
void EmitImageFormat(utils::StringStream& out, const builtin::TexelFormat fmt);
/// Handles a unary op expression
/// @param out the output stream
/// @param expr the expression to emit
/// @returns true if the expression was emitted
bool EmitUnaryOp(utils::StringStream& out, const ast::UnaryOpExpression* expr);
void EmitUnaryOp(utils::StringStream& out, const ast::UnaryOpExpression* expr);
/// Handles generating a variable
/// @param out the output stream
/// @param var the variable to generate
/// @returns true if the variable was emitted
bool EmitVariable(utils::StringStream& out, const ast::Variable* var);
void EmitVariable(utils::StringStream& out, const ast::Variable* var);
/// Handles generating a attribute list
/// @param out the output stream
/// @param attrs the attribute list
/// @returns true if the attributes were emitted
bool EmitAttributes(utils::StringStream& out, utils::VectorRef<const ast::Attribute*> attrs);
void EmitAttributes(utils::StringStream& out, utils::VectorRef<const ast::Attribute*> attrs);
};
} // namespace tint::writer::wgsl

View File

@ -14,6 +14,8 @@
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
namespace tint::writer::wgsl {
namespace {
@ -23,8 +25,9 @@ TEST_F(WgslGeneratorImplTest, EmitAlias_F32) {
auto* alias = Alias("a", ty.f32());
GeneratorImpl& gen = Build();
gen.EmitTypeDecl(alias);
ASSERT_TRUE(gen.EmitTypeDecl(alias)) << gen.Diagnostics();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(alias a = f32;
)");
}
@ -38,9 +41,11 @@ TEST_F(WgslGeneratorImplTest, EmitTypeDecl_Struct) {
auto* alias = Alias("B", ty.Of(s));
GeneratorImpl& gen = Build();
gen.EmitTypeDecl(s);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
ASSERT_TRUE(gen.EmitTypeDecl(s)) << gen.Diagnostics();
ASSERT_TRUE(gen.EmitTypeDecl(alias)) << gen.Diagnostics();
gen.EmitTypeDecl(alias);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(struct A {
a : f32,
b : i32,
@ -59,7 +64,8 @@ TEST_F(WgslGeneratorImplTest, EmitAlias_ToStruct) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitTypeDecl(alias)) << gen.Diagnostics();
gen.EmitTypeDecl(alias);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(alias B = A;
)");
}

View File

@ -15,6 +15,8 @@
#include "src/tint/utils/string_stream.h"
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
using namespace tint::number_suffixes; // NOLINT
namespace tint::writer::wgsl {
@ -30,7 +32,8 @@ TEST_F(WgslGeneratorImplTest, IndexAccessor) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, expr)) << gen.Diagnostics();
gen.EmitExpression(out, expr);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "ary[5i]");
}
@ -44,7 +47,8 @@ TEST_F(WgslGeneratorImplTest, IndexAccessor_OfDref) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, expr)) << gen.Diagnostics();
gen.EmitExpression(out, expr);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "(*(p))[5i]");
}

View File

@ -14,6 +14,8 @@
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
namespace tint::writer::wgsl {
namespace {
@ -29,7 +31,8 @@ TEST_F(WgslGeneratorImplTest, Emit_Assign) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(assign)) << gen.Diagnostics();
gen.EmitStatement(assign);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), " lhs = rhs;\n");
}

View File

@ -15,6 +15,8 @@
#include "src/tint/utils/string_stream.h"
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
namespace tint::writer::wgsl {
namespace {
@ -51,7 +53,8 @@ TEST_P(WgslBinaryTest, Emit) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, expr)) << gen.Diagnostics();
gen.EmitExpression(out, expr);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), params.result);
}
INSTANTIATE_TEST_SUITE_P(

View File

@ -15,6 +15,8 @@
#include "src/tint/utils/string_stream.h"
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
using namespace tint::number_suffixes; // NOLINT
namespace tint::writer::wgsl {
@ -29,7 +31,8 @@ TEST_F(WgslGeneratorImplTest, EmitExpression_Bitcast) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, bitcast)) << gen.Diagnostics();
gen.EmitExpression(out, bitcast);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "bitcast<f32>(1i)");
}

View File

@ -14,6 +14,8 @@
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
namespace tint::writer::wgsl {
namespace {
@ -27,7 +29,8 @@ TEST_F(WgslGeneratorImplTest, Emit_Block) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(b)) << gen.Diagnostics();
gen.EmitStatement(b);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( {
return;
}

View File

@ -14,6 +14,8 @@
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
namespace tint::writer::wgsl {
namespace {
@ -27,7 +29,8 @@ TEST_F(WgslGeneratorImplTest, Emit_Break) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(b)) << gen.Diagnostics();
gen.EmitStatement(b);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), " break;\n");
}

View File

@ -16,6 +16,8 @@
#include "src/tint/utils/string_stream.h"
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
using namespace tint::number_suffixes; // NOLINT
namespace tint::writer::wgsl {
@ -35,7 +37,8 @@ TEST_F(WgslGeneratorImplTest, EmitExpression_Call_WithoutParams) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, call)) << gen.Diagnostics();
gen.EmitExpression(out, call);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "my_func()");
}
@ -58,7 +61,8 @@ TEST_F(WgslGeneratorImplTest, EmitExpression_Call_WithParams) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, call)) << gen.Diagnostics();
gen.EmitExpression(out, call);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "my_func(param1, param2)");
}
@ -79,7 +83,8 @@ TEST_F(WgslGeneratorImplTest, EmitStatement_Call) {
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.Diagnostics();
gen.EmitStatement(stmt);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), " my_func(param1, param2);\n");
}

View File

@ -14,6 +14,8 @@
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
using namespace tint::number_suffixes; // NOLINT
namespace tint::writer::wgsl {
@ -30,7 +32,8 @@ TEST_F(WgslGeneratorImplTest, Emit_Case) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitCase(s->body[0])) << gen.Diagnostics();
gen.EmitCase(s->body[0]);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( case 5i: {
break;
}
@ -52,7 +55,8 @@ TEST_F(WgslGeneratorImplTest, Emit_Case_MultipleSelectors) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitCase(s->body[0])) << gen.Diagnostics();
gen.EmitCase(s->body[0]);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( case 5i, 6i: {
break;
}
@ -67,7 +71,8 @@ TEST_F(WgslGeneratorImplTest, Emit_Case_Default) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitCase(s->body[0])) << gen.Diagnostics();
gen.EmitCase(s->body[0]);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( default: {
break;
}

View File

@ -15,6 +15,8 @@
#include "src/tint/utils/string_stream.h"
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
using namespace tint::number_suffixes; // NOLINT
namespace tint::writer::wgsl {
@ -29,7 +31,8 @@ TEST_F(WgslGeneratorImplTest, EmitExpression_Cast_Scalar_F32_From_I32) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, cast)) << gen.Diagnostics();
gen.EmitExpression(out, cast);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "f32(1i)");
}
@ -42,7 +45,8 @@ TEST_F(WgslGeneratorImplTest, EmitExpression_Cast_Scalar_F16_From_I32) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, cast)) << gen.Diagnostics();
gen.EmitExpression(out, cast);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "f16(1i)");
}
@ -53,7 +57,8 @@ TEST_F(WgslGeneratorImplTest, EmitExpression_Cast_Vector_F32_From_I32) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, cast)) << gen.Diagnostics();
gen.EmitExpression(out, cast);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "vec3<f32>(vec3<i32>(1i, 2i, 3i))");
}
@ -66,7 +71,8 @@ TEST_F(WgslGeneratorImplTest, EmitExpression_Cast_Vector_F16_From_I32) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, cast)) << gen.Diagnostics();
gen.EmitExpression(out, cast);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "vec3<f16>(vec3<i32>(1i, 2i, 3i))");
}

View File

@ -14,6 +14,8 @@
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
using namespace tint::number_suffixes; // NOLINT
namespace tint::writer::wgsl {
@ -25,8 +27,8 @@ TEST_F(WgslGeneratorImplTest, Emit_GlobalConstAssert) {
GlobalConstAssert(true);
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(const_assert true;
)");
}
@ -35,8 +37,8 @@ TEST_F(WgslGeneratorImplTest, Emit_FunctionConstAssert) {
Func("f", utils::Empty, ty.void_(), utils::Vector{ConstAssert(true)});
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(fn f() {
const_assert true;
}

View File

@ -12,9 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "gmock/gmock.h"
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
using ::testing::HasSubstr;
using namespace tint::number_suffixes; // NOLINT
@ -28,8 +29,8 @@ TEST_F(WgslGeneratorImplTest_Constructor, Bool) {
WrapInFunction(Expr(false));
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_THAT(gen.result(), HasSubstr("false"));
}
@ -37,8 +38,8 @@ TEST_F(WgslGeneratorImplTest_Constructor, Int) {
WrapInFunction(Expr(-12345_i));
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_THAT(gen.result(), HasSubstr("-12345"));
}
@ -46,8 +47,8 @@ TEST_F(WgslGeneratorImplTest_Constructor, UInt) {
WrapInFunction(Expr(56779_u));
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_THAT(gen.result(), HasSubstr("56779u"));
}
@ -57,7 +58,8 @@ TEST_F(WgslGeneratorImplTest_Constructor, F32) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_THAT(gen.result(), HasSubstr("1073741824.0f"));
}
@ -69,7 +71,8 @@ TEST_F(WgslGeneratorImplTest_Constructor, F16) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_THAT(gen.result(), HasSubstr("32752.0h"));
}
@ -78,7 +81,8 @@ TEST_F(WgslGeneratorImplTest_Constructor, Type_F32) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_THAT(gen.result(), HasSubstr("f32(-0.00001200000042445026f)"));
}
@ -89,7 +93,8 @@ TEST_F(WgslGeneratorImplTest_Constructor, Type_F16) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_THAT(gen.result(), HasSubstr("f16(-0.00001198053359985352h)"));
}
@ -98,7 +103,8 @@ TEST_F(WgslGeneratorImplTest_Constructor, Type_Bool) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_THAT(gen.result(), HasSubstr("bool(true)"));
}
@ -107,7 +113,8 @@ TEST_F(WgslGeneratorImplTest_Constructor, Type_Int) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_THAT(gen.result(), HasSubstr("i32(-12345i)"));
}
@ -116,7 +123,8 @@ TEST_F(WgslGeneratorImplTest_Constructor, Type_Uint) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_THAT(gen.result(), HasSubstr("u32(12345u)"));
}
@ -125,7 +133,8 @@ TEST_F(WgslGeneratorImplTest_Constructor, Type_Vec_F32) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_THAT(gen.result(), HasSubstr("vec3<f32>(1.0f, 2.0f, 3.0f)"));
}
@ -136,7 +145,8 @@ TEST_F(WgslGeneratorImplTest_Constructor, Type_Vec_F16) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_THAT(gen.result(), HasSubstr("vec3<f16>(1.0h, 2.0h, 3.0h)"));
}
@ -145,7 +155,8 @@ TEST_F(WgslGeneratorImplTest_Constructor, Type_Mat_F32) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_THAT(gen.result(), HasSubstr("mat2x3<f32>(vec3<f32>(1.0f, 2.0f, 3.0f), "
"vec3<f32>(3.0f, 4.0f, 5.0f))"));
}
@ -157,7 +168,8 @@ TEST_F(WgslGeneratorImplTest_Constructor, Type_Mat_F16) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_THAT(gen.result(), HasSubstr("mat2x3<f16>(vec3<f16>(1.0h, 2.0h, 3.0h), "
"vec3<f16>(3.0h, 4.0h, 5.0h))"));
}
@ -168,7 +180,8 @@ TEST_F(WgslGeneratorImplTest_Constructor, Type_Array) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_THAT(gen.result(),
HasSubstr("array<vec3<f32>, 3u>(vec3<f32>(1.0f, 2.0f, 3.0f), "
"vec3<f32>(4.0f, 5.0f, 6.0f), vec3<f32>(7.0f, 8.0f, 9.0f))"));
@ -180,7 +193,8 @@ TEST_F(WgslGeneratorImplTest_Constructor, Type_ImplicitArray) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_THAT(gen.result(),
HasSubstr("array(vec3<f32>(1.0f, 2.0f, 3.0f), "
"vec3<f32>(4.0f, 5.0f, 6.0f), vec3<f32>(7.0f, 8.0f, 9.0f))"));

View File

@ -14,6 +14,8 @@
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
namespace tint::writer::wgsl {
namespace {
@ -29,7 +31,8 @@ TEST_F(WgslGeneratorImplTest, Emit_Continue) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(c)) << gen.Diagnostics();
gen.EmitStatement(c);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), " continue;\n");
}

View File

@ -14,6 +14,8 @@
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
namespace tint::writer::wgsl {
namespace {
@ -23,8 +25,8 @@ TEST_F(WgslGeneratorImplTest, Emit_DiagnosticDirective) {
DiagnosticDirective(builtin::DiagnosticSeverity::kError, "chromium_unreachable_code");
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate());
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(diagnostic(error, chromium_unreachable_code);
)");
@ -36,8 +38,8 @@ TEST_F(WgslGeneratorImplTest, Emit_DiagnosticAttribute) {
Func("foo", {}, ty.void_(), {}, utils::Vector{attr});
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate());
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(@diagnostic(error, chromium_unreachable_code)
fn foo() {
}

View File

@ -14,6 +14,8 @@
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
namespace tint::writer::wgsl {
namespace {
@ -29,7 +31,8 @@ TEST_F(WgslGeneratorImplTest, Emit_Discard) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.Diagnostics();
gen.EmitStatement(stmt);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), " discard;\n");
}

View File

@ -14,6 +14,8 @@
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
namespace tint::writer::wgsl {
namespace {
@ -24,7 +26,8 @@ TEST_F(WgslGeneratorImplTest, Emit_Enable) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitEnable(enable));
gen.EmitEnable(enable);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(enable f16;
)");
}

View File

@ -18,6 +18,8 @@
#include "src/tint/builtin/builtin_value.h"
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
using namespace tint::number_suffixes; // NOLINT
namespace tint::writer::wgsl {
@ -34,8 +36,8 @@ TEST_F(WgslGeneratorImplTest, Emit_Function) {
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.EmitFunction(func));
gen.EmitFunction(func);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( fn my_func() {
return;
}
@ -56,8 +58,8 @@ TEST_F(WgslGeneratorImplTest, Emit_Function_WithParams) {
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.EmitFunction(func));
gen.EmitFunction(func);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( fn my_func(a : f32, b : i32) {
return;
}
@ -77,8 +79,8 @@ TEST_F(WgslGeneratorImplTest, Emit_Function_WithAttribute_WorkgroupSize) {
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.EmitFunction(func));
gen.EmitFunction(func);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( @compute @workgroup_size(2i, 4i, 6i)
fn my_func() {
return;
@ -98,8 +100,8 @@ TEST_F(WgslGeneratorImplTest, Emit_Function_WithAttribute_MustUse) {
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.EmitFunction(func));
gen.EmitFunction(func);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( @must_use
fn my_func() -> i32 {
return 1i;
@ -121,8 +123,8 @@ TEST_F(WgslGeneratorImplTest, Emit_Function_WithAttribute_WorkgroupSize_WithIden
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.EmitFunction(func));
gen.EmitFunction(func);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( @compute @workgroup_size(2i, height)
fn my_func() {
return;
@ -148,8 +150,8 @@ TEST_F(WgslGeneratorImplTest, Emit_Function_EntryPoint_Parameters) {
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.EmitFunction(func));
gen.EmitFunction(func);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( @fragment
fn frag_main(@builtin(position) coord : vec4<f32>, @location(1) loc1 : f32) {
}
@ -171,8 +173,8 @@ TEST_F(WgslGeneratorImplTest, Emit_Function_EntryPoint_ReturnValue) {
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.EmitFunction(func));
gen.EmitFunction(func);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( @fragment
fn frag_main() -> @location(1) f32 {
return 1.0f;
@ -234,7 +236,8 @@ TEST_F(WgslGeneratorImplTest, Emit_Function_Multiple_EntryPoint_With_Same_Module
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(struct Data {
d : f32,
}

View File

@ -18,6 +18,8 @@
#include "src/tint/type/texture_dimension.h"
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
using namespace tint::number_suffixes; // NOLINT
namespace tint::writer::wgsl {
@ -34,8 +36,8 @@ TEST_F(WgslGeneratorImplTest, Emit_GlobalDeclAfterFunction) {
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( @compute @workgroup_size(1i, 1i, 1i)
fn test_function() {
var a : f32;
@ -78,8 +80,8 @@ TEST_F(WgslGeneratorImplTest, Emit_GlobalsInterleaved) {
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( var<private> a0 : f32;
struct S0 {
@ -111,8 +113,8 @@ TEST_F(WgslGeneratorImplTest, Emit_Global_Sampler) {
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), " @group(0) @binding(0) var s : sampler;\n");
}
@ -123,8 +125,8 @@ TEST_F(WgslGeneratorImplTest, Emit_Global_Texture) {
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), " @group(0) @binding(0) var t : texture_1d<f32>;\n");
}
@ -135,8 +137,8 @@ TEST_F(WgslGeneratorImplTest, Emit_GlobalConst) {
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( const explicit : f32 = 1.0f;
const inferred = 1.0f;
@ -150,8 +152,8 @@ TEST_F(WgslGeneratorImplTest, Emit_OverridableConstants) {
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( override a : f32;
@id(7) override b : f32;

View File

@ -15,6 +15,8 @@
#include "src/tint/utils/string_stream.h"
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
namespace tint::writer::wgsl {
namespace {
@ -28,7 +30,8 @@ TEST_F(WgslGeneratorImplTest, EmitIdentifierExpression_Single) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, i)) << gen.Diagnostics();
gen.EmitExpression(out, i);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "glsl");
}

View File

@ -14,6 +14,8 @@
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
namespace tint::writer::wgsl {
namespace {
@ -31,7 +33,8 @@ TEST_F(WgslGeneratorImplTest, Emit_If) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(i)) << gen.Diagnostics();
gen.EmitStatement(i);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( if (cond) {
return;
}
@ -54,7 +57,8 @@ TEST_F(WgslGeneratorImplTest, Emit_IfWithElseIf) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(i)) << gen.Diagnostics();
gen.EmitStatement(i);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( if (cond) {
return;
} else if (else_cond) {
@ -77,7 +81,8 @@ TEST_F(WgslGeneratorImplTest, Emit_IfWithElse) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(i)) << gen.Diagnostics();
gen.EmitStatement(i);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( if (cond) {
return;
} else {
@ -105,7 +110,8 @@ TEST_F(WgslGeneratorImplTest, Emit_IfWithMultiple) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(i)) << gen.Diagnostics();
gen.EmitStatement(i);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( if (cond) {
return;
} else if (else_cond) {

View File

@ -17,6 +17,8 @@
#include "src/tint/utils/string_stream.h"
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
using namespace tint::number_suffixes; // NOLINT
namespace tint::writer::wgsl {
@ -116,7 +118,8 @@ TEST_P(WgslGenerator_F32LiteralTest, Emit) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitLiteral(out, v)) << gen.Diagnostics();
gen.EmitLiteral(out, v);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), GetParam().expected);
}
@ -164,7 +167,8 @@ TEST_P(WgslGenerator_F16LiteralTest, Emit) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitLiteral(out, v)) << gen.Diagnostics();
gen.EmitLiteral(out, v);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), GetParam().expected);
}

View File

@ -14,6 +14,8 @@
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
using namespace tint::number_suffixes; // NOLINT
namespace tint::writer::wgsl {
@ -33,7 +35,8 @@ TEST_F(WgslGeneratorImplTest, Emit_Loop) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(l)) << gen.Diagnostics();
gen.EmitStatement(l);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( loop {
break;
}
@ -54,7 +57,8 @@ TEST_F(WgslGeneratorImplTest, Emit_LoopWithContinuing) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(l)) << gen.Diagnostics();
gen.EmitStatement(l);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( loop {
break;
@ -79,7 +83,8 @@ TEST_F(WgslGeneratorImplTest, Emit_LoopWithContinuing_BreakIf) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(l)) << gen.Diagnostics();
gen.EmitStatement(l);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( loop {
discard;
@ -105,7 +110,8 @@ TEST_F(WgslGeneratorImplTest, Emit_ForLoopWithMultiStmtInit) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(f)) << gen.Diagnostics();
gen.EmitStatement(f);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( for({
_ = 1i;
_ = 2i;
@ -127,7 +133,8 @@ TEST_F(WgslGeneratorImplTest, Emit_ForLoopWithSimpleCond) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(f)) << gen.Diagnostics();
gen.EmitStatement(f);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( for(; true; ) {
return;
}
@ -147,7 +154,8 @@ TEST_F(WgslGeneratorImplTest, Emit_ForLoopWithSimpleCont) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(f)) << gen.Diagnostics();
gen.EmitStatement(f);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( for(; ; i = (i + 1i)) {
return;
}
@ -169,7 +177,8 @@ TEST_F(WgslGeneratorImplTest, Emit_ForLoopWithMultiStmtCont) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(f)) << gen.Diagnostics();
gen.EmitStatement(f);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( for(; ; {
_ = 1i;
_ = 2i;
@ -191,7 +200,8 @@ TEST_F(WgslGeneratorImplTest, Emit_ForLoopWithSimpleInitCondCont) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(f)) << gen.Diagnostics();
gen.EmitStatement(f);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( for(var i : i32; true; i = (i + 1i)) {
return;
}
@ -213,7 +223,8 @@ TEST_F(WgslGeneratorImplTest, Emit_ForLoopWithMultiStmtInitCondCont) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(f)) << gen.Diagnostics();
gen.EmitStatement(f);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( for({
_ = 1i;
_ = 2i;
@ -238,7 +249,8 @@ TEST_F(WgslGeneratorImplTest, Emit_While) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(f)) << gen.Diagnostics();
gen.EmitStatement(f);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( while(true) {
return;
}
@ -257,7 +269,8 @@ TEST_F(WgslGeneratorImplTest, Emit_While_WithContinue) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(f)) << gen.Diagnostics();
gen.EmitStatement(f);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( while(true) {
continue;
}
@ -278,7 +291,8 @@ TEST_F(WgslGeneratorImplTest, Emit_WhileMultiCond) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(f)) << gen.Diagnostics();
gen.EmitStatement(f);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( while((true && false)) {
return;
}

View File

@ -15,6 +15,8 @@
#include "src/tint/utils/string_stream.h"
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
namespace tint::writer::wgsl {
namespace {
@ -30,7 +32,8 @@ TEST_F(WgslGeneratorImplTest, EmitExpression_MemberAccessor) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, expr)) << gen.Diagnostics();
gen.EmitExpression(out, expr);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "str.mem");
}
@ -45,7 +48,8 @@ TEST_F(WgslGeneratorImplTest, EmitExpression_MemberAccessor_OfDref) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, expr)) << gen.Diagnostics();
gen.EmitExpression(out, expr);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "(*(p)).mem");
}

View File

@ -14,6 +14,8 @@
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
using namespace tint::number_suffixes; // NOLINT
namespace tint::writer::wgsl {
@ -29,7 +31,8 @@ TEST_F(WgslGeneratorImplTest, Emit_Return) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(r)) << gen.Diagnostics();
gen.EmitStatement(r);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), " return;\n");
}
@ -41,7 +44,8 @@ TEST_F(WgslGeneratorImplTest, Emit_ReturnWithValue) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(r)) << gen.Diagnostics();
gen.EmitStatement(r);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), " return 123i;\n");
}

View File

@ -14,6 +14,8 @@
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
using namespace tint::number_suffixes; // NOLINT
namespace tint::writer::wgsl {
@ -43,7 +45,8 @@ TEST_F(WgslGeneratorImplTest, Emit_Switch) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(s)) << gen.Diagnostics();
gen.EmitStatement(s);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( switch(cond) {
case 5i: {
break;
@ -68,8 +71,8 @@ TEST_F(WgslGeneratorImplTest, Emit_Switch_MixedDefault) {
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(s)) << gen.Diagnostics();
gen.EmitStatement(s);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"( switch(cond) {
case 5i, default: {
break;

View File

@ -15,6 +15,8 @@
#include "src/tint/sem/variable.h"
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
namespace tint::writer::wgsl {
namespace {
@ -25,7 +27,8 @@ TEST_F(WgslGeneratorImplTest, Generate) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(fn my_func() {
}
)");

View File

@ -20,6 +20,8 @@
#include "src/tint/utils/string_stream.h"
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
using namespace tint::number_suffixes; // NOLINT
namespace tint::writer::wgsl {
@ -34,7 +36,8 @@ TEST_F(WgslGeneratorImplTest, EmitType_Alias) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.Diagnostics();
gen.EmitExpression(out, type);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "alias");
}
@ -44,7 +47,8 @@ TEST_F(WgslGeneratorImplTest, EmitType_Array) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.Diagnostics();
gen.EmitExpression(out, type);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "array<bool, 4u>");
}
@ -55,7 +59,8 @@ TEST_F(WgslGeneratorImplTest, EmitType_Array_Attribute) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.Diagnostics();
gen.EmitExpression(out, type);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "@stride(16) array<bool, 4u>");
}
@ -65,7 +70,8 @@ TEST_F(WgslGeneratorImplTest, EmitType_RuntimeArray) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.Diagnostics();
gen.EmitExpression(out, type);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "array<bool>");
}
@ -75,7 +81,8 @@ TEST_F(WgslGeneratorImplTest, EmitType_Bool) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.Diagnostics();
gen.EmitExpression(out, type);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "bool");
}
@ -85,7 +92,8 @@ TEST_F(WgslGeneratorImplTest, EmitType_F32) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.Diagnostics();
gen.EmitExpression(out, type);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "f32");
}
@ -97,7 +105,8 @@ TEST_F(WgslGeneratorImplTest, EmitType_F16) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.Diagnostics();
gen.EmitExpression(out, type);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "f16");
}
@ -107,7 +116,8 @@ TEST_F(WgslGeneratorImplTest, EmitType_I32) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.Diagnostics();
gen.EmitExpression(out, type);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "i32");
}
@ -117,7 +127,8 @@ TEST_F(WgslGeneratorImplTest, EmitType_Matrix_F32) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.Diagnostics();
gen.EmitExpression(out, type);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "mat2x3<f32>");
}
@ -129,7 +140,8 @@ TEST_F(WgslGeneratorImplTest, EmitType_Matrix_F16) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.Diagnostics();
gen.EmitExpression(out, type);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "mat2x3<f16>");
}
@ -140,7 +152,8 @@ TEST_F(WgslGeneratorImplTest, EmitType_Pointer) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.Diagnostics();
gen.EmitExpression(out, type);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "ptr<workgroup, f32>");
}
@ -152,7 +165,8 @@ TEST_F(WgslGeneratorImplTest, EmitType_PointerAccessMode) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.Diagnostics();
gen.EmitExpression(out, type);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "ptr<storage, f32, read_write>");
}
@ -166,7 +180,8 @@ TEST_F(WgslGeneratorImplTest, EmitType_Struct) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.Diagnostics();
gen.EmitExpression(out, type);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "S");
}
@ -178,7 +193,8 @@ TEST_F(WgslGeneratorImplTest, EmitType_StructOffsetDecl) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitStructType(s)) << gen.Diagnostics();
gen.EmitStructType(s);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(struct S {
@size(8)
padding : u32,
@ -201,7 +217,8 @@ TEST_F(WgslGeneratorImplTest, EmitType_StructOffsetDecl_WithSymbolCollisions) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitStructType(s)) << gen.Diagnostics();
gen.EmitStructType(s);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(struct S {
@size(8)
padding : u32,
@ -223,7 +240,8 @@ TEST_F(WgslGeneratorImplTest, EmitType_StructAlignDecl) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitStructType(s)) << gen.Diagnostics();
gen.EmitStructType(s);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(struct S {
@align(8)
a : i32,
@ -241,7 +259,8 @@ TEST_F(WgslGeneratorImplTest, EmitType_StructSizeDecl) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitStructType(s)) << gen.Diagnostics();
gen.EmitStructType(s);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(struct S {
@size(16)
a : i32,
@ -259,7 +278,8 @@ TEST_F(WgslGeneratorImplTest, EmitType_Struct_WithAttribute) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitStructType(s)) << gen.Diagnostics();
gen.EmitStructType(s);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(struct S {
a : i32,
@align(8)
@ -277,7 +297,8 @@ TEST_F(WgslGeneratorImplTest, EmitType_Struct_WithEntryPointAttributes) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitStructType(s)) << gen.Diagnostics();
gen.EmitStructType(s);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(struct S {
@builtin(vertex_index)
a : u32,
@ -293,7 +314,8 @@ TEST_F(WgslGeneratorImplTest, EmitType_U32) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.Diagnostics();
gen.EmitExpression(out, type);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "u32");
}
@ -303,7 +325,8 @@ TEST_F(WgslGeneratorImplTest, EmitType_Vector_F32) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.Diagnostics();
gen.EmitExpression(out, type);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "vec3<f32>");
}
@ -315,7 +338,8 @@ TEST_F(WgslGeneratorImplTest, EmitType_Vector_F16) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.Diagnostics();
gen.EmitExpression(out, type);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "vec3<f16>");
}
@ -337,7 +361,8 @@ TEST_P(WgslGenerator_DepthTextureTest, EmitType_DepthTexture) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.Diagnostics();
gen.EmitExpression(out, type);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), param.name);
}
INSTANTIATE_TEST_SUITE_P(
@ -358,7 +383,8 @@ TEST_P(WgslGenerator_SampledTextureTest, EmitType_SampledTexture_F32) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.Diagnostics();
gen.EmitExpression(out, type);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), std::string(param.name) + "<f32>");
}
@ -371,7 +397,8 @@ TEST_P(WgslGenerator_SampledTextureTest, EmitType_SampledTexture_I32) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.Diagnostics();
gen.EmitExpression(out, type);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), std::string(param.name) + "<i32>");
}
@ -384,7 +411,8 @@ TEST_P(WgslGenerator_SampledTextureTest, EmitType_SampledTexture_U32) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.Diagnostics();
gen.EmitExpression(out, type);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), std::string(param.name) + "<u32>");
}
INSTANTIATE_TEST_SUITE_P(
@ -407,7 +435,8 @@ TEST_P(WgslGenerator_MultiampledTextureTest, EmitType_MultisampledTexture_F32) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.Diagnostics();
gen.EmitExpression(out, type);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), std::string(param.name) + "<f32>");
}
@ -420,7 +449,8 @@ TEST_P(WgslGenerator_MultiampledTextureTest, EmitType_MultisampledTexture_I32) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.Diagnostics();
gen.EmitExpression(out, type);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), std::string(param.name) + "<i32>");
}
@ -433,7 +463,8 @@ TEST_P(WgslGenerator_MultiampledTextureTest, EmitType_MultisampledTexture_U32) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.Diagnostics();
gen.EmitExpression(out, type);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), std::string(param.name) + "<u32>");
}
INSTANTIATE_TEST_SUITE_P(WgslGeneratorImplTest,
@ -461,7 +492,8 @@ TEST_P(WgslGenerator_StorageTextureTest, EmitType_StorageTexture) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.Diagnostics();
gen.EmitExpression(out, type);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), param.name);
}
INSTANTIATE_TEST_SUITE_P(
@ -492,7 +524,8 @@ TEST_P(WgslGenerator_ImageFormatTest, EmitType_StorageTexture_ImageFormat) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitImageFormat(out, param.fmt)) << gen.Diagnostics();
gen.EmitImageFormat(out, param.fmt);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), param.name);
}
@ -523,7 +556,8 @@ TEST_F(WgslGeneratorImplTest, EmitType_Sampler) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.Diagnostics();
gen.EmitExpression(out, type);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "sampler");
}
@ -534,7 +568,8 @@ TEST_F(WgslGeneratorImplTest, EmitType_SamplerComparison) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.Diagnostics();
gen.EmitExpression(out, type);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "sampler_comparison");
}

View File

@ -15,6 +15,8 @@
#include "src/tint/utils/string_stream.h"
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
namespace tint::writer::wgsl {
namespace {
@ -28,7 +30,8 @@ TEST_F(WgslUnaryOpTest, AddressOf) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, op)) << gen.Diagnostics();
gen.EmitExpression(out, op);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "&(expr)");
}
@ -40,7 +43,8 @@ TEST_F(WgslUnaryOpTest, Complement) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, op)) << gen.Diagnostics();
gen.EmitExpression(out, op);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "~(expr)");
}
@ -53,7 +57,8 @@ TEST_F(WgslUnaryOpTest, Indirection) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, op)) << gen.Diagnostics();
gen.EmitExpression(out, op);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "*(expr)");
}
@ -65,7 +70,8 @@ TEST_F(WgslUnaryOpTest, Not) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, op)) << gen.Diagnostics();
gen.EmitExpression(out, op);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "!(expr)");
}
@ -77,7 +83,8 @@ TEST_F(WgslUnaryOpTest, Negation) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, op)) << gen.Diagnostics();
gen.EmitExpression(out, op);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), "-(expr)");
}

View File

@ -15,6 +15,8 @@
#include "src/tint/ast/variable_decl_statement.h"
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
using namespace tint::number_suffixes; // NOLINT
namespace tint::writer::wgsl {
@ -32,7 +34,8 @@ TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.Diagnostics();
gen.EmitStatement(stmt);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), " var a : f32;\n");
}
@ -46,7 +49,8 @@ TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_InferredType) {
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.Diagnostics();
gen.EmitStatement(stmt);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), " var a = 123i;\n");
}
@ -59,9 +63,8 @@ TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_Const_AInt) {
});
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(fn f() {
const C = 1;
let l = C;
@ -78,9 +81,8 @@ TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_Const_AFloat) {
});
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(fn f() {
const C = 1.0;
let l = C;
@ -97,9 +99,8 @@ TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_Const_i32) {
});
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(fn f() {
const C = 1i;
let l = C;
@ -116,9 +117,8 @@ TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_Const_u32) {
});
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(fn f() {
const C = 1u;
let l = C;
@ -135,9 +135,8 @@ TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_Const_f32) {
});
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(fn f() {
const C = 1.0f;
let l = C;
@ -156,9 +155,8 @@ TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_Const_f16) {
});
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(enable f16;
fn f() {
@ -177,9 +175,8 @@ TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_Const_vec3_AInt) {
});
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(fn f() {
const C = vec3(1, 2, 3);
let l = C;
@ -196,9 +193,8 @@ TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_Const_vec3_AFloat) {
});
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(fn f() {
const C = vec3(1.0, 2.0, 3.0);
let l = C;
@ -215,9 +211,8 @@ TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_Const_vec3_f32) {
});
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(fn f() {
const C = vec3<f32>(1.0f, 2.0f, 3.0f);
let l = C;
@ -236,9 +231,8 @@ TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_Const_vec3_f16) {
});
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(enable f16;
fn f() {
@ -257,9 +251,8 @@ TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_Const_mat2x3_AFloat) {
});
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(fn f() {
const C = mat2x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0);
let l = C;
@ -276,9 +269,8 @@ TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_Const_mat2x3_f32) {
});
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(fn f() {
const C = mat2x3<f32>(1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f);
let l = C;
@ -297,9 +289,8 @@ TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_Const_mat2x3_f16) {
});
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(enable f16;
fn f() {
@ -318,9 +309,8 @@ TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_Const_arr_f32) {
});
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(fn f() {
const C = array<f32, 3u>(1.0f, 2.0f, 3.0f);
let l = C;
@ -340,9 +330,8 @@ TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_Const_arr_vec2_bool) {
});
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.Diagnostics();
gen.Generate();
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(gen.result(), R"(fn f() {
const C = array<vec2<bool>, 3u>(vec2<bool>(true, false), vec2<bool>(false, true), vec2<bool>(true, true));
let l = C;

View File

@ -15,6 +15,8 @@
#include "src/tint/utils/string_stream.h"
#include "src/tint/writer/wgsl/test_helper.h"
#include "gmock/gmock.h"
using namespace tint::number_suffixes; // NOLINT
namespace tint::writer::wgsl {
@ -28,7 +30,8 @@ TEST_F(WgslGeneratorImplTest, EmitVariable) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitVariable(out, v)) << gen.Diagnostics();
gen.EmitVariable(out, v);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), R"(var<private> a : f32;)");
}
@ -38,7 +41,8 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_AddressSpace) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitVariable(out, v)) << gen.Diagnostics();
gen.EmitVariable(out, v);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), R"(var<private> a : f32;)");
}
@ -50,7 +54,8 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Access_Read) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitVariable(out, v)) << gen.Diagnostics();
gen.EmitVariable(out, v);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), R"(@binding(0) @group(0) var<storage, read> a : S;)");
}
@ -62,7 +67,8 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Access_ReadWrite) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitVariable(out, v)) << gen.Diagnostics();
gen.EmitVariable(out, v);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), R"(@binding(0) @group(0) var<storage, read_write> a : S;)");
}
@ -72,7 +78,8 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Decorated) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitVariable(out, v)) << gen.Diagnostics();
gen.EmitVariable(out, v);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), R"(@group(1) @binding(2) var a : sampler;)");
}
@ -82,7 +89,8 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Initializer) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitVariable(out, v)) << gen.Diagnostics();
gen.EmitVariable(out, v);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), R"(var<private> a : f32 = 1.0f;)");
}
@ -93,7 +101,8 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Let_Explicit) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitVariable(out, v)) << gen.Diagnostics();
gen.EmitVariable(out, v);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), R"(let a : f32 = 1.0f;)");
}
@ -104,7 +113,8 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Let_Inferred) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitVariable(out, v)) << gen.Diagnostics();
gen.EmitVariable(out, v);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), R"(let a = 1.0f;)");
}
@ -115,7 +125,8 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Const_Explicit) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitVariable(out, v)) << gen.Diagnostics();
gen.EmitVariable(out, v);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), R"(const a : f32 = 1.0f;)");
}
@ -126,7 +137,8 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Const_Inferred) {
GeneratorImpl& gen = Build();
utils::StringStream out;
ASSERT_TRUE(gen.EmitVariable(out, v)) << gen.Diagnostics();
gen.EmitVariable(out, v);
EXPECT_THAT(gen.Diagnostics(), testing::IsEmpty());
EXPECT_EQ(out.str(), R"(const a = 1.0f;)");
}