summaryrefslogtreecommitdiff
path: root/gn/src/gn/functions_target_unittest.cc
blob: 04b2a4bc6d6e41ecced6a31e0c997967732fb4a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "gn/scheduler.h"
#include "gn/scope.h"
#include "gn/test_with_scheduler.h"
#include "gn/test_with_scope.h"
#include "util/test/test.h"

using FunctionsTarget = TestWithScheduler;

// Checks that we find unused identifiers in targets.
TEST_F(FunctionsTarget, CheckUnused) {
  TestWithScope setup;

  // The target generator needs a place to put the targets or it will fail.
  Scope::ItemVector item_collector;
  setup.scope()->set_item_collector(&item_collector);

  // Test a good one first.
  TestParseInput good_input(
      "source_set(\"foo\") {\n"
      "}\n");
  ASSERT_FALSE(good_input.has_error());
  Err err;
  good_input.parsed()->Execute(setup.scope(), &err);
  ASSERT_FALSE(err.has_error()) << err.message();

  // Test a source set with an unused variable.
  TestParseInput source_set_input(
      "source_set(\"foo\") {\n"
      "  unused = 5\n"
      "}\n");
  ASSERT_FALSE(source_set_input.has_error());
  err = Err();
  source_set_input.parsed()->Execute(setup.scope(), &err);
  ASSERT_TRUE(err.has_error());
}

// Checks that we find uses of identifiers marked as not needed.
TEST_F(FunctionsTarget, CheckNotNeeded) {
  TestWithScope setup;

  // The target generator needs a place to put the targets or it will fail.
  Scope::ItemVector item_collector;
  setup.scope()->set_item_collector(&item_collector);

  TestParseInput nonscoped_input(
      "source_set(\"foo\") {\n"
      "  a = 1\n"
      "  not_needed([ \"a\" ])\n"
      "}\n");
  ASSERT_FALSE(nonscoped_input.has_error());
  Err err;
  nonscoped_input.parsed()->Execute(setup.scope(), &err);
  ASSERT_FALSE(err.has_error()) << err.message();

  TestParseInput scoped_input(
      "source_set(\"foo\") {\n"
      "  a = {x = 1 y = 2}\n"
      "  not_needed(a, \"*\")\n"
      "}\n");
  ASSERT_FALSE(scoped_input.has_error());
  err = Err();
  scoped_input.parsed()->Execute(setup.scope(), &err);
  ASSERT_FALSE(err.has_error()) << err.message();

  TestParseInput nonexistent_arg_input(
      "source_set(\"foo\") {\n"
      "  a = {x = 1}\n"
      "  not_needed(a, [ \"x\", \"y\" ])\n"
      "}\n");
  ASSERT_FALSE(nonexistent_arg_input.has_error());
  err = Err();
  nonexistent_arg_input.parsed()->Execute(setup.scope(), &err);
  ASSERT_FALSE(err.has_error()) << err.message();

  TestParseInput exclusion_input(
      "source_set(\"foo\") {\n"
      "  x = 1\n"
      "  y = 2\n"
      "  not_needed(\"*\", [ \"y\" ])\n"
      "}\n");
  ASSERT_FALSE(exclusion_input.has_error());
  err = Err();
  exclusion_input.parsed()->Execute(setup.scope(), &err);
  ASSERT_TRUE(err.has_error()) << err.message();
  EXPECT_EQ("Assignment had no effect.", err.message());

  TestParseInput error_input(
      "source_set(\"foo\") {\n"
      "  a = {x = 1 y = 2}\n"
      "  not_needed(a, [ \"x \"], [ \"y\" ])\n"
      "}\n");
  ASSERT_FALSE(error_input.has_error());
  err = Err();
  error_input.parsed()->Execute(setup.scope(), &err);
  ASSERT_TRUE(err.has_error());
  EXPECT_EQ("Not supported with a variable list.", err.message());

  TestParseInput argcount_error_input(
      "source_set(\"foo\") {\n"
      "  not_needed()\n"
      "}\n");
  ASSERT_FALSE(argcount_error_input.has_error());
  err = Err();
  argcount_error_input.parsed()->Execute(setup.scope(), &err);
  ASSERT_TRUE(err.has_error());
  EXPECT_EQ("Wrong number of arguments.", err.message());

  TestParseInput scope_error_input(
      "source_set(\"foo\") {\n"
      "  a = {x = 1 y = 2}\n"
      "  not_needed(a)\n"
      "}\n");
  ASSERT_FALSE(scope_error_input.has_error());
  err = Err();
  scope_error_input.parsed()->Execute(setup.scope(), &err);
  ASSERT_TRUE(err.has_error());
  EXPECT_EQ("Wrong number of arguments.", err.message());

  TestParseInput string_error_input(
      "source_set(\"foo\") {\n"
      "  not_needed(\"*\", {}, \"*\")\n"
      "}\n");
  ASSERT_FALSE(string_error_input.has_error());
  err = Err();
  string_error_input.parsed()->Execute(setup.scope(), &err);
  ASSERT_TRUE(err.has_error());
  EXPECT_EQ("Wrong number of arguments.", err.message());

  TestParseInput template_input(
      R"(# Test that not_needed() propagates through templates correctly;
      # no error should arise from not using "a".
      template("inner_templ") {
        source_set(target_name) {
          not_needed(invoker, [ "a" ])
        }
      }
      template("outer_templ") {
        inner_templ(target_name) {
          forward_variables_from(invoker, "*")
        }
      }
      outer_templ("foo") {
        a = 1
      })");
  ASSERT_FALSE(template_input.has_error());
  err = Err();
  template_input.parsed()->Execute(setup.scope(), &err);
  ASSERT_FALSE(err.has_error()) << err.message();
}

// Checks that the defaults applied to a template invoked by target() use
// the name of the template, rather than the string "target" (which is the
// name of the actual function being called).
TEST_F(FunctionsTarget, TemplateDefaults) {
  TestWithScope setup;

  // The target generator needs a place to put the targets or it will fail.
  Scope::ItemVector item_collector;
  setup.scope()->set_item_collector(&item_collector);

  // Test a good one first.
  TestParseInput good_input(
      R"(# Make a template with defaults set.
      template("my_templ") {
        source_set(target_name) {
          forward_variables_from(invoker, "*")
        }
      }
      set_defaults("my_templ") {
        default_value = 1
      }

      # Invoke the template with target(). This will fail to execute if the
      # defaults were not set properly, because "default_value" won't exist.
      target("my_templ", "foo") {
        print(default_value)
      })");
  ASSERT_FALSE(good_input.has_error());
  Err err;
  good_input.parsed()->Execute(setup.scope(), &err);
  ASSERT_FALSE(err.has_error()) << err.message();
}

// Checks that we find unused identifiers in targets.
TEST_F(FunctionsTarget, MixedSourceError) {
  TestWithScope setup;

  // The target generator needs a place to put the targets or it will fail.
  Scope::ItemVector item_collector;
  setup.scope()->set_item_collector(&item_collector);

  // Test a good one first.
  TestParseInput good_input(
      "source_set(\"foo\") {\n"
      "  sources = [ \"cpp.cc\", \"rust.rs\" ]"
      "}\n");
  ASSERT_FALSE(good_input.has_error());
  Err err;
  good_input.parsed()->Execute(setup.scope(), &err);
  ASSERT_TRUE(err.has_error());
  ASSERT_EQ(err.message(), "More than one language used in target sources.");
}