blob: 700923cfce80deac4979595b2babc988cc390d62 [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
Ben Clayton8454d822021-03-10 11:41:49 +000015#include "gtest/gtest-spi.h"
Ben Clayton90da7452021-02-02 15:06:05 +000016#include "src/ast/return_statement.h"
Ben Clayton10d5c6a2020-11-13 21:58:28 +000017#include "src/ast/test_helper.h"
Dan Sinclair6e581892020-03-02 15:47:43 -050018
19namespace tint {
dan sinclair989cee62020-03-26 15:31:43 +000020namespace {
Dan Sinclair6e581892020-03-02 15:47:43 -050021
Ben Claytonc40f6272021-01-26 16:57:10 +000022using ProgramTest = ast::TestHelper;
Dan Sinclair6e581892020-03-02 15:47:43 -050023
Ben Clayton527a0bb2021-02-19 19:04:18 +000024TEST_F(ProgramTest, Unbuilt) {
25 Program program;
26 EXPECT_FALSE(program.IsValid());
27}
28
Ben Claytonc40f6272021-01-26 16:57:10 +000029TEST_F(ProgramTest, Creation) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +000030 Program program(std::move(*this));
31 EXPECT_EQ(program.AST().Functions().size(), 0u);
Dan Sinclair6e581892020-03-02 15:47:43 -050032}
33
Ben Claytonc40f6272021-01-26 16:57:10 +000034TEST_F(ProgramTest, ToStrEmitsPreambleAndPostamble) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +000035 Program program(std::move(*this));
36 const auto str = program.to_str();
Ryan Harrison0a196c12020-04-17 13:18:20 +000037 auto* const expected = "Module{\n}\n";
David Neto0afb9432020-03-18 20:09:44 +000038 EXPECT_EQ(str, expected);
39}
40
Ben Clayton8454d822021-03-10 11:41:49 +000041TEST_F(ProgramTest, EmptyIsValid) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +000042 Program program(std::move(*this));
43 EXPECT_TRUE(program.IsValid());
Dan Sinclair6e581892020-03-02 15:47:43 -050044}
45
Ben Clayton8454d822021-03-10 11:41:49 +000046TEST_F(ProgramTest, Assert_GlobalVariable) {
Ben Clayton37571bc2021-02-16 23:57:01 +000047 Global("var", ty.f32(), ast::StorageClass::kInput);
Ben Claytona6b9a8e2021-01-26 16:57:10 +000048
49 Program program(std::move(*this));
50 EXPECT_TRUE(program.IsValid());
Dan Sinclair6e581892020-03-02 15:47:43 -050051}
52
Ben Clayton8454d822021-03-10 11:41:49 +000053TEST_F(ProgramTest, Assert_NullGlobalVariable) {
54 EXPECT_FATAL_FAILURE(
55 {
56 ProgramBuilder b;
57 b.AST().AddGlobalVariable(nullptr);
58 },
59 "internal compiler error");
Dan Sinclair3eaf8272020-03-16 14:17:52 +000060}
61
Ben Clayton8454d822021-03-10 11:41:49 +000062TEST_F(ProgramTest, Assert_NullConstructedType) {
63 EXPECT_FATAL_FAILURE(
64 {
65 ProgramBuilder b;
66 b.AST().AddConstructedType(nullptr);
67 },
68 "internal compiler error");
Dan Sinclair3eaf8272020-03-16 14:17:52 +000069}
70
Ben Clayton8454d822021-03-10 11:41:49 +000071TEST_F(ProgramTest, Assert_Null_Function) {
72 EXPECT_FATAL_FAILURE(
73 {
74 ProgramBuilder b;
75 b.AST().AddFunction(nullptr);
76 },
77 "internal compiler error");
Ben Clayton844217f2021-01-27 18:49:05 +000078}
Ben Claytonce9229c2021-02-24 13:51:13 +000079
80TEST_F(ProgramTest, DiagnosticsMove) {
81 Diagnostics().add_error("an error message");
82
83 Program program_a(std::move(*this));
84 EXPECT_FALSE(program_a.IsValid());
85 EXPECT_EQ(program_a.Diagnostics().count(), 1u);
86 EXPECT_EQ(program_a.Diagnostics().error_count(), 1u);
87 EXPECT_EQ(program_a.Diagnostics().begin()->message, "an error message");
88
89 Program program_b(std::move(program_a));
90 EXPECT_FALSE(program_b.IsValid());
91 EXPECT_EQ(program_b.Diagnostics().count(), 1u);
92 EXPECT_EQ(program_b.Diagnostics().error_count(), 1u);
93 EXPECT_EQ(program_b.Diagnostics().begin()->message, "an error message");
94}
95
dan sinclair989cee62020-03-26 15:31:43 +000096} // namespace
Dan Sinclair6e581892020-03-02 15:47:43 -050097} // namespace tint