feisty meow concerns codebase  2.140
test_command_line.cpp
Go to the documentation of this file.
1 /*****************************************************************************\
2 * *
3 * Name : test_command_line *
4 * Author : Chris Koeritz *
5 * *
6 * Purpose: *
7 * *
8 * Tests the command_line class by showing our parameters. *
9 * *
10 *******************************************************************************
11 * Copyright (c) 1992-$now By Author. This program is free software; you can *
12 * redistribute it and/or modify it under the terms of the GNU General Public *
13 * License as published by the Free Software Foundation; either version 2 of *
14 * the License or (at your option) any later version. This is online at: *
15 * http://www.fsf.org/copyleft/gpl.html *
16 * Please send any updates to: fred@gruntose.com *
17 \*****************************************************************************/
18 
19 #include <basis/functions.h>
20 #include <basis/guards.h>
21 #include <basis/astring.h>
22 
26 #include <loggers/console_logger.h>
29 #include <filesystem/filename.h>
32 #include <unit_test/unit_base.h>
33 
34 using namespace application;
35 using namespace basis;
36 //using namespace configuration;
37 //using namespace mathematics;
38 using namespace filesystem;
39 using namespace loggers;
40 using namespace structures;
41 //using namespace textual;
42 //using namespace timely;
43 using namespace unit_test;
44 
45 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
46 
47 class test_command_line : virtual public unit_base, virtual public application_shell
48 {
49 public:
50  test_command_line() : application_shell() {}
51  DEFINE_CLASS_NAME("test_command_line");
52  int execute();
53 };
54 
55 int test_command_line::execute()
56 {
57  FUNCDEF("execute");
58 
59  LOG("these are the commands we got passed...");
61  for (int i = 0; i < cmds.length(); i++) {
62  LOG(a_sprintf("%02d: cmd=%s", i, cmds[i].s()));
63  }
64 
65  // test 1 is a simple probe on the actual arguments to this program.
66  {
68  filename prog(cl1.program_name());
69  log(astring("got a program name of: [") + prog.dirname() + astring("] ")
70  + prog.basename());
71  log(astring("got parms of:"));
72  for (int i = 0; i < cl1.entries(); i++) {
73  command_parameter got = cl1.get(i);
74  log(astring(astring::SPRINTF, "%d: type=%s text=%s", i,
75  (got.type()==command_parameter::VALUE)? "VALUE"
76  : (got.type()==command_parameter::CHAR_FLAG)? "CHAR_FLAG"
77  : (got.type()==command_parameter::STRING_FLAG)? "STRING_FLAG"
78  : "UNKNOWN",
79  got.text().s()));
80  }
81  }
82 
83  // test 2 ensures that our new special flag ending support (standard unix
84  // flag of --) is performing.
85  {
86  astring cmd_line = "tossedout spumeco -g -q --fleem -r -- spugnats.txt crumbole.h";
87  command_line cl2(cmd_line);
88 
89  command_parameter spumeco = cl2.get(0);
90 //log(a_sprintf("parm 0: type=%d text=%s", spumeco.type(), spumeco.text().s()));
91  if (spumeco.type() != command_parameter::VALUE)
92  non_continuable_error(class_name(), "test 2", "spumeco is wrong type");
93  if (spumeco.text() != astring("spumeco"))
94  non_continuable_error(class_name(), "test 2", "spumeco is erroneous");
95 
96  command_parameter gflag = cl2.get(1);
97  if (gflag.type() != command_parameter::CHAR_FLAG)
98  non_continuable_error(class_name(), "test 2", "G flag had wrong type");
99  if (gflag.text() != astring("g"))
100  non_continuable_error(class_name(), "test 2", "G flag had wrong value");
101 
102  command_parameter qflag = cl2.get(2);
103  if (qflag.type() != command_parameter::CHAR_FLAG)
104  non_continuable_error(class_name(), "test 2", "Q flag had wrong type");
105  if (qflag.text() != astring("q"))
106  non_continuable_error(class_name(), "test 2", "Q flag had wrong value");
107 
108  command_parameter fleemflag = cl2.get(3);
109  if (fleemflag.type() != command_parameter::STRING_FLAG)
110  non_continuable_error(class_name(), "test 2", "fleem flag had wrong type");
111  if (fleemflag.text() != astring("fleem"))
112  non_continuable_error(class_name(), "test 2", "fleem flag had wrong value");
113 
114  command_parameter rflag = cl2.get(4);
115  if (rflag.type() != command_parameter::CHAR_FLAG)
116  non_continuable_error(class_name(), "test 2", "R flag had wrong type");
117  if (rflag.text() != astring("r"))
118  non_continuable_error(class_name(), "test 2", "R flag had wrong value");
119 
120  command_parameter spugval = cl2.get(5);
121  if (spugval.type() != command_parameter::VALUE)
122  non_continuable_error(class_name(), "test 2", "spugval had wrong type");
123  if (spugval.text() != astring("spugnats.txt"))
124  non_continuable_error(class_name(), "test 2", "spugval had wrong value");
125 
126  command_parameter crumval = cl2.get(6);
127  if (crumval.type() != command_parameter::VALUE)
128  non_continuable_error(class_name(), "test 2", "crumval had wrong type");
129  if (crumval.text() != astring("crumbole.h"))
130  non_continuable_error(class_name(), "test 2", "crumval had wrong value");
131 
132  command_parameter bogus = cl2.get(7);
133  if (bogus.type() != command_parameter::BOGUS_ITEM)
134  non_continuable_error(class_name(), "test 2", "bogus parameter had wrong type");
135  }
136 
137 //more tests!
138 
139  critical_events::alert_message(astring(class_name()) + ": works for those functions tested.");
140 
141  return 0;
142 }
143 
144 HOOPLE_MAIN(test_command_line, )
145 
The application_shell is a base object for console programs.
static structures::string_array get_command_line()
returns the command line passed to the program as a list of strings.
const basis::astring & text() const
observes the string contents.
parameter_types type() const
observes the type of the parameter.
Definition: command_line.h:69
a_sprintf is a specialization of astring that provides printf style support.
Definition: astring.h:440
int length() const
Returns the current reported length of the allocated C array.
Definition: array.h:115
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
const char * s() const
synonym for observe. the 's' stands for "string", if that helps.
Definition: astring.h:113
Provides operations commonly needed on file names.
Definition: filename.h:64
An array of strings with some additional helpful methods.
Definition: string_array.h:32
#define non_continuable_error(c, f, i)
an extra piece of information used, if available, in bounds_halt below.
#define DEFINE_CLASS_NAME(objname)
Defines the name of a class by providing a couple standard methods.
Definition: enhance_cpp.h:42
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition: enhance_cpp.h:54
Provides macros that implement the 'main' program of an application.
#define HOOPLE_MAIN(obj_name, obj_args)
options that should work for most unix and linux apps.
Definition: hoople_main.h:61
Implements an application lock to ensure only one is running at once.
char ** _global_argv
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
A platform independent way to obtain the timestamp of a file.
Definition: byte_filer.cpp:37
A logger that sends to the console screen using the standard output device.
A dynamic container class that holds any kind of object via pointers.
Definition: amorph.h:55
Useful support functions for unit testing, especially within hoople.
Definition: unit_base.cpp:35
#define LOG(s)