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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/trace_event/traced_value.h"
#include <cmath>
#include <cstddef>
#include <utility>
#include "base/strings/string_util.h"
#include "base/values.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace trace_event {
TEST(TraceEventArgumentTest, InitializerListCreatedContainers) {
std::string json;
TracedValue::Build(
{
{"empty_array", TracedValue::Array({})},
{"empty_dictionary", TracedValue::Dictionary({})},
{"nested_array", TracedValue::Array({
TracedValue::Array({}),
TracedValue::Dictionary({}),
true,
})},
{"nested_dictionary", TracedValue::Dictionary({
{"d", TracedValue::Dictionary({})},
{"a", TracedValue::Array({})},
{"b", true},
})},
})
->AppendAsTraceFormat(&json);
EXPECT_EQ(
"{\"empty_array\":[],\"empty_dictionary\":{},"
"\"nested_array\":[[],{},true],"
"\"nested_dictionary\":{\"d\":{},\"a\":[],\"b\":true}}",
json);
}
TEST(TraceEventArgumentTest, InitializerListCreatedFlatDictionary) {
std::string json;
TracedValue::Build({{"bool_var", true},
{"double_var", 3.14},
{"int_var", 2020},
{"literal_var", "literal"}})
->AppendAsTraceFormat(&json);
EXPECT_EQ(
"{\"bool_var\":true,\"double_var\":3.14,\"int_var\":2020,\""
"literal_var\":\"literal\"}",
json);
}
TEST(TraceEventArgumentTest, ArrayAndDictionaryScope) {
std::unique_ptr<TracedValue> value(new TracedValue());
{
auto dictionary = value->BeginDictionaryScoped("dictionary_name");
value->SetInteger("my_int", 1);
}
{
auto array = value->BeginArrayScoped("array_name");
value->AppendInteger(2);
}
{
auto surround_dictionary =
value->BeginDictionaryScoped("outside_dictionary");
value->SetBoolean("my_bool", true);
{
auto inside_array = value->BeginArrayScoped("inside_array");
value->AppendBoolean(false);
}
{
auto inside_array = value->BeginDictionaryScoped("inside_dictionary");
value->SetBoolean("inner_bool", false);
}
}
{
auto surround_array = value->BeginArrayScoped("outside_array");
value->AppendBoolean(false);
{
auto inside_dictionary = value->AppendDictionaryScoped();
value->SetBoolean("my_bool", true);
}
{
auto inside_array = value->AppendArrayScoped();
value->AppendBoolean(false);
}
}
{
auto dictionary = value->BeginDictionaryScopedWithCopiedName(
std::string("wonderful_") + std::string("world"));
}
{
auto array = value->BeginArrayScopedWithCopiedName(
std::string("wonderful_") + std::string("array"));
}
std::string json;
value->AppendAsTraceFormat(&json);
EXPECT_EQ(
"{"
"\"dictionary_name\":{\"my_int\":1},"
"\"array_name\":[2],"
"\"outside_dictionary\":{\"my_bool\":true,\"inside_array\":[false],"
"\"inside_dictionary\":{\"inner_bool\":false}},"
"\"outside_array\":[false,{\"my_bool\":true},[false]],"
"\"wonderful_world\":{},"
"\"wonderful_array\":[]"
"}",
json);
}
std::string SayHello() {
// Create a string by concatenating two strings, so that there is no literal
// corresponding to the result.
return std::string("hello ") + std::string("world");
}
TEST(TraceEventArgumentTest, StringAndPointerConstructors) {
std::string json;
const char* const_char_ptr_var = "const char* value";
TracedValue::Build(
{
{"literal_var", "literal"},
{"std_string_var", std::string("std::string value")},
{"string_from_function", SayHello()},
{"string_from_lambda", []() { return std::string("hello"); }()},
{"base_string_piece_var",
base::StringPiece("base::StringPiece value")},
{"const_char_ptr_var", const_char_ptr_var},
{"void_nullptr", static_cast<void*>(nullptr)},
{"int_nullptr", static_cast<int*>(nullptr)},
{"void_1234ptr", reinterpret_cast<void*>(0x1234)},
})
->AppendAsTraceFormat(&json);
EXPECT_EQ(
"{\"literal_var\":\"literal\","
"\"std_string_var\":\"std::string value\","
"\"string_from_function\":\"hello world\","
"\"string_from_lambda\":\"hello\","
"\"base_string_piece_var\":\"base::StringPiece value\","
"\"const_char_ptr_var\":\"const char* value\","
"\"void_nullptr\":\"0x0\","
"\"int_nullptr\":\"0x0\","
"\"void_1234ptr\":\"0x1234\"}",
json);
}
TEST(TraceEventArgumentTest, FlatDictionary) {
std::unique_ptr<TracedValue> value(new TracedValue());
value->SetBoolean("bool", true);
value->SetDouble("double", 0.0);
value->SetInteger("int", 2014);
value->SetString("string", "string");
value->SetPointer("ptr", reinterpret_cast<void*>(0x1234));
std::string json = "PREFIX";
value->AppendAsTraceFormat(&json);
EXPECT_EQ(
"PREFIX{\"bool\":true,\"double\":0.0,\"int\":2014,\"string\":\"string\","
"\"ptr\":\"0x1234\"}",
json);
}
TEST(TraceEventArgumentTest, NoDotPathExpansion) {
std::unique_ptr<TracedValue> value(new TracedValue());
value->SetBoolean("bo.ol", true);
value->SetDouble("doub.le", 0.0);
value->SetInteger("in.t", 2014);
value->SetString("str.ing", "str.ing");
std::string json;
value->AppendAsTraceFormat(&json);
EXPECT_EQ(
"{\"bo.ol\":true,\"doub.le\":0.0,\"in.t\":2014,\"str.ing\":\"str.ing\"}",
json);
}
TEST(TraceEventArgumentTest, Hierarchy) {
std::unique_ptr<TracedValue> value(new TracedValue());
value->BeginArray("a1");
value->AppendInteger(1);
value->AppendBoolean(true);
value->BeginDictionary();
value->SetInteger("i2", 3);
value->EndDictionary();
value->EndArray();
value->SetBoolean("b0", true);
value->SetDouble("d0", 0.0);
value->BeginDictionary("dict1");
value->BeginDictionary("dict2");
value->SetBoolean("b2", false);
value->EndDictionary();
value->SetInteger("i1", 2014);
value->SetString("s1", "foo");
value->EndDictionary();
value->SetInteger("i0", 2014);
value->SetString("s0", "foo");
std::string json;
value->AppendAsTraceFormat(&json);
EXPECT_EQ(
"{\"a1\":[1,true,{\"i2\":3}],\"b0\":true,\"d0\":0.0,\"dict1\":{\"dict2\":"
"{\"b2\":false},\"i1\":2014,\"s1\":\"foo\"},\"i0\":2014,\"s0\":"
"\"foo\"}",
json);
}
TEST(TraceEventArgumentTest, LongStrings) {
std::string kLongString = "supercalifragilisticexpialidocious";
std::string kLongString2 = "0123456789012345678901234567890123456789";
char kLongString3[4096];
for (size_t i = 0; i < sizeof(kLongString3); ++i)
kLongString3[i] = 'a' + (i % 25);
kLongString3[sizeof(kLongString3) - 1] = '\0';
std::unique_ptr<TracedValue> value(new TracedValue());
value->SetString("a", "short");
value->SetString("b", kLongString);
value->BeginArray("c");
value->AppendString(kLongString2);
value->AppendString("");
value->BeginDictionary();
value->SetString("a", kLongString3);
value->EndDictionary();
value->EndArray();
std::string json;
value->AppendAsTraceFormat(&json);
EXPECT_EQ("{\"a\":\"short\",\"b\":\"" + kLongString + "\",\"c\":[\"" +
kLongString2 + "\",\"\",{\"a\":\"" + kLongString3 + "\"}]}",
json);
}
TEST(TraceEventArgumentTest, PassTracedValue) {
auto dict_value = std::make_unique<TracedValue>();
dict_value->SetInteger("a", 1);
auto nested_dict_value = std::make_unique<TracedValue>();
nested_dict_value->SetInteger("b", 2);
nested_dict_value->BeginArray("c");
nested_dict_value->AppendString("foo");
nested_dict_value->EndArray();
dict_value->SetValue("e", nested_dict_value.get());
// Check the merged result.
std::string json;
dict_value->AppendAsTraceFormat(&json);
EXPECT_EQ("{\"a\":1,\"e\":{\"b\":2,\"c\":[\"foo\"]}}", json);
// Check that the passed nestd dict was left unouthced.
json = "";
nested_dict_value->AppendAsTraceFormat(&json);
EXPECT_EQ("{\"b\":2,\"c\":[\"foo\"]}", json);
// And that it is still usable.
nested_dict_value->SetInteger("f", 3);
nested_dict_value->BeginDictionary("g");
nested_dict_value->EndDictionary();
json = "";
nested_dict_value->AppendAsTraceFormat(&json);
EXPECT_EQ("{\"b\":2,\"c\":[\"foo\"],\"f\":3,\"g\":{}}", json);
}
TEST(TraceEventArgumentTest, NanAndInfinityJSON) {
TracedValueJSON value;
value.SetDouble("nan", std::nan(""));
value.SetDouble("infinity", INFINITY);
value.SetDouble("negInfinity", -INFINITY);
std::string json;
value.AppendAsTraceFormat(&json);
EXPECT_EQ(
"{\"nan\":\"NaN\",\"infinity\":\"Infinity\","
"\"negInfinity\":\"-Infinity\"}",
json);
std::string formatted_json = value.ToFormattedJSON();
// Remove CR and LF to make the result platform-independent.
ReplaceChars(formatted_json, "\n\r", "", &formatted_json);
EXPECT_EQ(
"{"
" \"infinity\": \"Infinity\","
" \"nan\": \"NaN\","
" \"negInfinity\": \"-Infinity\""
"}",
formatted_json);
}
} // namespace trace_event
} // namespace base
|