Add test for assignment statement.

This CL adds unit tests for the assignment statement methods.

Change-Id: I4aea8f22b530bf43b7dfdc2b82b5414cf6590fea
Bug: tint:11
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/16341
Commit-Queue: Sarah Mashayekhi <sarahmashay@google.com>
Reviewed-by: Sarah Mashayekhi <sarahmashay@google.com>
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index a79ceba..a246ee2 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -195,6 +195,7 @@
 set(TINT_TEST_SRCS
   ast/array_accessor_expression_test.cc
   ast/as_expression_test.cc
+  ast/assignment_statement_test.cc
   ast/binding_decoration_test.cc
   ast/bool_literal_test.cc
   ast/builtin_decoration_test.cc
diff --git a/src/ast/assignment_statement.cc b/src/ast/assignment_statement.cc
index 8fe711b..47b5543 100644
--- a/src/ast/assignment_statement.cc
+++ b/src/ast/assignment_statement.cc
@@ -17,6 +17,8 @@
 namespace tint {
 namespace ast {
 
+AssignmentStatement::AssignmentStatement() : Statement() {}
+
 AssignmentStatement::AssignmentStatement(std::unique_ptr<Expression> lhs,
                                          std::unique_ptr<Expression> rhs)
     : Statement(), lhs_(std::move(lhs)), rhs_(std::move(rhs)) {}
@@ -29,16 +31,19 @@
 AssignmentStatement::~AssignmentStatement() = default;
 
 bool AssignmentStatement::IsValid() const {
-  return lhs_ != nullptr && rhs_ != nullptr;
+  if (lhs_ == nullptr || !lhs_->IsValid())
+    return false;
+  if (rhs_ == nullptr || !rhs_->IsValid())
+    return false;
+
+  return true;
 }
 
 void AssignmentStatement::to_str(std::ostream& out, size_t indent) const {
   make_indent(out, indent);
   out << "Assignment{" << std::endl;
   lhs_->to_str(out, indent + 2);
-  out << std::endl;
   rhs_->to_str(out, indent + 2);
-  out << std::endl;
   make_indent(out, indent);
   out << "}" << std::endl;
 }
diff --git a/src/ast/assignment_statement.h b/src/ast/assignment_statement.h
index 6441874..4f6e6b8 100644
--- a/src/ast/assignment_statement.h
+++ b/src/ast/assignment_statement.h
@@ -29,6 +29,8 @@
 class AssignmentStatement : public Statement {
  public:
   /// Constructor
+  AssignmentStatement();
+  /// Constructor
   /// @param lhs the left side of the expression
   /// @param rhs the right side of the expression
   AssignmentStatement(std::unique_ptr<Expression> lhs,
diff --git a/src/ast/assignment_statement_test.cc b/src/ast/assignment_statement_test.cc
new file mode 100644
index 0000000..132ee6f
--- /dev/null
+++ b/src/ast/assignment_statement_test.cc
@@ -0,0 +1,109 @@
+// 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/assignment_statement.h"
+
+#include "gtest/gtest.h"
+#include "src/ast/identifier_expression.h"
+
+namespace tint {
+namespace ast {
+
+using AssignmentStatementTest = testing::Test;
+
+TEST_F(AssignmentStatementTest, Creation) {
+  auto lhs = std::make_unique<ast::IdentifierExpression>("lhs");
+  auto rhs = std::make_unique<ast::IdentifierExpression>("rhs");
+
+  auto lhs_ptr = lhs.get();
+  auto rhs_ptr = rhs.get();
+
+  AssignmentStatement stmt(std::move(lhs), std::move(rhs));
+  EXPECT_EQ(stmt.lhs(), lhs_ptr);
+  EXPECT_EQ(stmt.rhs(), rhs_ptr);
+}
+
+TEST_F(AssignmentStatementTest, CreationWithSource) {
+  auto lhs = std::make_unique<ast::IdentifierExpression>("lhs");
+  auto rhs = std::make_unique<ast::IdentifierExpression>("rhs");
+
+  AssignmentStatement stmt(Source{20, 2}, std::move(lhs), std::move(rhs));
+  auto src = stmt.source();
+  EXPECT_EQ(src.line, 20);
+  EXPECT_EQ(src.column, 2);
+}
+
+TEST_F(AssignmentStatementTest, IsAssign) {
+  auto lhs = std::make_unique<ast::IdentifierExpression>("lhs");
+  auto rhs = std::make_unique<ast::IdentifierExpression>("rhs");
+
+  AssignmentStatement stmt(std::move(lhs), std::move(rhs));
+  EXPECT_TRUE(stmt.IsAssign());
+}
+
+TEST_F(AssignmentStatementTest, IsValid) {
+  auto lhs = std::make_unique<ast::IdentifierExpression>("lhs");
+  auto rhs = std::make_unique<ast::IdentifierExpression>("rhs");
+
+  AssignmentStatement stmt(std::move(lhs), std::move(rhs));
+  EXPECT_TRUE(stmt.IsValid());
+}
+
+TEST_F(AssignmentStatementTest, IsValid_MissingLHS) {
+  auto rhs = std::make_unique<ast::IdentifierExpression>("rhs");
+
+  AssignmentStatement stmt;
+  stmt.set_rhs(std::move(rhs));
+  EXPECT_FALSE(stmt.IsValid());
+}
+
+TEST_F(AssignmentStatementTest, IsValid_MissingRHS) {
+  auto lhs = std::make_unique<ast::IdentifierExpression>("lhs");
+
+  AssignmentStatement stmt;
+  stmt.set_lhs(std::move(lhs));
+  EXPECT_FALSE(stmt.IsValid());
+}
+
+TEST_F(AssignmentStatementTest, IsValid_InvalidLHS) {
+  auto lhs = std::make_unique<ast::IdentifierExpression>("");
+  auto rhs = std::make_unique<ast::IdentifierExpression>("rhs");
+  AssignmentStatement stmt(std::move(lhs), std::move(rhs));
+  EXPECT_FALSE(stmt.IsValid());
+}
+
+TEST_F(AssignmentStatementTest, IsValid_InvalidRHS) {
+  auto lhs = std::make_unique<ast::IdentifierExpression>("lhs");
+  auto rhs = std::make_unique<ast::IdentifierExpression>("");
+  AssignmentStatement stmt(std::move(lhs), std::move(rhs));
+  EXPECT_FALSE(stmt.IsValid());
+}
+
+TEST_F(AssignmentStatementTest, ToStr) {
+  auto lhs = std::make_unique<ast::IdentifierExpression>("lhs");
+  auto rhs = std::make_unique<ast::IdentifierExpression>("rhs");
+
+  AssignmentStatement stmt(std::move(lhs), std::move(rhs));
+  std::ostringstream out;
+  stmt.to_str(out, 2);
+
+  EXPECT_EQ(out.str(), R"(  Assignment{
+    Identifier{lhs}
+    Identifier{rhs}
+  }
+)");
+}
+
+}  // namespace ast
+}  // namespace tint