blob: f1f5551cc93e418b4e1469c6a07f07b23b67f25d [file] [log] [blame]
dan sinclairc2d97ae2020-04-03 02:35:23 +00001// Copyright 2020 The Tint Authors. //
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#include "src/scope_stack.h"
15
16#include "gtest/gtest.h"
17#include "src/ast/variable.h"
18
19namespace tint {
20namespace {
21
22using ScopeStackTest = testing::Test;
23
24TEST_F(ScopeStackTest, Global) {
25 ScopeStack<uint32_t> s;
26 s.set_global("var", 5);
27
28 uint32_t val = 0;
29 EXPECT_TRUE(s.get("var", &val));
Ryan Harrison0a196c12020-04-17 13:18:20 +000030 EXPECT_EQ(val, 5u);
dan sinclairc2d97ae2020-04-03 02:35:23 +000031}
32
33TEST_F(ScopeStackTest, Global_SetWithPointer) {
34 ast::Variable v;
35 v.set_name("my_var");
36
37 ScopeStack<ast::Variable*> s;
38 s.set_global("var", &v);
39
Greg Roth82293602020-04-06 17:16:56 +000040 ast::Variable* v2 = nullptr;
dan sinclairc2d97ae2020-04-03 02:35:23 +000041 EXPECT_TRUE(s.get("var", &v2));
42 EXPECT_EQ(v2->name(), "my_var");
43}
44
45TEST_F(ScopeStackTest, Global_CanNotPop) {
46 ScopeStack<uint32_t> s;
47 s.set_global("var", 5);
48 s.pop_scope();
49
50 uint32_t val = 0;
51 EXPECT_TRUE(s.get("var", &val));
Ryan Harrison0a196c12020-04-17 13:18:20 +000052 EXPECT_EQ(val, 5u);
dan sinclairc2d97ae2020-04-03 02:35:23 +000053}
54
55TEST_F(ScopeStackTest, Scope) {
56 ScopeStack<uint32_t> s;
57 s.push_scope();
58 s.set("var", 5);
59
60 uint32_t val = 0;
61 EXPECT_TRUE(s.get("var", &val));
Ryan Harrison0a196c12020-04-17 13:18:20 +000062 EXPECT_EQ(val, 5u);
dan sinclairc2d97ae2020-04-03 02:35:23 +000063}
64
65TEST_F(ScopeStackTest, Get_MissingName) {
66 ScopeStack<uint32_t> s;
67 uint32_t ret = 0;
68 EXPECT_FALSE(s.get("val", &ret));
Ryan Harrison0a196c12020-04-17 13:18:20 +000069 EXPECT_EQ(ret, 0u);
dan sinclairc2d97ae2020-04-03 02:35:23 +000070}
71
72TEST_F(ScopeStackTest, Has) {
73 ScopeStack<uint32_t> s;
74 s.set_global("var2", 3);
75 s.push_scope();
76 s.set("var", 5);
77
78 EXPECT_TRUE(s.has("var"));
79 EXPECT_TRUE(s.has("var2"));
80}
81
82TEST_F(ScopeStackTest, ReturnsScopeBeforeGlobalFirst) {
83 ScopeStack<uint32_t> s;
84 s.set_global("var", 3);
85 s.push_scope();
86 s.set("var", 5);
87
88 uint32_t ret;
89 EXPECT_TRUE(s.get("var", &ret));
Ryan Harrison0a196c12020-04-17 13:18:20 +000090 EXPECT_EQ(ret, 5u);
dan sinclairc2d97ae2020-04-03 02:35:23 +000091}
92
93} // namespace
94} // namespace tint