From 460370b7d311df829ae76a47245ef2b59726378b Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Tue, 29 Jan 2019 10:30:14 -0800 Subject: [PATCH] More readable instrumentation test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: @public Evolves setup and helpers in `InstrumentationTest` to avoid repetition across test cases, and allow for more readable test by hiding default values where they don’t matter. Reviewed By: SidharthGuglani Differential Revision: D13839521 fbshipit-source-id: 7f7ad49fec84e0bbb09ad746dd8c28bd34de25b2 --- tests/InstrumentationTest.cpp | 81 +++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 33 deletions(-) diff --git a/tests/InstrumentationTest.cpp b/tests/InstrumentationTest.cpp index 42e36efa..cb02bd99 100644 --- a/tests/InstrumentationTest.cpp +++ b/tests/InstrumentationTest.cpp @@ -14,6 +14,9 @@ #include #include +bool operator==(const YGMarkerLayoutData&, const YGMarkerLayoutData&); +void PrintTo(const YGMarkerLayoutData, std::ostream*); + namespace facebook { namespace yoga { namespace marker { @@ -44,16 +47,19 @@ struct MarkerTest : public ::testing::Test { static void* startMarker(YGMarker, YGNodeRef, YGMarkerData); static void endMarker(YGMarker, YGNodeRef, YGMarkerData, void*); - static uniquePtr makeConfig(); - static uniquePtr makeNode(uniquePtr&); - static uniquePtr makeNode(uniquePtr&, uniquePtr&); + uniquePtr makeNode(); + uniquePtr addChild(uniquePtr& owner); + static void calculateLayout( + uniquePtr& node, + float width = YGUndefined, + float height = YGUndefined); void SetUp() override; + uniquePtr config; }; TEST_F(MarkerTest, marker_start_works) { - auto config = makeConfig(); - auto root = makeNode(config); + auto root = makeNode(); decltype(MarkerSection::data)* dataAddress; { @@ -70,8 +76,7 @@ TEST_F(MarkerTest, marker_start_works) { } TEST_F(MarkerTest, marker_end_works) { - auto config = makeConfig(); - auto root = makeNode(config); + auto root = makeNode(); { MarkerSection marker{root.get()}; } @@ -90,35 +95,34 @@ TEST_F(MarkerTest, marker_end_works) { } TEST_F(MarkerTest, layout_marker) { - auto config = makeConfig(); - auto root = makeNode(config); + auto root = makeNode(); - YGNodeCalculateLayout(root.get(), YGUndefined, YGUndefined, YGDirectionLTR); + calculateLayout(root); ASSERT_EQ(markerCookie.start.marker, YGMarkerLayout); ASSERT_EQ(markerCookie.start.node, root.get()); } TEST_F(MarkerTest, layout_marker_counts_single_node_layout) { - auto config = makeConfig(); - auto root = makeNode(config); + auto root = makeNode(); - YGNodeCalculateLayout(root.get(), YGUndefined, YGUndefined, YGDirectionLTR); + calculateLayout(root); - ASSERT_EQ(markerCookie.end.markerData.layout.layouts, 1); - ASSERT_EQ(markerCookie.end.markerData.layout.measures, 0); + ASSERT_EQ( + markerCookie.end.markerData.layout, + (YGMarkerLayoutData{.layouts = 1, .measures = 0})); } TEST_F(MarkerTest, layout_marker_counts_multi_node_layout) { - auto config = makeConfig(); - auto root = makeNode(config); - auto childA = makeNode(config, root); - auto childB = makeNode(config, root); + auto root = makeNode(); + auto childA = addChild(root); + auto childB = addChild(root); - YGNodeCalculateLayout(root.get(), YGUndefined, YGUndefined, YGDirectionLTR); + calculateLayout(root); - ASSERT_EQ(markerCookie.end.markerData.layout.layouts, 3); - ASSERT_EQ(markerCookie.end.markerData.layout.measures, 4); + ASSERT_EQ( + markerCookie.end.markerData.layout, + (YGMarkerLayoutData{.layouts = 3, .measures = 4})); } void* MarkerTest::startMarker( @@ -142,27 +146,29 @@ void MarkerTest::endMarker( }; } -uniquePtr MarkerTest::makeConfig() { - auto c = uniquePtr{YGConfigNew(), &YGConfigFree}; - YGConfigSetMarkerCallbacks(c.get(), {startMarker, endMarker}); - return c; -} - -uniquePtr MarkerTest::makeNode(uniquePtr& config) { +uniquePtr MarkerTest::makeNode() { auto n = uniquePtr{YGNodeNewWithConfig(config.get()), &YGNodeFree}; return n; } -uniquePtr MarkerTest::makeNode( - uniquePtr& config, - uniquePtr& owner) { - auto n = makeNode(config); +uniquePtr MarkerTest::addChild(uniquePtr& owner) { + auto n = makeNode(); YGNodeInsertChild(owner.get(), n.get(), YGNodeGetChildCount(owner.get())); return n; } +void MarkerTest::calculateLayout( + uniquePtr& node, + float width, + float height) { + YGNodeCalculateLayout(node.get(), width, height, YGDirectionLTR); +} + void MarkerTest::SetUp() { markerCookie = {}; + + config = uniquePtr{YGConfigNew(), &YGConfigFree}; + YGConfigSetMarkerCallbacks(config.get(), {startMarker, endMarker}); } decltype(MarkerTest::markerCookie) MarkerTest::markerCookie = {}; @@ -171,3 +177,12 @@ decltype(MarkerTest::markerCookie) MarkerTest::markerCookie = {}; } // namespace marker } // namespace yoga } // namespace facebook + +bool operator==(const YGMarkerLayoutData& lhs, const YGMarkerLayoutData& rhs) { + return lhs.layouts == rhs.layouts && lhs.measures == rhs.measures; +} + +void PrintTo(const YGMarkerLayoutData data, std::ostream* os) { + *os << "YGMarkerLayoutData{ layouts = " << data.layouts + << ", measures = " << data.measures << " }"; +}