From f0f460556adf87edf04d982ca194b8e71273fd70 Mon Sep 17 00:00:00 2001 From: David Vacca Date: Sun, 16 Sep 2018 23:46:06 -0700 Subject: [PATCH] Introduce toDynamic conversion for ParagraphAttributes class Summary: This diff introduces a way to convert ParagrapgAttributes object to dynamic objects Reviewed By: shergin Differential Revision: D9798895 fbshipit-source-id: 5b139a079c8681749c3e13938482b47e4153019d --- ReactCommon/fabric/attributedstring/BUCK | 2 +- .../fabric/attributedstring/conversions.h | 11 ++++++ .../tests/ParagraphAttributesTest.cpp | 37 +++++++++++++++++++ .../fabric/core/shadownode/LocalData.h | 1 - 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 ReactCommon/fabric/attributedstring/tests/ParagraphAttributesTest.cpp diff --git a/ReactCommon/fabric/attributedstring/BUCK b/ReactCommon/fabric/attributedstring/BUCK index 27e0692e7b3..e767d40ee9d 100644 --- a/ReactCommon/fabric/attributedstring/BUCK +++ b/ReactCommon/fabric/attributedstring/BUCK @@ -73,7 +73,7 @@ fb_xplat_cxx_test( "-Wall", ], contacts = ["oncall+react_native@xmail.facebook.com"], - platforms = APPLE, + platforms = (ANDROID, APPLE), deps = [ "xplat//folly:molly", "xplat//third-party/gmock:gtest", diff --git a/ReactCommon/fabric/attributedstring/conversions.h b/ReactCommon/fabric/attributedstring/conversions.h index e864eb6f057..c19a70d7ffc 100644 --- a/ReactCommon/fabric/attributedstring/conversions.h +++ b/ReactCommon/fabric/attributedstring/conversions.h @@ -7,6 +7,7 @@ #pragma once +#include #include #include #include @@ -188,5 +189,15 @@ inline std::string toString(const TextDecorationLinePattern &textDecorationLineP } } +inline folly::dynamic toDynamic(const ParagraphAttributes ¶graphAttributes) { + auto values = folly::dynamic::object(); + values("maximumNumberOfLines", paragraphAttributes.maximumNumberOfLines); + values("ellipsizeMode", toString(paragraphAttributes.ellipsizeMode)); + values("adjustsFontSizeToFit", paragraphAttributes.adjustsFontSizeToFit); + values("minimumFontSize", paragraphAttributes.minimumFontSize); + values("maximumFontSize", paragraphAttributes.maximumFontSize); + return values; +} + } // namespace react } // namespace facebook diff --git a/ReactCommon/fabric/attributedstring/tests/ParagraphAttributesTest.cpp b/ReactCommon/fabric/attributedstring/tests/ParagraphAttributesTest.cpp new file mode 100644 index 00000000000..162ca8a2aee --- /dev/null +++ b/ReactCommon/fabric/attributedstring/tests/ParagraphAttributesTest.cpp @@ -0,0 +1,37 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include + +#include +#include +#include +#include +#include + +namespace facebook { +namespace react { + + +TEST(ParagraphAttributesTest, testToDynamic) { + auto paragraphAttributes = ParagraphAttributes(); + paragraphAttributes.maximumNumberOfLines = 2; + paragraphAttributes.adjustsFontSizeToFit = false; + paragraphAttributes.ellipsizeMode = EllipsizeMode::Middle; + paragraphAttributes.minimumFontSize = 10; + paragraphAttributes.maximumFontSize = 20; + + auto result = toDynamic(paragraphAttributes); + assert(result["maximumNumberOfLines"] == paragraphAttributes.maximumNumberOfLines); + assert(result["adjustsFontSizeToFit"] == paragraphAttributes.adjustsFontSizeToFit); + assert(result["ellipsizeMode"] == toString(paragraphAttributes.ellipsizeMode)); + assert(result["minimumFontSize"] == paragraphAttributes.minimumFontSize); + assert(result["maximumFontSize"] == paragraphAttributes.maximumFontSize); +} + +} +} diff --git a/ReactCommon/fabric/core/shadownode/LocalData.h b/ReactCommon/fabric/core/shadownode/LocalData.h index 2463f73bb14..baccf1cc22f 100644 --- a/ReactCommon/fabric/core/shadownode/LocalData.h +++ b/ReactCommon/fabric/core/shadownode/LocalData.h @@ -29,7 +29,6 @@ class LocalData: public Sealable, public DebugStringConvertible { - // Nothing. }; } // namespace react