1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/strings/string_number_conversions.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "tools/gn/test_with_scope.h"
9 TEST(Template
, Basic
) {
12 "template(\"foo\") {\n"
13 " print(target_name)\n"
14 " print(invoker.bar)\n"
19 ASSERT_FALSE(input
.has_error());
22 input
.parsed()->Execute(setup
.scope(), &err
);
23 ASSERT_FALSE(err
.has_error()) << err
.message();
25 EXPECT_EQ("lala\n42\n", setup
.print_output());
28 TEST(Template
, UnusedTargetNameShouldThrowError
) {
31 "template(\"foo\") {\n"
32 " print(invoker.bar)\n"
37 ASSERT_FALSE(input
.has_error());
40 input
.parsed()->Execute(setup
.scope(), &err
);
41 EXPECT_TRUE(err
.has_error());
44 TEST(Template
, UnusedInvokerShouldThrowError
) {
47 "template(\"foo\") {\n"
48 " print(target_name)\n"
53 ASSERT_FALSE(input
.has_error());
56 input
.parsed()->Execute(setup
.scope(), &err
);
57 EXPECT_TRUE(err
.has_error());
60 TEST(Template
, UnusedVarInInvokerShouldThrowError
) {
63 "template(\"foo\") {\n"
64 " print(target_name)\n"
65 " print(invoker.bar)\n"
69 " baz = [ \"foo\" ]\n"
71 ASSERT_FALSE(input
.has_error());
74 input
.parsed()->Execute(setup
.scope(), &err
);
75 EXPECT_TRUE(err
.has_error());
78 // Previous versions of the template implementation would copy templates by
79 // value when it makes a closure. Doing a sequence of them means that every new
80 // one copies all previous ones, which gives a significant blow-up in memory.
81 // If this test doesn't crash with out-of-memory, it passed.
82 TEST(Template
, MemoryBlowUp
) {
85 for (int i
= 0; i
< 100; i
++)
86 code
+= "template(\"test" + base::IntToString(i
) + "\") {}\n";
88 TestParseInput
input(code
);
91 input
.parsed()->Execute(setup
.scope(), &err
);
92 ASSERT_FALSE(input
.has_error());