blob: 7fad54b5ab6a764147074d3e892761f54a00024b [file] [log] [blame]
Dan Sinclair6e581892020-03-02 15:47:43 -05001// 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
15#include "src/ast/module.h"
16
David Neto0afb9432020-03-18 20:09:44 +000017#include <sstream>
Dan Sinclair6e581892020-03-02 15:47:43 -050018#include <utility>
19
David Neto0afb9432020-03-18 20:09:44 +000020#include "gmock/gmock.h"
Dan Sinclair3eaf8272020-03-16 14:17:52 +000021#include "src/ast/entry_point.h"
22#include "src/ast/function.h"
23#include "src/ast/import.h"
24#include "src/ast/type/f32_type.h"
25#include "src/ast/variable.h"
Dan Sinclair6e581892020-03-02 15:47:43 -050026
27namespace tint {
28namespace ast {
29
30using ModuleTest = testing::Test;
31
32TEST_F(ModuleTest, Creation) {
33 Module m;
34
35 EXPECT_EQ(m.imports().size(), 0);
36}
37
David Neto0afb9432020-03-18 20:09:44 +000038TEST_F(ModuleTest, ToStrEmitsPreambleAndPostamble) {
39 Module m;
40 const auto str = m.to_str();
41 const auto expected = "Module{\n}\n";
42 EXPECT_EQ(str, expected);
43}
44
Dan Sinclair6e581892020-03-02 15:47:43 -050045TEST_F(ModuleTest, Imports) {
46 Module m;
47
48 m.AddImport(std::make_unique<Import>("GLSL.std.430", "std::glsl"));
49 m.AddImport(std::make_unique<Import>("OpenCL.debug.100", "std::debug"));
50
51 EXPECT_EQ(2, m.imports().size());
52 EXPECT_EQ("std::glsl", m.imports()[0]->name());
53}
54
David Neto0afb9432020-03-18 20:09:44 +000055TEST_F(ModuleTest, ToStrWithImport) {
56 Module m;
57 m.AddImport(std::make_unique<Import>("GLSL.std.430", "std::glsl"));
58 const auto str = m.to_str();
59 EXPECT_EQ(str, R"(Module{
60 Import{"GLSL.std.430" as std::glsl}
61}
62)");
63}
64
Dan Sinclair6e581892020-03-02 15:47:43 -050065TEST_F(ModuleTest, LookupImport) {
66 Module m;
67
68 auto i = std::make_unique<Import>("GLSL.std.430", "std::glsl");
69 m.AddImport(std::move(i));
70 m.AddImport(std::make_unique<Import>("OpenCL.debug.100", "std::debug"));
71
72 auto import = m.FindImportByName("std::glsl");
73 ASSERT_NE(nullptr, import);
74 EXPECT_EQ(import->path(), "GLSL.std.430");
75 EXPECT_EQ(import->name(), "std::glsl");
76}
77
78TEST_F(ModuleTest, LookupImportMissing) {
79 Module m;
80 EXPECT_EQ(nullptr, m.FindImportByName("Missing"));
81}
82
83TEST_F(ModuleTest, IsValid_Empty) {
84 Module m;
85 EXPECT_TRUE(m.IsValid());
86}
87
Dan Sinclair3eaf8272020-03-16 14:17:52 +000088TEST_F(ModuleTest, IsValid_Import) {
89 Module m;
90 m.AddImport(std::make_unique<Import>("GLSL.std.430", "std::glsl"));
91 EXPECT_TRUE(m.IsValid());
92}
93
94TEST_F(ModuleTest, IsValid_Null_Import) {
95 Module m;
96 m.AddImport(nullptr);
97 EXPECT_FALSE(m.IsValid());
98}
99
100TEST_F(ModuleTest, IsValid_Invalid_Import) {
Dan Sinclair6e581892020-03-02 15:47:43 -0500101 Module m;
102 m.AddImport(std::make_unique<Import>());
103 EXPECT_FALSE(m.IsValid());
104}
105
Dan Sinclair3eaf8272020-03-16 14:17:52 +0000106TEST_F(ModuleTest, IsValid_GlobalVariable) {
107 type::F32Type f32;
108 auto var = std::make_unique<Variable>("var", StorageClass::kInput, &f32);
109
Dan Sinclair6e581892020-03-02 15:47:43 -0500110 Module m;
Dan Sinclair3eaf8272020-03-16 14:17:52 +0000111 m.AddGlobalVariable(std::move(var));
Dan Sinclair6e581892020-03-02 15:47:43 -0500112 EXPECT_TRUE(m.IsValid());
113}
114
Dan Sinclair3eaf8272020-03-16 14:17:52 +0000115TEST_F(ModuleTest, IsValid_Null_GlobalVariable) {
116 Module m;
117 m.AddGlobalVariable(nullptr);
118 EXPECT_FALSE(m.IsValid());
119}
120
121TEST_F(ModuleTest, IsValid_Invalid_GlobalVariable) {
122 auto var = std::make_unique<Variable>("var", StorageClass::kInput, nullptr);
123
124 Module m;
125 m.AddGlobalVariable(std::move(var));
126 EXPECT_FALSE(m.IsValid());
127}
128
129TEST_F(ModuleTest, IsValid_EntryPoint) {
130 Module m;
131 m.AddEntryPoint(
132 std::make_unique<EntryPoint>(PipelineStage::kVertex, "main", "vtx_main"));
133 EXPECT_TRUE(m.IsValid());
134}
135
136TEST_F(ModuleTest, IsValid_Null_EntryPoint) {
137 Module m;
138 m.AddEntryPoint(nullptr);
139 EXPECT_FALSE(m.IsValid());
140}
141
142TEST_F(ModuleTest, IsValid_Invalid_EntryPoint) {
143 Module m;
144 m.AddEntryPoint(std::make_unique<EntryPoint>());
145 EXPECT_FALSE(m.IsValid());
146}
147
148TEST_F(ModuleTest, IsValid_Alias) {
149 type::F32Type f32;
150 type::AliasType alias("alias", &f32);
151
152 Module m;
153 m.AddAliasType(&alias);
154 EXPECT_TRUE(m.IsValid());
155}
156
157TEST_F(ModuleTest, IsValid_Null_Alias) {
158 Module m;
159 m.AddAliasType(nullptr);
160 EXPECT_FALSE(m.IsValid());
161}
162
163TEST_F(ModuleTest, IsValid_Function) {
164 type::F32Type f32;
165 auto func = std::make_unique<Function>(
166 "main", std::vector<std::unique_ptr<Variable>>(), &f32);
167
168 Module m;
169 m.AddFunction(std::move(func));
170 EXPECT_TRUE(m.IsValid());
171}
172
173TEST_F(ModuleTest, IsValid_Null_Function) {
174 Module m;
175 m.AddFunction(nullptr);
176 EXPECT_FALSE(m.IsValid());
177}
178
179TEST_F(ModuleTest, IsValid_Invalid_Function) {
180 auto func = std::make_unique<Function>();
181
182 Module m;
183 m.AddFunction(std::move(func));
184 EXPECT_FALSE(m.IsValid());
185}
186
Dan Sinclair6e581892020-03-02 15:47:43 -0500187} // namespace ast
188} // namespace tint