diff --git a/ReactCommon/fabric/debug/DebugStringConvertible.cpp b/ReactCommon/fabric/debug/DebugStringConvertible.cpp index 20c182c4833..eab3557a072 100644 --- a/ReactCommon/fabric/debug/DebugStringConvertible.cpp +++ b/ReactCommon/fabric/debug/DebugStringConvertible.cpp @@ -13,33 +13,41 @@ namespace react { #if RN_DEBUG_STRING_CONVERTIBLE std::string DebugStringConvertible::getDebugChildrenDescription( - DebugStringConvertibleOptions options, - int depth) const { - if (depth >= options.maximumDepth) { + DebugStringConvertibleOptions options) const { + if (options.depth >= options.maximumDepth) { return ""; } - std::string childrenString = ""; + options.depth++; + + auto trailing = options.format ? std::string{"\n"} : std::string{""}; + auto childrenString = std::string{""}; for (auto child : getDebugChildren()) { if (!child) { continue; } - childrenString += child->getDebugDescription(options, depth + 1); + childrenString += child->getDebugDescription(options) + trailing; + } + + if (!childrenString.empty() && !trailing.empty()) { + // Removing trailing fragment. + childrenString.erase(childrenString.end() - 1); } return childrenString; } std::string DebugStringConvertible::getDebugPropsDescription( - DebugStringConvertibleOptions options, - int depth) const { - if (depth >= options.maximumDepth) { + DebugStringConvertibleOptions options) const { + if (options.depth >= options.maximumDepth) { return ""; } - std::string propsString = ""; + options.depth++; + + auto propsString = std::string{""}; for (auto prop : getDebugProps()) { if (!prop) { @@ -48,7 +56,7 @@ std::string DebugStringConvertible::getDebugPropsDescription( auto name = prop->getDebugName(); auto value = prop->getDebugValue(); - auto children = prop->getDebugPropsDescription(options, depth + 1); + auto children = prop->getDebugPropsDescription(options); auto valueAndChildren = value + (children.empty() ? "" : "(" + children + ")"); propsString += @@ -64,22 +72,22 @@ std::string DebugStringConvertible::getDebugPropsDescription( } std::string DebugStringConvertible::getDebugDescription( - DebugStringConvertibleOptions options, - int depth) const { + DebugStringConvertibleOptions options) const { auto nameString = getDebugName(); auto valueString = getDebugValue(); - auto childrenString = getDebugChildrenDescription(options, depth); - auto propsString = getDebugPropsDescription(options, depth); + auto childrenString = getDebugChildrenDescription(options); + auto propsString = getDebugPropsDescription(options); - auto leading = options.format ? std::string(depth * 2, ' ') : std::string{""}; + auto leading = + options.format ? std::string(options.depth * 2, ' ') : std::string{""}; auto trailing = options.format ? std::string{"\n"} : std::string{""}; return leading + "<" + nameString + (valueString.empty() ? "" : "=" + valueString) + (propsString.empty() ? "" : " " + propsString) + - (childrenString.empty() ? "/>" + trailing - : ">" + trailing + childrenString + leading + - "" + trailing); + (childrenString.empty() ? "/>" + : ">" + trailing + childrenString + trailing + + leading + ""); } std::string DebugStringConvertible::getDebugName() const { diff --git a/ReactCommon/fabric/debug/DebugStringConvertible.h b/ReactCommon/fabric/debug/DebugStringConvertible.h index 2c38093f177..e03ec6f60f0 100644 --- a/ReactCommon/fabric/debug/DebugStringConvertible.h +++ b/ReactCommon/fabric/debug/DebugStringConvertible.h @@ -30,6 +30,7 @@ using SharedDebugStringConvertibleList = struct DebugStringConvertibleOptions { bool format{true}; + int depth{0}; int maximumDepth{INT_MAX}; }; @@ -63,17 +64,14 @@ class DebugStringConvertible { // Default implementation returns a description of the subtree // rooted at this node, represented in XML-like format. virtual std::string getDebugDescription( - DebugStringConvertibleOptions options = {}, - int depth = 0) const; + DebugStringConvertibleOptions options = {}) const; // Do same as `getDebugDescription` but return only *children* and // *properties* parts (which are used in `getDebugDescription`). virtual std::string getDebugPropsDescription( - DebugStringConvertibleOptions options = {}, - int depth = 0) const; + DebugStringConvertibleOptions options = {}) const; virtual std::string getDebugChildrenDescription( - DebugStringConvertibleOptions options = {}, - int depth = 0) const; + DebugStringConvertibleOptions options = {}) const; }; #else diff --git a/ReactCommon/fabric/debug/tests/DebugStringConvertibleTest.cpp b/ReactCommon/fabric/debug/tests/DebugStringConvertibleTest.cpp index 99a1e43a365..edeafdb174b 100644 --- a/ReactCommon/fabric/debug/tests/DebugStringConvertibleTest.cpp +++ b/ReactCommon/fabric/debug/tests/DebugStringConvertibleTest.cpp @@ -19,7 +19,7 @@ TEST(DebugStringConvertibleTest, handleSimpleNode) { ASSERT_STREQ(item->getDebugName().c_str(), "View"); ASSERT_STREQ(item->getDebugValue().c_str(), "hello"); - ASSERT_STREQ(item->getDebugDescription().c_str(), "\n"); + ASSERT_STREQ(item->getDebugDescription().c_str(), ""); } TEST(DebugStringConvertibleTest, handleSimpleNodeWithProps) { @@ -31,7 +31,7 @@ TEST(DebugStringConvertibleTest, handleSimpleNodeWithProps) { ASSERT_STREQ(item->getDebugName().c_str(), "View"); ASSERT_STREQ(item->getDebugValue().c_str(), "hello"); - ASSERT_STREQ(item->getDebugDescription().c_str(), "\n"); + ASSERT_STREQ(item->getDebugDescription().c_str(), ""); } TEST(DebugStringConvertibleTest, handleSimpleNodeWithChildren) { @@ -45,7 +45,7 @@ TEST(DebugStringConvertibleTest, handleSimpleNodeWithChildren) { ASSERT_STREQ(item->getDebugValue().c_str(), "hello"); ASSERT_STREQ( item->getDebugDescription().c_str(), - "\n \n\n"); + "\n \n"); } TEST(DebugStringConvertibleTest, handleNestedNode) { @@ -61,7 +61,7 @@ TEST(DebugStringConvertibleTest, handleNestedNode) { ASSERT_STREQ(item->getDebugValue().c_str(), "hello"); ASSERT_STREQ( item->getDebugDescription().c_str(), - "\n \n\n"); + "\n \n"); } TEST(DebugStringConvertibleTest, handleNodeWithComplexProps) { @@ -80,5 +80,5 @@ TEST(DebugStringConvertibleTest, handleNodeWithComplexProps) { ASSERT_STREQ(item->getDebugValue().c_str(), "hello"); ASSERT_STREQ( item->getDebugDescription().c_str(), - "\n"); + ""); }