Add RelationalExpression tests

This CL adds test for the RelationalExpression AST node.

Bug: tint:11
Change-Id: Id16e97110423dd9d987b045325696227df0d2913
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/16667
Reviewed-by: Sarah Mashayekhi <sarahmashay@google.com>
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index d2c061f..6dc58c29 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -226,6 +226,7 @@
   ast/module_test.cc
   ast/nop_statement_test.cc
   ast/regardless_statement_test.cc
+  ast/relational_expression_test.cc
   ast/set_decoration_test.cc
   ast/struct_member_test.cc
   ast/struct_member_offset_decoration_test.cc
diff --git a/src/ast/relational_expression.cc b/src/ast/relational_expression.cc
index 794df41..a5e3863 100644
--- a/src/ast/relational_expression.cc
+++ b/src/ast/relational_expression.cc
@@ -17,6 +17,8 @@
 namespace tint {
 namespace ast {
 
+RelationalExpression::RelationalExpression() : Expression() {}
+
 RelationalExpression::RelationalExpression(Relation relation,
                                            std::unique_ptr<Expression> lhs,
                                            std::unique_ptr<Expression> rhs)
@@ -37,18 +39,26 @@
 RelationalExpression::~RelationalExpression() = default;
 
 bool RelationalExpression::IsValid() const {
-  return relation_ != Relation::kNone && lhs_ != nullptr && rhs_ != nullptr;
+  if (lhs_ == nullptr || !lhs_->IsValid()) {
+    return false;
+  }
+  if (rhs_ == nullptr || !rhs_->IsValid()) {
+    return false;
+  }
+  return relation_ != Relation::kNone;
 }
 
 void RelationalExpression::to_str(std::ostream& out, size_t indent) const {
   make_indent(out, indent);
-  out << relation_ << "(" << std::endl;
+  out << "Relation{" << std::endl;
   lhs_->to_str(out, indent + 2);
-  out << std::endl;
+
+  make_indent(out, indent + 2);
+  out << relation_ << std::endl;
+
   rhs_->to_str(out, indent + 2);
-  out << std::endl;
   make_indent(out, indent);
-  out << ")" << std::endl;
+  out << "}" << std::endl;
 }
 
 }  // namespace ast
diff --git a/src/ast/relational_expression.h b/src/ast/relational_expression.h
index 1404353..bc45d4f 100644
--- a/src/ast/relational_expression.h
+++ b/src/ast/relational_expression.h
@@ -62,6 +62,8 @@
 class RelationalExpression : public Expression {
  public:
   /// Constructor
+  RelationalExpression();
+  /// Constructor
   /// @param relation the relation type
   /// @param lhs the left side of the expression
   /// @param rhs the right side of the expression
diff --git a/src/ast/relational_expression_test.cc b/src/ast/relational_expression_test.cc
new file mode 100644
index 0000000..ff41032
--- /dev/null
+++ b/src/ast/relational_expression_test.cc
@@ -0,0 +1,122 @@
+// 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/relational_expression.h"
+
+#include <sstream>
+
+#include "gtest/gtest.h"
+#include "src/ast/identifier_expression.h"
+
+namespace tint {
+namespace ast {
+
+using RelationalExpressionTest = testing::Test;
+
+TEST_F(RelationalExpressionTest, Creation) {
+  auto lhs = std::make_unique<IdentifierExpression>("lhs");
+  auto rhs = std::make_unique<IdentifierExpression>("rhs");
+
+  auto lhs_ptr = lhs.get();
+  auto rhs_ptr = rhs.get();
+
+  RelationalExpression r(Relation::kEqual, std::move(lhs), std::move(rhs));
+  EXPECT_EQ(r.lhs(), lhs_ptr);
+  EXPECT_EQ(r.rhs(), rhs_ptr);
+  EXPECT_EQ(r.relation(), Relation::kEqual);
+}
+
+TEST_F(RelationalExpressionTest, Creation_WithSource) {
+  auto lhs = std::make_unique<IdentifierExpression>("lhs");
+  auto rhs = std::make_unique<IdentifierExpression>("rhs");
+
+  RelationalExpression r(Source{20, 2}, Relation::kEqual, std::move(lhs),
+                         std::move(rhs));
+  auto src = r.source();
+  EXPECT_EQ(src.line, 20);
+  EXPECT_EQ(src.column, 2);
+}
+
+TEST_F(RelationalExpressionTest, IsRelational) {
+  RelationalExpression r;
+  EXPECT_TRUE(r.IsRelational());
+}
+
+TEST_F(RelationalExpressionTest, IsValid) {
+  auto lhs = std::make_unique<IdentifierExpression>("lhs");
+  auto rhs = std::make_unique<IdentifierExpression>("rhs");
+
+  RelationalExpression r(Relation::kEqual, std::move(lhs), std::move(rhs));
+  EXPECT_TRUE(r.IsValid());
+}
+
+TEST_F(RelationalExpressionTest, IsValid_Null_LHS) {
+  auto rhs = std::make_unique<IdentifierExpression>("rhs");
+
+  RelationalExpression r;
+  r.set_relation(Relation::kEqual);
+  r.set_rhs(std::move(rhs));
+  EXPECT_FALSE(r.IsValid());
+}
+
+TEST_F(RelationalExpressionTest, IsValid_Invalid_LHS) {
+  auto lhs = std::make_unique<IdentifierExpression>("");
+  auto rhs = std::make_unique<IdentifierExpression>("rhs");
+
+  RelationalExpression r(Relation::kEqual, std::move(lhs), std::move(rhs));
+  EXPECT_FALSE(r.IsValid());
+}
+
+TEST_F(RelationalExpressionTest, IsValid_Null_RHS) {
+  auto lhs = std::make_unique<IdentifierExpression>("lhs");
+
+  RelationalExpression r;
+  r.set_relation(Relation::kEqual);
+  r.set_lhs(std::move(lhs));
+  EXPECT_FALSE(r.IsValid());
+}
+
+TEST_F(RelationalExpressionTest, IsValid_Invalid_RHS) {
+  auto lhs = std::make_unique<IdentifierExpression>("lhs");
+  auto rhs = std::make_unique<IdentifierExpression>("");
+
+  RelationalExpression r(Relation::kEqual, std::move(lhs), std::move(rhs));
+  EXPECT_FALSE(r.IsValid());
+}
+
+TEST_F(RelationalExpressionTest, IsValid_Relation_None) {
+  auto lhs = std::make_unique<IdentifierExpression>("lhs");
+  auto rhs = std::make_unique<IdentifierExpression>("rhs");
+
+  RelationalExpression r(Relation::kNone, std::move(lhs), std::move(rhs));
+  EXPECT_FALSE(r.IsValid());
+}
+
+TEST_F(RelationalExpressionTest, ToStr) {
+  auto lhs = std::make_unique<IdentifierExpression>("lhs");
+  auto rhs = std::make_unique<IdentifierExpression>("rhs");
+
+  RelationalExpression r(Relation::kEqual, std::move(lhs), std::move(rhs));
+  std::ostringstream out;
+  r.to_str(out, 2);
+  EXPECT_EQ(out.str(), R"(  Relation{
+    Identifier{lhs}
+    equal
+    Identifier{rhs}
+  }
+)");
+}
+
+}  // namespace ast
+}  // namespace tint