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.
4
5 #include <stdint.h>
6
7 #include "gn/test_with_scope.h"
8 #include "gn/value.h"
9 #include "util/test/test.h"
10
TEST(Value,ToString)11 TEST(Value, ToString) {
12 Value strval(nullptr, "hi\" $me\\you\\$\\\"");
13 EXPECT_EQ("hi\" $me\\you\\$\\\"", strval.ToString(false));
14 EXPECT_EQ("\"hi\\\" \\$me\\you\\\\\\$\\\\\\\"\"", strval.ToString(true));
15
16 // crbug.com/470217
17 Value strval2(nullptr, "\\foo\\\\bar\\");
18 EXPECT_EQ("\"\\foo\\\\\\bar\\\\\"", strval2.ToString(true));
19
20 // Void type.
21 EXPECT_EQ("<void>", Value().ToString(false));
22
23 // Test lists, bools, and ints.
24 Value listval(nullptr, Value::LIST);
25 listval.list_value().push_back(Value(nullptr, "hi\"me"));
26 listval.list_value().push_back(Value(nullptr, true));
27 listval.list_value().push_back(Value(nullptr, false));
28 listval.list_value().push_back(Value(nullptr, static_cast<int64_t>(42)));
29 // Printing lists always causes embedded strings to be quoted (ignoring the
30 // quote flag), or else they wouldn't make much sense.
31 EXPECT_EQ("[\"hi\\\"me\", true, false, 42]", listval.ToString(false));
32 EXPECT_EQ("[\"hi\\\"me\", true, false, 42]", listval.ToString(true));
33
34 // Scopes.
35 TestWithScope setup;
36 Scope* scope = new Scope(setup.settings());
37 Value scopeval(nullptr, std::unique_ptr<Scope>(scope));
38 EXPECT_EQ("{ }", scopeval.ToString(false));
39
40 // Test that an empty scope equals an empty scope.
41 EXPECT_TRUE(scopeval == scopeval);
42
43 scope->SetValue("a", Value(nullptr, static_cast<int64_t>(42)), nullptr);
44 scope->SetValue("b", Value(nullptr, "hello, world"), nullptr);
45 EXPECT_EQ("{\n a = 42\n b = \"hello, world\"\n}", scopeval.ToString(false));
46 EXPECT_TRUE(scopeval == scopeval);
47
48 Scope* inner_scope = new Scope(setup.settings());
49 Value inner_scopeval(nullptr, std::unique_ptr<Scope>(inner_scope));
50 inner_scope->SetValue("d", Value(nullptr, static_cast<int64_t>(42)), nullptr);
51 scope->SetValue("c", inner_scopeval, nullptr);
52
53 // Test inner scope equality.
54 EXPECT_TRUE(scopeval == scopeval);
55
56 // Nested scopes should not be equal.
57 Scope* nested_scope = new Scope(scope);
58 Value nested_scopeval(nullptr, std::unique_ptr<Scope>(nested_scope));
59 EXPECT_FALSE(nested_scopeval == nested_scopeval);
60 }
61