From 16302ab0ce9263a31e36b93648959575e8c5c02b Mon Sep 17 00:00:00 2001 From: dufourj Date: Mon, 17 Apr 2006 08:18:37 +0000 Subject: Tester : - interfaçage avec interpreter. TODO: - gestion du contrôle-c. --- i/marvin/src/tester/Makefile.defs | 2 +- i/marvin/src/tester/test_tester.cc | 36 ++++++++++--------------- i/marvin/src/tester/tester.cc | 54 +++++++++++++++++++++++++------------- i/marvin/src/tester/tester.hh | 20 ++++++++++++-- 4 files changed, 69 insertions(+), 43 deletions(-) (limited to 'i/marvin/src') diff --git a/i/marvin/src/tester/Makefile.defs b/i/marvin/src/tester/Makefile.defs index ec5bd6a..eeff4ab 100644 --- a/i/marvin/src/tester/Makefile.defs +++ b/i/marvin/src/tester/Makefile.defs @@ -1,5 +1,5 @@ PROGRAMS += test_tester -tester_OBJECTS = tester.o $(config_OBJECTS) $(interpreter_OBJECTS) +tester_OBJECTS = tester.o $(config_OBJECTS) $(interpreter_OBJECTS) $(log_OBJECTS) test_tester_OBJECTS = test_tester.o $(tester_OBJECTS) diff --git a/i/marvin/src/tester/test_tester.cc b/i/marvin/src/tester/test_tester.cc index 8db2a31..b89b25f 100644 --- a/i/marvin/src/tester/test_tester.cc +++ b/i/marvin/src/tester/test_tester.cc @@ -24,48 +24,40 @@ // }}} #include "tester.hh" -class TestTester +class TestTester : public Tester { - Tester t; public: // Constructor TestTester (int argc, char ** argv) - : t (argc, argv) { } - bool funcA (const Interpreter::Args &a) + : Tester (argc, argv) { } + void funcA (const Interpreter::Args &a) { std::cout << " a " << a << std::endl; - return true; } - bool funcB (void) + void funcB (void) { std::cout << " b ( )" << std::endl; - return true; } - bool funcC (int i) + void funcC (int i) { std::cout << " c ( " << i << " )" << std::endl; - return true; } - bool funcD (const std::string &s) + void funcD (const std::string &s) { std::cout << " d ( " << s << " )" << std::endl; - return true; } - bool funcE (int i, const std::string &s, double d) + void funcE (int i, const std::string &s, double d) { std::cout << " e ( " << i << ' ' << s << ' ' << d << " )" << std::endl; - return true; } - int main (void) + void preRun (void) { // Add functions. - t.add ("a", Interpreter::memFunc (*this, &TestTester::funcA)); - t.add ("b", Interpreter::memFunc (*this, &TestTester::funcB)); - t.add ("c", Interpreter::memFunc (*this, &TestTester::funcC)); - t.add ("d", Interpreter::memFunc (*this, &TestTester::funcD)); - t.add ("e", Interpreter::memFunc (*this, &TestTester::funcE)); - t.run (); - return 0; + add ("a", Interpreter::memFunc (*this, &TestTester::funcA)); + add ("b", Interpreter::memFunc (*this, &TestTester::funcB)); + add ("c", Interpreter::memFunc (*this, &TestTester::funcC)); + add ("d", Interpreter::memFunc (*this, &TestTester::funcD)); + add ("e", Interpreter::memFunc (*this, &TestTester::funcE)); } }; @@ -75,7 +67,7 @@ main (int argc, char **argv) try { TestTester tt (argc, argv); - return tt.main (); + tt.run (); } catch (const std::exception &e) { diff --git a/i/marvin/src/tester/tester.cc b/i/marvin/src/tester/tester.cc index 2ded27f..58e62c8 100644 --- a/i/marvin/src/tester/tester.cc +++ b/i/marvin/src/tester/tester.cc @@ -27,8 +27,7 @@ #include // getopt #include // std::exception #include // std::exit -#include // std::ifstream -#include // std::stringstream +#include // std::ostringstream /// Constructor. Tester::Tester (int argc, char **argv) @@ -40,14 +39,15 @@ Tester::Tester (int argc, char **argv) /// Getopt command line. /// Supported arguemnts : -/// - -c "commands list" +/// - -c 'commands list' /// - -f +/// - -h : help void Tester::getOpt (int argc, char **argv) { - char *optstring = "c:f:"; + char *optstring = "hc:f:"; int option; - std::ifstream file; + std::string strTmp; // Check number of args if (argc < 2) @@ -65,19 +65,17 @@ Tester::getOpt (int argc, char **argv) switch (option) { case 'c': - commands_.append (optarg); + // Check commands + strTmp = optarg; + listOpts_.push_back (Opt(false, strTmp)); break; case 'f': - file.open (optarg); - if (file.is_open ()) - { - std::stringstream buffer_tmp; - buffer_tmp << file.rdbuf(); - commands_.append (buffer_tmp.str ()); - file.close (); - } - else - throw std::runtime_error ("File not found : " + std::string (optarg)); + strTmp = optarg; + listOpts_.push_back (Opt(true, strTmp)); + break; + case 'h': + usage (); + std::exit (0); break; case '?': default: @@ -106,10 +104,30 @@ Tester::add (const std::string &s, Interpreter::Func *f) interpreter_.add (s, f); } +/// Run commands. void Tester::run (void) { - // TODO called interpreter with the commands_, in check mode then in run - // mode. + // Pre-run + preRun (); + // Run + ListOpts::const_iterator it; + // Check all, dry run + for (it = listOpts_.begin (); it != listOpts_.end (); it++) + { + if (it->first) + interpreter_.interpretFile (it->second, true); + else + interpreter_.interpretString (it->second, true); + } + // Run ! + for (it = listOpts_.begin (); it != listOpts_.end (); it++) + { + if (it->first) + interpreter_.interpretFile (it->second, false); + else + interpreter_.interpretString (it->second, false); + } + postRun (); } diff --git a/i/marvin/src/tester/tester.hh b/i/marvin/src/tester/tester.hh index c476125..5f4e68c 100644 --- a/i/marvin/src/tester/tester.hh +++ b/i/marvin/src/tester/tester.hh @@ -26,8 +26,10 @@ // }}} #include "config/config.hh" #include "interpreter/interpreter.hh" +#include "log/data_circular_buffer_factory.hh" #include +#include class Tester { @@ -38,8 +40,11 @@ class Tester Interpreter interpreter_; /// Program name. std::string program_; - /// Commands to parse. - std::string commands_; + /// Internal typedef. + typedef std::pair Opt; + typedef std::list ListOpts; + /// Options list. + ListOpts listOpts_; /// Getopt command line. void getOpt (int argc, char **argv); /// Print usage to stdout. @@ -47,10 +52,21 @@ class Tester public: /// Constructor. Tester (int argc, char ** argv); + /// Destructor. + virtual ~Tester (void) { } /// Add a test function, Tester owns f. void add (const std::string &s, Interpreter::Func *f); /// Run commands. + /// Call preRun, check command, run them, call postRun. void run (void); + /// Executed before checking/running commands. + virtual void preRun (void) { } + /// Executed after running commands. + virtual void postRun (void) { } + private: + /// DataCircularBufferFactory for beeing destruct at the end of the program. + /// It must stay at the end of the program ! + DataCircularBufferFactory dbc; }; #endif // tester_hh -- cgit v1.2.3