Add cast expression tests

This CL adds unit tests for the cast expression AST element.

Bug: tint:11
Change-Id: Ia5d82a00ef2a4c16d6591095b7674f1c14a33c5f
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/16462
Commit-Queue: David Neto <dneto@google.com>
Reviewed-by: David Neto <dneto@google.com>
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 374e44a..e4f4718 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -202,6 +202,7 @@
   ast/builtin_decoration_test.cc
   ast/call_expression_test.cc
   ast/case_statement_test.cc
+  ast/cast_expression_test.cc
   ast/entry_point_test.cc
   ast/import_test.cc
   ast/int_literal_test.cc
diff --git a/src/ast/cast_expression.cc b/src/ast/cast_expression.cc
index 1d52350..a0d836f 100644
--- a/src/ast/cast_expression.cc
+++ b/src/ast/cast_expression.cc
@@ -17,6 +17,8 @@
 namespace tint {
 namespace ast {
 
+CastExpression::CastExpression() : Expression() {}
+
 CastExpression::CastExpression(type::Type* type,
                                std::unique_ptr<Expression> expr)
     : Expression(), type_(type), expr_(std::move(expr)) {}
@@ -29,13 +31,17 @@
 CastExpression::~CastExpression() = default;
 
 bool CastExpression::IsValid() const {
-  return type_ != nullptr && expr_ != nullptr;
+  if (expr_ == nullptr || !expr_->IsValid())
+    return false;
+  return type_ != nullptr;
 }
 
 void CastExpression::to_str(std::ostream& out, size_t indent) const {
-  out << "cast<" << type_->type_name() << ">(";
-  expr_->to_str(out, indent);
-  out << ")";
+  make_indent(out, indent);
+  out << "Cast<" << type_->type_name() << ">(" << std::endl;
+  expr_->to_str(out, indent + 2);
+  make_indent(out, indent);
+  out << ")" << std::endl;
 }
 
 }  // namespace ast
diff --git a/src/ast/cast_expression.h b/src/ast/cast_expression.h
index dcb8a3f..0deb715 100644
--- a/src/ast/cast_expression.h
+++ b/src/ast/cast_expression.h
@@ -30,6 +30,8 @@
 class CastExpression : public Expression {
  public:
   /// Constructor
+  CastExpression();
+  /// Constructor
   /// @param type the type
   /// @param expr the expr
   CastExpression(type::Type* type, std::unique_ptr<Expression> expr);
@@ -70,7 +72,7 @@
  private:
   CastExpression(const CastExpression&) = delete;
 
-  type::Type* type_;
+  type::Type* type_ = nullptr;
   std::unique_ptr<Expression> expr_;
 };
 
diff --git a/src/ast/cast_expression_test.cc b/src/ast/cast_expression_test.cc
new file mode 100644
index 0000000..711918e
--- /dev/null
+++ b/src/ast/cast_expression_test.cc
@@ -0,0 +1,96 @@
+// Copyright 2020 The Tint Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "src/ast/cast_expression.h"
+
+#include "gtest/gtest.h"
+#include "src/ast/identifier_expression.h"
+#include "src/ast/type/f32_type.h"
+
+namespace tint {
+namespace ast {
+
+using CastExpressionTest = testing::Test;
+
+TEST_F(CastExpressionTest, Creation) {
+  type::F32Type f32;
+  auto expr = std::make_unique<IdentifierExpression>("expr");
+  auto expr_ptr = expr.get();
+
+  CastExpression c(&f32, std::move(expr));
+  EXPECT_EQ(c.type(), &f32);
+  EXPECT_EQ(c.expr(), expr_ptr);
+}
+
+TEST_F(CastExpressionTest, Creation_withSource) {
+  type::F32Type f32;
+  auto expr = std::make_unique<IdentifierExpression>("expr");
+
+  CastExpression c(Source{20, 2}, &f32, std::move(expr));
+  auto src = c.source();
+  EXPECT_EQ(src.line, 20);
+  EXPECT_EQ(src.column, 2);
+}
+
+TEST_F(CastExpressionTest, IsCast) {
+  CastExpression c;
+  EXPECT_TRUE(c.IsCast());
+}
+
+TEST_F(CastExpressionTest, IsValid) {
+  type::F32Type f32;
+  auto expr = std::make_unique<IdentifierExpression>("expr");
+
+  CastExpression c(&f32, std::move(expr));
+  EXPECT_TRUE(c.IsValid());
+}
+
+TEST_F(CastExpressionTest, IsValid_MissingType) {
+  auto expr = std::make_unique<IdentifierExpression>("expr");
+
+  CastExpression c;
+  c.set_expr(std::move(expr));
+  EXPECT_FALSE(c.IsValid());
+}
+
+TEST_F(CastExpressionTest, IsValid_MissingExpression) {
+  type::F32Type f32;
+
+  CastExpression c;
+  c.set_type(&f32);
+  EXPECT_FALSE(c.IsValid());
+}
+
+TEST_F(CastExpressionTest, IsValid_InvalidExpression) {
+  type::F32Type f32;
+  auto expr = std::make_unique<IdentifierExpression>("");
+  CastExpression c(&f32, std::move(expr));
+  EXPECT_FALSE(c.IsValid());
+}
+
+TEST_F(CastExpressionTest, ToStr) {
+  type::F32Type f32;
+  auto expr = std::make_unique<IdentifierExpression>("expr");
+
+  CastExpression c(Source{20, 2}, &f32, std::move(expr));
+  std::ostringstream out;
+  c.to_str(out, 2);
+  EXPECT_EQ(out.str(), R"(  Cast<__f32>(
+    Identifier{expr}
+  )
+)");
+}
+
+}  // namespace ast
+}  // namespace tint