JD2022-TU1/main/extern/UbiServices/custom/testsUnit/services/testNewsLink.cpp

131 lines
3.7 KiB
C++

////////////////////////////////////////////////////////////////////////////////
// (c) Copyright, Ubisoft Entertainment Inc. || Created by Harbour //
////////////////////////////////////////////////////////////////////////////////
#include "tests/config.h"
#include "tests/fixture/memoryCheckFixture.h"
#include <ubiservices/core/JSON/json.h>
#include <ubiservices/core/JSON/jsonWriter.h>
#include <ubiservices/loggingHelper.h>
#include <ubiservices/services/news/newsLink.h>
namespace US_NS {;
US_DEFINE_TEST(TestNewsLink);
#if !defined(US_DLL)
// FOR DEBUGGING !!!
//#define ENABLENEWSLINKUNITLOG
#if defined(ENABLE_NEWSLINKUNIT_LOG)
#define ENABLE_VERBOSE_LOGGING() \
LoggingHelper::enableVerbose(); \
LoggingHelper::enableMultiLines();
#else
#define ENABLE_VERBOSE_LOGGING()
#endif
namespace NewsLinkPrivate
{
bool extractData(const Json& source, NewsLink& target);
}
typedef LeakChecker NEWSLINKUNIT;
namespace TestNewsLink_BF
{
Json getNewsLink(bool missingField, bool extraField, bool withoutActionValues)
{
JsonWriter writer;
if (!missingField)
{
writer["type"] = "typeValue";
}
writer["param"] = "paramValue";
// Can be empty !
if (!withoutActionValues)
{
writer["actionName"] = "actionNameValue";
writer["actionDescription"] = "actionDescriptionValue";
}
if (extraField)
{
writer["extraField"] = "extraFieldValue";
}
return writer.getJson();
}
}
TEST_F(NEWSLINKUNIT, LOCAL_ParsingJSON)
{
ENABLE_VERBOSE_LOGGING();
SAVE_ALLOC_COUNT(testCount);
const Json jsonData = TestNewsLink_BF::getNewsLink(false, false, false);
NewsLink newsLinkData;
EXPECT_TRUE(NewsLinkPrivate::extractData(jsonData, newsLinkData)) << "Parsing Valid JSON";
EXPECT_EQ("typeValue", newsLinkData.m_type);
EXPECT_EQ("paramValue", newsLinkData.m_param);
EXPECT_EQ("actionNameValue", newsLinkData.m_actionName);
EXPECT_EQ("actionDescriptionValue", newsLinkData.m_actionDescription);
EXPECT_ALLOC_COUNT_PC_X1_EQ(408u, 408, testCount);
}
TEST_F(NEWSLINKUNIT, LOCAL_ParsingJSONMissingField)
{
ENABLE_VERBOSE_LOGGING();
SAVE_ALLOC_COUNT(testCount);
const Json jsonData = TestNewsLink_BF::getNewsLink(true, false, false);
NewsLink newsLinkData;
EXPECT_FALSE(NewsLinkPrivate::extractData(jsonData, newsLinkData)) << "Parsing JSON missing field";
EXPECT_ALLOC_COUNT_PC_X1_EQ(408u, 408, testCount);
}
TEST_F(NEWSLINKUNIT, LOCAL_ParsingJSONWithExtraField)
{
ENABLE_VERBOSE_LOGGING();
SAVE_ALLOC_COUNT(testCount);
const Json jsonData = TestNewsLink_BF::getNewsLink(false, true, false);
NewsLink newsLinkData;
EXPECT_TRUE(NewsLinkPrivate::extractData(jsonData, newsLinkData)) << "Parsing JSON extra field";
EXPECT_EQ("typeValue", newsLinkData.m_type);
EXPECT_EQ("paramValue", newsLinkData.m_param);
EXPECT_EQ("actionNameValue", newsLinkData.m_actionName);
EXPECT_EQ("actionDescriptionValue", newsLinkData.m_actionDescription);
EXPECT_ALLOC_COUNT_PC_X1_EQ(408u, 408, testCount);
}
TEST_F(NEWSLINKUNIT, LOCAL_ParsingJSONWithoutActionData)
{
ENABLE_VERBOSE_LOGGING();
SAVE_ALLOC_COUNT(testCount);
const Json jsonData = TestNewsLink_BF::getNewsLink(false, false, true);
NewsLink newsLinkData;
EXPECT_TRUE(NewsLinkPrivate::extractData(jsonData, newsLinkData)) << "Parsing JSON without action data";
EXPECT_EQ("typeValue", newsLinkData.m_type);
EXPECT_EQ("paramValue", newsLinkData.m_param);
EXPECT_TRUE(newsLinkData.m_actionName.isEmpty());
EXPECT_TRUE(newsLinkData.m_actionDescription.isEmpty());
EXPECT_ALLOC_COUNT_PC_X1_EQ(408u, 408, testCount);
}
#endif
};