Make YGNode as c++ struct with properties exposed through accessors

Summary: Moved c implementation of `YGNode` to C++ struct. Not moving to C++ class as the React Classes dependent on `Yoga.h` assume it to be C. Thats why keeping `Yoga.h` C compatible. Sorry for the long diff, didn't thought that it will turn out to be this much big.Will keep an eye on number of lines next time 😉

Reviewed By: emilsjolander

Differential Revision: D6592257

fbshipit-source-id: 641e8b9462ad00731a094511f9f5608b23a6bb21
This commit is contained in:
Pritesh Nandgaonkar
2017-12-19 11:18:00 -08:00
committed by Facebook Github Bot
parent dbf6a12134
commit fbd332dee8
16 changed files with 1957 additions and 1091 deletions

View File

@@ -8,6 +8,7 @@
*/
#include <gtest/gtest.h>
#include <yoga/YGNode.h>
#include <yoga/Yoga.h>
static YGSize _measure(YGNodeRef node,
@@ -15,7 +16,7 @@ static YGSize _measure(YGNodeRef node,
YGMeasureMode widthMode,
float height,
YGMeasureMode heightMode) {
int *measureCount = (int *) YGNodeGetContext(node);
int* measureCount = (int*)node->getContext();
if (measureCount) {
(*measureCount)++;
}
@@ -46,7 +47,7 @@ static YGSize _measure_assert_negative(YGNodeRef node,
YGMeasureMode heightMode) {
EXPECT_GE(width, 0);
EXPECT_GE(height, 0);
return YGSize{
.width = 0, .height = 0,
};
@@ -60,8 +61,8 @@ TEST(YogaTest, dont_measure_single_grow_shrink_child) {
int measureCount = 0;
const YGNodeRef root_child0 = YGNodeNew();
YGNodeSetContext(root_child0, &measureCount);
YGNodeSetMeasureFunc(root_child0, _measure);
root_child0->setContext(&measureCount);
root_child0->setMeasureFunc(_measure);
YGNodeStyleSetFlexGrow(root_child0, 1);
YGNodeStyleSetFlexShrink(root_child0, 1);
YGNodeInsertChild(root, root_child0, 0);
@@ -83,8 +84,8 @@ TEST(YogaTest, measure_absolute_child_with_no_constraints) {
const YGNodeRef root_child0_child0 = YGNodeNew();
YGNodeStyleSetPositionType(root_child0_child0, YGPositionTypeAbsolute);
YGNodeSetContext(root_child0_child0, &measureCount);
YGNodeSetMeasureFunc(root_child0_child0, _measure);
root_child0_child0->setContext(&measureCount);
root_child0_child0->setMeasureFunc(_measure);
YGNodeInsertChild(root_child0, root_child0_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
@@ -103,8 +104,8 @@ TEST(YogaTest, dont_measure_when_min_equals_max) {
int measureCount = 0;
const YGNodeRef root_child0 = YGNodeNew();
YGNodeSetContext(root_child0, &measureCount);
YGNodeSetMeasureFunc(root_child0, _measure);
root_child0->setContext(&measureCount);
root_child0->setMeasureFunc(_measure);
YGNodeStyleSetMinWidth(root_child0, 10);
YGNodeStyleSetMaxWidth(root_child0, 10);
YGNodeStyleSetMinHeight(root_child0, 10);
@@ -131,8 +132,8 @@ TEST(YogaTest, dont_measure_when_min_equals_max_percentages) {
int measureCount = 0;
const YGNodeRef root_child0 = YGNodeNew();
YGNodeSetContext(root_child0, &measureCount);
YGNodeSetMeasureFunc(root_child0, _measure);
root_child0->setContext(&measureCount);
root_child0->setMeasureFunc(_measure);
YGNodeStyleSetMinWidthPercent(root_child0, 10);
YGNodeStyleSetMaxWidthPercent(root_child0, 10);
YGNodeStyleSetMinHeightPercent(root_child0, 10);
@@ -157,7 +158,7 @@ TEST(YogaTest, measure_nodes_with_margin_auto_and_stretch) {
YGNodeStyleSetHeight(root, 500);
const YGNodeRef root_child0 = YGNodeNew();
YGNodeSetMeasureFunc(root_child0, _measure);
root_child0->setMeasureFunc(_measure);
YGNodeStyleSetMarginAuto(root_child0, YGEdgeLeft);
YGNodeInsertChild(root, root_child0, 0);
@@ -180,8 +181,8 @@ TEST(YogaTest, dont_measure_when_min_equals_max_mixed_width_percent) {
int measureCount = 0;
const YGNodeRef root_child0 = YGNodeNew();
YGNodeSetContext(root_child0, &measureCount);
YGNodeSetMeasureFunc(root_child0, _measure);
root_child0->setContext(&measureCount);
root_child0->setMeasureFunc(_measure);
YGNodeStyleSetMinWidthPercent(root_child0, 10);
YGNodeStyleSetMaxWidthPercent(root_child0, 10);
YGNodeStyleSetMinHeight(root_child0, 10);
@@ -208,8 +209,8 @@ TEST(YogaTest, dont_measure_when_min_equals_max_mixed_height_percent) {
int measureCount = 0;
const YGNodeRef root_child0 = YGNodeNew();
YGNodeSetContext(root_child0, &measureCount);
YGNodeSetMeasureFunc(root_child0, _measure);
root_child0->setContext(&measureCount);
root_child0->setMeasureFunc(_measure);
YGNodeStyleSetMinWidth(root_child0, 10);
YGNodeStyleSetMaxWidth(root_child0, 10);
YGNodeStyleSetMinHeightPercent(root_child0, 10);
@@ -233,7 +234,7 @@ TEST(YogaTest, measure_enough_size_should_be_in_single_line) {
const YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetAlignSelf(root_child0, YGAlignFlexStart);
YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text);
root_child0->setMeasureFunc(_simulate_wrapping_text);
YGNodeInsertChild(root, root_child0, 0);
@@ -251,8 +252,8 @@ TEST(YogaTest, measure_not_enough_size_should_wrap) {
const YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetAlignSelf(root_child0, YGAlignFlexStart);
YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text);
// YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text);
root_child0->setMeasureFunc(_simulate_wrapping_text);
YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
@@ -274,8 +275,8 @@ TEST(YogaTest, measure_zero_space_should_grow) {
const YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetFlexDirection(root_child0, YGFlexDirectionColumn);
YGNodeStyleSetPadding(root_child0, YGEdgeAll, 100);
YGNodeSetContext(root_child0, &measureCount);
YGNodeSetMeasureFunc(root_child0, _measure);
root_child0->setContext(&measureCount);
root_child0->setMeasureFunc(_measure);
YGNodeInsertChild(root, root_child0, 0);
@@ -300,7 +301,8 @@ TEST(YogaTest, measure_flex_direction_row_and_padding) {
YGNodeStyleSetHeight(root, 50);
const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text);
root_child0->setMeasureFunc(_simulate_wrapping_text);
// YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text);
YGNodeInsertChild(root, root_child0, 0);
const YGNodeRef root_child1 = YGNodeNewWithConfig(config);
@@ -339,7 +341,8 @@ TEST(YogaTest, measure_flex_direction_column_and_padding) {
YGNodeStyleSetHeight(root, 50);
const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text);
root_child0->setMeasureFunc(_simulate_wrapping_text);
// YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text);
YGNodeInsertChild(root, root_child0, 0);
const YGNodeRef root_child1 = YGNodeNewWithConfig(config);
@@ -378,7 +381,8 @@ TEST(YogaTest, measure_flex_direction_row_no_padding) {
YGNodeStyleSetHeight(root, 50);
const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text);
// YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text);
root_child0->setMeasureFunc(_simulate_wrapping_text);
YGNodeInsertChild(root, root_child0, 0);
const YGNodeRef root_child1 = YGNodeNewWithConfig(config);
@@ -418,7 +422,7 @@ TEST(YogaTest, measure_flex_direction_row_no_padding_align_items_flexstart) {
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text);
root_child0->setMeasureFunc(_simulate_wrapping_text);
YGNodeInsertChild(root, root_child0, 0);
const YGNodeRef root_child1 = YGNodeNewWithConfig(config);
@@ -457,7 +461,7 @@ TEST(YogaTest, measure_with_fixed_size) {
YGNodeStyleSetHeight(root, 50);
const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text);
root_child0->setMeasureFunc(_simulate_wrapping_text);
YGNodeStyleSetWidth(root_child0, 10);
YGNodeStyleSetHeight(root_child0, 10);
YGNodeInsertChild(root, root_child0, 0);
@@ -498,7 +502,7 @@ TEST(YogaTest, measure_with_flex_shrink) {
YGNodeStyleSetHeight(root, 50);
const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text);
root_child0->setMeasureFunc(_simulate_wrapping_text);
YGNodeStyleSetFlexShrink(root_child0, 1);
YGNodeInsertChild(root, root_child0, 0);
@@ -537,7 +541,7 @@ TEST(YogaTest, measure_no_padding) {
YGNodeStyleSetHeight(root, 50);
const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeSetMeasureFunc(root_child0, _simulate_wrapping_text);
root_child0->setMeasureFunc(_simulate_wrapping_text);
YGNodeStyleSetFlexShrink(root_child0, 1);
YGNodeInsertChild(root, root_child0, 0);
@@ -570,7 +574,7 @@ TEST(YogaTest, measure_no_padding) {
#if GTEST_HAS_DEATH_TEST
TEST(YogaDeathTest, cannot_add_child_to_node_with_measure_func) {
const YGNodeRef root = YGNodeNew();
YGNodeSetMeasureFunc(root, _measure);
root->setMeasureFunc(_measure);
const YGNodeRef root_child0 = YGNodeNew();
ASSERT_DEATH(YGNodeInsertChild(root, root_child0, 0), "Cannot add child.*");
@@ -582,8 +586,7 @@ TEST(YogaDeathTest, cannot_add_nonnull_measure_func_to_non_leaf_node) {
const YGNodeRef root = YGNodeNew();
const YGNodeRef root_child0 = YGNodeNew();
YGNodeInsertChild(root, root_child0, 0);
ASSERT_DEATH(YGNodeSetMeasureFunc(root, _measure), "Cannot set measure function.*");
ASSERT_DEATH(root->setMeasureFunc(_measure), "Cannot set measure function.*");
YGNodeFreeRecursive(root);
}
@@ -592,46 +595,45 @@ TEST(YogaDeathTest, cannot_add_nonnull_measure_func_to_non_leaf_node) {
TEST(YogaTest, can_nullify_measure_func_on_any_node) {
const YGNodeRef root = YGNodeNew();
YGNodeInsertChild(root, YGNodeNew(), 0);
YGNodeSetMeasureFunc(root, NULL);
ASSERT_TRUE(YGNodeGetMeasureFunc(root) == NULL);
root->setMeasureFunc(nullptr);
ASSERT_TRUE(root->getMeasure() == NULL);
YGNodeFreeRecursive(root);
}
TEST(YogaTest, cant_call_negative_measure) {
const YGConfigRef config = YGConfigNew();
const YGNodeRef root = YGNodeNewWithConfig(config);
YGNodeStyleSetFlexDirection(root, YGFlexDirectionColumn);
YGNodeStyleSetWidth(root, 50);
YGNodeStyleSetHeight(root, 10);
const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeSetMeasureFunc(root_child0, _measure_assert_negative);
root_child0->setMeasureFunc(_measure_assert_negative);
YGNodeStyleSetMargin(root_child0, YGEdgeTop, 20);
YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
YGNodeFreeRecursive(root);
YGConfigFree(config);
}
TEST(YogaTest, cant_call_negative_measure_horizontal) {
const YGConfigRef config = YGConfigNew();
const YGNodeRef root = YGNodeNewWithConfig(config);
YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
YGNodeStyleSetWidth(root, 10);
YGNodeStyleSetHeight(root, 20);
const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeSetMeasureFunc(root_child0, _measure_assert_negative);
root_child0->setMeasureFunc(_measure_assert_negative);
YGNodeStyleSetMargin(root_child0, YGEdgeStart, 20);
YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
YGNodeFreeRecursive(root);
YGConfigFree(config);
}
@@ -661,8 +663,7 @@ TEST(YogaTest, percent_with_text_node) {
YGNodeInsertChild(root, root_child0, 0);
const YGNodeRef root_child1 = YGNodeNewWithConfig(config);
YGNodeSetMeasureFunc(root_child1, _measure_90_10);
root_child1->setMeasureFunc(_measure_90_10);
YGNodeStyleSetMaxWidthPercent(root_child1, 50);
YGNodeStyleSetPaddingPercent(root_child1, YGEdgeTop, 50);
YGNodeInsertChild(root, root_child1, 1);