Skip to content

Commit ca57884

Browse files
committed
redesign search path to test data
1 parent 0505da8 commit ca57884

File tree

8 files changed

+82
-89
lines changed

8 files changed

+82
-89
lines changed

src/main.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml
3434
#include "examples.h"
3535
#include "wsjcpp_yaml.h"
3636

37-
// #include "wsjcpp_core.h"
38-
3937
class MyLogger : public IWsjcppYamlLog {
4038
public:
4139
// IWsjcppYamlLog
@@ -76,19 +74,11 @@ int _tmain(int argc, _TCHAR* argv[]) {
7674
int main(int argc, char* argv[]) {
7775
#endif
7876
std::string TAG = "MAIN";
79-
// WsjcppCore::initRandom();
80-
// std::string appName = std::string(WSJCPP_APP_NAME);
81-
// std::string appVersion = std::string(WSJCPP_APP_VERSION);
82-
// std::string appLogPath = ".wsjcpp-yaml-logs";
83-
// WsjcppLog::setLogDirectory(appLogPath);
84-
// if (!WsjcppCore::dirExists(appLogPath)) {
85-
// WsjcppCore::makeDir(appLogPath);
86-
// }
8777
MyLogger *pLogger = new MyLogger();
8878

8979
WsjcppYaml yaml;
9080
yaml.setLogger(pLogger);
91-
std::string sError;
81+
std::string sError;
9282
std::string sFilePath = "./unit-tests.wsjcpp/data-tests/read-file/example-voiting-app/docker-compose.yml";
9383
std::cout << "Reading file " << sFilePath << "..." << std::endl;
9484
if (!yaml.loadFromFile(sFilePath, sError)) {
@@ -97,7 +87,7 @@ int main(int argc, char* argv[]) {
9787
return -1;
9888
}
9989
std::cout << "Done." << std::endl;
100-
90+
10191
std::cout << "Saving to file " << sFilePath << "..." << std::endl;
10292
if (!yaml.saveToFile(sFilePath, sError)) {
10393
std::cerr << "Could not save data to file: " << sFilePath.c_str() << std::endl << "Error: " << sError.c_str() << std::endl;

src/tests/helpers.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
/*
3+
MIT License
4+
5+
Copyright (c) 2019-2025 wsjcpp
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.
24+
25+
Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml
26+
*/
27+
28+
#pragma once
29+
30+
#include <string>
31+
#include <fstream>
32+
33+
#if defined(_WIN32)
34+
#include <direct.h>
35+
#define GetCurrentDir _getcwd
36+
#else
37+
#include <unistd.h>
38+
#define GetCurrentDir getcwd
39+
#endif
40+
41+
static std::string find_test_data_file(const std::string &sFilePath) {
42+
char buff[FILENAME_MAX];
43+
char *path = GetCurrentDir(buff, FILENAME_MAX);
44+
if (path == NULL) {
45+
std::cerr << "ERROR: Could not get current directory path" << std::endl;
46+
return "did_not_found_file: " + sFilePath;
47+
}
48+
std::cout << "Current path: " << buff << std::endl;
49+
50+
std::string sRet = "../../src/tests/" + sFilePath;
51+
52+
// find path
53+
for (int i = 0; i < 5; i++) {
54+
std::ifstream file_(sRet.c_str());
55+
if (!file_) {
56+
sRet = "../" + sRet;
57+
} else {
58+
return sRet;
59+
}
60+
}
61+
return sFilePath;
62+
}

src/tests/test_double_value.cpp

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,11 @@ Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml
2929
#include <cmath>
3030
#include <fstream>
3131
#include <wsjcpp_yaml.h>
32-
33-
#if defined(_WIN32)
34-
#include <direct.h>
35-
#define GetCurrentDir _getcwd
36-
#else
37-
#include <unistd.h>
38-
#define GetCurrentDir getcwd
39-
#endif
32+
#include "helpers.h"
4033

4134
int main() {
42-
char buff[FILENAME_MAX];
43-
GetCurrentDir(buff, FILENAME_MAX );
44-
std::cout << "Current path: " << buff << std::endl;
45-
std::string sFilepath = "../../../src/tests/data/float-double/example.yml";
46-
// find path
47-
{
48-
std::ifstream file_(sFilepath.c_str());
49-
if (!file_) {
50-
sFilepath = "../" + sFilepath;
51-
}
52-
}
35+
std::string sFilepath = find_test_data_file("data/float-double/example.yml");
36+
5337
WsjcppYaml yaml;
5438
std::string sError;
5539
if (!yaml.loadFromFile(sFilepath, sError)) {
@@ -108,4 +92,4 @@ int main() {
10892
// compareD("has double value is 1.0003 (double 2)", yaml["double"].valDouble(), 1.0003f);
10993

11094
return ret;
111-
}
95+
}

src/tests/test_float_value.cpp

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,10 @@ Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml
2929
#include <cmath>
3030
#include <fstream>
3131
#include <wsjcpp_yaml.h>
32-
33-
#if defined(_WIN32)
34-
#include <direct.h>
35-
#define GetCurrentDir _getcwd
36-
#else
37-
#include <unistd.h>
38-
#define GetCurrentDir getcwd
39-
#endif
32+
#include "helpers.h"
4033

4134
int main() {
42-
char buff[FILENAME_MAX];
43-
GetCurrentDir(buff, FILENAME_MAX);
44-
std::cout << "Current path: " << buff << std::endl;
45-
std::string sFilepath = "../../../src/tests/data/float-double/example.yml";
46-
// find path
47-
{
48-
std::ifstream file_(sFilepath.c_str());
49-
if (!file_) {
50-
sFilepath = "../" + sFilepath;
51-
}
52-
}
35+
std::string sFilepath = find_test_data_file("data/float-double/example.yml");
5336

5437
WsjcppYaml yaml;
5538
std::string sError;
@@ -85,4 +68,4 @@ int main() {
8568
}
8669

8770
return ret;
88-
}
71+
}

src/tests/test_memory_leaks_linux.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml
2525
*/
2626

2727
#include <iostream>
28-
#include <fstream>
2928
#include <wsjcpp_yaml.h>
29+
#include "helpers.h"
3030

3131
#if defined(_WIN32)
3232
int main() {
@@ -39,14 +39,7 @@ int main() {
3939
#include "process_mem_usage.h"
4040

4141
int createManyTimesObjects() {
42-
std::string sFilepath = "../../../src/tests/data/for-memory-leak/some.yml";
43-
// find path
44-
{
45-
std::ifstream file_(sFilepath.c_str());
46-
if (!file_) {
47-
sFilepath = "../" + sFilepath;
48-
}
49-
}
42+
std::string sFilepath = find_test_data_file("data/for-memory-leak/some.yml");
5043

5144
std::string sError;
5245
for (int i = 0; i < 10000; i++) {

src/tests/test_read_write_file.cpp

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,12 @@ Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml
2828
#include <sstream>
2929
#include <fstream>
3030
#include <wsjcpp_yaml.h>
31+
#include "helpers.h"
3132

3233
int main() {
33-
std::string sFilepath = "../../../src/tests/data/read-write-file/docker-compose.yml";
34-
// find path
35-
{
36-
std::ifstream file_(sFilepath.c_str());
37-
if (!file_) {
38-
sFilepath = "../" + sFilepath;
39-
}
40-
}
4134

42-
std::string sFilepathOutput = "../../../src/tests/data/read-write-file/docker-compose.output.yml";
43-
// find path
44-
{
45-
std::ifstream file_(sFilepathOutput.c_str());
46-
if (!file_) {
47-
sFilepathOutput = "../" + sFilepathOutput;
48-
}
49-
}
35+
std::string sFilepath = find_test_data_file("data/read-write-file/docker-compose.yml");
36+
std::string sFilepathOutput = find_test_data_file("data/read-write-file/docker-compose.output.yml");
5037

5138
WsjcppYaml yaml;
5239
std::string sError;
@@ -116,4 +103,4 @@ int main() {
116103
}
117104

118105
return ret;
119-
}
106+
}

src/tests/test_read_yaml.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml
2525
*/
2626

2727
#include <wsjcpp_yaml.h>
28+
#include "helpers.h"
2829

2930
int main() {
3031
WsjcppYaml yaml;
31-
std::string sFilepath = "../../../../src/tests/data/read-file/example-voiting-app/docker-compose.yml";
32+
std::string sFilepath = find_test_data_file("data/read-file/example-voiting-app/docker-compose.yml");
3233
std::string sError;
3334
if (!yaml.loadFromFile(sFilepath, sError)) {
3435
std::cerr << "Error parsing: " << sError << std::endl;

src/tests/test_remove_element_from_array.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,11 @@ Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml
2525
*/
2626

2727
#include <iostream>
28-
#include <fstream>
2928
#include <wsjcpp_yaml.h>
29+
#include "helpers.h"
3030

3131
int main() {
32-
std::string sFilepath = "../../../src/tests/data/remove-element-from-array.yml";
33-
// find path
34-
{
35-
std::ifstream file_(sFilepath.c_str());
36-
if (!file_) {
37-
sFilepath = "../" + sFilepath;
38-
}
39-
}
32+
std::string sFilepath = find_test_data_file("data/remove-element-from-array.yml");
4033

4134
WsjcppYaml yaml;
4235
std::string sError;
@@ -96,4 +89,4 @@ int main() {
9689
}
9790

9891
return ret;
99-
}
92+
}

0 commit comments

Comments
 (0)