Skip to content

Commit 6d253a3

Browse files
committed
Fixed a bug where not all generators where using the --registerfile argument.
Created a new unit test TestCLI.testRegisterFileAllGenerators.
1 parent 43f77a5 commit 6d253a3

File tree

4 files changed

+158
-1
lines changed

4 files changed

+158
-1
lines changed

src/bin2cpp/ArrayGenerator.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ namespace bin2cpp
138138
fprintf(cpp, "%s", getSaveMethodTemplate().c_str());
139139
fprintf(cpp, " };\n");
140140
fprintf(cpp, " const %s & %s() { static %s _instance; return _instance; }\n", mBaseClass.c_str(), getterFunctionName.c_str(), className.c_str());
141+
if (isRegisterFileEnabled())
142+
{
143+
std::string fileManagerTemplate = getFileManagerRegistrationTemplate();
144+
fprintf(cpp, "%s", fileManagerTemplate.c_str());
145+
}
141146
fprintf(cpp, "}; //%s\n", mNamespace.c_str());
142147

143148
fclose(input);

src/bin2cpp/StringGenerator.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ namespace bin2cpp
150150
fprintf(cpp, "%s", getSaveMethodTemplate().c_str());
151151
fprintf(cpp, " };\n");
152152
fprintf(cpp, " const %s & %s() { static %s _instance; return _instance; }\n", mBaseClass.c_str(), getterFunctionName.c_str(), className.c_str());
153+
if (isRegisterFileEnabled())
154+
{
155+
std::string fileManagerTemplate = getFileManagerRegistrationTemplate();
156+
fprintf(cpp, "%s", fileManagerTemplate.c_str());
157+
}
153158
fprintf(cpp, "}; //%s\n", mNamespace.c_str());
154159

155160
fclose(input);

src/bin2cpp/Win32ResourceGenerator.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ namespace bin2cpp
174174
fprintf(cpp, " const char * mBuffer;\n");
175175
fprintf(cpp, " };\n");
176176
fprintf(cpp, " const %s & %s() { static %s _instance; return _instance; }\n", mBaseClass.c_str(), getterFunctionName.c_str(), className.c_str());
177+
if (isRegisterFileEnabled())
178+
{
179+
std::string fileManagerTemplate = getFileManagerRegistrationTemplate();
180+
fprintf(cpp, "%s", fileManagerTemplate.c_str());
181+
}
177182
fprintf(cpp, "}; //%s\n", mNamespace.c_str());
178183

179184
fclose(input);

test/bin2cpp_unittest/TestCLI.cpp

Lines changed: 143 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1526,7 +1526,7 @@ TEST_F(TestCLI, testRegisterFile)
15261526
ASSERT_TRUE( ra::filesystem::CopyFile(cppFilePath, backupCppFile1) );
15271527

15281528
//buid same command again but with --registerfile argument
1529-
ra::strings::Replace(cmdline, " >", "--registerfile >");
1529+
ra::strings::Replace(cmdline, " >", " --registerfile >");
15301530

15311531
//prepare execution of second command line
15321532
{
@@ -1590,3 +1590,145 @@ TEST_F(TestCLI, testRegisterFile)
15901590
ASSERT_TRUE(deleteFile(backupHeaderFile2.c_str()));
15911591
ASSERT_TRUE(deleteFile(backupCppFile2.c_str()));
15921592
}
1593+
1594+
TEST_F(TestCLI, testRegisterFileAllGenerators)
1595+
{
1596+
static const std::string expectedFilePath = getExpectedFilePath();
1597+
static const std::string outputFilePath = getActualFilePath();
1598+
1599+
std::string headerFileName = std::string("_") + ra::testing::GetTestCaseName().c_str() + ".h";
1600+
std::string headerFilePath = gGeneratedFilesDir + headerFileName;
1601+
std::string cppFilePath = headerFilePath; ra::strings::Replace(cppFilePath, ".h", ".cpp");
1602+
1603+
const char * generators[] = {
1604+
"segment",
1605+
"string",
1606+
"array",
1607+
"win32",
1608+
};
1609+
const size_t num_generators = sizeof(generators)/sizeof(generators[0]);
1610+
1611+
for(size_t i=0; i<num_generators; i++)
1612+
{
1613+
const char * generator = generators[i];
1614+
printf("Testing generator '%s'\n", generator);
1615+
1616+
//build command line
1617+
std::string cmdline;
1618+
cmdline.append(getBin2cppPath());
1619+
cmdline.append(" --file=");
1620+
cmdline.append(getBin2cppPath()); //itself
1621+
cmdline.append(" --output=generated_files");
1622+
cmdline.append(" --headerfile=");
1623+
cmdline.append(headerFileName);
1624+
cmdline.append(" --identifier=");
1625+
cmdline.append(ra::testing::GetTestCaseName().c_str());
1626+
cmdline.append(" --generator=");
1627+
cmdline.append(generator);
1628+
1629+
cmdline.append(" >");
1630+
cmdline.append(outputFilePath.c_str());
1631+
1632+
//prepare execution of first command line
1633+
{
1634+
//delete generated files
1635+
ASSERT_TRUE(deleteFile(headerFilePath.c_str()));
1636+
ASSERT_TRUE(deleteFile(cppFilePath.c_str()));
1637+
1638+
//run the command
1639+
int returnCode = system(cmdline.c_str());
1640+
#ifdef __linux__
1641+
returnCode = WEXITSTATUS(returnCode);
1642+
#endif
1643+
ASSERT_EQ(0, returnCode) << "The command line '" << cmdline.c_str() << "' returned " << returnCode;
1644+
1645+
//load output file
1646+
ra::strings::StringVector lines;
1647+
bool loaded = ra::filesystem::ReadTextFile(outputFilePath.c_str(), lines);
1648+
ASSERT_TRUE(loaded);
1649+
1650+
//assert standard output log
1651+
ASSERT_TEXT_IN_FILE(true, outputFilePath.c_str(), "Copyright (C)");
1652+
ASSERT_TEXT_IN_FILE(false, outputFilePath.c_str(), "Usage:");
1653+
1654+
//assert generated code
1655+
ASSERT_TRUE(ra::filesystem::FileExists(headerFilePath.c_str()));
1656+
1657+
//cleanup
1658+
ASSERT_TRUE(deleteFile(outputFilePath.c_str()));
1659+
}
1660+
1661+
//backup the generated files
1662+
std::string backupHeaderFile1 = headerFilePath;
1663+
std::string backupCppFile1 = cppFilePath;
1664+
ra::strings::Replace(backupHeaderFile1, ".h", ".1.h");
1665+
ra::strings::Replace(backupCppFile1, ".cpp", ".1.cpp");
1666+
ASSERT_TRUE( ra::filesystem::CopyFile(headerFilePath, backupHeaderFile1) );
1667+
ASSERT_TRUE( ra::filesystem::CopyFile(cppFilePath, backupCppFile1) );
1668+
1669+
//buid same command again but with --registerfile argument
1670+
ra::strings::Replace(cmdline, " >", " --registerfile >");
1671+
1672+
//prepare execution of second command line
1673+
{
1674+
//delete generated files
1675+
ASSERT_TRUE(deleteFile(headerFilePath.c_str()));
1676+
ASSERT_TRUE(deleteFile(cppFilePath.c_str()));
1677+
1678+
//run the command
1679+
int returnCode = system(cmdline.c_str());
1680+
#ifdef __linux__
1681+
returnCode = WEXITSTATUS(returnCode);
1682+
#endif
1683+
ASSERT_EQ(0, returnCode) << "The command line '" << cmdline.c_str() << "' returned " << returnCode;
1684+
1685+
//load output file
1686+
ra::strings::StringVector lines;
1687+
bool loaded = ra::filesystem::ReadTextFile(outputFilePath.c_str(), lines);
1688+
ASSERT_TRUE(loaded);
1689+
1690+
//assert standard output log
1691+
ASSERT_TEXT_IN_FILE(true, outputFilePath.c_str(), "Copyright (C)");
1692+
ASSERT_TEXT_IN_FILE(false, outputFilePath.c_str(), "Usage:");
1693+
1694+
//assert generated code
1695+
ASSERT_TRUE(ra::filesystem::FileExists(headerFilePath.c_str()));
1696+
1697+
//cleanup
1698+
ASSERT_TRUE(deleteFile(outputFilePath.c_str()));
1699+
}
1700+
1701+
//backup the generated files (again)
1702+
std::string backupHeaderFile2 = headerFilePath;
1703+
std::string backupCppFile2 = cppFilePath;
1704+
ra::strings::Replace(backupHeaderFile2, ".h", ".2.h");
1705+
ra::strings::Replace(backupCppFile2, ".cpp", ".2.cpp");
1706+
ASSERT_TRUE( ra::filesystem::CopyFile(headerFilePath, backupHeaderFile2) );
1707+
ASSERT_TRUE( ra::filesystem::CopyFile(cppFilePath, backupCppFile2) );
1708+
1709+
//assert the generated source files are different
1710+
bool identical = ra::testing::IsFileEquals(backupCppFile1.c_str(), backupCppFile2.c_str());
1711+
ASSERT_FALSE(identical);
1712+
1713+
//assert second file register itself
1714+
{
1715+
const char * token = "RegisterFile(&";
1716+
int line = -1;
1717+
int col = -1;
1718+
std::string content;
1719+
bool readed = ra::filesystem::ReadTextFile(backupCppFile2.c_str(), content);
1720+
ASSERT_TRUE( readed );
1721+
bool textFound = ra::testing::FindInFile(backupCppFile2.c_str(), token, line, col);
1722+
ASSERT_TRUE(textFound) << "The token '" << token << "' was NOT found in file '" << backupCppFile2.c_str() << "'.\n\n" << content;
1723+
}
1724+
1725+
//cleanup
1726+
ASSERT_TRUE(deleteFile(outputFilePath.c_str()));
1727+
ASSERT_TRUE(deleteFile(headerFilePath.c_str()));
1728+
ASSERT_TRUE(deleteFile(cppFilePath.c_str()));
1729+
ASSERT_TRUE(deleteFile(backupHeaderFile1.c_str()));
1730+
ASSERT_TRUE(deleteFile(backupCppFile1.c_str()));
1731+
ASSERT_TRUE(deleteFile(backupHeaderFile2.c_str()));
1732+
ASSERT_TRUE(deleteFile(backupCppFile2.c_str()));
1733+
}
1734+
}

0 commit comments

Comments
 (0)