00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <stdexcept>
00033 #include <string>
00034 #include <vector>
00035 #include <stdlib.h>
00036 #include <unistd.h>
00037 #include <stdio.h>
00038 #include <time.h>
00039 #include <gtest/gtest.h>
00040 #include <boost/algorithm/string.hpp>
00041 #include <boost/filesystem.hpp>
00042 #include "rospack/rospack.h"
00043 #include "utils.h"
00044
00045
00046 TEST(rospack, reentrant)
00047 {
00048 rospack::ROSPack rp;
00049 std::string output;
00050 int ret = rp.run(std::string("plugins --attrib=foo --top=precedence1 roslang"));
00051 ASSERT_EQ(ret, 0);
00052 ret = rp.run(std::string("find roslang"));
00053 ASSERT_EQ(ret, 0);
00054 ret = rp.run(std::string("list-names"));
00055 ASSERT_EQ(ret, 0);
00056 std::vector<std::string> output_list;
00057 output = rp.getOutput();
00058 boost::trim(output);
00059 boost::split(output_list, output, boost::is_any_of("\n"));
00060 ASSERT_EQ((int)output_list.size(), 4);
00061 ret = rp.run(std::string("list"));
00062 ASSERT_EQ(ret, 0);
00063 output = rp.getOutput();
00064 boost::trim(output);
00065 boost::split(output_list, output, boost::is_any_of("\n"));
00066 ASSERT_EQ((int)output_list.size(), 4);
00067 std::vector<std::string> path_name;
00068 boost::split(path_name, output_list[0], boost::is_any_of(" "));
00069 ASSERT_EQ((int)path_name.size(), 2);
00070 }
00071
00072 TEST(rospack, multiple_rospack_objects)
00073 {
00074 rospack::ROSPack rp;
00075 std::string output;
00076 int ret = rp.run(std::string("plugins --attrib=foo --top=precedence1 roslang"));
00077 ASSERT_EQ(ret, 0);
00078 ret = rp.run(std::string("find roslang"));
00079 ASSERT_EQ(ret, 0);
00080 ret = rp.run(std::string("list-names"));
00081 ASSERT_EQ(ret, 0);
00082 std::vector<std::string> output_list;
00083 output = rp.getOutput();
00084 boost::trim(output);
00085 boost::split(output_list, output, boost::is_any_of("\n"));
00086 ASSERT_EQ((int)output_list.size(), 4);
00087 ret = rp.run(std::string("list"));
00088 ASSERT_EQ(ret, 0);
00089 output = rp.getOutput();
00090 boost::trim(output);
00091 boost::split(output_list, output, boost::is_any_of("\n"));
00092 ASSERT_EQ((int)output_list.size(), 4);
00093 std::vector<std::string> path_name;
00094 boost::split(path_name, output_list[0], boost::is_any_of(" "));
00095 ASSERT_EQ((int)path_name.size(), 2);
00096
00097 rospack::ROSPack rp2;
00098 ret = rp2.run(std::string("plugins --attrib=foo --top=precedence1 roslang"));
00099 ASSERT_EQ(ret, 0);
00100 ret = rp2.run(std::string("find roslang"));
00101 ASSERT_EQ(ret, 0);
00102 ret = rp2.run(std::string("list-names"));
00103 ASSERT_EQ(ret, 0);
00104 output_list.clear();
00105 output = rp2.getOutput();
00106 boost::trim(output);
00107 boost::split(output_list, output, boost::is_any_of("\n"));
00108 ASSERT_EQ((int)output_list.size(), 4);
00109 ret = rp2.run(std::string("list"));
00110 ASSERT_EQ(ret, 0);
00111 output = rp2.getOutput();
00112 boost::trim(output);
00113 boost::split(output_list, output, boost::is_any_of("\n"));
00114 ASSERT_EQ((int)output_list.size(), 4);
00115 path_name.clear();
00116 boost::split(path_name, output_list[0], boost::is_any_of(" "));
00117 ASSERT_EQ((int)path_name.size(), 2);
00118 }
00119
00120 TEST(rospack, deduplicate_tokens)
00121 {
00122 std::string input = "foo foo bar bat bar foobar batbar";
00123 std::string truth = "foo bar bat foobar batbar";
00124 std::string output;
00125 rospack::deduplicate_tokens(input, false, output);
00126 ASSERT_EQ(truth, output);
00127 }
00128
00129 int main(int argc, char **argv)
00130 {
00131
00132 (void)rospack::ROSPACK_NAME;
00133 (void)rospack::ROSSTACK_NAME;
00134
00135 char buf[1024];
00136 std::string rr = std::string(getcwd(buf, sizeof(buf))) + "/test2";
00137 setenv("ROS_ROOT", rr.c_str(), 1);
00138 unsetenv("ROS_PACKAGE_PATH");
00139 char path[PATH_MAX];
00140 if(getcwd(path,sizeof(path)))
00141 {
00142 boost::filesystem::path p(path);
00143 p = p.parent_path();
00144 std::string newpath = p.string();
00145 char* oldpath = getenv("PATH");
00146 if(oldpath)
00147 newpath += std::string(":") + oldpath;
00148 setenv("PATH", newpath.c_str(), 1);
00149 }
00150
00151 testing::InitGoogleTest(&argc, argv);
00152 return RUN_ALL_TESTS();
00153 }