OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project 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 "src/tracing/traced-value.h" |
| 6 #include "test/cctest/cctest.h" |
| 7 |
| 8 using v8::tracing::TracedValue; |
| 9 |
| 10 TEST(FlatDictionary) { |
| 11 auto value = TracedValue::Create(); |
| 12 value->SetInteger("int", 2014); |
| 13 value->SetDouble("double", 0.0); |
| 14 value->SetBoolean("bool", true); |
| 15 value->SetString("string", "string"); |
| 16 std::string json = "PREFIX"; |
| 17 value->AppendAsTraceFormat(&json); |
| 18 CHECK_EQ( |
| 19 "PREFIX{\"int\":2014,\"double\":0.000000,\"bool\":true,\"string\":" |
| 20 "\"string\"}", |
| 21 json); |
| 22 } |
| 23 |
| 24 TEST(NoDotPathExpansion) { |
| 25 auto value = TracedValue::Create(); |
| 26 value->SetInteger("in.t", 2014); |
| 27 value->SetDouble("doub.le", 0.0); |
| 28 value->SetBoolean("bo.ol", true); |
| 29 value->SetString("str.ing", "str.ing"); |
| 30 std::string json; |
| 31 value->AppendAsTraceFormat(&json); |
| 32 CHECK_EQ( |
| 33 "{\"in.t\":2014,\"doub.le\":0.000000,\"bo.ol\":true,\"str.ing\":\"str." |
| 34 "ing\"}", |
| 35 json); |
| 36 } |
| 37 |
| 38 TEST(Hierarchy) { |
| 39 auto value = TracedValue::Create(); |
| 40 value->SetInteger("i0", 2014); |
| 41 value->BeginDictionary("dict1"); |
| 42 value->SetInteger("i1", 2014); |
| 43 value->BeginDictionary("dict2"); |
| 44 value->SetBoolean("b2", false); |
| 45 value->EndDictionary(); |
| 46 value->SetString("s1", "foo"); |
| 47 value->EndDictionary(); |
| 48 value->SetDouble("d0", 0.0); |
| 49 value->SetBoolean("b0", true); |
| 50 value->BeginArray("a1"); |
| 51 value->AppendInteger(1); |
| 52 value->AppendBoolean(true); |
| 53 value->BeginDictionary(); |
| 54 value->SetInteger("i2", 3); |
| 55 value->EndDictionary(); |
| 56 value->EndArray(); |
| 57 value->SetString("s0", "foo"); |
| 58 |
| 59 value->BeginArray("arr1"); |
| 60 value->BeginDictionary(); |
| 61 value->EndDictionary(); |
| 62 value->BeginArray(); |
| 63 value->EndArray(); |
| 64 value->BeginDictionary(); |
| 65 value->EndDictionary(); |
| 66 value->EndArray(); |
| 67 |
| 68 std::string json; |
| 69 value->AppendAsTraceFormat(&json); |
| 70 CHECK_EQ( |
| 71 "{\"i0\":2014,\"dict1\":{\"i1\":2014,\"dict2\":{\"b2\":false}," |
| 72 "\"s1\":\"foo\"},\"d0\":0.000000,\"b0\":true,\"a1\":[1,true,{\"i2\":3}]," |
| 73 "\"s0\":\"foo\",\"arr1\":[{},[],{}]}", |
| 74 json); |
| 75 } |
| 76 |
| 77 TEST(LongStrings) { |
| 78 std::string long_string = "supercalifragilisticexpialidocious"; |
| 79 std::string long_string2 = "0123456789012345678901234567890123456789"; |
| 80 char long_string3[4096]; |
| 81 for (size_t i = 0; i < sizeof(long_string3); ++i) |
| 82 long_string3[i] = static_cast<char>('a' + (i % 26)); |
| 83 long_string3[sizeof(long_string3) - 1] = '\0'; |
| 84 |
| 85 auto value = TracedValue::Create(); |
| 86 value->SetString("a", "short"); |
| 87 value->SetString("b", long_string); |
| 88 value->BeginArray("c"); |
| 89 value->AppendString(long_string2); |
| 90 value->AppendString(""); |
| 91 value->BeginDictionary(); |
| 92 value->SetString("a", long_string3); |
| 93 value->EndDictionary(); |
| 94 value->EndArray(); |
| 95 |
| 96 std::string json; |
| 97 value->AppendAsTraceFormat(&json); |
| 98 CHECK_EQ("{\"a\":\"short\",\"b\":\"" + long_string + "\",\"c\":[\"" + |
| 99 long_string2 + "\",\"\",{\"a\":\"" + long_string3 + "\"}]}", |
| 100 json); |
| 101 } |
| 102 |
| 103 TEST(Escaping) { |
| 104 const char* string1 = "abc\"\'\\\\x\"y\'z\n\x09\x17"; |
| 105 std::string chars127; |
| 106 for (int i = 1; i <= 127; ++i) { |
| 107 chars127 += static_cast<char>(i); |
| 108 } |
| 109 auto value = TracedValue::Create(); |
| 110 value->SetString("a", string1); |
| 111 value->SetString("b", chars127); |
| 112 |
| 113 std::string json; |
| 114 value->AppendAsTraceFormat(&json); |
| 115 // Cannot use the expected value literal directly in CHECK_EQ |
| 116 // as it fails to process # character on Windows. |
| 117 const char* expected = |
| 118 "{\"a\":\"abc\\\"\'\\\\\\\\x\\\"y\'z\\n\\t\\u0017\",\"b\":" |
| 119 "\"\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\u0008\\t\\n\\u000B" |
| 120 "\\u000C\\u000D\\u000E\\u000F\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\" |
| 121 "u0016\\u0017\\u0018\\u0019\\u001A\\u001B\\u001C\\u001D\\u001E\\u001F " |
| 122 "!\\\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`" |
| 123 "abcdefghijklmnopqrstuvwxyz{|}~\177\"}"; |
| 124 CHECK_EQ(expected, json); |
| 125 } |
OLD | NEW |