Skip to content

Commit 025a862

Browse files
generatedunixname1563563004708334meta-codesync[bot]
authored andcommitted
xplat/js/react-native-github/packages/react-native/ReactCommon/yoga/yoga/debug/Log.cpp (#2014)
Summary: Pull Request resolved: react/yoga#2014 Reviewed By: christophpurrer Differential Revision: D117646058 fbshipit-source-id: 07ffdf6e19f78ba7ac93c05dd05ed55dde3e62b1
1 parent cf97656 commit 025a862

1 file changed

Lines changed: 102 additions & 0 deletions

File tree

  • packages/react-native/ReactCommon/yoga/yoga/debug/tests
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#include <gtest/gtest.h>
9+
10+
#include <stdexcept>
11+
#include <string>
12+
13+
#include <yoga/Yoga.h>
14+
15+
namespace {
16+
17+
struct LogRecord {
18+
int calls = 0;
19+
YGConfigConstRef config = nullptr;
20+
YGNodeConstRef node = nullptr;
21+
YGLogLevel level = YGLogLevelDebug;
22+
};
23+
24+
int recordLog(
25+
YGConfigConstRef config,
26+
YGNodeConstRef node,
27+
YGLogLevel level,
28+
const char* /*format*/,
29+
va_list /*args*/) {
30+
auto* record = static_cast<LogRecord*>(YGConfigGetContext(config));
31+
record->calls++;
32+
record->config = config;
33+
record->node = node;
34+
record->level = level;
35+
return 0;
36+
}
37+
38+
YGSize measureInvalidWidth(
39+
YGNodeConstRef /*node*/,
40+
float /*width*/,
41+
YGMeasureMode /*widthMode*/,
42+
float /*height*/,
43+
YGMeasureMode /*heightMode*/) {
44+
return YGSize{-1.0f, 10.0f};
45+
}
46+
47+
class LogTest : public ::testing::Test {
48+
protected:
49+
void SetUp() override {
50+
config_ = YGConfigNew();
51+
YGConfigSetContext(config_, &record_);
52+
YGConfigSetLogger(config_, recordLog);
53+
}
54+
55+
void TearDown() override {
56+
if (node_ != nullptr) {
57+
YGNodeFree(node_);
58+
}
59+
YGConfigFree(config_);
60+
}
61+
62+
LogRecord record_;
63+
YGConfigRef config_ = nullptr;
64+
YGNodeRef node_ = nullptr;
65+
};
66+
67+
TEST_F(LogTest, testConfigFailureForwardsLoggerMetadata) {
68+
EXPECT_THROW(YGConfigSetPointScaleFactor(config_, -1.0f), std::logic_error);
69+
70+
EXPECT_EQ(record_.calls, 1);
71+
EXPECT_EQ(record_.config, config_);
72+
EXPECT_EQ(record_.node, nullptr);
73+
EXPECT_EQ(record_.level, YGLogLevelFatal);
74+
}
75+
76+
TEST_F(LogTest, testInvalidNodeMeasurementUsesNodesConfiguredLogger) {
77+
node_ = YGNodeNewWithConfig(config_);
78+
YGNodeSetMeasureFunc(node_, measureInvalidWidth);
79+
80+
YGNodeCalculateLayout(node_, YGUndefined, YGUndefined, YGDirectionLTR);
81+
82+
EXPECT_EQ(record_.calls, 1);
83+
EXPECT_EQ(record_.config, config_);
84+
EXPECT_EQ(record_.node, node_);
85+
EXPECT_EQ(record_.level, YGLogLevelWarn);
86+
}
87+
88+
TEST_F(LogTest, testNullLoggerRestoresDefaultErrorLogger) {
89+
YGConfigSetLogger(config_, nullptr);
90+
testing::internal::CaptureStdout();
91+
testing::internal::CaptureStderr();
92+
93+
EXPECT_THROW(YGConfigSetPointScaleFactor(config_, -1.0f), std::logic_error);
94+
95+
const std::string stdoutOutput = testing::internal::GetCapturedStdout();
96+
const std::string stderrOutput = testing::internal::GetCapturedStderr();
97+
EXPECT_EQ(record_.calls, 0);
98+
EXPECT_TRUE(stdoutOutput.empty());
99+
EXPECT_FALSE(stderrOutput.empty());
100+
}
101+
102+
} // namespace

0 commit comments

Comments
 (0)