blob: f9f596882d749a4c38d1a4f31efc9ee4f75cb6ac [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 {
dan sinclair989cee62020-03-26 15:31:43 +000029namespace {
Dan Sinclair6e581892020-03-02 15:47:43 -050030
31using ModuleTest = testing::Test;
32
33TEST_F(ModuleTest, Creation) {
34 Module m;
35
36 EXPECT_EQ(m.imports().size(), 0);
37}
38
David Neto0afb9432020-03-18 20:09:44 +000039TEST_F(ModuleTest, ToStrEmitsPreambleAndPostamble) {
40 Module m;
41 const auto str = m.to_str();
42 const auto expected = "Module{\n}\n";
43 EXPECT_EQ(str, expected);
44}
45
Dan Sinclair6e581892020-03-02 15:47:43 -050046TEST_F(ModuleTest, Imports) {
47 Module m;
48
49 m.AddImport(std::make_unique<Import>("GLSL.std.430", "std::glsl"));
50 m.AddImport(std::make_unique<Import>("OpenCL.debug.100", "std::debug"));
51
52 EXPECT_EQ(2, m.imports().size());
53 EXPECT_EQ("std::glsl", m.imports()[0]->name());
54}
55
David Neto0afb9432020-03-18 20:09:44 +000056TEST_F(ModuleTest, ToStrWithImport) {
57 Module m;
58 m.AddImport(std::make_unique<Import>("GLSL.std.430", "std::glsl"));
59 const auto str = m.to_str();
60 EXPECT_EQ(str, R"(Module{
61 Import{"GLSL.std.430" as std::glsl}
62}
63)");
64}
65
Dan Sinclair6e581892020-03-02 15:47:43 -050066TEST_F(ModuleTest, LookupImport) {
67 Module m;
68
69 auto i = std::make_unique<Import>("GLSL.std.430", "std::glsl");
70 m.AddImport(std::move(i));
71 m.AddImport(std::make_unique<Import>("OpenCL.debug.100", "std::debug"));
72
73 auto import = m.FindImportByName("std::glsl");
74 ASSERT_NE(nullptr, import);
75 EXPECT_EQ(import->path(), "GLSL.std.430");
76 EXPECT_EQ(import->name(), "std::glsl");
77}
78
79TEST_F(ModuleTest, LookupImportMissing) {
80 Module m;
81 EXPECT_EQ(nullptr, m.FindImportByName("Missing"));
82}
83
84TEST_F(ModuleTest, IsValid_Empty) {
85 Module m;
86 EXPECT_TRUE(m.IsValid());
87}
88
Dan Sinclair3eaf8272020-03-16 14:17:52 +000089TEST_F(ModuleTest, IsValid_Import) {
90 Module m;
91 m.AddImport(std::make_unique<Import>("GLSL.std.430", "std::glsl"));
92 EXPECT_TRUE(m.IsValid());
93}
94
95TEST_F(ModuleTest, IsValid_Null_Import) {
96 Module m;
97 m.AddImport(nullptr);
98 EXPECT_FALSE(m.IsValid());
99}
100
101TEST_F(ModuleTest, IsValid_Invalid_Import) {
Dan Sinclair6e581892020-03-02 15:47:43 -0500102 Module m;
103 m.AddImport(std::make_unique<Import>());
104 EXPECT_FALSE(m.IsValid());
105}
106
Dan Sinclair3eaf8272020-03-16 14:17:52 +0000107TEST_F(ModuleTest, IsValid_GlobalVariable) {
108 type::F32Type f32;
109 auto var = std::make_unique<Variable>("var", StorageClass::kInput, &f32);
110
Dan Sinclair6e581892020-03-02 15:47:43 -0500111 Module m;
Dan Sinclair3eaf8272020-03-16 14:17:52 +0000112 m.AddGlobalVariable(std::move(var));
Dan Sinclair6e581892020-03-02 15:47:43 -0500113 EXPECT_TRUE(m.IsValid());
114}
115
Dan Sinclair3eaf8272020-03-16 14:17:52 +0000116TEST_F(ModuleTest, IsValid_Null_GlobalVariable) {
117 Module m;
118 m.AddGlobalVariable(nullptr);
119 EXPECT_FALSE(m.IsValid());
120}
121
122TEST_F(ModuleTest, IsValid_Invalid_GlobalVariable) {
123 auto var = std::make_unique<Variable>("var", StorageClass::kInput, nullptr);
124
125 Module m;
126 m.AddGlobalVariable(std::move(var));
127 EXPECT_FALSE(m.IsValid());
128}
129
130TEST_F(ModuleTest, IsValid_EntryPoint) {
131 Module m;
132 m.AddEntryPoint(
133 std::make_unique<EntryPoint>(PipelineStage::kVertex, "main", "vtx_main"));
134 EXPECT_TRUE(m.IsValid());
135}
136
137TEST_F(ModuleTest, IsValid_Null_EntryPoint) {
138 Module m;
139 m.AddEntryPoint(nullptr);
140 EXPECT_FALSE(m.IsValid());
141}
142
143TEST_F(ModuleTest, IsValid_Invalid_EntryPoint) {
144 Module m;
145 m.AddEntryPoint(std::make_unique<EntryPoint>());
146 EXPECT_FALSE(m.IsValid());
147}
148
149TEST_F(ModuleTest, IsValid_Alias) {
150 type::F32Type f32;
151 type::AliasType alias("alias", &f32);
152
153 Module m;
154 m.AddAliasType(&alias);
155 EXPECT_TRUE(m.IsValid());
156}
157
158TEST_F(ModuleTest, IsValid_Null_Alias) {
159 Module m;
160 m.AddAliasType(nullptr);
161 EXPECT_FALSE(m.IsValid());
162}
163
164TEST_F(ModuleTest, IsValid_Function) {
165 type::F32Type f32;
dan sinclair3ffec802020-04-06 19:37:37 +0000166 auto func = std::make_unique<Function>("main", VariableList(), &f32);
Dan Sinclair3eaf8272020-03-16 14:17:52 +0000167
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 sinclair989cee62020-03-26 15:31:43 +0000187} // namespace
Dan Sinclair6e581892020-03-02 15:47:43 -0500188} // namespace ast
189} // namespace tint