Skip to content

Commit 399f83c

Browse files
committed
Fixed issues with environmentfunc.cpp, filesystemfunc.cpp and stringfunc.cpp. Added new unit test to test these APIs.
1 parent 8d50e05 commit 399f83c

File tree

11 files changed

+1024
-51
lines changed

11 files changed

+1024
-51
lines changed

src/bin2cpp_unittest/CMakeLists.txt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,26 @@ if (WIN32)
6969
)
7070
endif()
7171

72-
add_executable(bin2cpp_unittest application.cpp application.h gtesthelper.cpp gtesthelper.h TestCLI.cpp TestCLI.h TestCommon.cpp TestCommon.h TestExtraction.cpp TestExtraction.h main.cpp ${GENERATED_TEST_FILES})
72+
add_executable(bin2cpp_unittest
73+
application.cpp
74+
application.h
75+
gtesthelper.cpp
76+
gtesthelper.h
77+
TestCLI.cpp
78+
TestCLI.h
79+
TestCommon.cpp
80+
TestCommon.h
81+
TestExtraction.cpp
82+
TestExtraction.h
83+
TestEnvironmentFunc.cpp
84+
TestEnvironmentFunc.h
85+
TestFilesystemFunc.cpp
86+
TestFilesystemFunc.h
87+
TestStringFunc.cpp
88+
TestStringFunc.h
89+
main.cpp
90+
${GENERATED_TEST_FILES}
91+
)
7392

7493
if (WIN32)
7594
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include "TestEnvironmentFunc.h"
2+
#include "environmentfunc.h"
3+
4+
using namespace environment;
5+
6+
namespace environment { namespace test
7+
{
8+
9+
//--------------------------------------------------------------------------------------------------
10+
void TestEnvironmentFunc::SetUp()
11+
{
12+
}
13+
//--------------------------------------------------------------------------------------------------
14+
void TestEnvironmentFunc::TearDown()
15+
{
16+
}
17+
//--------------------------------------------------------------------------------------------------
18+
TEST_F(TestEnvironmentFunc, testGetEnvironmentVariable)
19+
{
20+
//test NULL
21+
{
22+
static const std::string EXPECTED = "";
23+
std::string actual = environment::getEnvironmentVariable(NULL);
24+
ASSERT_EQ(EXPECTED, actual);
25+
}
26+
27+
//test empty string
28+
{
29+
static const std::string EXPECTED = "";
30+
std::string actual = environment::getEnvironmentVariable("");
31+
ASSERT_EQ(EXPECTED, actual);
32+
}
33+
34+
//test not found
35+
{
36+
static const std::string EXPECTED = "";
37+
std::string actual = environment::getEnvironmentVariable("FOO_BAR_BIG_BANG");
38+
ASSERT_EQ(EXPECTED, actual);
39+
}
40+
41+
#ifdef WIN32
42+
//test TEMP
43+
{
44+
std::string actual = environment::getEnvironmentVariable("TEMP");
45+
ASSERT_NE("", actual);
46+
}
47+
#elif UNIX
48+
//test SHELL
49+
{
50+
std::string actual = environment::getEnvironmentVariable("SHELL");
51+
ASSERT_NE("", actual);
52+
}
53+
#endif
54+
}
55+
//--------------------------------------------------------------------------------------------------
56+
} // End namespace test
57+
} // End namespace environment
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef TESTENVIRONMENTFUNC_H
2+
#define TESTENVIRONMENTFUNC_H
3+
4+
#include <gtest/gtest.h>
5+
6+
namespace environment { namespace test
7+
{
8+
class TestEnvironmentFunc : public ::testing::Test
9+
{
10+
public:
11+
virtual void SetUp();
12+
virtual void TearDown();
13+
};
14+
15+
} // End namespace test
16+
} // End namespace environment
17+
18+
#endif //TESTENVIRONMENTFUNC_H
Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
#include "TestFilesystemFunc.h"
2+
#include "filesystemfunc.h"
3+
#include "gtesthelper.h"
4+
5+
using namespace filesystem;
6+
7+
namespace filesystem { namespace test
8+
{
9+
bool createDummyFile(const char * iPath)
10+
{
11+
FILE * f = fopen(iPath, "w");
12+
if (f == NULL)
13+
return false;
14+
fputs("FOO!\n", f);
15+
fputs("&\n", f);
16+
fputs("BAR\n", f);
17+
fclose(f);
18+
19+
return true;
20+
}
21+
22+
int countValues(const std::vector<std::string> & iList, const std::string & iValue)
23+
{
24+
int count = 0;
25+
for(size_t i=0; i<iList.size(); i++)
26+
{
27+
const std::string & value = iList[i];
28+
if (value == iValue)
29+
count++;
30+
}
31+
return count;
32+
}
33+
34+
//--------------------------------------------------------------------------------------------------
35+
void TestFilesystemFunc::SetUp()
36+
{
37+
}
38+
//--------------------------------------------------------------------------------------------------
39+
void TestFilesystemFunc::TearDown()
40+
{
41+
}
42+
//--------------------------------------------------------------------------------------------------
43+
TEST_F(TestFilesystemFunc, testGetFileSize)
44+
{
45+
//test NULL
46+
{
47+
const char * path = NULL;
48+
uint32_t size = filesystem::getFileSize(path);
49+
ASSERT_EQ(0, size);
50+
}
51+
52+
//test actual value
53+
{
54+
//create dummy file
55+
std::string filename = gTestHelper::getInstance().getTestQualifiedName();
56+
ASSERT_TRUE( createDummyFile(filename.c_str()) );
57+
58+
#ifdef WIN32
59+
static const uint32_t EXPECTED = 14;
60+
#elif UNIX
61+
static const uint32_t EXPECTED = 11;
62+
#endif
63+
64+
//test `const char *` api
65+
uint32_t size = filesystem::getFileSize(filename.c_str());
66+
ASSERT_EQ(EXPECTED, size);
67+
size = 0;
68+
69+
//test `FILE*` api
70+
FILE * ptr = fopen(filename.c_str(), "r");
71+
ASSERT_TRUE(ptr != NULL);
72+
size = filesystem::getFileSize(ptr);
73+
fclose(ptr);
74+
ASSERT_EQ(EXPECTED, size);
75+
}
76+
77+
}
78+
//--------------------------------------------------------------------------------------------------
79+
TEST_F(TestFilesystemFunc, testGetFilename)
80+
{
81+
//test NULL
82+
{
83+
static const std::string EXPECTED = "";
84+
std::string filename = filesystem::getFilename(NULL);
85+
ASSERT_EQ(EXPECTED, filename);
86+
}
87+
88+
//test filename only
89+
{
90+
static const std::string EXPECTED = "foo.bar";
91+
std::string filename = filesystem::getFilename("foo.bar");
92+
ASSERT_EQ(EXPECTED, filename);
93+
}
94+
95+
//test full path (unix style)
96+
{
97+
static const std::string EXPECTED = "foo.bar";
98+
std::string filename = filesystem::getFilename("/home/myFolder/foo.bar");
99+
ASSERT_EQ(EXPECTED, filename);
100+
}
101+
102+
//test full path (windows style)
103+
{
104+
static const std::string EXPECTED = "foo.bar";
105+
std::string filename = filesystem::getFilename("C:\\Users\\Antoine\\Desktop\\myFolder\\foo.bar");
106+
ASSERT_EQ(EXPECTED, filename);
107+
}
108+
}
109+
//--------------------------------------------------------------------------------------------------
110+
TEST_F(TestFilesystemFunc, testFileExists)
111+
{
112+
//test NULL
113+
{
114+
bool exists = filesystem::fileExists(NULL);
115+
ASSERT_FALSE(exists);
116+
}
117+
118+
//test not found
119+
{
120+
bool exists = filesystem::fileExists("foo.bar.notfound.bang");
121+
ASSERT_FALSE(exists);
122+
}
123+
124+
//test found
125+
{
126+
//create dummy file
127+
std::string filename = gTestHelper::getInstance().getTestQualifiedName();
128+
ASSERT_TRUE( createDummyFile(filename.c_str()) );
129+
130+
bool exists = filesystem::fileExists(filename.c_str());
131+
ASSERT_TRUE(exists);
132+
}
133+
}
134+
//--------------------------------------------------------------------------------------------------
135+
TEST_F(TestFilesystemFunc, testFolderExists)
136+
{
137+
//test NULL
138+
{
139+
bool exists = filesystem::folderExists(NULL);
140+
ASSERT_FALSE(exists);
141+
}
142+
143+
//test not found
144+
{
145+
bool exists = filesystem::folderExists("/home/fooBAR");
146+
ASSERT_FALSE(exists);
147+
}
148+
149+
//test found
150+
{
151+
//create dummy file
152+
std::string currentFolder = filesystem::getCurrentFolder();
153+
ASSERT_TRUE( !currentFolder.empty() );
154+
155+
bool exists = filesystem::folderExists(currentFolder.c_str());
156+
ASSERT_TRUE(exists);
157+
}
158+
}
159+
//--------------------------------------------------------------------------------------------------
160+
TEST_F(TestFilesystemFunc, testGetTemporaryFileName)
161+
{
162+
//test not empty
163+
{
164+
std::string filename = filesystem::getTemporaryFileName();
165+
ASSERT_TRUE( !filename.empty() );
166+
}
167+
168+
//test repetitive
169+
{
170+
std::vector<std::string> filenames;
171+
static const size_t numTest = 20;
172+
for(size_t i=0; i<numTest; i++)
173+
{
174+
std::string filename = filesystem::getTemporaryFileName();
175+
filenames.push_back(filename);
176+
}
177+
178+
//assert that all values are unique
179+
for(size_t i=0; i<numTest; i++)
180+
{
181+
const std::string & filename = filenames[i];
182+
int count = countValues(filenames, filename);
183+
ASSERT_EQ(1, count) << "Found value '" << filename << "' " << count << " times in the list.";
184+
}
185+
}
186+
}
187+
//--------------------------------------------------------------------------------------------------
188+
TEST_F(TestFilesystemFunc, testGetTemporaryFilePath)
189+
{
190+
//test not empty
191+
{
192+
std::string path = filesystem::getTemporaryFilePath();
193+
ASSERT_TRUE( !path.empty() );
194+
}
195+
196+
//test repetitive
197+
{
198+
std::vector<std::string> paths;
199+
static const size_t numTest = 20;
200+
for(size_t i=0; i<numTest; i++)
201+
{
202+
std::string path = filesystem::getTemporaryFilePath();
203+
paths.push_back(path);
204+
}
205+
206+
//assert that all values are unique
207+
for(size_t i=0; i<numTest; i++)
208+
{
209+
const std::string & path = paths[i];
210+
int count = countValues(paths, path);
211+
ASSERT_EQ(1, count) << "Found value '" << path << "' " << count << " times in the list.";
212+
}
213+
}
214+
}
215+
//--------------------------------------------------------------------------------------------------
216+
TEST_F(TestFilesystemFunc, testGetParentPath)
217+
{
218+
//test no folder
219+
{
220+
static const std::string EXPECTED = "";
221+
std::string parent = filesystem::getParentPath("filename.bar");
222+
ASSERT_EQ(EXPECTED, parent);
223+
}
224+
225+
//test unix style
226+
{
227+
static const std::string EXPECTED = "/home/myFolder";
228+
std::string parent = filesystem::getParentPath("/home/myFolder/foo.bar");
229+
ASSERT_EQ(EXPECTED, parent);
230+
}
231+
232+
//test windows style
233+
{
234+
static const std::string EXPECTED = "C:\\Users\\Antoine\\Desktop\\myFolder";
235+
std::string parent = filesystem::getParentPath("C:\\Users\\Antoine\\Desktop\\myFolder\\foo.bar");
236+
ASSERT_EQ(EXPECTED, parent);
237+
}
238+
}
239+
//--------------------------------------------------------------------------------------------------
240+
TEST_F(TestFilesystemFunc, testGetShortPathForm)
241+
{
242+
//test spaces in filename
243+
{
244+
static const std::string EXPECTED = "ABC~1.TXT";
245+
std::string shortPath = filesystem::getShortPathForm("a b c.txt");
246+
ASSERT_EQ(EXPECTED, shortPath);
247+
}
248+
249+
//test too long file extension
250+
{
251+
static const std::string EXPECTED = "ABCDEF~1.TEX";
252+
std::string shortPath = filesystem::getShortPathForm("abcdefgh.text");
253+
ASSERT_EQ(EXPECTED, shortPath);
254+
}
255+
256+
//test too long filename
257+
{
258+
static const std::string EXPECTED = "ABCDEF~1.TXT";
259+
std::string shortPath = filesystem::getShortPathForm("abcdefghijklmnopqrstuvwxyz.txt");
260+
ASSERT_EQ(EXPECTED, shortPath);
261+
}
262+
263+
//test spaces in file extension
264+
{
265+
static const std::string EXPECTED = "ABCDE~1.TXT";
266+
std::string shortPath = filesystem::getShortPathForm("abcde.t x t");
267+
ASSERT_EQ(EXPECTED, shortPath);
268+
}
269+
270+
//test program files (windows style)
271+
{
272+
static const std::string EXPECTED = "PROGRA~1";
273+
std::string shortPath = filesystem::getShortPathForm("Program Files (x86)");
274+
ASSERT_EQ(EXPECTED, shortPath);
275+
}
276+
}
277+
//--------------------------------------------------------------------------------------------------
278+
} // End namespace test
279+
} // End namespace filesystem

0 commit comments

Comments
 (0)