From 65b7ec18c1cd359da3e17eadd123a4d1914b8568 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Joaquim=20Casals=20Bu=C3=B1uel?= <jcasals@iri.upc.edu>
Date: Mon, 18 Feb 2019 09:50:37 +0100
Subject: [PATCH] Fixed relative paths tests

---
 hello_plugin/hello_plugin.cpp                 |  2 +-
 hello_plugin/params_autoconf.cpp              |  2 +-
 .../base/yaml}/parser_yaml.hpp                | 36 ++++++++++++++++---
 test/gtest_param_server.cpp                   | 11 +++---
 test/gtest_parser_yaml.cpp                    | 13 ++++---
 test/params1.yaml                             |  2 +-
 test/params3.yaml                             |  4 +--
 7 files changed, 49 insertions(+), 21 deletions(-)
 rename {hello_plugin => include/base/yaml}/parser_yaml.hpp (87%)

diff --git a/hello_plugin/hello_plugin.cpp b/hello_plugin/hello_plugin.cpp
index 9077d04f3..8091a6f1f 100644
--- a/hello_plugin/hello_plugin.cpp
+++ b/hello_plugin/hello_plugin.cpp
@@ -9,7 +9,7 @@
 #include "base/wolf.h"
 // #include "sensor_odom_2D.cpp"
 #include <yaml-cpp/yaml.h>
-#include "parser_yaml.hpp"
+#include "base/yaml/parser_yaml.hpp"
 #include "base/params_server.hpp"
 
 #include "../hello_wolf/capture_range_bearing.h"
diff --git a/hello_plugin/params_autoconf.cpp b/hello_plugin/params_autoconf.cpp
index cf95715f8..ead3ae34a 100644
--- a/hello_plugin/params_autoconf.cpp
+++ b/hello_plugin/params_autoconf.cpp
@@ -9,7 +9,7 @@
 #include "base/wolf.h"
 // #include "sensor_odom_2D.cpp"
 #include <yaml-cpp/yaml.h>
-#include "parser_yaml.hpp"
+#include "base/yaml/parser_yaml.hpp"
 #include "base/params_server.hpp"
 
 #include "../hello_wolf/capture_range_bearing.h"
diff --git a/hello_plugin/parser_yaml.hpp b/include/base/yaml/parser_yaml.hpp
similarity index 87%
rename from hello_plugin/parser_yaml.hpp
rename to include/base/yaml/parser_yaml.hpp
index b505b32ec..491c15f6b 100644
--- a/hello_plugin/parser_yaml.hpp
+++ b/include/base/yaml/parser_yaml.hpp
@@ -62,6 +62,8 @@ class parserYAML {
     vector<ParamsInitProcessor> _paramsProc;
     vector<string> _files;
     string _file;
+    bool _relative_path;
+    string _path_root;
 public:
     parserYAML(){
         _params = map<string, string>();
@@ -70,6 +72,8 @@ public:
         _paramsProc = vector<ParamsInitProcessor>();
         _file = "";
         _files = vector<string>();
+        _path_root = "";
+        _relative_path = false;
     }
     parserYAML(string file){
         _params = map<string, string>();
@@ -78,6 +82,18 @@ public:
         _paramsProc = vector<ParamsInitProcessor>();
         _files = vector<string>();
         _file = file;
+        _path_root = "";
+        _relative_path = false;
+    }
+    parserYAML(string file, string path_root){
+        _params = map<string, string>();
+        _active_name = "";
+        _paramsSens = vector<ParamsInitSensor>();
+        _paramsProc = vector<ParamsInitProcessor>();
+        _files = vector<string>();
+        _file = file;
+        _path_root = path_root;
+        _relative_path = true;
     }
     ~parserYAML(){
         //
@@ -104,16 +120,25 @@ string parserYAML::tagsToString(vector<std::string> &tags){
     return hdr;
 }
 void parserYAML::walkTree(string file){
-    YAML::Node n = YAML::LoadFile(file);
+    YAML::Node n;
+    // cout << "RELATIVE? " << _relative_path << " path root " << _path_root << " file " << file << endl;
+    if(not _relative_path) n = YAML::LoadFile(file);
+    else n = YAML::LoadFile(_path_root + file);
     vector<string> hdrs = vector<string>();
     walkTreeR(n, hdrs, "");
 }
 void parserYAML::walkTree(string file, vector<string>& tags){
-    YAML::Node n = YAML::LoadFile(file);
+    YAML::Node n;
+    // cout << "RELATIVE? " << _relative_path << " path root " << _path_root << " file " << file << endl;
+    if(not _relative_path) n = YAML::LoadFile(file);
+    else n = YAML::LoadFile(_path_root + file);
     walkTreeR(n, tags, "");
 }
 void parserYAML::walkTree(string file, vector<string>& tags, string hdr){
-    YAML::Node n = YAML::LoadFile(file);
+    YAML::Node n;
+    // cout << "RELATIVE? " << _relative_path << " path root " << _path_root << " file " << file << endl;
+    if(not _relative_path) n = YAML::LoadFile(file);
+    else n = YAML::LoadFile(_path_root + file);
     walkTreeR(n, tags, hdr);
 }
 void parserYAML::walkTreeR(YAML::Node n, vector<string>& tags, string hdr){
@@ -172,7 +197,10 @@ void parserYAML::updateActiveName(string tag){
     this->_active_name = tag;
 }
 void parserYAML::parseFirstLevel(string file){
-    YAML::Node n = YAML::LoadFile(file);
+    YAML::Node n;
+    // cout << "RELATIVE? " << _relative_path << " path root " << _path_root << " file " << file << endl;
+    if(not _relative_path) n = YAML::LoadFile(file);
+    else n = YAML::LoadFile(_path_root + file);
     YAML::Node n_config = n["config"];
     assert(n_config.Type() == YAML::NodeType::Map && "trying to parse config node but found a non-Map node");
     for(const auto& kv : n_config["sensors"]){
diff --git a/test/gtest_param_server.cpp b/test/gtest_param_server.cpp
index 4c85b1f77..9dc28c7e4 100644
--- a/test/gtest_param_server.cpp
+++ b/test/gtest_param_server.cpp
@@ -1,23 +1,24 @@
 #include "utils_gtest.h"
 #include "base/converter.h"
 #include "base/wolf.h"
-#include "../hello_plugin/parser_yaml.hpp"
+#include "base/yaml/parser_yaml.hpp"
 #include "base/params_server.hpp"
 
 using namespace std;
 using namespace wolf;
 
-parserYAML parse(string _file)
+std::string wolf_root = _WOLF_ROOT_DIR;
+
+parserYAML parse(string _file, string _path_root)
 {
-  parserYAML parser = parserYAML(_file);
+  parserYAML parser = parserYAML(_file, _path_root);
   parser.parse();
   return parser;
 }
 
 TEST(ParamsServer, Default)
 {
-  std::string wolf_root = _WOLF_ROOT_DIR;
-  auto parser = parse(wolf_root + "/test/params1.yaml");
+  auto parser = parse("/test/params1.yaml", wolf_root);
   auto params = parser.getParams();
   paramsServer server = paramsServer(params, parser.sensorsSerialization(), parser.processorsSerialization());
   EXPECT_EQ(server.getParam<double>("should_not_exist", "2.6"), 2.6);
diff --git a/test/gtest_parser_yaml.cpp b/test/gtest_parser_yaml.cpp
index b7710b3ca..4ed91f95b 100644
--- a/test/gtest_parser_yaml.cpp
+++ b/test/gtest_parser_yaml.cpp
@@ -1,24 +1,23 @@
 #include "utils_gtest.h"
 #include "base/converter.h"
 #include "base/wolf.h"
-#include "../hello_plugin/parser_yaml.hpp"
+#include "base/yaml/parser_yaml.hpp"
 
 using namespace std;
 using namespace wolf;
 
 std::string wolf_root = _WOLF_ROOT_DIR;
 
-parserYAML parse(string _file)
+parserYAML parse(string _file, string _path_root)
 {
-  parserYAML parser = parserYAML(_file);
+  parserYAML parser = parserYAML(_file, _path_root);
   parser.parse();
   return parser;
 }
 
 TEST(ParserYAML, RegularParse)
 {
-  cout << "WOOT " << wolf_root + "/test/params1.yaml" << endl;
-  auto parser = parse(wolf_root + "/test/params1.yaml");
+  auto parser = parse("/test/params1.yaml", wolf_root);
   auto params = parser.getParams();
   // for(auto it : params)
   //   cout << it.first << " %% " << it.second << endl;
@@ -27,13 +26,13 @@ TEST(ParserYAML, RegularParse)
 }
 TEST(ParserYAML, ParseMap)
 {
-  auto parser = parse(wolf_root + "/test/params2.yaml");
+  auto parser = parse("/test/params2.yaml", wolf_root);
   auto params = parser.getParams();
   EXPECT_EQ(params["processor1/mymap"], "[{k1:v1},{k2:v2},{k3:[v3,v4,v5]}]");
 }
 TEST(ParserYAML, JumpFile)
 {
-  auto parser = parse(wolf_root + "/test/params3.yaml");
+  auto parser = parse("/test/params3.yaml", wolf_root);
   auto params = parser.getParams();
   EXPECT_EQ(params["my_proc_test/max_buff_length"], "100");
   EXPECT_EQ(params["my_proc_test/jump/voting_active"], "false");
diff --git a/test/params1.yaml b/test/params1.yaml
index 9281f2ce9..5f89b4307 100644
--- a/test/params1.yaml
+++ b/test/params1.yaml
@@ -24,7 +24,7 @@ config:
       type: "ODOM 2D"
       name: "my_proc_test"
       sensorname: "odom"
-      follow: "../test/params3.1.yaml"
+      follow: "/test/params3.1.yaml"
 files:
   - "/home/jcasals/workspace/wip/wolf/lib/libsensor_odo.so"
   - "/home/jcasals/workspace/wip/wolf/lib/librange_bearing.so"
\ No newline at end of file
diff --git a/test/params3.yaml b/test/params3.yaml
index ee07b011e..3917ee1cd 100644
--- a/test/params3.yaml
+++ b/test/params3.yaml
@@ -24,8 +24,8 @@ config:
       type: "ODOM 2D"
       name: "my_proc_test"
       sensorname: "odom"
-      follow: "../test/params3.1.yaml"
-      jump: "@../test/params3.1.yaml"
+      follow: "/test/params3.1.yaml"
+      jump: "@/test/params3.1.yaml"
 files:
   - "/home/jcasals/workspace/wip/wolf/lib/libsensor_odo.so"
   - "/home/jcasals/workspace/wip/wolf/lib/librange_bearing.so"
\ No newline at end of file
-- 
GitLab