ast tests: Replace std::make_unique<T> -> create<T>

create() is currently just a simple forwarder to std::make_unique<>, but
will be later replaced with a function that returns a raw pointer,
and owned by the context.

Bug: tint:322
Change-Id: I0e68992963f52e432d4d485feae1123f35732552
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/32664
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton 2020-11-13 22:03:58 +00:00 committed by Commit Bot service account
parent 4f7c2955ab
commit 327b1c7cd3
27 changed files with 426 additions and 431 deletions

View File

@ -24,8 +24,8 @@ namespace {
using ArrayAccessorExpressionTest = TestHelper; using ArrayAccessorExpressionTest = TestHelper;
TEST_F(ArrayAccessorExpressionTest, Create) { TEST_F(ArrayAccessorExpressionTest, Create) {
auto ary = std::make_unique<IdentifierExpression>("ary"); auto ary = create<IdentifierExpression>("ary");
auto idx = std::make_unique<IdentifierExpression>("idx"); auto idx = create<IdentifierExpression>("idx");
auto* ary_ptr = ary.get(); auto* ary_ptr = ary.get();
auto* idx_ptr = idx.get(); auto* idx_ptr = idx.get();
@ -35,8 +35,8 @@ TEST_F(ArrayAccessorExpressionTest, Create) {
ASSERT_EQ(exp.idx_expr(), idx_ptr); ASSERT_EQ(exp.idx_expr(), idx_ptr);
} }
TEST_F(ArrayAccessorExpressionTest, CreateWithSource) { TEST_F(ArrayAccessorExpressionTest, CreateWithSource) {
auto ary = std::make_unique<IdentifierExpression>("ary"); auto ary = create<IdentifierExpression>("ary");
auto idx = std::make_unique<IdentifierExpression>("idx"); auto idx = create<IdentifierExpression>("idx");
ArrayAccessorExpression exp(Source{Source::Location{20, 2}}, std::move(ary), ArrayAccessorExpression exp(Source{Source::Location{20, 2}}, std::move(ary),
std::move(idx)); std::move(idx));
@ -51,15 +51,15 @@ TEST_F(ArrayAccessorExpressionTest, IsArrayAccessor) {
} }
TEST_F(ArrayAccessorExpressionTest, IsValid) { TEST_F(ArrayAccessorExpressionTest, IsValid) {
auto ary = std::make_unique<IdentifierExpression>("ary"); auto ary = create<IdentifierExpression>("ary");
auto idx = std::make_unique<IdentifierExpression>("idx"); auto idx = create<IdentifierExpression>("idx");
ArrayAccessorExpression exp(std::move(ary), std::move(idx)); ArrayAccessorExpression exp(std::move(ary), std::move(idx));
EXPECT_TRUE(exp.IsValid()); EXPECT_TRUE(exp.IsValid());
} }
TEST_F(ArrayAccessorExpressionTest, IsValid_MissingArray) { TEST_F(ArrayAccessorExpressionTest, IsValid_MissingArray) {
auto idx = std::make_unique<IdentifierExpression>("idx"); auto idx = create<IdentifierExpression>("idx");
ArrayAccessorExpression exp; ArrayAccessorExpression exp;
exp.set_idx_expr(std::move(idx)); exp.set_idx_expr(std::move(idx));
@ -67,7 +67,7 @@ TEST_F(ArrayAccessorExpressionTest, IsValid_MissingArray) {
} }
TEST_F(ArrayAccessorExpressionTest, IsValid_MissingIndex) { TEST_F(ArrayAccessorExpressionTest, IsValid_MissingIndex) {
auto ary = std::make_unique<IdentifierExpression>("ary"); auto ary = create<IdentifierExpression>("ary");
ArrayAccessorExpression exp; ArrayAccessorExpression exp;
exp.set_array(std::move(ary)); exp.set_array(std::move(ary));
@ -75,22 +75,22 @@ TEST_F(ArrayAccessorExpressionTest, IsValid_MissingIndex) {
} }
TEST_F(ArrayAccessorExpressionTest, IsValid_InvalidArray) { TEST_F(ArrayAccessorExpressionTest, IsValid_InvalidArray) {
auto ary = std::make_unique<IdentifierExpression>(""); auto ary = create<IdentifierExpression>("");
auto idx = std::make_unique<IdentifierExpression>("idx"); auto idx = create<IdentifierExpression>("idx");
ArrayAccessorExpression exp(std::move(ary), std::move(idx)); ArrayAccessorExpression exp(std::move(ary), std::move(idx));
EXPECT_FALSE(exp.IsValid()); EXPECT_FALSE(exp.IsValid());
} }
TEST_F(ArrayAccessorExpressionTest, IsValid_InvalidIndex) { TEST_F(ArrayAccessorExpressionTest, IsValid_InvalidIndex) {
auto ary = std::make_unique<IdentifierExpression>("ary"); auto ary = create<IdentifierExpression>("ary");
auto idx = std::make_unique<IdentifierExpression>(""); auto idx = create<IdentifierExpression>("");
ArrayAccessorExpression exp(std::move(ary), std::move(idx)); ArrayAccessorExpression exp(std::move(ary), std::move(idx));
EXPECT_FALSE(exp.IsValid()); EXPECT_FALSE(exp.IsValid());
} }
TEST_F(ArrayAccessorExpressionTest, ToStr) { TEST_F(ArrayAccessorExpressionTest, ToStr) {
auto ary = std::make_unique<IdentifierExpression>("ary"); auto ary = create<IdentifierExpression>("ary");
auto idx = std::make_unique<IdentifierExpression>("idx"); auto idx = create<IdentifierExpression>("idx");
ArrayAccessorExpression exp(std::move(ary), std::move(idx)); ArrayAccessorExpression exp(std::move(ary), std::move(idx));
std::ostringstream out; std::ostringstream out;

View File

@ -24,8 +24,8 @@ namespace {
using AssignmentStatementTest = TestHelper; using AssignmentStatementTest = TestHelper;
TEST_F(AssignmentStatementTest, Creation) { TEST_F(AssignmentStatementTest, Creation) {
auto lhs = std::make_unique<ast::IdentifierExpression>("lhs"); auto lhs = create<ast::IdentifierExpression>("lhs");
auto rhs = std::make_unique<ast::IdentifierExpression>("rhs"); auto rhs = create<ast::IdentifierExpression>("rhs");
auto* lhs_ptr = lhs.get(); auto* lhs_ptr = lhs.get();
auto* rhs_ptr = rhs.get(); auto* rhs_ptr = rhs.get();
@ -36,8 +36,8 @@ TEST_F(AssignmentStatementTest, Creation) {
} }
TEST_F(AssignmentStatementTest, CreationWithSource) { TEST_F(AssignmentStatementTest, CreationWithSource) {
auto lhs = std::make_unique<ast::IdentifierExpression>("lhs"); auto lhs = create<ast::IdentifierExpression>("lhs");
auto rhs = std::make_unique<ast::IdentifierExpression>("rhs"); auto rhs = create<ast::IdentifierExpression>("rhs");
AssignmentStatement stmt(Source{Source::Location{20, 2}}, std::move(lhs), AssignmentStatement stmt(Source{Source::Location{20, 2}}, std::move(lhs),
std::move(rhs)); std::move(rhs));
@ -47,23 +47,23 @@ TEST_F(AssignmentStatementTest, CreationWithSource) {
} }
TEST_F(AssignmentStatementTest, IsAssign) { TEST_F(AssignmentStatementTest, IsAssign) {
auto lhs = std::make_unique<ast::IdentifierExpression>("lhs"); auto lhs = create<ast::IdentifierExpression>("lhs");
auto rhs = std::make_unique<ast::IdentifierExpression>("rhs"); auto rhs = create<ast::IdentifierExpression>("rhs");
AssignmentStatement stmt(std::move(lhs), std::move(rhs)); AssignmentStatement stmt(std::move(lhs), std::move(rhs));
EXPECT_TRUE(stmt.IsAssign()); EXPECT_TRUE(stmt.IsAssign());
} }
TEST_F(AssignmentStatementTest, IsValid) { TEST_F(AssignmentStatementTest, IsValid) {
auto lhs = std::make_unique<ast::IdentifierExpression>("lhs"); auto lhs = create<ast::IdentifierExpression>("lhs");
auto rhs = std::make_unique<ast::IdentifierExpression>("rhs"); auto rhs = create<ast::IdentifierExpression>("rhs");
AssignmentStatement stmt(std::move(lhs), std::move(rhs)); AssignmentStatement stmt(std::move(lhs), std::move(rhs));
EXPECT_TRUE(stmt.IsValid()); EXPECT_TRUE(stmt.IsValid());
} }
TEST_F(AssignmentStatementTest, IsValid_MissingLHS) { TEST_F(AssignmentStatementTest, IsValid_MissingLHS) {
auto rhs = std::make_unique<ast::IdentifierExpression>("rhs"); auto rhs = create<ast::IdentifierExpression>("rhs");
AssignmentStatement stmt; AssignmentStatement stmt;
stmt.set_rhs(std::move(rhs)); stmt.set_rhs(std::move(rhs));
@ -71,7 +71,7 @@ TEST_F(AssignmentStatementTest, IsValid_MissingLHS) {
} }
TEST_F(AssignmentStatementTest, IsValid_MissingRHS) { TEST_F(AssignmentStatementTest, IsValid_MissingRHS) {
auto lhs = std::make_unique<ast::IdentifierExpression>("lhs"); auto lhs = create<ast::IdentifierExpression>("lhs");
AssignmentStatement stmt; AssignmentStatement stmt;
stmt.set_lhs(std::move(lhs)); stmt.set_lhs(std::move(lhs));
@ -79,22 +79,22 @@ TEST_F(AssignmentStatementTest, IsValid_MissingRHS) {
} }
TEST_F(AssignmentStatementTest, IsValid_InvalidLHS) { TEST_F(AssignmentStatementTest, IsValid_InvalidLHS) {
auto lhs = std::make_unique<ast::IdentifierExpression>(""); auto lhs = create<ast::IdentifierExpression>("");
auto rhs = std::make_unique<ast::IdentifierExpression>("rhs"); auto rhs = create<ast::IdentifierExpression>("rhs");
AssignmentStatement stmt(std::move(lhs), std::move(rhs)); AssignmentStatement stmt(std::move(lhs), std::move(rhs));
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt.IsValid());
} }
TEST_F(AssignmentStatementTest, IsValid_InvalidRHS) { TEST_F(AssignmentStatementTest, IsValid_InvalidRHS) {
auto lhs = std::make_unique<ast::IdentifierExpression>("lhs"); auto lhs = create<ast::IdentifierExpression>("lhs");
auto rhs = std::make_unique<ast::IdentifierExpression>(""); auto rhs = create<ast::IdentifierExpression>("");
AssignmentStatement stmt(std::move(lhs), std::move(rhs)); AssignmentStatement stmt(std::move(lhs), std::move(rhs));
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt.IsValid());
} }
TEST_F(AssignmentStatementTest, ToStr) { TEST_F(AssignmentStatementTest, ToStr) {
auto lhs = std::make_unique<ast::IdentifierExpression>("lhs"); auto lhs = create<ast::IdentifierExpression>("lhs");
auto rhs = std::make_unique<ast::IdentifierExpression>("rhs"); auto rhs = create<ast::IdentifierExpression>("rhs");
AssignmentStatement stmt(std::move(lhs), std::move(rhs)); AssignmentStatement stmt(std::move(lhs), std::move(rhs));
std::ostringstream out; std::ostringstream out;

View File

@ -26,8 +26,8 @@ namespace {
using BinaryExpressionTest = TestHelper; using BinaryExpressionTest = TestHelper;
TEST_F(BinaryExpressionTest, Creation) { TEST_F(BinaryExpressionTest, Creation) {
auto lhs = std::make_unique<IdentifierExpression>("lhs"); auto lhs = create<IdentifierExpression>("lhs");
auto rhs = std::make_unique<IdentifierExpression>("rhs"); auto rhs = create<IdentifierExpression>("rhs");
auto* lhs_ptr = lhs.get(); auto* lhs_ptr = lhs.get();
auto* rhs_ptr = rhs.get(); auto* rhs_ptr = rhs.get();
@ -39,8 +39,8 @@ TEST_F(BinaryExpressionTest, Creation) {
} }
TEST_F(BinaryExpressionTest, Creation_WithSource) { TEST_F(BinaryExpressionTest, Creation_WithSource) {
auto lhs = std::make_unique<IdentifierExpression>("lhs"); auto lhs = create<IdentifierExpression>("lhs");
auto rhs = std::make_unique<IdentifierExpression>("rhs"); auto rhs = create<IdentifierExpression>("rhs");
BinaryExpression r(Source{Source::Location{20, 2}}, BinaryOp::kEqual, BinaryExpression r(Source{Source::Location{20, 2}}, BinaryOp::kEqual,
std::move(lhs), std::move(rhs)); std::move(lhs), std::move(rhs));
@ -55,15 +55,15 @@ TEST_F(BinaryExpressionTest, IsBinaryal) {
} }
TEST_F(BinaryExpressionTest, IsValid) { TEST_F(BinaryExpressionTest, IsValid) {
auto lhs = std::make_unique<IdentifierExpression>("lhs"); auto lhs = create<IdentifierExpression>("lhs");
auto rhs = std::make_unique<IdentifierExpression>("rhs"); auto rhs = create<IdentifierExpression>("rhs");
BinaryExpression r(BinaryOp::kEqual, std::move(lhs), std::move(rhs)); BinaryExpression r(BinaryOp::kEqual, std::move(lhs), std::move(rhs));
EXPECT_TRUE(r.IsValid()); EXPECT_TRUE(r.IsValid());
} }
TEST_F(BinaryExpressionTest, IsValid_Null_LHS) { TEST_F(BinaryExpressionTest, IsValid_Null_LHS) {
auto rhs = std::make_unique<IdentifierExpression>("rhs"); auto rhs = create<IdentifierExpression>("rhs");
BinaryExpression r; BinaryExpression r;
r.set_op(BinaryOp::kEqual); r.set_op(BinaryOp::kEqual);
@ -72,15 +72,15 @@ TEST_F(BinaryExpressionTest, IsValid_Null_LHS) {
} }
TEST_F(BinaryExpressionTest, IsValid_Invalid_LHS) { TEST_F(BinaryExpressionTest, IsValid_Invalid_LHS) {
auto lhs = std::make_unique<IdentifierExpression>(""); auto lhs = create<IdentifierExpression>("");
auto rhs = std::make_unique<IdentifierExpression>("rhs"); auto rhs = create<IdentifierExpression>("rhs");
BinaryExpression r(BinaryOp::kEqual, std::move(lhs), std::move(rhs)); BinaryExpression r(BinaryOp::kEqual, std::move(lhs), std::move(rhs));
EXPECT_FALSE(r.IsValid()); EXPECT_FALSE(r.IsValid());
} }
TEST_F(BinaryExpressionTest, IsValid_Null_RHS) { TEST_F(BinaryExpressionTest, IsValid_Null_RHS) {
auto lhs = std::make_unique<IdentifierExpression>("lhs"); auto lhs = create<IdentifierExpression>("lhs");
BinaryExpression r; BinaryExpression r;
r.set_op(BinaryOp::kEqual); r.set_op(BinaryOp::kEqual);
@ -89,24 +89,24 @@ TEST_F(BinaryExpressionTest, IsValid_Null_RHS) {
} }
TEST_F(BinaryExpressionTest, IsValid_Invalid_RHS) { TEST_F(BinaryExpressionTest, IsValid_Invalid_RHS) {
auto lhs = std::make_unique<IdentifierExpression>("lhs"); auto lhs = create<IdentifierExpression>("lhs");
auto rhs = std::make_unique<IdentifierExpression>(""); auto rhs = create<IdentifierExpression>("");
BinaryExpression r(BinaryOp::kEqual, std::move(lhs), std::move(rhs)); BinaryExpression r(BinaryOp::kEqual, std::move(lhs), std::move(rhs));
EXPECT_FALSE(r.IsValid()); EXPECT_FALSE(r.IsValid());
} }
TEST_F(BinaryExpressionTest, IsValid_Binary_None) { TEST_F(BinaryExpressionTest, IsValid_Binary_None) {
auto lhs = std::make_unique<IdentifierExpression>("lhs"); auto lhs = create<IdentifierExpression>("lhs");
auto rhs = std::make_unique<IdentifierExpression>("rhs"); auto rhs = create<IdentifierExpression>("rhs");
BinaryExpression r(BinaryOp::kNone, std::move(lhs), std::move(rhs)); BinaryExpression r(BinaryOp::kNone, std::move(lhs), std::move(rhs));
EXPECT_FALSE(r.IsValid()); EXPECT_FALSE(r.IsValid());
} }
TEST_F(BinaryExpressionTest, ToStr) { TEST_F(BinaryExpressionTest, ToStr) {
auto lhs = std::make_unique<IdentifierExpression>("lhs"); auto lhs = create<IdentifierExpression>("lhs");
auto rhs = std::make_unique<IdentifierExpression>("rhs"); auto rhs = create<IdentifierExpression>("rhs");
BinaryExpression r(BinaryOp::kEqual, std::move(lhs), std::move(rhs)); BinaryExpression r(BinaryOp::kEqual, std::move(lhs), std::move(rhs));
std::ostringstream out; std::ostringstream out;

View File

@ -26,7 +26,7 @@ using BitcastExpressionTest = TestHelper;
TEST_F(BitcastExpressionTest, Create) { TEST_F(BitcastExpressionTest, Create) {
type::F32Type f32; type::F32Type f32;
auto expr = std::make_unique<IdentifierExpression>("expr"); auto expr = create<IdentifierExpression>("expr");
auto* expr_ptr = expr.get(); auto* expr_ptr = expr.get();
@ -37,7 +37,7 @@ TEST_F(BitcastExpressionTest, Create) {
TEST_F(BitcastExpressionTest, CreateWithSource) { TEST_F(BitcastExpressionTest, CreateWithSource) {
type::F32Type f32; type::F32Type f32;
auto expr = std::make_unique<IdentifierExpression>("expr"); auto expr = create<IdentifierExpression>("expr");
BitcastExpression exp(Source{Source::Location{20, 2}}, &f32, std::move(expr)); BitcastExpression exp(Source{Source::Location{20, 2}}, &f32, std::move(expr));
auto src = exp.source(); auto src = exp.source();
@ -52,14 +52,14 @@ TEST_F(BitcastExpressionTest, IsBitcast) {
TEST_F(BitcastExpressionTest, IsValid) { TEST_F(BitcastExpressionTest, IsValid) {
type::F32Type f32; type::F32Type f32;
auto expr = std::make_unique<IdentifierExpression>("expr"); auto expr = create<IdentifierExpression>("expr");
BitcastExpression exp(&f32, std::move(expr)); BitcastExpression exp(&f32, std::move(expr));
EXPECT_TRUE(exp.IsValid()); EXPECT_TRUE(exp.IsValid());
} }
TEST_F(BitcastExpressionTest, IsValid_MissingType) { TEST_F(BitcastExpressionTest, IsValid_MissingType) {
auto expr = std::make_unique<IdentifierExpression>("expr"); auto expr = create<IdentifierExpression>("expr");
BitcastExpression exp; BitcastExpression exp;
exp.set_expr(std::move(expr)); exp.set_expr(std::move(expr));
@ -76,14 +76,14 @@ TEST_F(BitcastExpressionTest, IsValid_MissingExpr) {
TEST_F(BitcastExpressionTest, IsValid_InvalidExpr) { TEST_F(BitcastExpressionTest, IsValid_InvalidExpr) {
type::F32Type f32; type::F32Type f32;
auto expr = std::make_unique<IdentifierExpression>(""); auto expr = create<IdentifierExpression>("");
BitcastExpression e(&f32, std::move(expr)); BitcastExpression e(&f32, std::move(expr));
EXPECT_FALSE(e.IsValid()); EXPECT_FALSE(e.IsValid());
} }
TEST_F(BitcastExpressionTest, ToStr) { TEST_F(BitcastExpressionTest, ToStr) {
type::F32Type f32; type::F32Type f32;
auto expr = std::make_unique<IdentifierExpression>("expr"); auto expr = create<IdentifierExpression>("expr");
BitcastExpression exp(&f32, std::move(expr)); BitcastExpression exp(&f32, std::move(expr));
std::ostringstream out; std::ostringstream out;

View File

@ -28,7 +28,7 @@ namespace {
using BlockStatementTest = TestHelper; using BlockStatementTest = TestHelper;
TEST_F(BlockStatementTest, Creation) { TEST_F(BlockStatementTest, Creation) {
auto d = std::make_unique<DiscardStatement>(); auto d = create<DiscardStatement>();
auto* ptr = d.get(); auto* ptr = d.get();
BlockStatement b; BlockStatement b;
@ -39,9 +39,9 @@ TEST_F(BlockStatementTest, Creation) {
} }
TEST_F(BlockStatementTest, Creation_WithInsert) { TEST_F(BlockStatementTest, Creation_WithInsert) {
auto s1 = std::make_unique<DiscardStatement>(); auto s1 = create<DiscardStatement>();
auto s2 = std::make_unique<DiscardStatement>(); auto s2 = create<DiscardStatement>();
auto s3 = std::make_unique<DiscardStatement>(); auto s3 = create<DiscardStatement>();
auto* p1 = s1.get(); auto* p1 = s1.get();
auto* p2 = s2.get(); auto* p2 = s2.get();
auto* p3 = s3.get(); auto* p3 = s3.get();
@ -73,7 +73,7 @@ TEST_F(BlockStatementTest, IsBlock) {
TEST_F(BlockStatementTest, IsValid) { TEST_F(BlockStatementTest, IsValid) {
BlockStatement b; BlockStatement b;
b.append(std::make_unique<DiscardStatement>()); b.append(create<DiscardStatement>());
EXPECT_TRUE(b.IsValid()); EXPECT_TRUE(b.IsValid());
} }
@ -84,20 +84,20 @@ TEST_F(BlockStatementTest, IsValid_Empty) {
TEST_F(BlockStatementTest, IsValid_NullBodyStatement) { TEST_F(BlockStatementTest, IsValid_NullBodyStatement) {
BlockStatement b; BlockStatement b;
b.append(std::make_unique<DiscardStatement>()); b.append(create<DiscardStatement>());
b.append(nullptr); b.append(nullptr);
EXPECT_FALSE(b.IsValid()); EXPECT_FALSE(b.IsValid());
} }
TEST_F(BlockStatementTest, IsValid_InvalidBodyStatement) { TEST_F(BlockStatementTest, IsValid_InvalidBodyStatement) {
BlockStatement b; BlockStatement b;
b.append(std::make_unique<IfStatement>()); b.append(create<IfStatement>());
EXPECT_FALSE(b.IsValid()); EXPECT_FALSE(b.IsValid());
} }
TEST_F(BlockStatementTest, ToStr) { TEST_F(BlockStatementTest, ToStr) {
BlockStatement b; BlockStatement b;
b.append(std::make_unique<DiscardStatement>()); b.append(create<DiscardStatement>());
std::ostringstream out; std::ostringstream out;
b.to_str(out, 2); b.to_str(out, 2);

View File

@ -115,58 +115,55 @@ class Builder {
/// @return an IdentifierExpression with the given name /// @return an IdentifierExpression with the given name
std::unique_ptr<ast::IdentifierExpression> make_expr( std::unique_ptr<ast::IdentifierExpression> make_expr(
const std::string& name) { const std::string& name) {
return std::make_unique<ast::IdentifierExpression>(name); return create<ast::IdentifierExpression>(name);
} }
/// @param name the identifier name /// @param name the identifier name
/// @return an IdentifierExpression with the given name /// @return an IdentifierExpression with the given name
std::unique_ptr<ast::IdentifierExpression> make_expr(const char* name) { std::unique_ptr<ast::IdentifierExpression> make_expr(const char* name) {
return std::make_unique<ast::IdentifierExpression>(name); return create<ast::IdentifierExpression>(name);
} }
/// @param value the float value /// @param value the float value
/// @return a Scalar constructor for the given value /// @return a Scalar constructor for the given value
std::unique_ptr<ast::ScalarConstructorExpression> make_expr(float value) { std::unique_ptr<ast::ScalarConstructorExpression> make_expr(float value) {
return std::make_unique<ast::ScalarConstructorExpression>( return create<ast::ScalarConstructorExpression>(make_literal(value));
make_literal(value));
} }
/// @param value the int value /// @param value the int value
/// @return a Scalar constructor for the given value /// @return a Scalar constructor for the given value
std::unique_ptr<ast::ScalarConstructorExpression> make_expr(int32_t value) { std::unique_ptr<ast::ScalarConstructorExpression> make_expr(int32_t value) {
return std::make_unique<ast::ScalarConstructorExpression>( return create<ast::ScalarConstructorExpression>(make_literal(value));
make_literal(value));
} }
/// @param value the unsigned int value /// @param value the unsigned int value
/// @return a Scalar constructor for the given value /// @return a Scalar constructor for the given value
std::unique_ptr<ast::ScalarConstructorExpression> make_expr(uint32_t value) { std::unique_ptr<ast::ScalarConstructorExpression> make_expr(uint32_t value) {
return std::make_unique<ast::ScalarConstructorExpression>( return create<ast::ScalarConstructorExpression>(make_literal(value));
make_literal(value));
} }
/// @param val the boolan value /// @param val the boolan value
/// @return a boolean literal with the given value /// @return a boolean literal with the given value
std::unique_ptr<ast::BoolLiteral> make_literal(bool val) { std::unique_ptr<ast::BoolLiteral> make_literal(bool val) {
return std::make_unique<ast::BoolLiteral>(bool_type(), val); return create<ast::BoolLiteral>(bool_type(), val);
} }
/// @param val the float value /// @param val the float value
/// @return a float literal with the given value /// @return a float literal with the given value
std::unique_ptr<ast::FloatLiteral> make_literal(float val) { std::unique_ptr<ast::FloatLiteral> make_literal(float val) {
return std::make_unique<ast::FloatLiteral>(f32(), val); return create<ast::FloatLiteral>(f32(), val);
} }
/// @param val the unsigned int value /// @param val the unsigned int value
/// @return a UintLiteral with the given value /// @return a UintLiteral with the given value
std::unique_ptr<ast::UintLiteral> make_literal(uint32_t val) { std::unique_ptr<ast::UintLiteral> make_literal(uint32_t val) {
return std::make_unique<ast::UintLiteral>(u32(), val); return create<ast::UintLiteral>(u32(), val);
} }
/// @param val the integer value /// @param val the integer value
/// @return the SintLiteral with the given value /// @return the SintLiteral with the given value
std::unique_ptr<ast::SintLiteral> make_literal(int32_t val) { std::unique_ptr<ast::SintLiteral> make_literal(int32_t val) {
return std::make_unique<ast::SintLiteral>(i32(), val); return create<ast::SintLiteral>(i32(), val);
} }
/// Converts `arg` to an `ast::Expression` using `make_expr()`, then appends /// Converts `arg` to an `ast::Expression` using `make_expr()`, then appends
@ -198,8 +195,7 @@ class Builder {
ARGS&&... args) { ARGS&&... args) {
ast::ExpressionList vals; ast::ExpressionList vals;
append_expr(vals, std::forward<ARGS>(args)...); append_expr(vals, std::forward<ARGS>(args)...);
return std::make_unique<ast::TypeConstructorExpression>(ty, return create<ast::TypeConstructorExpression>(ty, std::move(vals));
std::move(vals));
} }
/// @param name the variable name /// @param name the variable name
@ -221,6 +217,13 @@ class Builder {
return ast::CallExpression{make_expr(func), std::move(params)}; return ast::CallExpression{make_expr(func), std::move(params)};
} }
/// @return a `std::unique_ptr` to a new `T` constructed with `args`
/// @param args the arguments to forward to the constructor for `T`
template <typename T, typename... ARGS>
std::unique_ptr<T> create(ARGS&&... args) {
return std::make_unique<T>(std::forward<ARGS>(args)...);
}
private: private:
Context* ctx_ = nullptr; Context* ctx_ = nullptr;
}; };

View File

@ -24,10 +24,10 @@ namespace {
using CallExpressionTest = TestHelper; using CallExpressionTest = TestHelper;
TEST_F(CallExpressionTest, Creation) { TEST_F(CallExpressionTest, Creation) {
auto func = std::make_unique<IdentifierExpression>("func"); auto func = create<IdentifierExpression>("func");
ExpressionList params; ExpressionList params;
params.push_back(std::make_unique<IdentifierExpression>("param1")); params.push_back(create<IdentifierExpression>("param1"));
params.push_back(std::make_unique<IdentifierExpression>("param2")); params.push_back(create<IdentifierExpression>("param2"));
auto* func_ptr = func.get(); auto* func_ptr = func.get();
auto* param1_ptr = params[0].get(); auto* param1_ptr = params[0].get();
@ -43,7 +43,7 @@ TEST_F(CallExpressionTest, Creation) {
} }
TEST_F(CallExpressionTest, Creation_WithSource) { TEST_F(CallExpressionTest, Creation_WithSource) {
auto func = std::make_unique<IdentifierExpression>("func"); auto func = create<IdentifierExpression>("func");
CallExpression stmt(Source{Source::Location{20, 2}}, std::move(func), {}); CallExpression stmt(Source{Source::Location{20, 2}}, std::move(func), {});
auto src = stmt.source(); auto src = stmt.source();
EXPECT_EQ(src.range.begin.line, 20u); EXPECT_EQ(src.range.begin.line, 20u);
@ -51,13 +51,13 @@ TEST_F(CallExpressionTest, Creation_WithSource) {
} }
TEST_F(CallExpressionTest, IsCall) { TEST_F(CallExpressionTest, IsCall) {
auto func = std::make_unique<IdentifierExpression>("func"); auto func = create<IdentifierExpression>("func");
CallExpression stmt(std::move(func), {}); CallExpression stmt(std::move(func), {});
EXPECT_TRUE(stmt.IsCall()); EXPECT_TRUE(stmt.IsCall());
} }
TEST_F(CallExpressionTest, IsValid) { TEST_F(CallExpressionTest, IsValid) {
auto func = std::make_unique<IdentifierExpression>("func"); auto func = create<IdentifierExpression>("func");
CallExpression stmt(std::move(func), {}); CallExpression stmt(std::move(func), {});
EXPECT_TRUE(stmt.IsValid()); EXPECT_TRUE(stmt.IsValid());
} }
@ -68,36 +68,36 @@ TEST_F(CallExpressionTest, IsValid_MissingFunction) {
} }
TEST_F(CallExpressionTest, IsValid_NullParam) { TEST_F(CallExpressionTest, IsValid_NullParam) {
auto func = std::make_unique<IdentifierExpression>("func"); auto func = create<IdentifierExpression>("func");
ExpressionList params; ExpressionList params;
params.push_back(std::make_unique<IdentifierExpression>("param1")); params.push_back(create<IdentifierExpression>("param1"));
params.push_back(nullptr); params.push_back(nullptr);
params.push_back(std::make_unique<IdentifierExpression>("param2")); params.push_back(create<IdentifierExpression>("param2"));
CallExpression stmt(std::move(func), std::move(params)); CallExpression stmt(std::move(func), std::move(params));
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt.IsValid());
} }
TEST_F(CallExpressionTest, IsValid_InvalidFunction) { TEST_F(CallExpressionTest, IsValid_InvalidFunction) {
auto func = std::make_unique<IdentifierExpression>(""); auto func = create<IdentifierExpression>("");
ExpressionList params; ExpressionList params;
params.push_back(std::make_unique<IdentifierExpression>("param1")); params.push_back(create<IdentifierExpression>("param1"));
CallExpression stmt(std::move(func), std::move(params)); CallExpression stmt(std::move(func), std::move(params));
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt.IsValid());
} }
TEST_F(CallExpressionTest, IsValid_InvalidParam) { TEST_F(CallExpressionTest, IsValid_InvalidParam) {
auto func = std::make_unique<IdentifierExpression>("func"); auto func = create<IdentifierExpression>("func");
ExpressionList params; ExpressionList params;
params.push_back(std::make_unique<IdentifierExpression>("")); params.push_back(create<IdentifierExpression>(""));
CallExpression stmt(std::move(func), std::move(params)); CallExpression stmt(std::move(func), std::move(params));
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt.IsValid());
} }
TEST_F(CallExpressionTest, ToStr_NoParams) { TEST_F(CallExpressionTest, ToStr_NoParams) {
auto func = std::make_unique<IdentifierExpression>("func"); auto func = create<IdentifierExpression>("func");
CallExpression stmt(std::move(func), {}); CallExpression stmt(std::move(func), {});
std::ostringstream out; std::ostringstream out;
stmt.to_str(out, 2); stmt.to_str(out, 2);
@ -110,10 +110,10 @@ TEST_F(CallExpressionTest, ToStr_NoParams) {
} }
TEST_F(CallExpressionTest, ToStr_WithParams) { TEST_F(CallExpressionTest, ToStr_WithParams) {
auto func = std::make_unique<IdentifierExpression>("func"); auto func = create<IdentifierExpression>("func");
ExpressionList params; ExpressionList params;
params.push_back(std::make_unique<IdentifierExpression>("param1")); params.push_back(create<IdentifierExpression>("param1"));
params.push_back(std::make_unique<IdentifierExpression>("param2")); params.push_back(create<IdentifierExpression>("param2"));
CallExpression stmt(std::move(func), std::move(params)); CallExpression stmt(std::move(func), std::move(params));
std::ostringstream out; std::ostringstream out;

View File

@ -25,8 +25,8 @@ namespace {
using CallStatementTest = TestHelper; using CallStatementTest = TestHelper;
TEST_F(CallStatementTest, Creation) { TEST_F(CallStatementTest, Creation) {
auto expr = std::make_unique<ast::CallExpression>( auto expr = create<ast::CallExpression>(
std::make_unique<ast::IdentifierExpression>("func"), ExpressionList{}); create<ast::IdentifierExpression>("func"), ExpressionList{});
auto* expr_ptr = expr.get(); auto* expr_ptr = expr.get();
CallStatement c(std::move(expr)); CallStatement c(std::move(expr));
@ -39,8 +39,8 @@ TEST_F(CallStatementTest, IsCall) {
} }
TEST_F(CallStatementTest, IsValid) { TEST_F(CallStatementTest, IsValid) {
CallStatement c(std::make_unique<ast::CallExpression>( CallStatement c(create<ast::CallExpression>(
std::make_unique<ast::IdentifierExpression>("func"), ExpressionList{})); create<ast::IdentifierExpression>("func"), ExpressionList{}));
EXPECT_TRUE(c.IsValid()); EXPECT_TRUE(c.IsValid());
} }
@ -50,13 +50,13 @@ TEST_F(CallStatementTest, IsValid_MissingExpr) {
} }
TEST_F(CallStatementTest, IsValid_InvalidExpr) { TEST_F(CallStatementTest, IsValid_InvalidExpr) {
CallStatement c(std::make_unique<ast::CallExpression>()); CallStatement c(create<ast::CallExpression>());
EXPECT_FALSE(c.IsValid()); EXPECT_FALSE(c.IsValid());
} }
TEST_F(CallStatementTest, ToStr) { TEST_F(CallStatementTest, ToStr) {
CallStatement c(std::make_unique<ast::CallExpression>( CallStatement c(create<ast::CallExpression>(
std::make_unique<ast::IdentifierExpression>("func"), ExpressionList{})); create<ast::IdentifierExpression>("func"), ExpressionList{}));
std::ostringstream out; std::ostringstream out;
c.to_str(out, 2); c.to_str(out, 2);

View File

@ -32,10 +32,10 @@ TEST_F(CaseStatementTest, Creation_i32) {
ast::type::I32Type i32; ast::type::I32Type i32;
CaseSelectorList b; CaseSelectorList b;
b.push_back(std::make_unique<SintLiteral>(&i32, 2)); b.push_back(create<SintLiteral>(&i32, 2));
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
auto* int_ptr = b.back().get(); auto* int_ptr = b.back().get();
auto* discard_ptr = body->get(0); auto* discard_ptr = body->get(0);
@ -51,10 +51,10 @@ TEST_F(CaseStatementTest, Creation_u32) {
ast::type::U32Type u32; ast::type::U32Type u32;
CaseSelectorList b; CaseSelectorList b;
b.push_back(std::make_unique<UintLiteral>(&u32, 2)); b.push_back(create<UintLiteral>(&u32, 2));
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
auto* int_ptr = b.back().get(); auto* int_ptr = b.back().get();
auto* discard_ptr = body->get(0); auto* discard_ptr = body->get(0);
@ -69,10 +69,10 @@ TEST_F(CaseStatementTest, Creation_u32) {
TEST_F(CaseStatementTest, Creation_WithSource) { TEST_F(CaseStatementTest, Creation_WithSource) {
ast::type::I32Type i32; ast::type::I32Type i32;
CaseSelectorList b; CaseSelectorList b;
b.push_back(std::make_unique<SintLiteral>(&i32, 2)); b.push_back(create<SintLiteral>(&i32, 2));
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
CaseStatement c(Source{Source::Location{20, 2}}, std::move(b), CaseStatement c(Source{Source::Location{20, 2}}, std::move(b),
std::move(body)); std::move(body));
@ -82,8 +82,8 @@ TEST_F(CaseStatementTest, Creation_WithSource) {
} }
TEST_F(CaseStatementTest, IsDefault_WithoutSelectors) { TEST_F(CaseStatementTest, IsDefault_WithoutSelectors) {
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
CaseStatement c; CaseStatement c;
c.set_body(std::move(body)); c.set_body(std::move(body));
@ -93,7 +93,7 @@ TEST_F(CaseStatementTest, IsDefault_WithoutSelectors) {
TEST_F(CaseStatementTest, IsDefault_WithSelectors) { TEST_F(CaseStatementTest, IsDefault_WithSelectors) {
ast::type::I32Type i32; ast::type::I32Type i32;
CaseSelectorList b; CaseSelectorList b;
b.push_back(std::make_unique<SintLiteral>(&i32, 2)); b.push_back(create<SintLiteral>(&i32, 2));
CaseStatement c; CaseStatement c;
c.set_selectors(std::move(b)); c.set_selectors(std::move(b));
@ -113,10 +113,10 @@ TEST_F(CaseStatementTest, IsValid) {
TEST_F(CaseStatementTest, IsValid_NullBodyStatement) { TEST_F(CaseStatementTest, IsValid_NullBodyStatement) {
ast::type::I32Type i32; ast::type::I32Type i32;
CaseSelectorList b; CaseSelectorList b;
b.push_back(std::make_unique<SintLiteral>(&i32, 2)); b.push_back(create<SintLiteral>(&i32, 2));
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
body->append(nullptr); body->append(nullptr);
CaseStatement c(std::move(b), std::move(body)); CaseStatement c(std::move(b), std::move(body));
@ -126,10 +126,10 @@ TEST_F(CaseStatementTest, IsValid_NullBodyStatement) {
TEST_F(CaseStatementTest, IsValid_InvalidBodyStatement) { TEST_F(CaseStatementTest, IsValid_InvalidBodyStatement) {
ast::type::I32Type i32; ast::type::I32Type i32;
CaseSelectorList b; CaseSelectorList b;
b.push_back(std::make_unique<SintLiteral>(&i32, 2)); b.push_back(create<SintLiteral>(&i32, 2));
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<IfStatement>()); body->append(create<IfStatement>());
CaseStatement c({std::move(b)}, std::move(body)); CaseStatement c({std::move(b)}, std::move(body));
EXPECT_FALSE(c.IsValid()); EXPECT_FALSE(c.IsValid());
@ -138,10 +138,10 @@ TEST_F(CaseStatementTest, IsValid_InvalidBodyStatement) {
TEST_F(CaseStatementTest, ToStr_WithSelectors_i32) { TEST_F(CaseStatementTest, ToStr_WithSelectors_i32) {
ast::type::I32Type i32; ast::type::I32Type i32;
CaseSelectorList b; CaseSelectorList b;
b.push_back(std::make_unique<SintLiteral>(&i32, -2)); b.push_back(create<SintLiteral>(&i32, -2));
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
CaseStatement c({std::move(b)}, std::move(body)); CaseStatement c({std::move(b)}, std::move(body));
std::ostringstream out; std::ostringstream out;
@ -155,10 +155,10 @@ TEST_F(CaseStatementTest, ToStr_WithSelectors_i32) {
TEST_F(CaseStatementTest, ToStr_WithSelectors_u32) { TEST_F(CaseStatementTest, ToStr_WithSelectors_u32) {
ast::type::U32Type u32; ast::type::U32Type u32;
CaseSelectorList b; CaseSelectorList b;
b.push_back(std::make_unique<UintLiteral>(&u32, 2)); b.push_back(create<UintLiteral>(&u32, 2));
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
CaseStatement c({std::move(b)}, std::move(body)); CaseStatement c({std::move(b)}, std::move(body));
std::ostringstream out; std::ostringstream out;
@ -173,11 +173,11 @@ TEST_F(CaseStatementTest, ToStr_WithMultipleSelectors) {
ast::type::I32Type i32; ast::type::I32Type i32;
CaseSelectorList b; CaseSelectorList b;
b.push_back(std::make_unique<SintLiteral>(&i32, 1)); b.push_back(create<SintLiteral>(&i32, 1));
b.push_back(std::make_unique<SintLiteral>(&i32, 2)); b.push_back(create<SintLiteral>(&i32, 2));
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
CaseStatement c(std::move(b), std::move(body)); CaseStatement c(std::move(b), std::move(body));
std::ostringstream out; std::ostringstream out;
@ -189,8 +189,8 @@ TEST_F(CaseStatementTest, ToStr_WithMultipleSelectors) {
} }
TEST_F(CaseStatementTest, ToStr_WithoutSelectors) { TEST_F(CaseStatementTest, ToStr_WithoutSelectors) {
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
CaseStatement c(CaseSelectorList{}, std::move(body)); CaseStatement c(CaseSelectorList{}, std::move(body));
std::ostringstream out; std::ostringstream out;

View File

@ -34,7 +34,7 @@ using DecoratedVariableTest = TestHelper;
TEST_F(DecoratedVariableTest, Creation) { TEST_F(DecoratedVariableTest, Creation) {
type::I32Type t; type::I32Type t;
auto var = std::make_unique<Variable>("my_var", StorageClass::kFunction, &t); auto var = create<Variable>("my_var", StorageClass::kFunction, &t);
DecoratedVariable dv(std::move(var)); DecoratedVariable dv(std::move(var));
EXPECT_EQ(dv.name(), "my_var"); EXPECT_EQ(dv.name(), "my_var");
@ -49,7 +49,7 @@ TEST_F(DecoratedVariableTest, Creation) {
TEST_F(DecoratedVariableTest, CreationWithSource) { TEST_F(DecoratedVariableTest, CreationWithSource) {
Source s{Source::Range{Source::Location{27, 4}, Source::Location{27, 5}}}; Source s{Source::Range{Source::Location{27, 4}, Source::Location{27, 5}}};
type::F32Type t; type::F32Type t;
auto var = std::make_unique<Variable>(s, "i", StorageClass::kPrivate, &t); auto var = create<Variable>(s, "i", StorageClass::kPrivate, &t);
DecoratedVariable dv(std::move(var)); DecoratedVariable dv(std::move(var));
EXPECT_EQ(dv.name(), "i"); EXPECT_EQ(dv.name(), "i");
@ -63,7 +63,7 @@ TEST_F(DecoratedVariableTest, CreationWithSource) {
TEST_F(DecoratedVariableTest, NoDecorations) { TEST_F(DecoratedVariableTest, NoDecorations) {
type::I32Type t; type::I32Type t;
auto var = std::make_unique<Variable>("my_var", StorageClass::kFunction, &t); auto var = create<Variable>("my_var", StorageClass::kFunction, &t);
DecoratedVariable dv(std::move(var)); DecoratedVariable dv(std::move(var));
EXPECT_FALSE(dv.HasLocationDecoration()); EXPECT_FALSE(dv.HasLocationDecoration());
EXPECT_FALSE(dv.HasBuiltinDecoration()); EXPECT_FALSE(dv.HasBuiltinDecoration());
@ -72,14 +72,13 @@ TEST_F(DecoratedVariableTest, NoDecorations) {
TEST_F(DecoratedVariableTest, WithDecorations) { TEST_F(DecoratedVariableTest, WithDecorations) {
type::F32Type t; type::F32Type t;
auto var = std::make_unique<Variable>("my_var", StorageClass::kFunction, &t); auto var = create<Variable>("my_var", StorageClass::kFunction, &t);
DecoratedVariable dv(std::move(var)); DecoratedVariable dv(std::move(var));
VariableDecorationList decos; VariableDecorationList decos;
decos.push_back(std::make_unique<LocationDecoration>(1, Source{})); decos.push_back(create<LocationDecoration>(1, Source{}));
decos.push_back( decos.push_back(create<BuiltinDecoration>(ast::Builtin::kPosition, Source{}));
std::make_unique<BuiltinDecoration>(ast::Builtin::kPosition, Source{})); decos.push_back(create<ConstantIdDecoration>(1200, Source{}));
decos.push_back(std::make_unique<ConstantIdDecoration>(1200, Source{}));
dv.set_decorations(std::move(decos)); dv.set_decorations(std::move(decos));
@ -90,11 +89,11 @@ TEST_F(DecoratedVariableTest, WithDecorations) {
TEST_F(DecoratedVariableTest, ConstantId) { TEST_F(DecoratedVariableTest, ConstantId) {
type::F32Type t; type::F32Type t;
auto var = std::make_unique<Variable>("my_var", StorageClass::kFunction, &t); auto var = create<Variable>("my_var", StorageClass::kFunction, &t);
DecoratedVariable dv(std::move(var)); DecoratedVariable dv(std::move(var));
VariableDecorationList decos; VariableDecorationList decos;
decos.push_back(std::make_unique<ConstantIdDecoration>(1200, Source{})); decos.push_back(create<ConstantIdDecoration>(1200, Source{}));
dv.set_decorations(std::move(decos)); dv.set_decorations(std::move(decos));
EXPECT_EQ(dv.constant_id(), 1200u); EXPECT_EQ(dv.constant_id(), 1200u);
@ -102,7 +101,7 @@ TEST_F(DecoratedVariableTest, ConstantId) {
TEST_F(DecoratedVariableTest, IsValid) { TEST_F(DecoratedVariableTest, IsValid) {
type::I32Type t; type::I32Type t;
auto var = std::make_unique<Variable>("my_var", StorageClass::kNone, &t); auto var = create<Variable>("my_var", StorageClass::kNone, &t);
DecoratedVariable dv(std::move(var)); DecoratedVariable dv(std::move(var));
EXPECT_TRUE(dv.IsValid()); EXPECT_TRUE(dv.IsValid());
} }
@ -114,13 +113,13 @@ TEST_F(DecoratedVariableTest, IsDecorated) {
TEST_F(DecoratedVariableTest, to_str) { TEST_F(DecoratedVariableTest, to_str) {
type::F32Type t; type::F32Type t;
auto var = std::make_unique<Variable>("my_var", StorageClass::kFunction, &t); auto var = create<Variable>("my_var", StorageClass::kFunction, &t);
DecoratedVariable dv(std::move(var)); DecoratedVariable dv(std::move(var));
dv.set_constructor(std::make_unique<IdentifierExpression>("expr")); dv.set_constructor(create<IdentifierExpression>("expr"));
VariableDecorationList decos; VariableDecorationList decos;
decos.push_back(std::make_unique<BindingDecoration>(2, Source{})); decos.push_back(create<BindingDecoration>(2, Source{}));
decos.push_back(std::make_unique<SetDecoration>(1, Source{})); decos.push_back(create<SetDecoration>(1, Source{}));
dv.set_decorations(std::move(decos)); dv.set_decorations(std::move(decos));
std::ostringstream out; std::ostringstream out;

View File

@ -42,7 +42,7 @@ TEST_F(DecorationTest, AsIncorrectType) {
} }
TEST_F(DecorationTest, Is) { TEST_F(DecorationTest, Is) {
auto decoration = std::make_unique<ConstantIdDecoration>(1, Source{}); auto decoration = create<ConstantIdDecoration>(1, Source{});
EXPECT_TRUE(decoration->Is<VariableDecoration>()); EXPECT_TRUE(decoration->Is<VariableDecoration>());
EXPECT_FALSE(decoration->Is<ArrayDecoration>()); EXPECT_FALSE(decoration->Is<ArrayDecoration>());
} }

View File

@ -29,10 +29,10 @@ using ElseStatementTest = TestHelper;
TEST_F(ElseStatementTest, Creation) { TEST_F(ElseStatementTest, Creation) {
ast::type::BoolType bool_type; ast::type::BoolType bool_type;
auto cond = std::make_unique<ScalarConstructorExpression>( auto cond = create<ScalarConstructorExpression>(
std::make_unique<BoolLiteral>(&bool_type, true)); create<BoolLiteral>(&bool_type, true));
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
auto* cond_ptr = cond.get(); auto* cond_ptr = cond.get();
auto* discard_ptr = body->get(0); auto* discard_ptr = body->get(0);
@ -44,8 +44,7 @@ TEST_F(ElseStatementTest, Creation) {
} }
TEST_F(ElseStatementTest, Creation_WithSource) { TEST_F(ElseStatementTest, Creation_WithSource) {
ElseStatement e(Source{Source::Location{20, 2}}, ElseStatement e(Source{Source::Location{20, 2}}, create<BlockStatement>());
std::make_unique<BlockStatement>());
auto src = e.source(); auto src = e.source();
EXPECT_EQ(src.range.begin.line, 20u); EXPECT_EQ(src.range.begin.line, 20u);
EXPECT_EQ(src.range.begin.column, 2u); EXPECT_EQ(src.range.begin.column, 2u);
@ -58,9 +57,9 @@ TEST_F(ElseStatementTest, IsElse) {
TEST_F(ElseStatementTest, HasCondition) { TEST_F(ElseStatementTest, HasCondition) {
ast::type::BoolType bool_type; ast::type::BoolType bool_type;
auto cond = std::make_unique<ScalarConstructorExpression>( auto cond = create<ScalarConstructorExpression>(
std::make_unique<BoolLiteral>(&bool_type, true)); create<BoolLiteral>(&bool_type, true));
ElseStatement e(std::move(cond), std::make_unique<BlockStatement>()); ElseStatement e(std::move(cond), create<BlockStatement>());
EXPECT_TRUE(e.HasCondition()); EXPECT_TRUE(e.HasCondition());
} }
@ -75,16 +74,16 @@ TEST_F(ElseStatementTest, IsValid) {
} }
TEST_F(ElseStatementTest, IsValid_WithBody) { TEST_F(ElseStatementTest, IsValid_WithBody) {
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
ElseStatement e(std::move(body)); ElseStatement e(std::move(body));
EXPECT_TRUE(e.IsValid()); EXPECT_TRUE(e.IsValid());
} }
TEST_F(ElseStatementTest, IsValid_WithNullBodyStatement) { TEST_F(ElseStatementTest, IsValid_WithNullBodyStatement) {
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
body->append(nullptr); body->append(nullptr);
ElseStatement e(std::move(body)); ElseStatement e(std::move(body));
@ -92,14 +91,14 @@ TEST_F(ElseStatementTest, IsValid_WithNullBodyStatement) {
} }
TEST_F(ElseStatementTest, IsValid_InvalidCondition) { TEST_F(ElseStatementTest, IsValid_InvalidCondition) {
auto cond = std::make_unique<ScalarConstructorExpression>(); auto cond = create<ScalarConstructorExpression>();
ElseStatement e(std::move(cond), std::make_unique<BlockStatement>()); ElseStatement e(std::move(cond), create<BlockStatement>());
EXPECT_FALSE(e.IsValid()); EXPECT_FALSE(e.IsValid());
} }
TEST_F(ElseStatementTest, IsValid_InvalidBodyStatement) { TEST_F(ElseStatementTest, IsValid_InvalidBodyStatement) {
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<IfStatement>()); body->append(create<IfStatement>());
ElseStatement e(std::move(body)); ElseStatement e(std::move(body));
EXPECT_FALSE(e.IsValid()); EXPECT_FALSE(e.IsValid());
@ -107,10 +106,10 @@ TEST_F(ElseStatementTest, IsValid_InvalidBodyStatement) {
TEST_F(ElseStatementTest, ToStr) { TEST_F(ElseStatementTest, ToStr) {
ast::type::BoolType bool_type; ast::type::BoolType bool_type;
auto cond = std::make_unique<ScalarConstructorExpression>( auto cond = create<ScalarConstructorExpression>(
std::make_unique<BoolLiteral>(&bool_type, true)); create<BoolLiteral>(&bool_type, true));
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
ElseStatement e(std::move(cond), std::move(body)); ElseStatement e(std::move(cond), std::move(body));
std::ostringstream out; std::ostringstream out;
@ -127,8 +126,8 @@ TEST_F(ElseStatementTest, ToStr) {
} }
TEST_F(ElseStatementTest, ToStr_NoCondition) { TEST_F(ElseStatementTest, ToStr_NoCondition) {
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
ElseStatement e(std::move(body)); ElseStatement e(std::move(body));
std::ostringstream out; std::ostringstream out;

View File

@ -37,8 +37,7 @@ TEST_F(FunctionTest, Creation) {
type::I32Type i32; type::I32Type i32;
VariableList params; VariableList params;
params.push_back( params.push_back(create<Variable>("var", StorageClass::kNone, &i32));
std::make_unique<Variable>("var", StorageClass::kNone, &i32));
auto* var_ptr = params[0].get(); auto* var_ptr = params[0].get();
Function f("func", std::move(params), &void_type); Function f("func", std::move(params), &void_type);
@ -53,8 +52,7 @@ TEST_F(FunctionTest, Creation_WithSource) {
type::I32Type i32; type::I32Type i32;
VariableList params; VariableList params;
params.push_back( params.push_back(create<Variable>("var", StorageClass::kNone, &i32));
std::make_unique<Variable>("var", StorageClass::kNone, &i32));
Function f(Source{Source::Location{20, 2}}, "func", std::move(params), Function f(Source{Source::Location{20, 2}}, "func", std::move(params),
&void_type); &void_type);
@ -89,25 +87,25 @@ TEST_F(FunctionTest, GetReferenceLocations) {
VariableDecorationList decos; VariableDecorationList decos;
DecoratedVariable loc1( DecoratedVariable loc1(
std::make_unique<ast::Variable>("loc1", StorageClass::kInput, &i32)); create<ast::Variable>("loc1", StorageClass::kInput, &i32));
decos.push_back(std::make_unique<ast::LocationDecoration>(0, Source{})); decos.push_back(create<ast::LocationDecoration>(0, Source{}));
loc1.set_decorations(std::move(decos)); loc1.set_decorations(std::move(decos));
DecoratedVariable loc2( DecoratedVariable loc2(
std::make_unique<ast::Variable>("loc2", StorageClass::kInput, &i32)); create<ast::Variable>("loc2", StorageClass::kInput, &i32));
decos.push_back(std::make_unique<ast::LocationDecoration>(1, Source{})); decos.push_back(create<ast::LocationDecoration>(1, Source{}));
loc2.set_decorations(std::move(decos)); loc2.set_decorations(std::move(decos));
DecoratedVariable builtin1( DecoratedVariable builtin1(
std::make_unique<ast::Variable>("builtin1", StorageClass::kInput, &i32)); create<ast::Variable>("builtin1", StorageClass::kInput, &i32));
decos.push_back(std::make_unique<ast::BuiltinDecoration>( decos.push_back(
ast::Builtin::kPosition, Source{})); create<ast::BuiltinDecoration>(ast::Builtin::kPosition, Source{}));
builtin1.set_decorations(std::move(decos)); builtin1.set_decorations(std::move(decos));
DecoratedVariable builtin2( DecoratedVariable builtin2(
std::make_unique<ast::Variable>("builtin2", StorageClass::kInput, &i32)); create<ast::Variable>("builtin2", StorageClass::kInput, &i32));
decos.push_back(std::make_unique<ast::BuiltinDecoration>( decos.push_back(
ast::Builtin::kFragDepth, Source{})); create<ast::BuiltinDecoration>(ast::Builtin::kFragDepth, Source{}));
builtin2.set_decorations(std::move(decos)); builtin2.set_decorations(std::move(decos));
Function f("func", VariableList{}, &void_type); Function f("func", VariableList{}, &void_type);
@ -132,25 +130,25 @@ TEST_F(FunctionTest, GetReferenceBuiltins) {
VariableDecorationList decos; VariableDecorationList decos;
DecoratedVariable loc1( DecoratedVariable loc1(
std::make_unique<ast::Variable>("loc1", StorageClass::kInput, &i32)); create<ast::Variable>("loc1", StorageClass::kInput, &i32));
decos.push_back(std::make_unique<ast::LocationDecoration>(0, Source{})); decos.push_back(create<ast::LocationDecoration>(0, Source{}));
loc1.set_decorations(std::move(decos)); loc1.set_decorations(std::move(decos));
DecoratedVariable loc2( DecoratedVariable loc2(
std::make_unique<ast::Variable>("loc2", StorageClass::kInput, &i32)); create<ast::Variable>("loc2", StorageClass::kInput, &i32));
decos.push_back(std::make_unique<ast::LocationDecoration>(1, Source{})); decos.push_back(create<ast::LocationDecoration>(1, Source{}));
loc2.set_decorations(std::move(decos)); loc2.set_decorations(std::move(decos));
DecoratedVariable builtin1( DecoratedVariable builtin1(
std::make_unique<ast::Variable>("builtin1", StorageClass::kInput, &i32)); create<ast::Variable>("builtin1", StorageClass::kInput, &i32));
decos.push_back(std::make_unique<ast::BuiltinDecoration>( decos.push_back(
ast::Builtin::kPosition, Source{})); create<ast::BuiltinDecoration>(ast::Builtin::kPosition, Source{}));
builtin1.set_decorations(std::move(decos)); builtin1.set_decorations(std::move(decos));
DecoratedVariable builtin2( DecoratedVariable builtin2(
std::make_unique<ast::Variable>("builtin2", StorageClass::kInput, &i32)); create<ast::Variable>("builtin2", StorageClass::kInput, &i32));
decos.push_back(std::make_unique<ast::BuiltinDecoration>( decos.push_back(
ast::Builtin::kFragDepth, Source{})); create<ast::BuiltinDecoration>(ast::Builtin::kFragDepth, Source{}));
builtin2.set_decorations(std::move(decos)); builtin2.set_decorations(std::move(decos));
Function f("func", VariableList{}, &void_type); Function f("func", VariableList{}, &void_type);
@ -187,11 +185,10 @@ TEST_F(FunctionTest, IsValid) {
type::I32Type i32; type::I32Type i32;
VariableList params; VariableList params;
params.push_back( params.push_back(create<Variable>("var", StorageClass::kNone, &i32));
std::make_unique<Variable>("var", StorageClass::kNone, &i32));
auto block = std::make_unique<ast::BlockStatement>(); auto block = create<ast::BlockStatement>();
block->append(std::make_unique<DiscardStatement>()); block->append(create<DiscardStatement>());
Function f("func", std::move(params), &void_type); Function f("func", std::move(params), &void_type);
f.set_body(std::move(block)); f.set_body(std::move(block));
@ -203,8 +200,7 @@ TEST_F(FunctionTest, IsValid_EmptyName) {
type::I32Type i32; type::I32Type i32;
VariableList params; VariableList params;
params.push_back( params.push_back(create<Variable>("var", StorageClass::kNone, &i32));
std::make_unique<Variable>("var", StorageClass::kNone, &i32));
Function f("", std::move(params), &void_type); Function f("", std::move(params), &void_type);
EXPECT_FALSE(f.IsValid()); EXPECT_FALSE(f.IsValid());
@ -214,8 +210,7 @@ TEST_F(FunctionTest, IsValid_MissingReturnType) {
type::I32Type i32; type::I32Type i32;
VariableList params; VariableList params;
params.push_back( params.push_back(create<Variable>("var", StorageClass::kNone, &i32));
std::make_unique<Variable>("var", StorageClass::kNone, &i32));
Function f("func", std::move(params), nullptr); Function f("func", std::move(params), nullptr);
EXPECT_FALSE(f.IsValid()); EXPECT_FALSE(f.IsValid());
@ -226,8 +221,7 @@ TEST_F(FunctionTest, IsValid_NullParam) {
type::I32Type i32; type::I32Type i32;
VariableList params; VariableList params;
params.push_back( params.push_back(create<Variable>("var", StorageClass::kNone, &i32));
std::make_unique<Variable>("var", StorageClass::kNone, &i32));
params.push_back(nullptr); params.push_back(nullptr);
Function f("func", std::move(params), &void_type); Function f("func", std::move(params), &void_type);
@ -238,8 +232,7 @@ TEST_F(FunctionTest, IsValid_InvalidParam) {
type::VoidType void_type; type::VoidType void_type;
VariableList params; VariableList params;
params.push_back( params.push_back(create<Variable>("var", StorageClass::kNone, nullptr));
std::make_unique<Variable>("var", StorageClass::kNone, nullptr));
Function f("func", std::move(params), &void_type); Function f("func", std::move(params), &void_type);
EXPECT_FALSE(f.IsValid()); EXPECT_FALSE(f.IsValid());
@ -250,11 +243,10 @@ TEST_F(FunctionTest, IsValid_NullBodyStatement) {
type::I32Type i32; type::I32Type i32;
VariableList params; VariableList params;
params.push_back( params.push_back(create<Variable>("var", StorageClass::kNone, &i32));
std::make_unique<Variable>("var", StorageClass::kNone, &i32));
auto block = std::make_unique<ast::BlockStatement>(); auto block = create<ast::BlockStatement>();
block->append(std::make_unique<DiscardStatement>()); block->append(create<DiscardStatement>());
block->append(nullptr); block->append(nullptr);
Function f("func", std::move(params), &void_type); Function f("func", std::move(params), &void_type);
@ -267,11 +259,10 @@ TEST_F(FunctionTest, IsValid_InvalidBodyStatement) {
type::I32Type i32; type::I32Type i32;
VariableList params; VariableList params;
params.push_back( params.push_back(create<Variable>("var", StorageClass::kNone, &i32));
std::make_unique<Variable>("var", StorageClass::kNone, &i32));
auto block = std::make_unique<ast::BlockStatement>(); auto block = create<ast::BlockStatement>();
block->append(std::make_unique<DiscardStatement>()); block->append(create<DiscardStatement>());
block->append(nullptr); block->append(nullptr);
Function f("func", std::move(params), &void_type); Function f("func", std::move(params), &void_type);
@ -283,8 +274,8 @@ TEST_F(FunctionTest, ToStr) {
type::VoidType void_type; type::VoidType void_type;
type::I32Type i32; type::I32Type i32;
auto block = std::make_unique<ast::BlockStatement>(); auto block = create<ast::BlockStatement>();
block->append(std::make_unique<DiscardStatement>()); block->append(create<DiscardStatement>());
Function f("func", {}, &void_type); Function f("func", {}, &void_type);
f.set_body(std::move(block)); f.set_body(std::move(block));
@ -303,12 +294,12 @@ TEST_F(FunctionTest, ToStr_WithDecoration) {
type::VoidType void_type; type::VoidType void_type;
type::I32Type i32; type::I32Type i32;
auto block = std::make_unique<ast::BlockStatement>(); auto block = create<ast::BlockStatement>();
block->append(std::make_unique<DiscardStatement>()); block->append(create<DiscardStatement>());
Function f("func", {}, &void_type); Function f("func", {}, &void_type);
f.set_body(std::move(block)); f.set_body(std::move(block));
f.add_decoration(std::make_unique<WorkgroupDecoration>(2, 4, 6, Source{})); f.add_decoration(create<WorkgroupDecoration>(2, 4, 6, Source{}));
std::ostringstream out; std::ostringstream out;
f.to_str(out, 2); f.to_str(out, 2);
@ -326,11 +317,10 @@ TEST_F(FunctionTest, ToStr_WithParams) {
type::I32Type i32; type::I32Type i32;
VariableList params; VariableList params;
params.push_back( params.push_back(create<Variable>("var", StorageClass::kNone, &i32));
std::make_unique<Variable>("var", StorageClass::kNone, &i32));
auto block = std::make_unique<ast::BlockStatement>(); auto block = create<ast::BlockStatement>();
block->append(std::make_unique<DiscardStatement>()); block->append(create<DiscardStatement>());
Function f("func", std::move(params), &void_type); Function f("func", std::move(params), &void_type);
f.set_body(std::move(block)); f.set_body(std::move(block));
@ -364,10 +354,8 @@ TEST_F(FunctionTest, TypeName_WithParams) {
type::F32Type f32; type::F32Type f32;
VariableList params; VariableList params;
params.push_back( params.push_back(create<Variable>("var1", StorageClass::kNone, &i32));
std::make_unique<Variable>("var1", StorageClass::kNone, &i32)); params.push_back(create<Variable>("var2", StorageClass::kNone, &f32));
params.push_back(
std::make_unique<Variable>("var2", StorageClass::kNone, &f32));
Function f("func", std::move(params), &void_type); Function f("func", std::move(params), &void_type);
EXPECT_EQ(f.type_name(), "__func__void__i32__f32"); EXPECT_EQ(f.type_name(), "__func__void__i32__f32");
@ -377,8 +365,8 @@ TEST_F(FunctionTest, GetLastStatement) {
type::VoidType void_type; type::VoidType void_type;
VariableList params; VariableList params;
auto body = std::make_unique<ast::BlockStatement>(); auto body = create<ast::BlockStatement>();
auto stmt = std::make_unique<DiscardStatement>(); auto stmt = create<DiscardStatement>();
auto* stmt_ptr = stmt.get(); auto* stmt_ptr = stmt.get();
body->append(std::move(stmt)); body->append(std::move(stmt));
Function f("func", std::move(params), &void_type); Function f("func", std::move(params), &void_type);
@ -391,7 +379,7 @@ TEST_F(FunctionTest, GetLastStatement_nullptr) {
type::VoidType void_type; type::VoidType void_type;
VariableList params; VariableList params;
auto body = std::make_unique<ast::BlockStatement>(); auto body = create<ast::BlockStatement>();
Function f("func", std::move(params), &void_type); Function f("func", std::move(params), &void_type);
f.set_body(std::move(body)); f.set_body(std::move(body));
@ -413,7 +401,7 @@ TEST_F(FunctionTest, WorkgroupSize_NoneSet) {
TEST_F(FunctionTest, WorkgroupSize) { TEST_F(FunctionTest, WorkgroupSize) {
type::VoidType void_type; type::VoidType void_type;
Function f("f", {}, &void_type); Function f("f", {}, &void_type);
f.add_decoration(std::make_unique<WorkgroupDecoration>(2u, 4u, 6u, Source{})); f.add_decoration(create<WorkgroupDecoration>(2u, 4u, 6u, Source{}));
uint32_t x = 0; uint32_t x = 0;
uint32_t y = 0; uint32_t y = 0;

View File

@ -25,9 +25,9 @@ namespace {
using IfStatementTest = TestHelper; using IfStatementTest = TestHelper;
TEST_F(IfStatementTest, Creation) { TEST_F(IfStatementTest, Creation) {
auto cond = std::make_unique<IdentifierExpression>("cond"); auto cond = create<IdentifierExpression>("cond");
auto body = std::make_unique<ast::BlockStatement>(); auto body = create<ast::BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
auto* cond_ptr = cond.get(); auto* cond_ptr = cond.get();
auto* stmt_ptr = body->get(0); auto* stmt_ptr = body->get(0);
@ -39,9 +39,9 @@ TEST_F(IfStatementTest, Creation) {
} }
TEST_F(IfStatementTest, Creation_WithSource) { TEST_F(IfStatementTest, Creation_WithSource) {
auto cond = std::make_unique<IdentifierExpression>("cond"); auto cond = create<IdentifierExpression>("cond");
auto body = std::make_unique<ast::BlockStatement>(); auto body = create<ast::BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
IfStatement stmt(Source{Source::Location{20, 2}}, std::move(cond), IfStatement stmt(Source{Source::Location{20, 2}}, std::move(cond),
std::move(body)); std::move(body));
@ -56,23 +56,23 @@ TEST_F(IfStatementTest, IsIf) {
} }
TEST_F(IfStatementTest, IsValid) { TEST_F(IfStatementTest, IsValid) {
auto cond = std::make_unique<IdentifierExpression>("cond"); auto cond = create<IdentifierExpression>("cond");
auto body = std::make_unique<ast::BlockStatement>(); auto body = create<ast::BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
IfStatement stmt(std::move(cond), std::move(body)); IfStatement stmt(std::move(cond), std::move(body));
EXPECT_TRUE(stmt.IsValid()); EXPECT_TRUE(stmt.IsValid());
} }
TEST_F(IfStatementTest, IsValid_WithElseStatements) { TEST_F(IfStatementTest, IsValid_WithElseStatements) {
auto cond = std::make_unique<IdentifierExpression>("cond"); auto cond = create<IdentifierExpression>("cond");
auto body = std::make_unique<ast::BlockStatement>(); auto body = create<ast::BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
ElseStatementList else_stmts; ElseStatementList else_stmts;
else_stmts.push_back(std::make_unique<ElseStatement>()); else_stmts.push_back(create<ElseStatement>());
else_stmts[0]->set_condition(std::make_unique<IdentifierExpression>("Ident")); else_stmts[0]->set_condition(create<IdentifierExpression>("Ident"));
else_stmts.push_back(std::make_unique<ElseStatement>()); else_stmts.push_back(create<ElseStatement>());
IfStatement stmt(std::move(cond), std::move(body)); IfStatement stmt(std::move(cond), std::move(body));
stmt.set_else_statements(std::move(else_stmts)); stmt.set_else_statements(std::move(else_stmts));
@ -80,26 +80,26 @@ TEST_F(IfStatementTest, IsValid_WithElseStatements) {
} }
TEST_F(IfStatementTest, IsValid_MissingCondition) { TEST_F(IfStatementTest, IsValid_MissingCondition) {
auto body = std::make_unique<ast::BlockStatement>(); auto body = create<ast::BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
IfStatement stmt(nullptr, std::move(body)); IfStatement stmt(nullptr, std::move(body));
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt.IsValid());
} }
TEST_F(IfStatementTest, IsValid_InvalidCondition) { TEST_F(IfStatementTest, IsValid_InvalidCondition) {
auto cond = std::make_unique<IdentifierExpression>(""); auto cond = create<IdentifierExpression>("");
auto body = std::make_unique<ast::BlockStatement>(); auto body = create<ast::BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
IfStatement stmt(std::move(cond), std::move(body)); IfStatement stmt(std::move(cond), std::move(body));
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt.IsValid());
} }
TEST_F(IfStatementTest, IsValid_NullBodyStatement) { TEST_F(IfStatementTest, IsValid_NullBodyStatement) {
auto cond = std::make_unique<IdentifierExpression>("cond"); auto cond = create<IdentifierExpression>("cond");
auto body = std::make_unique<ast::BlockStatement>(); auto body = create<ast::BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
body->append(nullptr); body->append(nullptr);
IfStatement stmt(std::move(cond), std::move(body)); IfStatement stmt(std::move(cond), std::move(body));
@ -107,24 +107,24 @@ TEST_F(IfStatementTest, IsValid_NullBodyStatement) {
} }
TEST_F(IfStatementTest, IsValid_InvalidBodyStatement) { TEST_F(IfStatementTest, IsValid_InvalidBodyStatement) {
auto cond = std::make_unique<IdentifierExpression>("cond"); auto cond = create<IdentifierExpression>("cond");
auto body = std::make_unique<ast::BlockStatement>(); auto body = create<ast::BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
body->append(std::make_unique<IfStatement>()); body->append(create<IfStatement>());
IfStatement stmt(std::move(cond), std::move(body)); IfStatement stmt(std::move(cond), std::move(body));
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt.IsValid());
} }
TEST_F(IfStatementTest, IsValid_NullElseStatement) { TEST_F(IfStatementTest, IsValid_NullElseStatement) {
auto cond = std::make_unique<IdentifierExpression>("cond"); auto cond = create<IdentifierExpression>("cond");
auto body = std::make_unique<ast::BlockStatement>(); auto body = create<ast::BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
ElseStatementList else_stmts; ElseStatementList else_stmts;
else_stmts.push_back(std::make_unique<ElseStatement>()); else_stmts.push_back(create<ElseStatement>());
else_stmts[0]->set_condition(std::make_unique<IdentifierExpression>("Ident")); else_stmts[0]->set_condition(create<IdentifierExpression>("Ident"));
else_stmts.push_back(std::make_unique<ElseStatement>()); else_stmts.push_back(create<ElseStatement>());
else_stmts.push_back(nullptr); else_stmts.push_back(nullptr);
IfStatement stmt(std::move(cond), std::move(body)); IfStatement stmt(std::move(cond), std::move(body));
@ -133,13 +133,13 @@ TEST_F(IfStatementTest, IsValid_NullElseStatement) {
} }
TEST_F(IfStatementTest, IsValid_InvalidElseStatement) { TEST_F(IfStatementTest, IsValid_InvalidElseStatement) {
auto cond = std::make_unique<IdentifierExpression>("cond"); auto cond = create<IdentifierExpression>("cond");
auto body = std::make_unique<ast::BlockStatement>(); auto body = create<ast::BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
ElseStatementList else_stmts; ElseStatementList else_stmts;
else_stmts.push_back(std::make_unique<ElseStatement>()); else_stmts.push_back(create<ElseStatement>());
else_stmts[0]->set_condition(std::make_unique<IdentifierExpression>("")); else_stmts[0]->set_condition(create<IdentifierExpression>(""));
IfStatement stmt(std::move(cond), std::move(body)); IfStatement stmt(std::move(cond), std::move(body));
stmt.set_else_statements(std::move(else_stmts)); stmt.set_else_statements(std::move(else_stmts));
@ -147,13 +147,13 @@ TEST_F(IfStatementTest, IsValid_InvalidElseStatement) {
} }
TEST_F(IfStatementTest, IsValid_MultipleElseWiththoutCondition) { TEST_F(IfStatementTest, IsValid_MultipleElseWiththoutCondition) {
auto cond = std::make_unique<IdentifierExpression>("cond"); auto cond = create<IdentifierExpression>("cond");
auto body = std::make_unique<ast::BlockStatement>(); auto body = create<ast::BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
ElseStatementList else_stmts; ElseStatementList else_stmts;
else_stmts.push_back(std::make_unique<ElseStatement>()); else_stmts.push_back(create<ElseStatement>());
else_stmts.push_back(std::make_unique<ElseStatement>()); else_stmts.push_back(create<ElseStatement>());
IfStatement stmt(std::move(cond), std::move(body)); IfStatement stmt(std::move(cond), std::move(body));
stmt.set_else_statements(std::move(else_stmts)); stmt.set_else_statements(std::move(else_stmts));
@ -161,14 +161,14 @@ TEST_F(IfStatementTest, IsValid_MultipleElseWiththoutCondition) {
} }
TEST_F(IfStatementTest, IsValid_ElseNotLast) { TEST_F(IfStatementTest, IsValid_ElseNotLast) {
auto cond = std::make_unique<IdentifierExpression>("cond"); auto cond = create<IdentifierExpression>("cond");
auto body = std::make_unique<ast::BlockStatement>(); auto body = create<ast::BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
ElseStatementList else_stmts; ElseStatementList else_stmts;
else_stmts.push_back(std::make_unique<ElseStatement>()); else_stmts.push_back(create<ElseStatement>());
else_stmts.push_back(std::make_unique<ElseStatement>()); else_stmts.push_back(create<ElseStatement>());
else_stmts[1]->set_condition(std::make_unique<IdentifierExpression>("ident")); else_stmts[1]->set_condition(create<IdentifierExpression>("ident"));
IfStatement stmt(std::move(cond), std::move(body)); IfStatement stmt(std::move(cond), std::move(body));
stmt.set_else_statements(std::move(else_stmts)); stmt.set_else_statements(std::move(else_stmts));
@ -176,9 +176,9 @@ TEST_F(IfStatementTest, IsValid_ElseNotLast) {
} }
TEST_F(IfStatementTest, ToStr) { TEST_F(IfStatementTest, ToStr) {
auto cond = std::make_unique<IdentifierExpression>("cond"); auto cond = create<IdentifierExpression>("cond");
auto body = std::make_unique<ast::BlockStatement>(); auto body = create<ast::BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
IfStatement stmt(std::move(cond), std::move(body)); IfStatement stmt(std::move(cond), std::move(body));
@ -196,22 +196,22 @@ TEST_F(IfStatementTest, ToStr) {
} }
TEST_F(IfStatementTest, ToStr_WithElseStatements) { TEST_F(IfStatementTest, ToStr_WithElseStatements) {
auto cond = std::make_unique<IdentifierExpression>("cond"); auto cond = create<IdentifierExpression>("cond");
auto body = std::make_unique<ast::BlockStatement>(); auto body = create<ast::BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
auto else_if_body = std::make_unique<BlockStatement>(); auto else_if_body = create<BlockStatement>();
else_if_body->append(std::make_unique<DiscardStatement>()); else_if_body->append(create<DiscardStatement>());
auto else_body = std::make_unique<BlockStatement>(); auto else_body = create<BlockStatement>();
else_body->append(std::make_unique<DiscardStatement>()); else_body->append(create<DiscardStatement>());
else_body->append(std::make_unique<DiscardStatement>()); else_body->append(create<DiscardStatement>());
ElseStatementList else_stmts; ElseStatementList else_stmts;
else_stmts.push_back(std::make_unique<ElseStatement>()); else_stmts.push_back(create<ElseStatement>());
else_stmts[0]->set_condition(std::make_unique<IdentifierExpression>("ident")); else_stmts[0]->set_condition(create<IdentifierExpression>("ident"));
else_stmts[0]->set_body(std::move(else_if_body)); else_stmts[0]->set_body(std::move(else_if_body));
else_stmts.push_back(std::make_unique<ElseStatement>()); else_stmts.push_back(create<ElseStatement>());
else_stmts[1]->set_body(std::move(else_body)); else_stmts[1]->set_body(std::move(else_body));
IfStatement stmt(std::move(cond), std::move(body)); IfStatement stmt(std::move(cond), std::move(body));

View File

@ -28,12 +28,12 @@ namespace {
using LoopStatementTest = TestHelper; using LoopStatementTest = TestHelper;
TEST_F(LoopStatementTest, Creation) { TEST_F(LoopStatementTest, Creation) {
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
auto* b_ptr = body->last(); auto* b_ptr = body->last();
auto continuing = std::make_unique<BlockStatement>(); auto continuing = create<BlockStatement>();
continuing->append(std::make_unique<DiscardStatement>()); continuing->append(create<DiscardStatement>());
auto* c_ptr = continuing->last(); auto* c_ptr = continuing->last();
LoopStatement l(std::move(body), std::move(continuing)); LoopStatement l(std::move(body), std::move(continuing));
@ -44,11 +44,11 @@ TEST_F(LoopStatementTest, Creation) {
} }
TEST_F(LoopStatementTest, Creation_WithSource) { TEST_F(LoopStatementTest, Creation_WithSource) {
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
auto continuing = std::make_unique<BlockStatement>(); auto continuing = create<BlockStatement>();
continuing->append(std::make_unique<DiscardStatement>()); continuing->append(create<DiscardStatement>());
LoopStatement l(Source{Source::Location{20, 2}}, std::move(body), LoopStatement l(Source{Source::Location{20, 2}}, std::move(body),
std::move(continuing)); std::move(continuing));
@ -63,79 +63,78 @@ TEST_F(LoopStatementTest, IsLoop) {
} }
TEST_F(LoopStatementTest, HasContinuing_WithoutContinuing) { TEST_F(LoopStatementTest, HasContinuing_WithoutContinuing) {
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
LoopStatement l(std::move(body), {}); LoopStatement l(std::move(body), {});
EXPECT_FALSE(l.has_continuing()); EXPECT_FALSE(l.has_continuing());
} }
TEST_F(LoopStatementTest, HasContinuing_WithContinuing) { TEST_F(LoopStatementTest, HasContinuing_WithContinuing) {
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
auto continuing = std::make_unique<BlockStatement>(); auto continuing = create<BlockStatement>();
continuing->append(std::make_unique<DiscardStatement>()); continuing->append(create<DiscardStatement>());
LoopStatement l(std::move(body), std::move(continuing)); LoopStatement l(std::move(body), std::move(continuing));
EXPECT_TRUE(l.has_continuing()); EXPECT_TRUE(l.has_continuing());
} }
TEST_F(LoopStatementTest, IsValid) { TEST_F(LoopStatementTest, IsValid) {
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
auto continuing = std::make_unique<BlockStatement>(); auto continuing = create<BlockStatement>();
continuing->append(std::make_unique<DiscardStatement>()); continuing->append(create<DiscardStatement>());
LoopStatement l(std::move(body), std::move(continuing)); LoopStatement l(std::move(body), std::move(continuing));
EXPECT_TRUE(l.IsValid()); EXPECT_TRUE(l.IsValid());
} }
TEST_F(LoopStatementTest, IsValid_WithoutContinuing) { TEST_F(LoopStatementTest, IsValid_WithoutContinuing) {
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
LoopStatement l(std::move(body), std::make_unique<BlockStatement>()); LoopStatement l(std::move(body), create<BlockStatement>());
EXPECT_TRUE(l.IsValid()); EXPECT_TRUE(l.IsValid());
} }
TEST_F(LoopStatementTest, IsValid_WithoutBody) { TEST_F(LoopStatementTest, IsValid_WithoutBody) {
LoopStatement l(std::make_unique<BlockStatement>(), LoopStatement l(create<BlockStatement>(), create<BlockStatement>());
std::make_unique<BlockStatement>());
EXPECT_TRUE(l.IsValid()); EXPECT_TRUE(l.IsValid());
} }
TEST_F(LoopStatementTest, IsValid_NullBodyStatement) { TEST_F(LoopStatementTest, IsValid_NullBodyStatement) {
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
body->append(nullptr); body->append(nullptr);
auto continuing = std::make_unique<BlockStatement>(); auto continuing = create<BlockStatement>();
continuing->append(std::make_unique<DiscardStatement>()); continuing->append(create<DiscardStatement>());
LoopStatement l(std::move(body), std::move(continuing)); LoopStatement l(std::move(body), std::move(continuing));
EXPECT_FALSE(l.IsValid()); EXPECT_FALSE(l.IsValid());
} }
TEST_F(LoopStatementTest, IsValid_InvalidBodyStatement) { TEST_F(LoopStatementTest, IsValid_InvalidBodyStatement) {
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
body->append(std::make_unique<IfStatement>()); body->append(create<IfStatement>());
auto continuing = std::make_unique<BlockStatement>(); auto continuing = create<BlockStatement>();
continuing->append(std::make_unique<DiscardStatement>()); continuing->append(create<DiscardStatement>());
LoopStatement l(std::move(body), std::move(continuing)); LoopStatement l(std::move(body), std::move(continuing));
EXPECT_FALSE(l.IsValid()); EXPECT_FALSE(l.IsValid());
} }
TEST_F(LoopStatementTest, IsValid_NullContinuingStatement) { TEST_F(LoopStatementTest, IsValid_NullContinuingStatement) {
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
auto continuing = std::make_unique<BlockStatement>(); auto continuing = create<BlockStatement>();
continuing->append(std::make_unique<DiscardStatement>()); continuing->append(create<DiscardStatement>());
continuing->append(nullptr); continuing->append(nullptr);
LoopStatement l(std::move(body), std::move(continuing)); LoopStatement l(std::move(body), std::move(continuing));
@ -143,20 +142,20 @@ TEST_F(LoopStatementTest, IsValid_NullContinuingStatement) {
} }
TEST_F(LoopStatementTest, IsValid_InvalidContinuingStatement) { TEST_F(LoopStatementTest, IsValid_InvalidContinuingStatement) {
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
auto continuing = std::make_unique<BlockStatement>(); auto continuing = create<BlockStatement>();
continuing->append(std::make_unique<DiscardStatement>()); continuing->append(create<DiscardStatement>());
continuing->append(std::make_unique<IfStatement>()); continuing->append(create<IfStatement>());
LoopStatement l(std::move(body), std::move(continuing)); LoopStatement l(std::move(body), std::move(continuing));
EXPECT_FALSE(l.IsValid()); EXPECT_FALSE(l.IsValid());
} }
TEST_F(LoopStatementTest, ToStr) { TEST_F(LoopStatementTest, ToStr) {
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
LoopStatement l(std::move(body), {}); LoopStatement l(std::move(body), {});
std::ostringstream out; std::ostringstream out;
@ -168,11 +167,11 @@ TEST_F(LoopStatementTest, ToStr) {
} }
TEST_F(LoopStatementTest, ToStr_WithContinuing) { TEST_F(LoopStatementTest, ToStr_WithContinuing) {
auto body = std::make_unique<BlockStatement>(); auto body = create<BlockStatement>();
body->append(std::make_unique<DiscardStatement>()); body->append(create<DiscardStatement>());
auto continuing = std::make_unique<BlockStatement>(); auto continuing = create<BlockStatement>();
continuing->append(std::make_unique<DiscardStatement>()); continuing->append(create<DiscardStatement>());
LoopStatement l(std::move(body), std::move(continuing)); LoopStatement l(std::move(body), std::move(continuing));
std::ostringstream out; std::ostringstream out;

View File

@ -26,8 +26,8 @@ namespace {
using MemberAccessorExpressionTest = TestHelper; using MemberAccessorExpressionTest = TestHelper;
TEST_F(MemberAccessorExpressionTest, Creation) { TEST_F(MemberAccessorExpressionTest, Creation) {
auto str = std::make_unique<IdentifierExpression>("structure"); auto str = create<IdentifierExpression>("structure");
auto mem = std::make_unique<IdentifierExpression>("member"); auto mem = create<IdentifierExpression>("member");
auto* str_ptr = str.get(); auto* str_ptr = str.get();
auto* mem_ptr = mem.get(); auto* mem_ptr = mem.get();
@ -38,8 +38,8 @@ TEST_F(MemberAccessorExpressionTest, Creation) {
} }
TEST_F(MemberAccessorExpressionTest, Creation_WithSource) { TEST_F(MemberAccessorExpressionTest, Creation_WithSource) {
auto str = std::make_unique<IdentifierExpression>("structure"); auto str = create<IdentifierExpression>("structure");
auto mem = std::make_unique<IdentifierExpression>("member"); auto mem = create<IdentifierExpression>("member");
MemberAccessorExpression stmt(Source{Source::Location{20, 2}}, std::move(str), MemberAccessorExpression stmt(Source{Source::Location{20, 2}}, std::move(str),
std::move(mem)); std::move(mem));
@ -54,15 +54,15 @@ TEST_F(MemberAccessorExpressionTest, IsMemberAccessor) {
} }
TEST_F(MemberAccessorExpressionTest, IsValid) { TEST_F(MemberAccessorExpressionTest, IsValid) {
auto str = std::make_unique<IdentifierExpression>("structure"); auto str = create<IdentifierExpression>("structure");
auto mem = std::make_unique<IdentifierExpression>("member"); auto mem = create<IdentifierExpression>("member");
MemberAccessorExpression stmt(std::move(str), std::move(mem)); MemberAccessorExpression stmt(std::move(str), std::move(mem));
EXPECT_TRUE(stmt.IsValid()); EXPECT_TRUE(stmt.IsValid());
} }
TEST_F(MemberAccessorExpressionTest, IsValid_NullStruct) { TEST_F(MemberAccessorExpressionTest, IsValid_NullStruct) {
auto mem = std::make_unique<IdentifierExpression>("member"); auto mem = create<IdentifierExpression>("member");
MemberAccessorExpression stmt; MemberAccessorExpression stmt;
stmt.set_member(std::move(mem)); stmt.set_member(std::move(mem));
@ -70,15 +70,15 @@ TEST_F(MemberAccessorExpressionTest, IsValid_NullStruct) {
} }
TEST_F(MemberAccessorExpressionTest, IsValid_InvalidStruct) { TEST_F(MemberAccessorExpressionTest, IsValid_InvalidStruct) {
auto str = std::make_unique<IdentifierExpression>(""); auto str = create<IdentifierExpression>("");
auto mem = std::make_unique<IdentifierExpression>("member"); auto mem = create<IdentifierExpression>("member");
MemberAccessorExpression stmt(std::move(str), std::move(mem)); MemberAccessorExpression stmt(std::move(str), std::move(mem));
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt.IsValid());
} }
TEST_F(MemberAccessorExpressionTest, IsValid_NullMember) { TEST_F(MemberAccessorExpressionTest, IsValid_NullMember) {
auto str = std::make_unique<IdentifierExpression>("structure"); auto str = create<IdentifierExpression>("structure");
MemberAccessorExpression stmt; MemberAccessorExpression stmt;
stmt.set_structure(std::move(str)); stmt.set_structure(std::move(str));
@ -86,16 +86,16 @@ TEST_F(MemberAccessorExpressionTest, IsValid_NullMember) {
} }
TEST_F(MemberAccessorExpressionTest, IsValid_InvalidMember) { TEST_F(MemberAccessorExpressionTest, IsValid_InvalidMember) {
auto str = std::make_unique<IdentifierExpression>("structure"); auto str = create<IdentifierExpression>("structure");
auto mem = std::make_unique<IdentifierExpression>(""); auto mem = create<IdentifierExpression>("");
MemberAccessorExpression stmt(std::move(str), std::move(mem)); MemberAccessorExpression stmt(std::move(str), std::move(mem));
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt.IsValid());
} }
TEST_F(MemberAccessorExpressionTest, ToStr) { TEST_F(MemberAccessorExpressionTest, ToStr) {
auto str = std::make_unique<IdentifierExpression>("structure"); auto str = create<IdentifierExpression>("structure");
auto mem = std::make_unique<IdentifierExpression>("member"); auto mem = create<IdentifierExpression>("member");
MemberAccessorExpression stmt(std::move(str), std::move(mem)); MemberAccessorExpression stmt(std::move(str), std::move(mem));
std::ostringstream out; std::ostringstream out;

View File

@ -48,7 +48,7 @@ TEST_F(ModuleTest, LookupFunction) {
type::F32Type f32; type::F32Type f32;
Module m; Module m;
auto func = std::make_unique<Function>("main", VariableList{}, &f32); auto func = create<Function>("main", VariableList{}, &f32);
auto* func_ptr = func.get(); auto* func_ptr = func.get();
m.AddFunction(std::move(func)); m.AddFunction(std::move(func));
EXPECT_EQ(func_ptr, m.FindFunctionByName("main")); EXPECT_EQ(func_ptr, m.FindFunctionByName("main"));
@ -66,7 +66,7 @@ TEST_F(ModuleTest, IsValid_Empty) {
TEST_F(ModuleTest, IsValid_GlobalVariable) { TEST_F(ModuleTest, IsValid_GlobalVariable) {
type::F32Type f32; type::F32Type f32;
auto var = std::make_unique<Variable>("var", StorageClass::kInput, &f32); auto var = create<Variable>("var", StorageClass::kInput, &f32);
Module m; Module m;
m.AddGlobalVariable(std::move(var)); m.AddGlobalVariable(std::move(var));
@ -80,7 +80,7 @@ TEST_F(ModuleTest, IsValid_Null_GlobalVariable) {
} }
TEST_F(ModuleTest, IsValid_Invalid_GlobalVariable) { TEST_F(ModuleTest, IsValid_Invalid_GlobalVariable) {
auto var = std::make_unique<Variable>("var", StorageClass::kInput, nullptr); auto var = create<Variable>("var", StorageClass::kInput, nullptr);
Module m; Module m;
m.AddGlobalVariable(std::move(var)); m.AddGlobalVariable(std::move(var));
@ -124,7 +124,7 @@ TEST_F(ModuleTest, IsValid_Struct_EmptyName) {
TEST_F(ModuleTest, IsValid_Function) { TEST_F(ModuleTest, IsValid_Function) {
type::F32Type f32; type::F32Type f32;
auto func = std::make_unique<Function>("main", VariableList(), &f32); auto func = create<Function>("main", VariableList(), &f32);
Module m; Module m;
m.AddFunction(std::move(func)); m.AddFunction(std::move(func));
@ -138,7 +138,7 @@ TEST_F(ModuleTest, IsValid_Null_Function) {
} }
TEST_F(ModuleTest, IsValid_Invalid_Function) { TEST_F(ModuleTest, IsValid_Invalid_Function) {
auto func = std::make_unique<Function>(); auto func = create<Function>();
Module m; Module m;
m.AddFunction(std::move(func)); m.AddFunction(std::move(func));

View File

@ -26,7 +26,7 @@ namespace {
using ReturnStatementTest = TestHelper; using ReturnStatementTest = TestHelper;
TEST_F(ReturnStatementTest, Creation) { TEST_F(ReturnStatementTest, Creation) {
auto expr = std::make_unique<IdentifierExpression>("expr"); auto expr = create<IdentifierExpression>("expr");
auto* expr_ptr = expr.get(); auto* expr_ptr = expr.get();
ReturnStatement r(std::move(expr)); ReturnStatement r(std::move(expr));
@ -51,7 +51,7 @@ TEST_F(ReturnStatementTest, HasValue_WithoutValue) {
} }
TEST_F(ReturnStatementTest, HasValue_WithValue) { TEST_F(ReturnStatementTest, HasValue_WithValue) {
auto expr = std::make_unique<IdentifierExpression>("expr"); auto expr = create<IdentifierExpression>("expr");
ReturnStatement r(std::move(expr)); ReturnStatement r(std::move(expr));
EXPECT_TRUE(r.has_value()); EXPECT_TRUE(r.has_value());
} }
@ -62,19 +62,19 @@ TEST_F(ReturnStatementTest, IsValid_WithoutValue) {
} }
TEST_F(ReturnStatementTest, IsValid_WithValue) { TEST_F(ReturnStatementTest, IsValid_WithValue) {
auto expr = std::make_unique<IdentifierExpression>("expr"); auto expr = create<IdentifierExpression>("expr");
ReturnStatement r(std::move(expr)); ReturnStatement r(std::move(expr));
EXPECT_TRUE(r.IsValid()); EXPECT_TRUE(r.IsValid());
} }
TEST_F(ReturnStatementTest, IsValid_InvalidValue) { TEST_F(ReturnStatementTest, IsValid_InvalidValue) {
auto expr = std::make_unique<IdentifierExpression>(""); auto expr = create<IdentifierExpression>("");
ReturnStatement r(std::move(expr)); ReturnStatement r(std::move(expr));
EXPECT_FALSE(r.IsValid()); EXPECT_FALSE(r.IsValid());
} }
TEST_F(ReturnStatementTest, ToStr_WithValue) { TEST_F(ReturnStatementTest, ToStr_WithValue) {
auto expr = std::make_unique<IdentifierExpression>("expr"); auto expr = create<IdentifierExpression>("expr");
ReturnStatement r(std::move(expr)); ReturnStatement r(std::move(expr));
std::ostringstream out; std::ostringstream out;
r.to_str(out, 2); r.to_str(out, 2);

View File

@ -26,7 +26,7 @@ using ScalarConstructorExpressionTest = TestHelper;
TEST_F(ScalarConstructorExpressionTest, Creation) { TEST_F(ScalarConstructorExpressionTest, Creation) {
ast::type::BoolType bool_type; ast::type::BoolType bool_type;
auto b = std::make_unique<BoolLiteral>(&bool_type, true); auto b = create<BoolLiteral>(&bool_type, true);
auto* b_ptr = b.get(); auto* b_ptr = b.get();
ScalarConstructorExpression c(std::move(b)); ScalarConstructorExpression c(std::move(b));
EXPECT_EQ(c.literal(), b_ptr); EXPECT_EQ(c.literal(), b_ptr);
@ -34,7 +34,7 @@ TEST_F(ScalarConstructorExpressionTest, Creation) {
TEST_F(ScalarConstructorExpressionTest, Creation_WithSource) { TEST_F(ScalarConstructorExpressionTest, Creation_WithSource) {
ast::type::BoolType bool_type; ast::type::BoolType bool_type;
auto b = std::make_unique<BoolLiteral>(&bool_type, true); auto b = create<BoolLiteral>(&bool_type, true);
ScalarConstructorExpression c(Source{Source::Location{20, 2}}, std::move(b)); ScalarConstructorExpression c(Source{Source::Location{20, 2}}, std::move(b));
auto src = c.source(); auto src = c.source();
EXPECT_EQ(src.range.begin.line, 20u); EXPECT_EQ(src.range.begin.line, 20u);
@ -43,7 +43,7 @@ TEST_F(ScalarConstructorExpressionTest, Creation_WithSource) {
TEST_F(ScalarConstructorExpressionTest, IsValid) { TEST_F(ScalarConstructorExpressionTest, IsValid) {
ast::type::BoolType bool_type; ast::type::BoolType bool_type;
auto b = std::make_unique<BoolLiteral>(&bool_type, true); auto b = create<BoolLiteral>(&bool_type, true);
ScalarConstructorExpression c(std::move(b)); ScalarConstructorExpression c(std::move(b));
EXPECT_TRUE(c.IsValid()); EXPECT_TRUE(c.IsValid());
} }
@ -55,7 +55,7 @@ TEST_F(ScalarConstructorExpressionTest, IsValid_MissingLiteral) {
TEST_F(ScalarConstructorExpressionTest, ToStr) { TEST_F(ScalarConstructorExpressionTest, ToStr) {
ast::type::BoolType bool_type; ast::type::BoolType bool_type;
auto b = std::make_unique<BoolLiteral>(&bool_type, true); auto b = create<BoolLiteral>(&bool_type, true);
ScalarConstructorExpression c(std::move(b)); ScalarConstructorExpression c(std::move(b));
std::ostringstream out; std::ostringstream out;
c.to_str(out, 2); c.to_str(out, 2);

View File

@ -30,8 +30,7 @@ using StructMemberTest = TestHelper;
TEST_F(StructMemberTest, Creation) { TEST_F(StructMemberTest, Creation) {
type::I32Type i32; type::I32Type i32;
StructMemberDecorationList decorations; StructMemberDecorationList decorations;
decorations.emplace_back( decorations.emplace_back(create<StructMemberOffsetDecoration>(4, Source{}));
std::make_unique<StructMemberOffsetDecoration>(4, Source{}));
StructMember st{"a", &i32, std::move(decorations)}; StructMember st{"a", &i32, std::move(decorations)};
EXPECT_EQ(st.name(), "a"); EXPECT_EQ(st.name(), "a");
@ -78,8 +77,7 @@ TEST_F(StructMemberTest, IsValid_NullType) {
TEST_F(StructMemberTest, IsValid_Null_Decoration) { TEST_F(StructMemberTest, IsValid_Null_Decoration) {
type::I32Type i32; type::I32Type i32;
StructMemberDecorationList decorations; StructMemberDecorationList decorations;
decorations.emplace_back( decorations.emplace_back(create<StructMemberOffsetDecoration>(4, Source{}));
std::make_unique<StructMemberOffsetDecoration>(4, Source{}));
decorations.push_back(nullptr); decorations.push_back(nullptr);
StructMember st{"a", &i32, std::move(decorations)}; StructMember st{"a", &i32, std::move(decorations)};
@ -89,8 +87,7 @@ TEST_F(StructMemberTest, IsValid_Null_Decoration) {
TEST_F(StructMemberTest, ToStr) { TEST_F(StructMemberTest, ToStr) {
type::I32Type i32; type::I32Type i32;
StructMemberDecorationList decorations; StructMemberDecorationList decorations;
decorations.emplace_back( decorations.emplace_back(create<StructMemberOffsetDecoration>(4, Source{}));
std::make_unique<StructMemberOffsetDecoration>(4, Source{}));
StructMember st{"a", &i32, std::move(decorations)}; StructMember st{"a", &i32, std::move(decorations)};
std::ostringstream out; std::ostringstream out;

View File

@ -33,7 +33,7 @@ TEST_F(StructTest, Creation) {
type::I32Type i32; type::I32Type i32;
StructMemberList members; StructMemberList members;
members.push_back( members.push_back(
std::make_unique<StructMember>("a", &i32, StructMemberDecorationList())); create<StructMember>("a", &i32, StructMemberDecorationList()));
Struct s{std::move(members)}; Struct s{std::move(members)};
EXPECT_EQ(s.members().size(), 1u); EXPECT_EQ(s.members().size(), 1u);
@ -49,10 +49,10 @@ TEST_F(StructTest, Creation_WithDecorations) {
StructMemberList members; StructMemberList members;
members.push_back( members.push_back(
std::make_unique<StructMember>("a", &i32, StructMemberDecorationList())); create<StructMember>("a", &i32, StructMemberDecorationList()));
StructDecorationList decos; StructDecorationList decos;
decos.push_back(std::make_unique<StructBlockDecoration>(Source{})); decos.push_back(create<StructBlockDecoration>(Source{}));
Struct s{std::move(decos), std::move(members)}; Struct s{std::move(decos), std::move(members)};
EXPECT_EQ(s.members().size(), 1u); EXPECT_EQ(s.members().size(), 1u);
@ -69,10 +69,10 @@ TEST_F(StructTest, CreationWithSourceAndDecorations) {
StructMemberList members; StructMemberList members;
members.emplace_back( members.emplace_back(
std::make_unique<StructMember>("a", &i32, StructMemberDecorationList())); create<StructMember>("a", &i32, StructMemberDecorationList()));
StructDecorationList decos; StructDecorationList decos;
decos.push_back(std::make_unique<StructBlockDecoration>(Source{})); decos.push_back(create<StructBlockDecoration>(Source{}));
Struct s{ Struct s{
Source{Source::Range{Source::Location{27, 4}, Source::Location{27, 8}}}, Source{Source::Range{Source::Location{27, 4}, Source::Location{27, 8}}},
@ -96,7 +96,7 @@ TEST_F(StructTest, IsValid_Null_StructMember) {
StructMemberList members; StructMemberList members;
members.push_back( members.push_back(
std::make_unique<StructMember>("a", &i32, StructMemberDecorationList())); create<StructMember>("a", &i32, StructMemberDecorationList()));
members.push_back(nullptr); members.push_back(nullptr);
Struct s{std::move(members)}; Struct s{std::move(members)};
@ -108,7 +108,7 @@ TEST_F(StructTest, IsValid_Invalid_StructMember) {
StructMemberList members; StructMemberList members;
members.push_back( members.push_back(
std::make_unique<StructMember>("", &i32, StructMemberDecorationList())); create<StructMember>("", &i32, StructMemberDecorationList()));
Struct s{std::move(members)}; Struct s{std::move(members)};
EXPECT_FALSE(s.IsValid()); EXPECT_FALSE(s.IsValid());
@ -119,10 +119,10 @@ TEST_F(StructTest, ToStr) {
StructMemberList members; StructMemberList members;
members.emplace_back( members.emplace_back(
std::make_unique<StructMember>("a", &i32, StructMemberDecorationList())); create<StructMember>("a", &i32, StructMemberDecorationList()));
StructDecorationList decos; StructDecorationList decos;
decos.push_back(std::make_unique<StructBlockDecoration>(Source{})); decos.push_back(create<StructBlockDecoration>(Source{}));
Struct s{std::move(decos), std::move(members)}; Struct s{std::move(decos), std::move(members)};

View File

@ -32,12 +32,12 @@ TEST_F(SwitchStatementTest, Creation) {
ast::type::I32Type i32; ast::type::I32Type i32;
CaseSelectorList lit; CaseSelectorList lit;
lit.push_back(std::make_unique<SintLiteral>(&i32, 1)); lit.push_back(create<SintLiteral>(&i32, 1));
auto ident = std::make_unique<IdentifierExpression>("ident"); auto ident = create<IdentifierExpression>("ident");
CaseStatementList body; CaseStatementList body;
body.push_back(std::make_unique<CaseStatement>( body.push_back(
std::move(lit), std::make_unique<ast::BlockStatement>())); create<CaseStatement>(std::move(lit), create<ast::BlockStatement>()));
auto* ident_ptr = ident.get(); auto* ident_ptr = ident.get();
auto* case_ptr = body[0].get(); auto* case_ptr = body[0].get();
@ -49,7 +49,7 @@ TEST_F(SwitchStatementTest, Creation) {
} }
TEST_F(SwitchStatementTest, Creation_WithSource) { TEST_F(SwitchStatementTest, Creation_WithSource) {
auto ident = std::make_unique<IdentifierExpression>("ident"); auto ident = create<IdentifierExpression>("ident");
SwitchStatement stmt(Source{Source::Location{20, 2}}, std::move(ident), SwitchStatement stmt(Source{Source::Location{20, 2}}, std::move(ident),
CaseStatementList()); CaseStatementList());
@ -67,12 +67,12 @@ TEST_F(SwitchStatementTest, IsValid) {
ast::type::I32Type i32; ast::type::I32Type i32;
CaseSelectorList lit; CaseSelectorList lit;
lit.push_back(std::make_unique<SintLiteral>(&i32, 2)); lit.push_back(create<SintLiteral>(&i32, 2));
auto ident = std::make_unique<IdentifierExpression>("ident"); auto ident = create<IdentifierExpression>("ident");
CaseStatementList body; CaseStatementList body;
body.push_back(std::make_unique<CaseStatement>( body.push_back(
std::move(lit), std::make_unique<ast::BlockStatement>())); create<CaseStatement>(std::move(lit), create<ast::BlockStatement>()));
SwitchStatement stmt(std::move(ident), std::move(body)); SwitchStatement stmt(std::move(ident), std::move(body));
EXPECT_TRUE(stmt.IsValid()); EXPECT_TRUE(stmt.IsValid());
@ -82,11 +82,11 @@ TEST_F(SwitchStatementTest, IsValid_Null_Condition) {
ast::type::I32Type i32; ast::type::I32Type i32;
CaseSelectorList lit; CaseSelectorList lit;
lit.push_back(std::make_unique<SintLiteral>(&i32, 2)); lit.push_back(create<SintLiteral>(&i32, 2));
CaseStatementList body; CaseStatementList body;
body.push_back(std::make_unique<CaseStatement>( body.push_back(
std::move(lit), std::make_unique<ast::BlockStatement>())); create<CaseStatement>(std::move(lit), create<ast::BlockStatement>()));
SwitchStatement stmt; SwitchStatement stmt;
stmt.set_body(std::move(body)); stmt.set_body(std::move(body));
@ -97,12 +97,12 @@ TEST_F(SwitchStatementTest, IsValid_Invalid_Condition) {
ast::type::I32Type i32; ast::type::I32Type i32;
CaseSelectorList lit; CaseSelectorList lit;
lit.push_back(std::make_unique<SintLiteral>(&i32, 2)); lit.push_back(create<SintLiteral>(&i32, 2));
auto ident = std::make_unique<IdentifierExpression>(""); auto ident = create<IdentifierExpression>("");
CaseStatementList body; CaseStatementList body;
body.push_back(std::make_unique<CaseStatement>( body.push_back(
std::move(lit), std::make_unique<ast::BlockStatement>())); create<CaseStatement>(std::move(lit), create<ast::BlockStatement>()));
SwitchStatement stmt(std::move(ident), std::move(body)); SwitchStatement stmt(std::move(ident), std::move(body));
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt.IsValid());
@ -112,12 +112,12 @@ TEST_F(SwitchStatementTest, IsValid_Null_BodyStatement) {
ast::type::I32Type i32; ast::type::I32Type i32;
CaseSelectorList lit; CaseSelectorList lit;
lit.push_back(std::make_unique<SintLiteral>(&i32, 2)); lit.push_back(create<SintLiteral>(&i32, 2));
auto ident = std::make_unique<IdentifierExpression>("ident"); auto ident = create<IdentifierExpression>("ident");
CaseStatementList body; CaseStatementList body;
body.push_back(std::make_unique<CaseStatement>( body.push_back(
std::move(lit), std::make_unique<ast::BlockStatement>())); create<CaseStatement>(std::move(lit), create<ast::BlockStatement>()));
body.push_back(nullptr); body.push_back(nullptr);
SwitchStatement stmt(std::move(ident), std::move(body)); SwitchStatement stmt(std::move(ident), std::move(body));
@ -125,21 +125,21 @@ TEST_F(SwitchStatementTest, IsValid_Null_BodyStatement) {
} }
TEST_F(SwitchStatementTest, IsValid_Invalid_BodyStatement) { TEST_F(SwitchStatementTest, IsValid_Invalid_BodyStatement) {
auto ident = std::make_unique<IdentifierExpression>("ident"); auto ident = create<IdentifierExpression>("ident");
auto case_body = std::make_unique<ast::BlockStatement>(); auto case_body = create<ast::BlockStatement>();
case_body->append(nullptr); case_body->append(nullptr);
CaseStatementList body; CaseStatementList body;
body.push_back(std::make_unique<CaseStatement>(CaseSelectorList{}, body.push_back(
std::move(case_body))); create<CaseStatement>(CaseSelectorList{}, std::move(case_body)));
SwitchStatement stmt(std::move(ident), std::move(body)); SwitchStatement stmt(std::move(ident), std::move(body));
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt.IsValid());
} }
TEST_F(SwitchStatementTest, ToStr_Empty) { TEST_F(SwitchStatementTest, ToStr_Empty) {
auto ident = std::make_unique<IdentifierExpression>("ident"); auto ident = create<IdentifierExpression>("ident");
SwitchStatement stmt(std::move(ident), {}); SwitchStatement stmt(std::move(ident), {});
std::ostringstream out; std::ostringstream out;
@ -156,12 +156,12 @@ TEST_F(SwitchStatementTest, ToStr) {
ast::type::I32Type i32; ast::type::I32Type i32;
CaseSelectorList lit; CaseSelectorList lit;
lit.push_back(std::make_unique<SintLiteral>(&i32, 2)); lit.push_back(create<SintLiteral>(&i32, 2));
auto ident = std::make_unique<IdentifierExpression>("ident"); auto ident = create<IdentifierExpression>("ident");
CaseStatementList body; CaseStatementList body;
body.push_back(std::make_unique<CaseStatement>( body.push_back(
std::move(lit), std::make_unique<ast::BlockStatement>())); create<CaseStatement>(std::move(lit), create<ast::BlockStatement>()));
SwitchStatement stmt(std::move(ident), std::move(body)); SwitchStatement stmt(std::move(ident), std::move(body));
std::ostringstream out; std::ostringstream out;

View File

@ -15,6 +15,9 @@
#ifndef SRC_AST_TEST_HELPER_H_ #ifndef SRC_AST_TEST_HELPER_H_
#define SRC_AST_TEST_HELPER_H_ #define SRC_AST_TEST_HELPER_H_
#include <memory>
#include <utility>
#include "gtest/gtest.h" #include "gtest/gtest.h"
namespace tint { namespace tint {
@ -26,6 +29,13 @@ class TestHelperBase : public BASE {
public: public:
TestHelperBase() {} TestHelperBase() {}
~TestHelperBase() = default; ~TestHelperBase() = default;
/// @return a `std::unique_ptr` to a new `T` constructed with `args`
/// @param args the arguments to forward to the constructor for `T`
template <typename T, typename... ARGS>
std::unique_ptr<T> create(ARGS&&... args) {
return std::make_unique<T>(std::forward<ARGS>(args)...);
}
}; };
using TestHelper = TestHelperBase<testing::Test>; using TestHelper = TestHelperBase<testing::Test>;

View File

@ -32,7 +32,7 @@ using TypeConstructorExpressionTest = TestHelper;
TEST_F(TypeConstructorExpressionTest, Creation) { TEST_F(TypeConstructorExpressionTest, Creation) {
type::F32Type f32; type::F32Type f32;
ExpressionList expr; ExpressionList expr;
expr.push_back(std::make_unique<IdentifierExpression>("expr")); expr.push_back(create<IdentifierExpression>("expr"));
auto* expr_ptr = expr[0].get(); auto* expr_ptr = expr[0].get();
TypeConstructorExpression t(&f32, std::move(expr)); TypeConstructorExpression t(&f32, std::move(expr));
@ -44,7 +44,7 @@ TEST_F(TypeConstructorExpressionTest, Creation) {
TEST_F(TypeConstructorExpressionTest, Creation_WithSource) { TEST_F(TypeConstructorExpressionTest, Creation_WithSource) {
type::F32Type f32; type::F32Type f32;
ExpressionList expr; ExpressionList expr;
expr.push_back(std::make_unique<IdentifierExpression>("expr")); expr.push_back(create<IdentifierExpression>("expr"));
TypeConstructorExpression t(Source{Source::Location{20, 2}}, &f32, TypeConstructorExpression t(Source{Source::Location{20, 2}}, &f32,
std::move(expr)); std::move(expr));
@ -61,7 +61,7 @@ TEST_F(TypeConstructorExpressionTest, IsTypeConstructor) {
TEST_F(TypeConstructorExpressionTest, IsValid) { TEST_F(TypeConstructorExpressionTest, IsValid) {
type::F32Type f32; type::F32Type f32;
ExpressionList expr; ExpressionList expr;
expr.push_back(std::make_unique<IdentifierExpression>("expr")); expr.push_back(create<IdentifierExpression>("expr"));
TypeConstructorExpression t(&f32, std::move(expr)); TypeConstructorExpression t(&f32, std::move(expr));
EXPECT_TRUE(t.IsValid()); EXPECT_TRUE(t.IsValid());
@ -77,7 +77,7 @@ TEST_F(TypeConstructorExpressionTest, IsValid_EmptyValue) {
TEST_F(TypeConstructorExpressionTest, IsValid_NullType) { TEST_F(TypeConstructorExpressionTest, IsValid_NullType) {
ExpressionList expr; ExpressionList expr;
expr.push_back(std::make_unique<IdentifierExpression>("expr")); expr.push_back(create<IdentifierExpression>("expr"));
TypeConstructorExpression t; TypeConstructorExpression t;
t.set_values(std::move(expr)); t.set_values(std::move(expr));
@ -87,7 +87,7 @@ TEST_F(TypeConstructorExpressionTest, IsValid_NullType) {
TEST_F(TypeConstructorExpressionTest, IsValid_NullValue) { TEST_F(TypeConstructorExpressionTest, IsValid_NullValue) {
type::F32Type f32; type::F32Type f32;
ExpressionList expr; ExpressionList expr;
expr.push_back(std::make_unique<IdentifierExpression>("expr")); expr.push_back(create<IdentifierExpression>("expr"));
expr.push_back(nullptr); expr.push_back(nullptr);
TypeConstructorExpression t(&f32, std::move(expr)); TypeConstructorExpression t(&f32, std::move(expr));
@ -97,7 +97,7 @@ TEST_F(TypeConstructorExpressionTest, IsValid_NullValue) {
TEST_F(TypeConstructorExpressionTest, IsValid_InvalidValue) { TEST_F(TypeConstructorExpressionTest, IsValid_InvalidValue) {
type::F32Type f32; type::F32Type f32;
ExpressionList expr; ExpressionList expr;
expr.push_back(std::make_unique<IdentifierExpression>("")); expr.push_back(create<IdentifierExpression>(""));
TypeConstructorExpression t(&f32, std::move(expr)); TypeConstructorExpression t(&f32, std::move(expr));
EXPECT_FALSE(t.IsValid()); EXPECT_FALSE(t.IsValid());
@ -107,9 +107,9 @@ TEST_F(TypeConstructorExpressionTest, ToStr) {
type::F32Type f32; type::F32Type f32;
type::VectorType vec(&f32, 3); type::VectorType vec(&f32, 3);
ExpressionList expr; ExpressionList expr;
expr.push_back(std::make_unique<IdentifierExpression>("expr_1")); expr.push_back(create<IdentifierExpression>("expr_1"));
expr.push_back(std::make_unique<IdentifierExpression>("expr_2")); expr.push_back(create<IdentifierExpression>("expr_2"));
expr.push_back(std::make_unique<IdentifierExpression>("expr_3")); expr.push_back(create<IdentifierExpression>("expr_3"));
TypeConstructorExpression t(&vec, std::move(expr)); TypeConstructorExpression t(&vec, std::move(expr));
std::ostringstream out; std::ostringstream out;

View File

@ -26,7 +26,7 @@ namespace {
using UnaryOpExpressionTest = TestHelper; using UnaryOpExpressionTest = TestHelper;
TEST_F(UnaryOpExpressionTest, Creation) { TEST_F(UnaryOpExpressionTest, Creation) {
auto ident = std::make_unique<IdentifierExpression>("ident"); auto ident = create<IdentifierExpression>("ident");
auto* ident_ptr = ident.get(); auto* ident_ptr = ident.get();
UnaryOpExpression u(UnaryOp::kNot, std::move(ident)); UnaryOpExpression u(UnaryOp::kNot, std::move(ident));
@ -35,7 +35,7 @@ TEST_F(UnaryOpExpressionTest, Creation) {
} }
TEST_F(UnaryOpExpressionTest, Creation_WithSource) { TEST_F(UnaryOpExpressionTest, Creation_WithSource) {
auto ident = std::make_unique<IdentifierExpression>("ident"); auto ident = create<IdentifierExpression>("ident");
UnaryOpExpression u(Source{Source::Location{20, 2}}, UnaryOp::kNot, UnaryOpExpression u(Source{Source::Location{20, 2}}, UnaryOp::kNot,
std::move(ident)); std::move(ident));
auto src = u.source(); auto src = u.source();
@ -49,7 +49,7 @@ TEST_F(UnaryOpExpressionTest, IsUnaryOp) {
} }
TEST_F(UnaryOpExpressionTest, IsValid) { TEST_F(UnaryOpExpressionTest, IsValid) {
auto ident = std::make_unique<IdentifierExpression>("ident"); auto ident = create<IdentifierExpression>("ident");
UnaryOpExpression u(UnaryOp::kNot, std::move(ident)); UnaryOpExpression u(UnaryOp::kNot, std::move(ident));
EXPECT_TRUE(u.IsValid()); EXPECT_TRUE(u.IsValid());
} }
@ -61,13 +61,13 @@ TEST_F(UnaryOpExpressionTest, IsValid_NullExpression) {
} }
TEST_F(UnaryOpExpressionTest, IsValid_InvalidExpression) { TEST_F(UnaryOpExpressionTest, IsValid_InvalidExpression) {
auto ident = std::make_unique<IdentifierExpression>(""); auto ident = create<IdentifierExpression>("");
UnaryOpExpression u(UnaryOp::kNot, std::move(ident)); UnaryOpExpression u(UnaryOp::kNot, std::move(ident));
EXPECT_FALSE(u.IsValid()); EXPECT_FALSE(u.IsValid());
} }
TEST_F(UnaryOpExpressionTest, ToStr) { TEST_F(UnaryOpExpressionTest, ToStr) {
auto ident = std::make_unique<IdentifierExpression>("ident"); auto ident = create<IdentifierExpression>("ident");
UnaryOpExpression u(UnaryOp::kNot, std::move(ident)); UnaryOpExpression u(UnaryOp::kNot, std::move(ident));
std::ostringstream out; std::ostringstream out;
u.to_str(out, 2); u.to_str(out, 2);

View File

@ -26,7 +26,7 @@ using VariableDeclStatementTest = TestHelper;
TEST_F(VariableDeclStatementTest, Creation) { TEST_F(VariableDeclStatementTest, Creation) {
type::F32Type f32; type::F32Type f32;
auto var = std::make_unique<Variable>("a", StorageClass::kNone, &f32); auto var = create<Variable>("a", StorageClass::kNone, &f32);
auto* var_ptr = var.get(); auto* var_ptr = var.get();
VariableDeclStatement stmt(std::move(var)); VariableDeclStatement stmt(std::move(var));
@ -35,7 +35,7 @@ TEST_F(VariableDeclStatementTest, Creation) {
TEST_F(VariableDeclStatementTest, Creation_WithSource) { TEST_F(VariableDeclStatementTest, Creation_WithSource) {
type::F32Type f32; type::F32Type f32;
auto var = std::make_unique<Variable>("a", StorageClass::kNone, &f32); auto var = create<Variable>("a", StorageClass::kNone, &f32);
VariableDeclStatement stmt(Source{Source::Location{20, 2}}, std::move(var)); VariableDeclStatement stmt(Source{Source::Location{20, 2}}, std::move(var));
auto src = stmt.source(); auto src = stmt.source();
@ -50,14 +50,14 @@ TEST_F(VariableDeclStatementTest, IsVariableDecl) {
TEST_F(VariableDeclStatementTest, IsValid) { TEST_F(VariableDeclStatementTest, IsValid) {
type::F32Type f32; type::F32Type f32;
auto var = std::make_unique<Variable>("a", StorageClass::kNone, &f32); auto var = create<Variable>("a", StorageClass::kNone, &f32);
VariableDeclStatement stmt(std::move(var)); VariableDeclStatement stmt(std::move(var));
EXPECT_TRUE(stmt.IsValid()); EXPECT_TRUE(stmt.IsValid());
} }
TEST_F(VariableDeclStatementTest, IsValid_InvalidVariable) { TEST_F(VariableDeclStatementTest, IsValid_InvalidVariable) {
type::F32Type f32; type::F32Type f32;
auto var = std::make_unique<Variable>("", StorageClass::kNone, &f32); auto var = create<Variable>("", StorageClass::kNone, &f32);
VariableDeclStatement stmt(std::move(var)); VariableDeclStatement stmt(std::move(var));
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt.IsValid());
} }
@ -69,7 +69,7 @@ TEST_F(VariableDeclStatementTest, IsValid_NullVariable) {
TEST_F(VariableDeclStatementTest, ToStr) { TEST_F(VariableDeclStatementTest, ToStr) {
type::F32Type f32; type::F32Type f32;
auto var = std::make_unique<Variable>("a", StorageClass::kNone, &f32); auto var = create<Variable>("a", StorageClass::kNone, &f32);
VariableDeclStatement stmt(Source{Source::Location{20, 2}}, std::move(var)); VariableDeclStatement stmt(Source{Source::Location{20, 2}}, std::move(var));
std::ostringstream out; std::ostringstream out;

View File

@ -80,7 +80,7 @@ TEST_F(VariableTest, IsValid) {
TEST_F(VariableTest, IsValid_WithConstructor) { TEST_F(VariableTest, IsValid_WithConstructor) {
type::I32Type t; type::I32Type t;
Variable v{"my_var", StorageClass::kNone, &t}; Variable v{"my_var", StorageClass::kNone, &t};
v.set_constructor(std::make_unique<IdentifierExpression>("ident")); v.set_constructor(create<IdentifierExpression>("ident"));
EXPECT_TRUE(v.IsValid()); EXPECT_TRUE(v.IsValid());
} }
@ -103,7 +103,7 @@ TEST_F(VariableTest, IsValid_MissingBoth) {
TEST_F(VariableTest, IsValid_InvalidConstructor) { TEST_F(VariableTest, IsValid_InvalidConstructor) {
type::I32Type t; type::I32Type t;
Variable v{"my_var", StorageClass::kNone, &t}; Variable v{"my_var", StorageClass::kNone, &t};
v.set_constructor(std::make_unique<IdentifierExpression>("")); v.set_constructor(create<IdentifierExpression>(""));
EXPECT_FALSE(v.IsValid()); EXPECT_FALSE(v.IsValid());
} }