2020-03-02 20:47:43 +00:00
|
|
|
// 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.
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
#include "src/program.h"
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2020-03-18 20:09:44 +00:00
|
|
|
#include <sstream>
|
2020-03-02 20:47:43 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2020-03-18 20:09:44 +00:00
|
|
|
#include "gmock/gmock.h"
|
2020-03-16 14:17:52 +00:00
|
|
|
#include "src/ast/function.h"
|
2020-11-13 21:58:28 +00:00
|
|
|
#include "src/ast/test_helper.h"
|
2020-03-16 14:17:52 +00:00
|
|
|
#include "src/ast/variable.h"
|
2021-01-21 15:42:10 +00:00
|
|
|
#include "src/type/alias_type.h"
|
|
|
|
#include "src/type/f32_type.h"
|
|
|
|
#include "src/type/struct_type.h"
|
2020-03-02 20:47:43 +00:00
|
|
|
|
|
|
|
namespace tint {
|
2020-03-26 15:31:43 +00:00
|
|
|
namespace {
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
using ProgramTest = ast::TestHelper;
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
TEST_F(ProgramTest, Creation) {
|
2021-01-26 16:57:10 +00:00
|
|
|
Program program(std::move(*this));
|
|
|
|
EXPECT_EQ(program.AST().Functions().size(), 0u);
|
2020-03-02 20:47:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
TEST_F(ProgramTest, ToStrEmitsPreambleAndPostamble) {
|
2021-01-26 16:57:10 +00:00
|
|
|
Program program(std::move(*this));
|
|
|
|
const auto str = program.to_str();
|
2020-04-17 13:18:20 +00:00
|
|
|
auto* const expected = "Module{\n}\n";
|
2020-03-18 20:09:44 +00:00
|
|
|
EXPECT_EQ(str, expected);
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
TEST_F(ProgramTest, IsValid_Empty) {
|
2021-01-26 16:57:10 +00:00
|
|
|
Program program(std::move(*this));
|
|
|
|
EXPECT_TRUE(program.IsValid());
|
2020-03-02 20:47:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
TEST_F(ProgramTest, IsValid_GlobalVariable) {
|
2021-01-26 16:57:10 +00:00
|
|
|
auto* var = Var("var", ast::StorageClass::kInput, ty.f32());
|
2021-01-26 16:57:10 +00:00
|
|
|
AST().AddGlobalVariable(var);
|
2021-01-26 16:57:10 +00:00
|
|
|
|
|
|
|
Program program(std::move(*this));
|
|
|
|
EXPECT_TRUE(program.IsValid());
|
2020-03-16 14:17:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
TEST_F(ProgramTest, IsValid_Null_GlobalVariable) {
|
2021-01-26 16:57:10 +00:00
|
|
|
AST().AddGlobalVariable(nullptr);
|
2021-01-26 16:57:10 +00:00
|
|
|
|
|
|
|
Program program(std::move(*this));
|
|
|
|
EXPECT_FALSE(program.IsValid());
|
2020-03-16 14:17:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
TEST_F(ProgramTest, IsValid_Invalid_GlobalVariable) {
|
|
|
|
auto* var = Var("var", ast::StorageClass::kInput, nullptr);
|
2021-01-26 16:57:10 +00:00
|
|
|
AST().AddGlobalVariable(var);
|
2021-01-26 16:57:10 +00:00
|
|
|
|
|
|
|
Program program(std::move(*this));
|
|
|
|
EXPECT_FALSE(program.IsValid());
|
2020-03-16 14:17:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
TEST_F(ProgramTest, IsValid_Alias) {
|
2021-01-26 16:57:10 +00:00
|
|
|
auto* alias = ty.alias("alias", ty.f32());
|
2021-01-26 16:57:10 +00:00
|
|
|
AST().AddConstructedType(alias);
|
2021-01-26 16:57:10 +00:00
|
|
|
|
|
|
|
Program program(std::move(*this));
|
|
|
|
EXPECT_TRUE(program.IsValid());
|
2020-03-16 14:17:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
TEST_F(ProgramTest, IsValid_Null_Alias) {
|
2021-01-26 16:57:10 +00:00
|
|
|
AST().AddConstructedType(nullptr);
|
2021-01-26 16:57:10 +00:00
|
|
|
|
|
|
|
Program program(std::move(*this));
|
|
|
|
EXPECT_FALSE(program.IsValid());
|
2020-03-16 14:17:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
TEST_F(ProgramTest, IsValid_Struct) {
|
2020-12-16 21:38:40 +00:00
|
|
|
auto* st = ty.struct_("name", {});
|
|
|
|
auto* alias = ty.alias("name", st);
|
2021-01-26 16:57:10 +00:00
|
|
|
AST().AddConstructedType(alias);
|
2021-01-26 16:57:10 +00:00
|
|
|
|
|
|
|
Program program(std::move(*this));
|
|
|
|
EXPECT_TRUE(program.IsValid());
|
2020-10-15 17:54:13 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
TEST_F(ProgramTest, IsValid_Struct_EmptyName) {
|
2020-12-16 21:38:40 +00:00
|
|
|
auto* st = ty.struct_("", {});
|
|
|
|
auto* alias = ty.alias("name", st);
|
2021-01-26 16:57:10 +00:00
|
|
|
AST().AddConstructedType(alias);
|
2021-01-26 16:57:10 +00:00
|
|
|
|
|
|
|
Program program(std::move(*this));
|
|
|
|
EXPECT_FALSE(program.IsValid());
|
2020-10-15 17:54:13 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
TEST_F(ProgramTest, IsValid_Function) {
|
2021-02-02 14:29:15 +00:00
|
|
|
Func("main", ast::VariableList(), ty.f32(), ast::StatementList{},
|
|
|
|
ast::FunctionDecorationList{});
|
2021-01-26 16:57:10 +00:00
|
|
|
|
|
|
|
Program program(std::move(*this));
|
|
|
|
EXPECT_TRUE(program.IsValid());
|
2020-03-16 14:17:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
TEST_F(ProgramTest, IsValid_Null_Function) {
|
2021-01-26 16:57:10 +00:00
|
|
|
AST().Functions().Add(nullptr);
|
2021-01-26 16:57:10 +00:00
|
|
|
|
|
|
|
Program program(std::move(*this));
|
|
|
|
EXPECT_FALSE(program.IsValid());
|
2020-03-16 14:17:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
TEST_F(ProgramTest, IsValid_Invalid_Function) {
|
2021-02-02 14:29:15 +00:00
|
|
|
Func("main", ast::VariableList{}, nullptr, ast::StatementList{},
|
|
|
|
ast::FunctionDecorationList{});
|
2021-01-26 16:57:10 +00:00
|
|
|
|
|
|
|
Program program(std::move(*this));
|
|
|
|
EXPECT_FALSE(program.IsValid());
|
2020-03-16 14:17:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-27 18:49:05 +00:00
|
|
|
TEST_F(ProgramTest, IsValid_GeneratesError) {
|
|
|
|
AST().AddGlobalVariable(nullptr);
|
|
|
|
|
|
|
|
Program program(std::move(*this));
|
|
|
|
EXPECT_FALSE(program.IsValid());
|
|
|
|
EXPECT_EQ(program.Diagnostics().count(), 1u);
|
|
|
|
EXPECT_EQ(program.Diagnostics().error_count(), 1u);
|
|
|
|
EXPECT_EQ(program.Diagnostics().begin()->message,
|
|
|
|
"invalid program generated");
|
|
|
|
}
|
2020-03-26 15:31:43 +00:00
|
|
|
} // namespace
|
2020-03-02 20:47:43 +00:00
|
|
|
} // namespace tint
|