11cb0ef41Sopenharmony_ci#include "tracing/traced_value.h"
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci#include <cmath>
41cb0ef41Sopenharmony_ci#include <cstddef>
51cb0ef41Sopenharmony_ci#include <cstring>
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#include "gtest/gtest.h"
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciusing node::tracing::TracedValue;
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciTEST(TracedValue, Object) {
121cb0ef41Sopenharmony_ci  auto traced_value = TracedValue::Create();
131cb0ef41Sopenharmony_ci  traced_value->SetString("a", "b");
141cb0ef41Sopenharmony_ci  traced_value->SetInteger("b", 1);
151cb0ef41Sopenharmony_ci  traced_value->SetDouble("c", 1.234);
161cb0ef41Sopenharmony_ci  traced_value->SetDouble("d", NAN);
171cb0ef41Sopenharmony_ci  traced_value->SetDouble("e", INFINITY);
181cb0ef41Sopenharmony_ci  traced_value->SetDouble("f", -INFINITY);
191cb0ef41Sopenharmony_ci  traced_value->SetDouble("g", 1.23e7);
201cb0ef41Sopenharmony_ci  traced_value->SetBoolean("h", false);
211cb0ef41Sopenharmony_ci  traced_value->SetBoolean("i", true);
221cb0ef41Sopenharmony_ci  traced_value->SetNull("j");
231cb0ef41Sopenharmony_ci  traced_value->BeginDictionary("k");
241cb0ef41Sopenharmony_ci  traced_value->SetString("l", "m");
251cb0ef41Sopenharmony_ci  traced_value->EndDictionary();
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  std::string string;
281cb0ef41Sopenharmony_ci  traced_value->AppendAsTraceFormat(&string);
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  static const char* check = "{\"a\":\"b\",\"b\":1,\"c\":1.234,\"d\":\"NaN\","
311cb0ef41Sopenharmony_ci                             "\"e\":\"Infinity\",\"f\":\"-Infinity\",\"g\":"
321cb0ef41Sopenharmony_ci                             "1.23e+07,\"h\":false,\"i\":true,\"j\":null,\"k\":"
331cb0ef41Sopenharmony_ci                             "{\"l\":\"m\"}}";
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  EXPECT_EQ(check, string);
361cb0ef41Sopenharmony_ci}
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ciTEST(TracedValue, Array) {
391cb0ef41Sopenharmony_ci  auto traced_value = TracedValue::CreateArray();
401cb0ef41Sopenharmony_ci  traced_value->AppendString("a");
411cb0ef41Sopenharmony_ci  traced_value->AppendInteger(1);
421cb0ef41Sopenharmony_ci  traced_value->AppendDouble(1.234);
431cb0ef41Sopenharmony_ci  traced_value->AppendDouble(NAN);
441cb0ef41Sopenharmony_ci  traced_value->AppendDouble(INFINITY);
451cb0ef41Sopenharmony_ci  traced_value->AppendDouble(-INFINITY);
461cb0ef41Sopenharmony_ci  traced_value->AppendDouble(1.23e7);
471cb0ef41Sopenharmony_ci  traced_value->AppendBoolean(false);
481cb0ef41Sopenharmony_ci  traced_value->AppendBoolean(true);
491cb0ef41Sopenharmony_ci  traced_value->AppendNull();
501cb0ef41Sopenharmony_ci  traced_value->BeginDictionary();
511cb0ef41Sopenharmony_ci  traced_value->BeginArray("foo");
521cb0ef41Sopenharmony_ci  traced_value->EndArray();
531cb0ef41Sopenharmony_ci  traced_value->EndDictionary();
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  std::string string;
561cb0ef41Sopenharmony_ci  traced_value->AppendAsTraceFormat(&string);
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  static const char* check = "[\"a\",1,1.234,\"NaN\",\"Infinity\","
591cb0ef41Sopenharmony_ci                             "\"-Infinity\",1.23e+07,false,true,null,"
601cb0ef41Sopenharmony_ci                             "{\"foo\":[]}]";
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  EXPECT_EQ(check, string);
631cb0ef41Sopenharmony_ci}
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci#define UTF8_SEQUENCE "1" "\xE2\x82\xAC" "23\"\x01\b\f\n\r\t\\"
661cb0ef41Sopenharmony_ci#if defined(NODE_HAVE_I18N_SUPPORT)
671cb0ef41Sopenharmony_ci# define UTF8_RESULT                                                          \
681cb0ef41Sopenharmony_ci  "\"1\\u20AC23\\\"\\u0001\\b\\f\\n\\r\\t\\\\\""
691cb0ef41Sopenharmony_ci#else
701cb0ef41Sopenharmony_ci# define UTF8_RESULT                                                          \
711cb0ef41Sopenharmony_ci  "\"1\\u00E2\\u0082\\u00AC23\\\"\\u0001\\b\\f\\n\\r\\t\\\\\""
721cb0ef41Sopenharmony_ci#endif
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ciTEST(TracedValue, EscapingObject) {
751cb0ef41Sopenharmony_ci  auto traced_value = TracedValue::Create();
761cb0ef41Sopenharmony_ci  traced_value->SetString("a", UTF8_SEQUENCE);
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci  std::string string;
791cb0ef41Sopenharmony_ci  traced_value->AppendAsTraceFormat(&string);
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci  static const char* check = "{\"a\":" UTF8_RESULT "}";
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci  EXPECT_EQ(check, string);
841cb0ef41Sopenharmony_ci}
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ciTEST(TracedValue, EscapingArray) {
871cb0ef41Sopenharmony_ci  auto traced_value = TracedValue::CreateArray();
881cb0ef41Sopenharmony_ci  traced_value->AppendString(UTF8_SEQUENCE);
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci  std::string string;
911cb0ef41Sopenharmony_ci  traced_value->AppendAsTraceFormat(&string);
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci  static const char* check = "[" UTF8_RESULT "]";
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci  EXPECT_EQ(check, string);
961cb0ef41Sopenharmony_ci}
97