blob: b5dbaa7876775f9b9ebbfbf0695a22f77c50dee8 [file] [log] [blame]
Ben Claytone43c8302021-01-29 11:59:32 +00001// Copyright 2021 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/program_builder.h"
16
17#include "gtest/gtest.h"
18
19namespace tint {
20namespace {
21
22using ProgramBuilderTest = testing::Test;
23
24TEST_F(ProgramBuilderTest, WrapDoesntAffectInner) {
25 Program inner([] {
26 ProgramBuilder builder;
27 auto* ty = builder.ty.f32();
28 auto* func = builder.Func("a", {}, ty, {}, {});
29 builder.AST().Functions().Add(func);
30 return builder;
31 }());
32
33 ASSERT_EQ(inner.AST().Functions().size(), 1u);
34 ASSERT_TRUE(inner.Symbols().Get("a").IsValid());
35 ASSERT_FALSE(inner.Symbols().Get("b").IsValid());
36
37 ProgramBuilder outer = ProgramBuilder::Wrap(&inner);
38
39 ASSERT_EQ(inner.AST().Functions().size(), 1u);
40 ASSERT_EQ(outer.AST().Functions().size(), 1u);
41 EXPECT_EQ(inner.AST().Functions()[0], outer.AST().Functions()[0]);
42 EXPECT_TRUE(inner.Symbols().Get("a").IsValid());
43 EXPECT_EQ(inner.Symbols().Get("a"), outer.Symbols().Get("a"));
44 EXPECT_TRUE(inner.Symbols().Get("a").IsValid());
45 EXPECT_TRUE(outer.Symbols().Get("a").IsValid());
46 EXPECT_FALSE(inner.Symbols().Get("b").IsValid());
47 EXPECT_FALSE(outer.Symbols().Get("b").IsValid());
48
49 auto* ty = outer.ty.f32();
50 auto* func = outer.Func("b", {}, ty, {}, {});
51 outer.AST().Functions().Add(func);
52
53 ASSERT_EQ(inner.AST().Functions().size(), 1u);
54 ASSERT_EQ(outer.AST().Functions().size(), 2u);
55 EXPECT_EQ(inner.AST().Functions()[0], outer.AST().Functions()[0]);
56 EXPECT_EQ(outer.AST().Functions()[1]->symbol(), outer.Symbols().Get("b"));
57 EXPECT_EQ(inner.Symbols().Get("a"), outer.Symbols().Get("a"));
58 EXPECT_TRUE(inner.Symbols().Get("a").IsValid());
59 EXPECT_TRUE(outer.Symbols().Get("a").IsValid());
60 EXPECT_FALSE(inner.Symbols().Get("b").IsValid());
61 EXPECT_TRUE(outer.Symbols().Get("b").IsValid());
62}
63
64} // namespace
65} // namespace tint