PhoenixMock  1.8.7
Tools to split/merge/print mock used in Phoenix
main.cpp
Go to the documentation of this file.
1 
2 /***************************************
3  Auteur : Pierre Aubert
4  Mail : pierre.aubert@lapp.in2p3.fr
5  Licence : CeCILL-C
6 ****************************************/
7 
8 #include <map>
9 #include <iostream>
10 #include "phoenix_assert.h"
11 #include "phoenix_check.h"
12 
13 #include "string_utils.h"
14 #include "string_system.h"
15 
17 
20 void fillMapString(std::map<std::string, bool> & mapStr, const std::list<std::string> & listStr){
21  for(std::list<std::string>::const_iterator it(listStr.begin()); it != listStr.end(); ++it){
22  mapStr[*it] = true;
23  }
24 }
25 
27 
32 bool checkResultSeparator(const std::string & resultCmd, const std::string & expectedResult, char separator){
33  std::list<std::string> listRes = cutStringList(resultCmd, separator);
34  std::list<std::string> listExpectedRes = cutStringList(expectedResult, separator);
35 
36  std::map<std::string, bool> mapExpectedRes;
37  fillMapString(mapExpectedRes, listExpectedRes);
38 
39  bool b(true);
40  for(std::list<std::string>::const_iterator it(listRes.begin()); it != listRes.end(); ++it){
41  b &= mapExpectedRes.find(*it) != mapExpectedRes.end();
42  }
43 
44  return b;
45 }
46 
48 
52 bool checkResult(const std::string & resultCmd, const std::string & expectedResult){
53  if(!checkResultSeparator(resultCmd, expectedResult, ' ')){
54  if(!checkResultSeparator(resultCmd, expectedResult, '\n')){
55  return false;
56  }
57  }
58  return true;
59 }
60 
62 
69 bool checkOptionCompletion(const std::string & program, const std::string & partialOption,
70  const std::string & prevCursorOption, const std::string & cursorOption, const std::string & expectedResult)
71 {
72  std::string command(program + " __bashcompletionmode=\""+cursorOption+"\" __bashcompletionmodeprev=\""+prevCursorOption+"\" prgName " + partialOption);
73  std::string resultCmd(phoenix_popen(command));
74 // std::cout << "checkOptionCompletion : command '"<<command<<"':\n\tresult = '"<<resultCmd<<"', expectedResult = '"<<expectedResult<<"'" << std::endl;
75 
76  bool b(checkResult(resultCmd, expectedResult));
77  return b;
78 }
79 
82  phoenix_assert(checkOptionCompletion(PROGRAM_OPTION_COMPLETION_MODE, "", "", "", "class source --help -h --version -v \n"));
83  phoenix_assert(checkOptionCompletion(PROGRAM_OPTION_COMPLETION_MODE, "class", "class", "", "--name -n --template -t --config -c --help -h --version -v \n"));
84  phoenix_assert(checkOptionCompletion(PROGRAM_OPTION_COMPLETION_MODE, "class", "class", "-", "--name -n --template -t --config -c --help -h --version -v \n"));
85  phoenix_assert(checkOptionCompletion(PROGRAM_OPTION_COMPLETION_MODE, "class", "class", "--", "--name --template --config --help --version \n"));
86  phoenix_assert(checkOptionCompletion(PROGRAM_OPTION_COMPLETION_MODE, "class", "class", "--n", "--name \n"));
87  phoenix_assert(checkOptionCompletion(PROGRAM_OPTION_COMPLETION_MODE, "class --name=test --", "--name=test", "--", "--template --config --help --version \n"));
88  phoenix_assert(checkOptionCompletion(PROGRAM_OPTION_COMPLETION_MODE, "class --name=\"test\" --", "--name=\"test\"", "--", "--template --config --help --version \n"));
89  phoenix_assert(checkOptionCompletion(PROGRAM_OPTION_COMPLETION_MODE, "class -n \"test\" --", "\"test\"", "--", "--template --config --help --version \n"));
90  phoenix_assert(checkOptionCompletion(PROGRAM_OPTION_COMPLETION_MODE, "source", "source", "", "--name -n --help -h --version -v \n"));
91  phoenix_assert(checkOptionCompletion(PROGRAM_OPTION_COMPLETION_MODE, "class --config ", "--config", "", "./\n"));
92  phoenix_assert(checkOptionCompletion(PROGRAM_OPTION_COMPLETION_MODE, "class --config " DIR_COMPLETION, "--config", DIR_COMPLETION, DIR_COMPLETION "/0-someDir\n" DIR_COMPLETION "/1-someFile.txt\n\n"));
93  phoenix_assert(checkOptionCompletion(PROGRAM_OPTION_COMPLETION_MODE, "class --config " DIR_COMPLETION "/0-someDir", "--config", DIR_COMPLETION "/0-someDir", DIR_COMPLETION "/0-someDir/someFile.txt\n\n"));
94  phoenix_assert(checkOptionCompletion(PROGRAM_OPTION_COMPLETION_MODE, "class --config " DIR_COMPLETION "/0-someDir/someFile.txt", "--config", DIR_COMPLETION "/0-someDir/someFile.txt", "\n"));
95  phoenix_assert(checkOptionCompletion(PROGRAM_OPTION_COMPLETION_MODE, "class --config " DIR_COMPLETION "/0-someDir/some", "--config", DIR_COMPLETION "/0-someDir/some", DIR_COMPLETION "/0-someDir/someFile.txt\n\n"));
96  phoenix_assert(checkOptionCompletion(PROGRAM_OPTION_COMPLETION_MODE_DIR, "class --dir " DIR_COMPLETION "/0-some", "--dir", DIR_COMPLETION "/0-some", DIR_COMPLETION "/0-someDir\n\n"));
97  phoenix_assert(checkOptionCompletion(PROGRAM_OPTION_COMPLETION_MODE_DIR, "class --dir " DIR_COMPLETION, "--dir", DIR_COMPLETION, DIR_COMPLETION "/0-someDir\n\n"));
98 }
99 
100 int main(int argc, char** argv){
101  testCompletion();
102  return 0;
103 }
104 
105 
string_utils.h
checkOptionCompletion
bool checkOptionCompletion(const std::string &program, const std::string &partialOption, const std::string &prevCursorOption, const std::string &cursorOption, const std::string &expectedResult)
Check the option completion of a program.
Definition: main.cpp:21
fillMapString
void fillMapString(std::map< std::string, bool > &mapStr, const std::list< std::string > &listStr)
Create a map with a string.
Definition: main.cpp:20
checkResultSeparator
bool checkResultSeparator(const std::string &resultCmd, const std::string &expectedResult, char separator)
Check the restut.
Definition: main.cpp:32
phoenix_popen
std::string phoenix_popen(const std::string &command)
Execute the given command and returns the output of this command.
Definition: string_system.cpp:341
cutStringList
std::list< std::string > cutStringList(const std::string &str, char separator)
Cut a string the the given separator char.
Definition: string_function.cpp:428
testCompletion
void testCompletion()
Test the completion of the option.
Definition: main.cpp:32
phoenix_assert
#define phoenix_assert(isOk)
Definition: phoenix_assert.h:19
string_system.h
checkResult
bool checkResult(const std::string &resultCmd, const std::string &expectedResult)
Check the restut.
Definition: main.cpp:52
phoenix_assert.h
main
int main(int argc, char **argv)
Definition: main.cpp:85
phoenix_check.h