Skip to content

Commit f78eeca

Browse files
committed
Added unit-test for ListOfDirs
1 parent cd4a7b0 commit f78eeca

File tree

9 files changed

+64
-8
lines changed

9 files changed

+64
-8
lines changed

src/wsjcpp_core.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -260,17 +260,20 @@ std::vector<std::string> WsjcppCore::listOfDirs(const std::string &sDirname) {
260260
return vDirs;
261261
}
262262
DIR *dir = opendir(sDirname.c_str());
263-
struct dirent *entry = readdir(dir);
264-
while (entry != NULL) {
265-
if (entry->d_type == DT_DIR) {
266-
std::string sDir(entry->d_name);
267-
if (sDir != "." && sDir != "..") {
268-
vDirs.push_back(sDir);
263+
if (dir != NULL) {
264+
struct dirent *entry = readdir(dir);
265+
while (entry != NULL) {
266+
if (entry->d_type == DT_DIR) {
267+
std::string sDir(entry->d_name);
268+
if (sDir != "." && sDir != "..") {
269+
vDirs.push_back(sDir);
270+
}
269271
}
272+
entry = readdir(dir);
270273
}
271-
entry = readdir(dir);
274+
closedir(dir);
272275
}
273-
closedir(dir);
276+
std::sort(vDirs.begin(), vDirs.end());
274277
return vDirs;
275278
}
276279

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_get_human_size_b
6363
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_get_human_size_bytes.cpp")
6464
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_test_resources.h")
6565
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_test_resources.cpp")
66+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_list_of_dirs.h")
67+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_list_of_dirs.cpp")
6668

6769
include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt)
6870

unit-tests.wsjcpp/data/list_of_dirs/a1/.gitkeeper

Whitespace-only changes.

unit-tests.wsjcpp/data/list_of_dirs/b2/.gitkeeper

Whitespace-only changes.

unit-tests.wsjcpp/data/list_of_dirs/c3/.gitkeeper

Whitespace-only changes.

unit-tests.wsjcpp/data/list_of_dirs/d4/.gitkeeper

Whitespace-only changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "unit_test_list_of_dirs.h"
2+
#include <vector>
3+
#include <wsjcpp_core.h>
4+
#include <algorithm>
5+
6+
REGISTRY_WSJCPP_UNIT_TEST(UnitTestListOfDirs)
7+
8+
UnitTestListOfDirs::UnitTestListOfDirs()
9+
: WsjcppUnitTestBase("UnitTestListOfDirs") {
10+
}
11+
12+
// ---------------------------------------------------------------------
13+
14+
void UnitTestListOfDirs::init() {
15+
// nothing
16+
}
17+
18+
// ---------------------------------------------------------------------
19+
20+
bool UnitTestListOfDirs::run() {
21+
bool bTestSuccess = true;
22+
std::vector<std::string> vDirs = WsjcppCore::listOfDirs("./data/list_of_dirs");
23+
24+
compareN(bTestSuccess, "size", vDirs.size(), 4);
25+
if (vDirs.size() == 4) {
26+
compareS(bTestSuccess, "name of dir1", vDirs[0], "a1");
27+
compareS(bTestSuccess, "name of dir2", vDirs[1], "b2");
28+
compareS(bTestSuccess, "name of dir3", vDirs[2], "c3");
29+
compareS(bTestSuccess, "name of dir4", vDirs[3], "d4");
30+
}
31+
32+
return bTestSuccess;
33+
}
34+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef UNIT_TEST_LIST_OF_DIRS_H
2+
#define UNIT_TEST_LIST_OF_DIRS_H
3+
4+
#include <wsjcpp_unit_tests.h>
5+
6+
// Description: TODO
7+
class UnitTestListOfDirs : public WsjcppUnitTestBase {
8+
public:
9+
UnitTestListOfDirs();
10+
virtual void init();
11+
virtual bool run();
12+
};
13+
14+
#endif // UNIT_TEST_LIST_OF_DIRS_H
15+

wsjcpp.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,5 @@ unit-tests:
7777
description: "Test function get human size in bytes"
7878
- name: "TestResources"
7979
description: "Test basic resources"
80+
- name: "ListOfDirs"
81+
description: "Check list of directories"

0 commit comments

Comments
 (0)