blob: 3a33389a3e66dff92e5dfaeee7e4da8f71ebb3e5 [file] [log] [blame] [edit]
// Copyright 2016 the V8 project 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 "src/tracing/traced-value.h"
#include "test/cctest/cctest.h"
using v8::tracing::TracedValue;
TEST(FlatDictionary) {
auto value = TracedValue::Create();
value->SetInteger("int", 2014);
value->SetDouble("double", 0.0);
value->SetBoolean("bool", true);
value->SetString("string", "string");
std::string json = "PREFIX";
value->AppendAsTraceFormat(&json);
CHECK_EQ(
"PREFIX{\"int\":2014,\"double\":0,\"bool\":true,\"string\":"
"\"string\"}",
json);
}
TEST(NoDotPathExpansion) {
auto value = TracedValue::Create();
value->SetInteger("in.t", 2014);
value->SetDouble("doub.le", -20.25);
value->SetBoolean("bo.ol", true);
value->SetString("str.ing", "str.ing");
std::string json;
value->AppendAsTraceFormat(&json);
CHECK_EQ(
"{\"in.t\":2014,\"doub.le\":-20.25,\"bo.ol\":true,\"str.ing\":\"str."
"ing\"}",
json);
}
TEST(Hierarchy) {
auto value = TracedValue::Create();
value->SetInteger("i0", 2014);
value->BeginDictionary("dict1");
value->SetInteger("i1", 2014);
value->BeginDictionary("dict2");
value->SetBoolean("b2", false);
value->EndDictionary();
value->SetString("s1", "foo");
value->EndDictionary();
value->SetDouble("d0", 0.0);
value->SetDouble("d1", 10.5);
value->SetBoolean("b0", true);
value->BeginArray("a1");
value->AppendInteger(1);
value->AppendBoolean(true);
value->BeginDictionary();
value->SetInteger("i2", 3);
value->EndDictionary();
value->EndArray();
value->SetString("s0", "foo");
value->BeginArray("arr1");
value->BeginDictionary();
value->EndDictionary();
value->BeginArray();
value->EndArray();
value->BeginDictionary();
value->EndDictionary();
value->EndArray();
std::string json;
value->AppendAsTraceFormat(&json);
CHECK_EQ(
"{\"i0\":2014,\"dict1\":{\"i1\":2014,\"dict2\":{\"b2\":false},"
"\"s1\":\"foo\"},\"d0\":0,\"d1\":10.5,\"b0\":true,\"a1\":[1,true,{\"i2\":"
"3}],\"s0\":\"foo\",\"arr1\":[{},[],{}]}",
json);
}
TEST(LongStrings) {
std::string long_string = "supercalifragilisticexpialidocious";
std::string long_string2 = "0123456789012345678901234567890123456789";
char long_string3[4096];
for (size_t i = 0; i < sizeof(long_string3); ++i)
long_string3[i] = static_cast<char>('a' + (i % 26));
long_string3[sizeof(long_string3) - 1] = '\0';
auto value = TracedValue::Create();
value->SetString("a", "short");
value->SetString("b", long_string);
value->BeginArray("c");
value->AppendString(long_string2);
value->AppendString("");
value->BeginDictionary();
value->SetString("a", long_string3);
value->EndDictionary();
value->EndArray();
std::string json;
value->AppendAsTraceFormat(&json);
CHECK_EQ("{\"a\":\"short\",\"b\":\"" + long_string + "\",\"c\":[\"" +
long_string2 + "\",\"\",{\"a\":\"" + long_string3 + "\"}]}",
json);
}
TEST(Escaping) {
const char* string1 = "abc\"\'\\\\x\"y\'z\n\x09\x17";
std::string chars127;
for (int i = 1; i <= 127; ++i) {
chars127 += static_cast<char>(i);
}
auto value = TracedValue::Create();
value->SetString("a", string1);
value->SetString("b", chars127);
std::string json;
value->AppendAsTraceFormat(&json);
// Cannot use the expected value literal directly in CHECK_EQ
// as it fails to process the # character on Windows.
const char* expected =
"{\"a\":\"abc\\\"\'\\\\\\\\x\\\"y\'z\\n\\t\\u0017\",\"b\":"
"\"\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\u0008\\t\\n\\u000B"
"\\u000C\\u000D\\u000E\\u000F\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\"
"u0016\\u0017\\u0018\\u0019\\u001A\\u001B\\u001C\\u001D\\u001E\\u001F "
"!\\\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`"
"abcdefghijklmnopqrstuvwxyz{|}~\x7F\"}";
CHECK_EQ(expected, json);
}