Skip to content

Commit

Permalink
Use EXPECT instead of ASSERT in tests
Browse files Browse the repository at this point in the history
Summary:
Changelog: [Internal]

1. Replace ASSERT_* with EXPECT_*. Assert is a fatal assertion. Expect is non-fatal assertion. So if assert fails, tests do not continue and therefore provide less information.

2. Rename tests in `RawPropsTest.cpp` from `ShadowNodeTest` to `RawPropsTest`.

Source: https://github.com/google/googletest/blob/master/googletest/docs/primer.md#basic-assertions

Reviewed By: shergin

Differential Revision: D19464967

fbshipit-source-id: add83751ebdb0a12fbf8f70b851747aa5624366a
  • Loading branch information
sammy-SC authored and facebook-github-bot committed Jan 20, 2020
1 parent 84adc85 commit 582738b
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 112 deletions.
32 changes: 16 additions & 16 deletions ReactCommon/fabric/core/tests/ComponentDescriptorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ TEST(ComponentDescriptorTest, createShadowNode) {
std::make_shared<TestComponentDescriptor>(
ComponentDescriptorParameters{eventDispatcher, nullptr, nullptr});

ASSERT_EQ(descriptor->getComponentHandle(), TestShadowNode::Handle());
ASSERT_STREQ(descriptor->getComponentName(), TestShadowNode::Name());
ASSERT_STREQ(descriptor->getComponentName(), "Test");
EXPECT_EQ(descriptor->getComponentHandle(), TestShadowNode::Handle());
EXPECT_STREQ(descriptor->getComponentName(), TestShadowNode::Name());
EXPECT_STREQ(descriptor->getComponentName(), "Test");

const auto &raw = RawProps(folly::dynamic::object("nativeID", "abc"));
SharedProps props = descriptor->cloneProps(nullptr, raw);
Expand All @@ -34,12 +34,12 @@ TEST(ComponentDescriptorTest, createShadowNode) {
/* .eventEmitter = */ descriptor->createEventEmitter(0, 9),
});

ASSERT_EQ(node->getComponentHandle(), TestShadowNode::Handle());
ASSERT_STREQ(node->getComponentName(), TestShadowNode::Name());
ASSERT_STREQ(node->getComponentName(), "Test");
ASSERT_EQ(node->getTag(), 9);
ASSERT_EQ(node->getSurfaceId(), 1);
ASSERT_STREQ(node->getProps()->nativeId.c_str(), "abc");
EXPECT_EQ(node->getComponentHandle(), TestShadowNode::Handle());
EXPECT_STREQ(node->getComponentName(), TestShadowNode::Name());
EXPECT_STREQ(node->getComponentName(), "Test");
EXPECT_EQ(node->getTag(), 9);
EXPECT_EQ(node->getSurfaceId(), 1);
EXPECT_STREQ(node->getProps()->nativeId.c_str(), "abc");
}

TEST(ComponentDescriptorTest, cloneShadowNode) {
Expand All @@ -61,10 +61,10 @@ TEST(ComponentDescriptorTest, cloneShadowNode) {
});
SharedShadowNode cloned = descriptor->cloneShadowNode(*node, {});

ASSERT_STREQ(cloned->getComponentName(), "Test");
ASSERT_EQ(cloned->getTag(), 9);
ASSERT_EQ(cloned->getSurfaceId(), 1);
ASSERT_STREQ(cloned->getProps()->nativeId.c_str(), "abc");
EXPECT_STREQ(cloned->getComponentName(), "Test");
EXPECT_EQ(cloned->getTag(), 9);
EXPECT_EQ(cloned->getSurfaceId(), 1);
EXPECT_STREQ(cloned->getProps()->nativeId.c_str(), "abc");
}

TEST(ComponentDescriptorTest, appendChild) {
Expand Down Expand Up @@ -108,7 +108,7 @@ TEST(ComponentDescriptorTest, appendChild) {
descriptor->appendChild(node1, node3);

auto node1Children = node1->getChildren();
ASSERT_EQ(node1Children.size(), 2);
ASSERT_EQ(node1Children.at(0), node2);
ASSERT_EQ(node1Children.at(1), node3);
EXPECT_EQ(node1Children.size(), 2);
EXPECT_EQ(node1Children.at(0), node2);
EXPECT_EQ(node1Children.at(1), node3);
}
6 changes: 3 additions & 3 deletions ReactCommon/fabric/core/tests/PrimitivesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ using namespace facebook::react;

TEST(SealableTest, sealObjectCorrectly) {
Sealable obj;
ASSERT_FALSE(obj.getSealed());
EXPECT_FALSE(obj.getSealed());

obj.seal();
ASSERT_TRUE(obj.getSealed());
EXPECT_TRUE(obj.getSealed());
}

TEST(SealableTest, handleAssignmentsCorrectly) {
Expand All @@ -37,5 +37,5 @@ TEST(SealableTest, handleAssignmentsCorrectly) {

// Fresh creation off other Sealable is still unsealed.
Sealable other3(obj);
ASSERT_FALSE(other3.getSealed());
EXPECT_FALSE(other3.getSealed());
}
116 changes: 58 additions & 58 deletions ReactCommon/fabric/core/tests/RawPropsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class PropsMultiLookup : public Props {
const float derivedFloatValue{40};
};

TEST(ShadowNodeTest, handleProps) {
TEST(RawPropsTest, handleProps) {
const auto &raw = RawProps(folly::dynamic::object("nativeID", "abc"));
auto parser = RawPropsParser();
parser.prepare<Props>();
Expand All @@ -127,23 +127,23 @@ TEST(ShadowNodeTest, handleProps) {
auto props = std::make_shared<Props>(Props(), raw);

// Props are not sealed after applying raw props.
ASSERT_FALSE(props->getSealed());
EXPECT_FALSE(props->getSealed());

ASSERT_STREQ(props->nativeId.c_str(), "abc");
EXPECT_STREQ(props->nativeId.c_str(), "abc");
}

TEST(ShadowNodeTest, handleRawPropsSingleString) {
TEST(RawPropsTest, handleRawPropsSingleString) {
const auto &raw = RawProps(folly::dynamic::object("nativeID", "abc"));
auto parser = RawPropsParser();
parser.prepare<Props>();
raw.parse(parser);

std::string value = (std::string)*raw.at("nativeID", nullptr, nullptr);

ASSERT_STREQ(value.c_str(), "abc");
EXPECT_STREQ(value.c_str(), "abc");
}

TEST(ShadowNodeTest, handleRawPropsSingleFloat) {
TEST(RawPropsTest, handleRawPropsSingleFloat) {
const auto &raw =
RawProps(folly::dynamic::object("floatValue", (float)42.42));
auto parser = RawPropsParser();
Expand All @@ -152,10 +152,10 @@ TEST(ShadowNodeTest, handleRawPropsSingleFloat) {

float value = (float)*raw.at("floatValue", nullptr, nullptr);

ASSERT_NEAR(value, 42.42, 0.00001);
EXPECT_NEAR(value, 42.42, 0.00001);
}

TEST(ShadowNodeTest, handleRawPropsSingleDouble) {
TEST(RawPropsTest, handleRawPropsSingleDouble) {
const auto &raw =
RawProps(folly::dynamic::object("doubleValue", (double)42.42));
auto parser = RawPropsParser();
Expand All @@ -164,32 +164,32 @@ TEST(ShadowNodeTest, handleRawPropsSingleDouble) {

double value = (double)*raw.at("doubleValue", nullptr, nullptr);

ASSERT_NEAR(value, 42.42, 0.00001);
EXPECT_NEAR(value, 42.42, 0.00001);
}

TEST(ShadowNodeTest, handleRawPropsSingleInt) {
TEST(RawPropsTest, handleRawPropsSingleInt) {
const auto &raw = RawProps(folly::dynamic::object("intValue", (int)42.42));
auto parser = RawPropsParser();
parser.prepare<PropsSingleInt>();
raw.parse(parser);

int value = (int)*raw.at("intValue", nullptr, nullptr);

ASSERT_EQ(value, 42);
EXPECT_EQ(value, 42);
}

TEST(ShadowNodeTest, handleRawPropsSingleIntGetManyTimes) {
TEST(RawPropsTest, handleRawPropsSingleIntGetManyTimes) {
const auto &raw = RawProps(folly::dynamic::object("intValue", (int)42.42));
auto parser = RawPropsParser();
parser.prepare<PropsSingleInt>();
raw.parse(parser);

ASSERT_EQ((int)*raw.at("intValue", nullptr, nullptr), 42);
ASSERT_EQ((int)*raw.at("intValue", nullptr, nullptr), 42);
ASSERT_EQ((int)*raw.at("intValue", nullptr, nullptr), 42);
EXPECT_EQ((int)*raw.at("intValue", nullptr, nullptr), 42);
EXPECT_EQ((int)*raw.at("intValue", nullptr, nullptr), 42);
EXPECT_EQ((int)*raw.at("intValue", nullptr, nullptr), 42);
}

TEST(ShadowNodeTest, handleRawPropsPrimitiveTypes) {
TEST(RawPropsTest, handleRawPropsPrimitiveTypes) {
const auto &raw = RawProps(folly::dynamic::object("intValue", (int)42)(
"doubleValue", (double)17.42)("floatValue", (float)66.67)(
"stringValue", "helloworld")("boolValue", true));
Expand All @@ -198,16 +198,16 @@ TEST(ShadowNodeTest, handleRawPropsPrimitiveTypes) {
parser.prepare<PropsPrimitiveTypes>();
raw.parse(parser);

ASSERT_EQ((int)*raw.at("intValue", nullptr, nullptr), 42);
ASSERT_NEAR((double)*raw.at("doubleValue", nullptr, nullptr), 17.42, 0.0001);
ASSERT_NEAR((float)*raw.at("floatValue", nullptr, nullptr), 66.67, 0.00001);
ASSERT_STREQ(
EXPECT_EQ((int)*raw.at("intValue", nullptr, nullptr), 42);
EXPECT_NEAR((double)*raw.at("doubleValue", nullptr, nullptr), 17.42, 0.0001);
EXPECT_NEAR((float)*raw.at("floatValue", nullptr, nullptr), 66.67, 0.00001);
EXPECT_STREQ(
((std::string)*raw.at("stringValue", nullptr, nullptr)).c_str(),
"helloworld");
ASSERT_EQ((bool)*raw.at("boolValue", nullptr, nullptr), true);
EXPECT_EQ((bool)*raw.at("boolValue", nullptr, nullptr), true);
}

TEST(ShadowNodeTest, handleRawPropsPrimitiveTypesGetTwice) {
TEST(RawPropsTest, handleRawPropsPrimitiveTypesGetTwice) {
const auto &raw = RawProps(folly::dynamic::object("intValue", (int)42)(
"doubleValue", (double)17.42)("floatValue", (float)66.67)(
"stringValue", "helloworld")("boolValue", true));
Expand All @@ -216,24 +216,24 @@ TEST(ShadowNodeTest, handleRawPropsPrimitiveTypesGetTwice) {
parser.prepare<PropsPrimitiveTypes>();
raw.parse(parser);

ASSERT_EQ((int)*raw.at("intValue", nullptr, nullptr), 42);
ASSERT_NEAR((double)*raw.at("doubleValue", nullptr, nullptr), 17.42, 0.0001);
ASSERT_NEAR((float)*raw.at("floatValue", nullptr, nullptr), 66.67, 0.00001);
ASSERT_STREQ(
EXPECT_EQ((int)*raw.at("intValue", nullptr, nullptr), 42);
EXPECT_NEAR((double)*raw.at("doubleValue", nullptr, nullptr), 17.42, 0.0001);
EXPECT_NEAR((float)*raw.at("floatValue", nullptr, nullptr), 66.67, 0.00001);
EXPECT_STREQ(
((std::string)*raw.at("stringValue", nullptr, nullptr)).c_str(),
"helloworld");
ASSERT_EQ((bool)*raw.at("boolValue", nullptr, nullptr), true);
EXPECT_EQ((bool)*raw.at("boolValue", nullptr, nullptr), true);

ASSERT_EQ((int)*raw.at("intValue", nullptr, nullptr), 42);
ASSERT_NEAR((double)*raw.at("doubleValue", nullptr, nullptr), 17.42, 0.0001);
ASSERT_NEAR((float)*raw.at("floatValue", nullptr, nullptr), 66.67, 0.00001);
ASSERT_STREQ(
EXPECT_EQ((int)*raw.at("intValue", nullptr, nullptr), 42);
EXPECT_NEAR((double)*raw.at("doubleValue", nullptr, nullptr), 17.42, 0.0001);
EXPECT_NEAR((float)*raw.at("floatValue", nullptr, nullptr), 66.67, 0.00001);
EXPECT_STREQ(
((std::string)*raw.at("stringValue", nullptr, nullptr)).c_str(),
"helloworld");
ASSERT_EQ((bool)*raw.at("boolValue", nullptr, nullptr), true);
EXPECT_EQ((bool)*raw.at("boolValue", nullptr, nullptr), true);
}

TEST(ShadowNodeTest, handleRawPropsPrimitiveTypesGetOutOfOrder) {
TEST(RawPropsTest, handleRawPropsPrimitiveTypesGetOutOfOrder) {
const auto &raw = RawProps(folly::dynamic::object("intValue", (int)42)(
"doubleValue", (double)17.42)("floatValue", (float)66.67)(
"stringValue", "helloworld")("boolValue", true));
Expand All @@ -242,41 +242,41 @@ TEST(ShadowNodeTest, handleRawPropsPrimitiveTypesGetOutOfOrder) {
parser.prepare<PropsPrimitiveTypes>();
raw.parse(parser);

ASSERT_EQ((int)*raw.at("intValue", nullptr, nullptr), 42);
ASSERT_NEAR((double)*raw.at("doubleValue", nullptr, nullptr), 17.42, 0.0001);
ASSERT_NEAR((float)*raw.at("floatValue", nullptr, nullptr), 66.67, 0.00001);
ASSERT_STREQ(
EXPECT_EQ((int)*raw.at("intValue", nullptr, nullptr), 42);
EXPECT_NEAR((double)*raw.at("doubleValue", nullptr, nullptr), 17.42, 0.0001);
EXPECT_NEAR((float)*raw.at("floatValue", nullptr, nullptr), 66.67, 0.00001);
EXPECT_STREQ(
((std::string)*raw.at("stringValue", nullptr, nullptr)).c_str(),
"helloworld");
ASSERT_EQ((bool)*raw.at("boolValue", nullptr, nullptr), true);
EXPECT_EQ((bool)*raw.at("boolValue", nullptr, nullptr), true);

ASSERT_NEAR((double)*raw.at("doubleValue", nullptr, nullptr), 17.42, 0.0001);
ASSERT_EQ((int)*raw.at("intValue", nullptr, nullptr), 42);
ASSERT_NEAR((float)*raw.at("floatValue", nullptr, nullptr), 66.67, 0.00001);
ASSERT_STREQ(
EXPECT_NEAR((double)*raw.at("doubleValue", nullptr, nullptr), 17.42, 0.0001);
EXPECT_EQ((int)*raw.at("intValue", nullptr, nullptr), 42);
EXPECT_NEAR((float)*raw.at("floatValue", nullptr, nullptr), 66.67, 0.00001);
EXPECT_STREQ(
((std::string)*raw.at("stringValue", nullptr, nullptr)).c_str(),
"helloworld");
ASSERT_EQ((bool)*raw.at("boolValue", nullptr, nullptr), true);
EXPECT_EQ((bool)*raw.at("boolValue", nullptr, nullptr), true);
}

TEST(ShadowNodeTest, handleRawPropsPrimitiveTypesIncomplete) {
TEST(RawPropsTest, handleRawPropsPrimitiveTypesIncomplete) {
const auto &raw = RawProps(folly::dynamic::object("intValue", (int)42));

auto parser = RawPropsParser();
parser.prepare<PropsPrimitiveTypes>();
raw.parse(parser);

ASSERT_EQ((int)*raw.at("intValue", nullptr, nullptr), 42);
ASSERT_EQ(raw.at("doubleValue", nullptr, nullptr), nullptr);
ASSERT_EQ(raw.at("floatValue", nullptr, nullptr), nullptr);
ASSERT_EQ((int)*raw.at("intValue", nullptr, nullptr), 42);
ASSERT_EQ(raw.at("stringValue", nullptr, nullptr), nullptr);
ASSERT_EQ(raw.at("boolValue", nullptr, nullptr), nullptr);
ASSERT_EQ((int)*raw.at("intValue", nullptr, nullptr), 42);
EXPECT_EQ((int)*raw.at("intValue", nullptr, nullptr), 42);
EXPECT_EQ(raw.at("doubleValue", nullptr, nullptr), nullptr);
EXPECT_EQ(raw.at("floatValue", nullptr, nullptr), nullptr);
EXPECT_EQ((int)*raw.at("intValue", nullptr, nullptr), 42);
EXPECT_EQ(raw.at("stringValue", nullptr, nullptr), nullptr);
EXPECT_EQ(raw.at("boolValue", nullptr, nullptr), nullptr);
EXPECT_EQ((int)*raw.at("intValue", nullptr, nullptr), 42);
}

#ifndef NDEBUG
TEST(ShadowNodeTest, handleRawPropsPrimitiveTypesIncorrectLookup) {
TEST(RawPropsTest, handleRawPropsPrimitiveTypesIncorrectLookup) {
const auto &raw = RawProps(folly::dynamic::object("intValue", (int)42));

auto parser = RawPropsParser();
Expand All @@ -286,12 +286,12 @@ TEST(ShadowNodeTest, handleRawPropsPrimitiveTypesIncorrectLookup) {
// Before D18662135, looking up an invalid key would trigger
// an infinite loop. This is out of contract, so we should only
// test this in debug.
ASSERT_EQ(raw.at("flurb", nullptr, nullptr), nullptr);
ASSERT_EQ((int)*raw.at("intValue", nullptr, nullptr), 42);
EXPECT_EQ(raw.at("flurb", nullptr, nullptr), nullptr);
EXPECT_EQ((int)*raw.at("intValue", nullptr, nullptr), 42);
}
#endif

TEST(ShadowNodeTest, handlePropsMultiLookup) {
TEST(RawPropsTest, handlePropsMultiLookup) {
const auto &raw = RawProps(folly::dynamic::object("floatValue", (float)10.0));
auto parser = RawPropsParser();
parser.prepare<PropsMultiLookup>();
Expand All @@ -300,8 +300,8 @@ TEST(ShadowNodeTest, handlePropsMultiLookup) {
auto props = std::make_shared<PropsMultiLookup>(PropsMultiLookup(), raw);

// Props are not sealed after applying raw props.
ASSERT_FALSE(props->getSealed());
EXPECT_FALSE(props->getSealed());

ASSERT_NEAR(props->floatValue, 10.0, 0.00001);
ASSERT_NEAR(props->derivedFloatValue, 20.0, 0.00001);
EXPECT_NEAR(props->floatValue, 10.0, 0.00001);
EXPECT_NEAR(props->derivedFloatValue, 20.0, 0.00001);
}
Loading

0 comments on commit 582738b

Please sign in to comment.