Skip to content

Commit fcb5c3b

Browse files
committed
Fixed #4 Implement getEnv
1 parent 20d0bb2 commit fcb5c3b

File tree

12 files changed

+109
-17
lines changed

12 files changed

+109
-17
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ script:
1919
- ./build_simple.sh
2020
- cd unit-tests.wsjcpp
2121
- ./build_simple.sh
22-
- ./unit-tests
22+
- ./run_unit-tests

.wsjcpp/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
logs/*
2+
cache/*
3+

src.wsjcpp/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Automaticly generated by wsjcpp@v0.0.1
22
cmake_minimum_required(VERSION 3.0)
33

4-
add_definitions(-DWSJCPP_VERSION="v0.0.5")
4+
add_definitions(-DWSJCPP_VERSION="v0.0.6")
55
add_definitions(-DWSJCPP_NAME="wsjcpp-core")
66

77
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
@@ -14,7 +14,7 @@ set (WSJCPP_LIBRARIES "")
1414
set (WSJCPP_INCLUDE_DIRS "")
1515
set (WSJCPP_SOURCES "")
1616

17-
# required-libraries
18-
list (APPEND WSJCPP_LIBRARIES "-lpthread")
17+
find_package(Threads REQUIRED)
18+
list (APPEND WSJCPP_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
1919

2020

src/wsjcpp_core.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,16 @@ std::string WSJCppCore::extractURLProtocol(const std::string& sValue) {
447447
return sRet;
448448
}
449449

450+
// ---------------------------------------------------------------------
451+
452+
bool WSJCppCore::getEnv(const std::string& sName, std::string& sValue) {
453+
if (const char* env_p = std::getenv(sName.c_str())) {
454+
sValue = std::string(env_p);
455+
return true;
456+
}
457+
return false;
458+
}
459+
450460
// ---------------------------------------------------------------------
451461
// WSJCppLog
452462

src/wsjcpp_core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class WSJCppCore {
5353
static unsigned long convertVoidToULong(void *p);
5454
static std::string getPointerAsHex(void *p);
5555
static std::string extractURLProtocol(const std::string& sValue);
56+
static bool getEnv(const std::string& sName, std::string& sValue);
5657
};
5758

5859

unit-tests.wsjcpp/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
tmp/*
22
logs/*
33
unit-tests
4+
.wsjcpp/*
5+
6+
# Vim temp files
7+
*.swp
48

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Automaticly generated by wsjcpp@v0.0.1
22
cmake_minimum_required(VERSION 3.0)
33

4-
project(unit-tests)
4+
project(unit-tests C CXX)
5+
add_definitions(-DWSJCPP_VERSION="ut-v0.0.6")
6+
add_definitions(-DWSJCPP_NAME="unit-tests-wsjcpp-core")
57

68
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
79
set(MACOSX TRUE)
@@ -14,7 +16,10 @@ set (WSJCPP_LIBRARIES "")
1416
set (WSJCPP_INCLUDE_DIRS "")
1517
set (WSJCPP_SOURCES "")
1618

17-
# wsjcpp-core:v0.0.5
19+
find_package(Threads REQUIRED)
20+
list (APPEND WSJCPP_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
21+
22+
# wsjcpp-core:v0.0.6
1823
list (APPEND WSJCPP_INCLUDE_DIRS "../src")
1924
list (APPEND WSJCPP_SOURCES "../src/wsjcpp_core.cpp")
2025
list (APPEND WSJCPP_SOURCES "../src/wsjcpp_core.h")
@@ -34,9 +39,10 @@ list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_to_upper.h")
3439
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_to_upper.cpp")
3540
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_create_uuid.h")
3641
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_create_uuid.cpp")
42+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_get_env.h")
43+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_get_env.cpp")
3744

38-
# required-libraries
39-
list (APPEND WSJCPP_LIBRARIES "-lpthread")
45+
include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt)
4046

4147

4248
include_directories(${WSJCPP_INCLUDE_DIRS})
@@ -52,3 +58,4 @@ install(
5258
/usr/bin
5359
)
5460

61+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## You can here write some custom includes
2+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
export TESTENVVALUE1="testvalue1"
4+
export TEST_ENV_VALUE2="test some value2"
5+
export "TEST_ENV_VALUE3"="932749387"
6+
7+
./unit-tests
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "unit_test_get_env.h"
2+
#include <vector>
3+
#include <wsjcpp_core.h>
4+
5+
REGISTRY_UNIT_TEST(UnitTestGetEnv)
6+
7+
UnitTestGetEnv::UnitTestGetEnv()
8+
: WSJCppUnitTestBase("UnitTestGetEnv") {
9+
}
10+
11+
// ---------------------------------------------------------------------
12+
13+
void UnitTestGetEnv::init() {
14+
// nothing
15+
}
16+
17+
// ---------------------------------------------------------------------
18+
19+
bool UnitTestGetEnv::run() {
20+
bool bTestSuccess = true;
21+
22+
struct LTest {
23+
LTest(const std::string &sName, bool bExpectedResult, const std::string &sExpectedValue) {
24+
this->sName = sName;
25+
this->sExpectedValue = sExpectedValue;
26+
this->bExpectedResult = bExpectedResult;
27+
};
28+
std::string sName;
29+
bool bExpectedResult;
30+
std::string sExpectedValue;
31+
};
32+
33+
std::vector<LTest> tests;
34+
tests.push_back(LTest("TESTENVVALUE1", true, "testvalue1"));
35+
tests.push_back(LTest("TESTENVVALUE1_", false, ""));
36+
tests.push_back(LTest("TEST_ENV_VALUE2", true, "test some value2"));
37+
tests.push_back(LTest("TEST_ENV_VALUE3", true, "932749387"));
38+
39+
for (int i = 0; i < tests.size(); i++) {
40+
LTest test = tests[i];
41+
std::string sValue;
42+
bool bResult = WSJCppCore::getEnv(test.sName, sValue);
43+
std::string testBaseName = "test#" + std::to_string(i) + " (" + test.sName + ")";
44+
compareB(bTestSuccess, testBaseName + "-result", bResult, test.bExpectedResult);
45+
compareS(bTestSuccess, testBaseName + "-value", sValue, test.sExpectedValue);
46+
}
47+
return bTestSuccess;
48+
}
49+

0 commit comments

Comments
 (0)