blob: afd15380d9cc555d34c107cca208112e963c1a02 [file] [log] [blame]
Dan Sinclair45d585f2020-03-17 14:59:46 +00001// Copyright 2020 The Tint Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
dan sinclaire49bd5e2020-03-30 22:46:48 +000015#include "src/ast/variable_decl_statement.h"
Dan Sinclair45d585f2020-03-17 14:59:46 +000016
Ben Clayton10d5c6a2020-11-13 21:58:28 +000017#include "src/ast/test_helper.h"
Dan Sinclair45d585f2020-03-17 14:59:46 +000018#include "src/ast/type/f32_type.h"
19#include "src/ast/variable.h"
20
21namespace tint {
22namespace ast {
dan sinclair989cee62020-03-26 15:31:43 +000023namespace {
Dan Sinclair45d585f2020-03-17 14:59:46 +000024
Ben Clayton10d5c6a2020-11-13 21:58:28 +000025using VariableDeclStatementTest = TestHelper;
Dan Sinclair45d585f2020-03-17 14:59:46 +000026
dan sinclaire49bd5e2020-03-30 22:46:48 +000027TEST_F(VariableDeclStatementTest, Creation) {
Ben Claytonf1b0e1e2020-11-30 23:30:58 +000028 type::F32 f32;
Ben Claytonb053acf2020-11-16 16:31:07 +000029 auto* var = create<Variable>("a", StorageClass::kNone, &f32);
Dan Sinclair45d585f2020-03-17 14:59:46 +000030
Ben Clayton4bfe4612020-11-16 16:41:47 +000031 VariableDeclStatement stmt(var);
32 EXPECT_EQ(stmt.variable(), var);
Dan Sinclair45d585f2020-03-17 14:59:46 +000033}
34
dan sinclaire49bd5e2020-03-30 22:46:48 +000035TEST_F(VariableDeclStatementTest, Creation_WithSource) {
Ben Claytonf1b0e1e2020-11-30 23:30:58 +000036 type::F32 f32;
Ben Claytonb053acf2020-11-16 16:31:07 +000037 auto* var = create<Variable>("a", StorageClass::kNone, &f32);
Dan Sinclair45d585f2020-03-17 14:59:46 +000038
Ben Clayton4bfe4612020-11-16 16:41:47 +000039 VariableDeclStatement stmt(Source{Source::Location{20, 2}}, var);
Dan Sinclair45d585f2020-03-17 14:59:46 +000040 auto src = stmt.source();
Ben Clayton5bee67f2020-10-30 20:44:53 +000041 EXPECT_EQ(src.range.begin.line, 20u);
42 EXPECT_EQ(src.range.begin.column, 2u);
Dan Sinclair45d585f2020-03-17 14:59:46 +000043}
44
dan sinclaire49bd5e2020-03-30 22:46:48 +000045TEST_F(VariableDeclStatementTest, IsVariableDecl) {
dan sinclair57927832020-12-02 15:18:59 +000046 VariableDeclStatement s;
Ben Clayton1d8098a2020-11-30 23:30:58 +000047 EXPECT_TRUE(s.Is<VariableDeclStatement>());
Dan Sinclair45d585f2020-03-17 14:59:46 +000048}
49
dan sinclaire49bd5e2020-03-30 22:46:48 +000050TEST_F(VariableDeclStatementTest, IsValid) {
Ben Claytonf1b0e1e2020-11-30 23:30:58 +000051 type::F32 f32;
Ben Claytonb053acf2020-11-16 16:31:07 +000052 auto* var = create<Variable>("a", StorageClass::kNone, &f32);
Ben Clayton4bfe4612020-11-16 16:41:47 +000053 VariableDeclStatement stmt(var);
Dan Sinclair45d585f2020-03-17 14:59:46 +000054 EXPECT_TRUE(stmt.IsValid());
55}
56
dan sinclaire49bd5e2020-03-30 22:46:48 +000057TEST_F(VariableDeclStatementTest, IsValid_InvalidVariable) {
Ben Claytonf1b0e1e2020-11-30 23:30:58 +000058 type::F32 f32;
Ben Claytonb053acf2020-11-16 16:31:07 +000059 auto* var = create<Variable>("", StorageClass::kNone, &f32);
Ben Clayton4bfe4612020-11-16 16:41:47 +000060 VariableDeclStatement stmt(var);
Dan Sinclair45d585f2020-03-17 14:59:46 +000061 EXPECT_FALSE(stmt.IsValid());
62}
63
dan sinclaire49bd5e2020-03-30 22:46:48 +000064TEST_F(VariableDeclStatementTest, IsValid_NullVariable) {
dan sinclair57927832020-12-02 15:18:59 +000065 VariableDeclStatement stmt;
Dan Sinclair45d585f2020-03-17 14:59:46 +000066 EXPECT_FALSE(stmt.IsValid());
67}
68
dan sinclaire49bd5e2020-03-30 22:46:48 +000069TEST_F(VariableDeclStatementTest, ToStr) {
Ben Claytonf1b0e1e2020-11-30 23:30:58 +000070 type::F32 f32;
Ben Claytonb053acf2020-11-16 16:31:07 +000071 auto* var = create<Variable>("a", StorageClass::kNone, &f32);
Dan Sinclair45d585f2020-03-17 14:59:46 +000072
Ben Clayton4bfe4612020-11-16 16:41:47 +000073 VariableDeclStatement stmt(Source{Source::Location{20, 2}}, var);
Dan Sinclair45d585f2020-03-17 14:59:46 +000074 std::ostringstream out;
75 stmt.to_str(out, 2);
dan sinclaire49bd5e2020-03-30 22:46:48 +000076 EXPECT_EQ(out.str(), R"( VariableDeclStatement{
Dan Sinclair45d585f2020-03-17 14:59:46 +000077 Variable{
78 a
79 none
80 __f32
81 }
82 }
83)");
84}
85
dan sinclair989cee62020-03-26 15:31:43 +000086} // namespace
Dan Sinclair45d585f2020-03-17 14:59:46 +000087} // namespace ast
88} // namespace tint