LRAUV  revA
CommandLine_Test.h
Go to the documentation of this file.
1 
9 #ifndef _COMMANDLINE_TEST_H_
10 #define _COMMANDLINE_TEST_H_
11 
12 #include "CommandLine.h"
13 #include "Supervisor.h"
14 #include "data/ConfigDataElement.h"
15 #include "data/Slate.h"
16 
17 #include <cxxtest/TestSuite.h>
18 
24 class CommandLine_Test : public CxxTest::TestSuite
25 {
26 public:
27 
28  // test parser
29  void testParse( void )
30  {
31  // Configure the slate for Unit testing
33 
34  // Create a test component with a name & elements that can be used later
35  TestAsyncComponent testComponent( "CommandLine_TestComponent" );
36 
37  // When creating a ConfigURI with no config file, have to initialize element
38  ConfigURI pauseCfg( testComponent.getName(), "pauseCfg", Units::SECOND );
39  ConfigDataElement* pauseCfgElement = new ConfigDataElement( pauseCfg, pauseCfg.getUnit().dataValue( pauseCfg.getBinaryType() ) );
40  Slate::MapDataElement( ( ConfigURI* )&pauseCfg, pauseCfgElement );
41 
42  DataURI waterTemp( testComponent.getName(), "waterTemp", Units::FAHRENHEIT );
44  Slate::CodifyElementURI( ( ElementURI& )waterTemp );
45  testComponent.newConfigReader( pauseCfg );
46 
47  int argc = 0;
48  char **argv = NULL;
49  Str dataDir;
50  Supervisor supervisor( argc, argv, dataDir );
51  CommandLine commandLine( supervisor );
52  for( unsigned int i = 0; i < commandLine.commands_.size(); ++i )
53  {
54  Command* command = commandLine.commands_[i];
55  for( int j = 0; j < command->getSyntaxCount(); ++j )
56  {
57  CommandSyntax* syntax = command->getSyntax( j );
58  int optional = 0;
59  // Loop through the syntax once, counting optional parts, then repeat omitting each optional part
60  for( int jj = -1; jj < optional; ++jj )
61  {
62  int jOptional = -1;
63  int kOptional = -1;
64  Str str = Str( command->getKeyword(), command->getKeywordLen() ) + " ";
65  for( int k = 0; k < syntax->getArgCount(); ++k )
66  {
67  CommandArg* arg = syntax->getArg( k );
68  if( jj < 0 )
69  {
70  if( arg->getRequired() == NOT_REQUIRED )
71  {
72  ++optional;
73  }
74  }
75  else
76  {
77  if( arg->getRequired() == NOT_REQUIRED )
78  {
79  ++jOptional;
80  if( jOptional == jj )
81  {
82  kOptional = k;
83  continue;
84  }
85  }
86  if( arg->getRequired() == REQUIRED_AFTER_PREVIOUS && kOptional == k - 1 )
87  {
88  ++kOptional;
89  continue;
90  }
91  }
92  switch( arg->getArgType() )
93  {
94  case ARG_NONE:
95  break;
96  case ARG_COMMAND:
97  str += "help ";
98  break;
99  case ARG_COMPONENT:
100  str += testComponent.getName() + " ";
101  break;
102  case ARG_CONFIG_DIR:
103  str += "sim ";
104  break;
105  case ARG_CONFIG_VARIABLE:
106  str += pauseCfg + " ";
107  break;
108  case ARG_DECIMATION_TYPE:
109  str += "NONE ";
110  break;
111  case ARG_FLOAT:
112  str += "12.34 ";
113  break;
114  case ARG_INT:
115  str += "1234 ";
116  break;
117  case ARG_KEYWORD:
118  str += Str( arg->getKeyword(), arg->getKeywordLen() ) + " ";
119  break;
120  case ARG_MISSION:
121  str += "Demo/NoMission.xml ";
122  break;
123  case ARG_QUOTED_STRING:
124  str += "\"quoted string\" ";
125  break;
126  case ARG_REGEX:
127  str += "[dD]epth ";
128  break;
129  case ARG_SECONDS:
130  str += "30 ";
131  break;
132  case ARG_SERVICE_TYPE:
133  str += "EXPRESS ";
134  break;
135  case ARG_STRING:
136  str += "a random string";
137  break;
138  case ARG_TIMESTAMP:
139  str += "19991231T1159 ";
140  break;
141  case ARG_TOKEN:
142  str += "token ";
143  break;
144  case ARG_UNIT:
145  str += "meter ";
146  break;
147  case ARG_UNIVERSAL:
148  str += "time ";
149  break;
150  case ARG_VARIABLE:
151  str += waterTemp + " ";
152  break;
153  }
154  }
155  printf( "parse: %s\n", str.cStr() );
158  bool ok = true;
159  for( size_t k = 0; ok && k + 1 < str.length(); ++k )
160  {
161  char theChar = str.cStr()[k];
163  CommandLine::ConsoleBuffer_[ CommandLine::InputIndex_ + 1 ] = 0;
165  //printf("CommandLine::ConsoleBuffer_[ %d ] = '%c', ok=%d\n",CommandLine::InputIndex_, theChar, ok );
166  if( ok )
167  {
169  }
170  }
171  TSM_ASSERT( str.cStr(), ok );
172  }
173  }
176  }
177  }
178 
179 };
180 
181 #endif // _COMMANDLINE_TEST_H
Unit tests for CommandLine class.
Definition: CommandLine_Test.h:24
int getArgCount() const
Definition: Command.h:229
FlexArray< Command * > commands_
Definition: CommandLine.h:80
const char * getKeyword()
Definition: Command.h:327
Definition: Command.h:25
CommandArg * getArg(int index) const
Definition: Command.h:204
const int getKeywordLen()
Definition: Command.h:331
static void MapDataElement(ElementURI *elementURI, DataElement *dataElement)
Maps the supplied DataElement to the suplied Uri.
Definition: Slate.cpp:436
Definition: Command.h:60
RequiredType getRequired() const
Definition: Command.h:87
static void CodifyElementURI(ElementURI &elementURI)
Adds a unique code to the elementURI.
Definition: Slate.cpp:258
Definition: Command.h:43
Definition: Command.h:33
static const BaseUnit SECOND
Definition: Units.h:43
size_t length() const
Definition: Str.h:196
Definition: Command.h:30
Provides a simple command line interface to the LRAUV.
Definition: CommandLine.h:38
Definition: ElementURI.h:166
Definition: Command.h:28
CommandArgType getArgType() const
Definition: Command.h:75
static void SimulateDirectoryWrites()
Definition: Slate.cpp:893
Contains the CommandLine class declaration.
Contains the Supervisor class declaration.
unsigned int size() const
Definition: FlexArray.cpp:69
static const Unit FAHRENHEIT
Definition: Units.h:177
ConfigReader * newConfigReader(const ConfigURI &configURI)
Definition: Component.cpp:578
Replacement for standard template class string.
Definition: Str.h:12
static char ConsoleBuffer_[BUFF_SIZE]
Definition: CommandLine.h:289
Definition: Command.h:42
CommandSyntax * getSyntax(int index)
Definition: Command.h:335
void testParse(void)
Definition: CommandLine_Test.h:29
const char * cStr() const
Definition: Str.h:188
Definition: Command.h:50
static const UniversalURI TIME
time, as defined by CF-metadata
Definition: UniversalURI.h:486
Definition: Command.h:35
Definition: Command.h:32
void clearParsed()
Definition: CommandLine.cpp:1941
Definition: ConfigDataElement.h:14
Definition: Command.h:188
Definition: Command.h:24
Definition: Command.h:27
int getSyntaxCount()
Definition: Command.h:339
Definition: ElementURI.h:229
Definition: Command.h:26
const CodedStr & getName(void) const
Returns the name.
Definition: Component.h:158
Definition: Command.h:292
bool parseBufferEnd()
If the last word in buffer is a valid arg, deal with it.
Definition: CommandLine.cpp:1757
Definition: Command.h:48
Code unit that represents a unique name for a DataElement.
Definition: ElementURI.h:27
Definition: Command.h:38
The supervisor is the "main" component in the vehicle software.
Definition: Supervisor.h:65
Definition: Command.h:36
static int InputIndex_
Definition: CommandLine.h:290
Definition: Command.h:41
Definition: Command.h:37
const char * getKeyword() const
Definition: Command.h:79
Dummy implementation of AsyncComponent for testing purposes.
Definition: TestAsyncComponent.h:19
static CommandLine * Instance_
Definition: CommandLine.h:336
Definition: Command.h:40
Definition: Command.h:39
Definition: Command.h:34
Definition: Command.h:31
Definition: Command.h:29
const int getKeywordLen() const
Definition: Command.h:83
Contains the Slate class definition.