ServiceWorker: Enqueue state change events until the worker thread gets ready
[chromium-blink-merge.git] / tools / gn / parse_tree_unittest.cc
blobbaae36a30644a5ce29e7151c3943279da253262d
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 "testing/gtest/include/gtest/gtest.h"
6 #include "tools/gn/input_file.h"
7 #include "tools/gn/parse_tree.h"
8 #include "tools/gn/scope.h"
9 #include "tools/gn/test_with_scope.h"
11 TEST(ParseTree, Accessor) {
12 TestWithScope setup;
14 // Make a pretend parse node with proper tracking that we can blame for the
15 // given value.
16 InputFile input_file(SourceFile("//foo"));
17 Token base_token(Location(&input_file, 1, 1, 1), Token::IDENTIFIER, "a");
18 Token member_token(Location(&input_file, 1, 1, 1), Token::IDENTIFIER, "b");
20 AccessorNode accessor;
21 accessor.set_base(base_token);
23 scoped_ptr<IdentifierNode> member_identifier(
24 new IdentifierNode(member_token));
25 accessor.set_member(member_identifier.Pass());
27 // The access should fail because a is not defined.
28 Err err;
29 Value result = accessor.Execute(setup.scope(), &err);
30 EXPECT_TRUE(err.has_error());
31 EXPECT_EQ(Value::NONE, result.type());
33 // Define a as a Scope. It should still fail because b isn't defined.
34 err = Err();
35 setup.scope()->SetValue(
36 "a", Value(nullptr, scoped_ptr<Scope>(new Scope(setup.scope()))),
37 nullptr);
38 result = accessor.Execute(setup.scope(), &err);
39 EXPECT_TRUE(err.has_error());
40 EXPECT_EQ(Value::NONE, result.type());
42 // Define b, accessor should succeed now.
43 const int64 kBValue = 42;
44 err = Err();
45 setup.scope()
46 ->GetMutableValue("a", false)
47 ->scope_value()
48 ->SetValue("b", Value(nullptr, kBValue), nullptr);
49 result = accessor.Execute(setup.scope(), &err);
50 EXPECT_FALSE(err.has_error());
51 ASSERT_EQ(Value::INTEGER, result.type());
52 EXPECT_EQ(kBValue, result.int_value());
55 TEST(ParseTree, BlockUnusedVars) {
56 TestWithScope setup;
58 // Printing both values should be OK.
59 TestParseInput input_all_used(
60 "{\n"
61 " a = 12\n"
62 " b = 13\n"
63 " print(\"$a $b\")\n"
64 "}");
65 EXPECT_FALSE(input_all_used.has_error());
67 Err err;
68 input_all_used.parsed()->Execute(setup.scope(), &err);
69 EXPECT_FALSE(err.has_error());
71 // Skipping one should throw an unused var error.
72 TestParseInput input_unused(
73 "{\n"
74 " a = 12\n"
75 " b = 13\n"
76 " print(\"$a\")\n"
77 "}");
78 EXPECT_FALSE(input_unused.has_error());
80 input_unused.parsed()->Execute(setup.scope(), &err);
81 EXPECT_TRUE(err.has_error());
83 // Also verify that the unused variable has the correct origin set. The
84 // origin will point to the value assigned to the variable (in this case, the
85 // "13" assigned to "b".
86 EXPECT_EQ(3, err.location().line_number());
87 EXPECT_EQ(7, err.location().char_offset());
91 TEST(ParseTree, OriginForDereference) {
92 TestWithScope setup;
93 TestParseInput input(
94 "a = 6\n"
95 "get_target_outputs(a)");
96 EXPECT_FALSE(input.has_error());
98 Err err;
99 input.parsed()->Execute(setup.scope(), &err);
100 EXPECT_TRUE(err.has_error());
102 // The origin for the "not a string" error message should be where the value
103 // was dereferenced (the "a" on the second line).
104 EXPECT_EQ(2, err.location().line_number());
105 EXPECT_EQ(20, err.location().char_offset());