Skip to content

Commit

Permalink
tester: changed to function operator class and to throw exceptions
Browse files Browse the repository at this point in the history
changed run function to a void function operator function
removed error message string member and its access functions
modified constructor to throw standard string exceptions
modified function operator to throw standard string exception
modified command line constructor to catch tester string exceptions
removed redundant '(void)' from function definitions
  • Loading branch information
thunder422 committed Nov 13, 2014
1 parent 14d3b32 commit 738eba0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 43 deletions.
26 changes: 11 additions & 15 deletions commandline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Interactive BASIC Compiler Project
// File: commandline.n - command line class source file
// Copyright (C) 2012-2013 Thunder422
// Copyright (C) 2012-2014 Thunder422
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -82,24 +82,20 @@ CommandLine::CommandLine(std::list<std::string> args) :
return;
}

Tester tester(m_programName, args, cout());
if (tester.hasError())
try
{
cout(&std::cerr) << tester.errorMessage() << '\n';
m_returnCode = 1;
return;
}
else if (tester.hasOption())
{
if (tester.run(std::move(copyrightStatement())))
Tester test(m_programName, args, cout());
if (test.hasOption())
{
test(std::move(copyrightStatement()));
m_returnCode = 0;
return;
}
else
{
cout(&std::cerr) << tester.errorMessage() << '\n';
m_returnCode = 1;
}
}
catch (std::string &errorMessage)
{
cout(&std::cerr) << errorMessage << '\n';
m_returnCode = 1;
return;
}

Expand Down
27 changes: 12 additions & 15 deletions test_ibcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// Interactive BASIC Compiler Project
// File: test_ibcp.cpp - tester class source file
// Copyright (C) 2010-2013 Thunder422
// Copyright (C) 2010-2014 Thunder422
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -280,7 +280,7 @@ Tester::Tester(const std::string &programName,
}
if (args.size() == 1)
{
m_errorMessage = m_programName + ": missing test file name";
throw m_programName + ": missing test file name";
}
else
{
Expand All @@ -300,22 +300,23 @@ Tester::Tester(const std::string &programName,
}
if (m_option == Option{}) // no matching names?
{
m_errorMessage = "usage: " + m_programName + " -t";
std::string errorMessage = "usage: " + m_programName + " -t";
if (m_recreate)
{
m_errorMessage += 'o';
errorMessage += 'o';
}
m_errorMessage += " (";
errorMessage += " (";
if (m_recreate)
{
m_errorMessage += name[Option::Parser] + '|';
errorMessage += name[Option::Parser] + '|';
}
m_errorMessage += name[Option::Expression] + '|'
errorMessage += name[Option::Expression] + '|'
+ name[Option::Translator] + ")[xx]";
throw errorMessage;
}
else if (m_option == Option::Parser && m_recreate)
{
m_errorMessage = m_programName + ": cannot use -to with "
throw m_programName + ": cannot use -to with "
+ name[Option::Parser] + " files";
}
}
Expand All @@ -339,13 +340,13 @@ bool Tester::isOption(const std::string &arg, const std::string &exp,


// function to return list of valid options
std::string Tester::options(void)
std::string Tester::options()
{
return "-t <test_file>|-tp|-te|-tt|-tc|-tr|-to <test_file>";
}


bool Tester::run(std::string copyrightStatement)
void Tester::operator()(std::string copyrightStatement)
{
std::ifstream ifs;

Expand All @@ -355,9 +356,7 @@ bool Tester::run(std::string copyrightStatement)
ifs.open(m_testFileName);
if (!ifs.is_open())
{
m_errorMessage = m_programName + ": error opening '"
+ m_testFileName + '\'';
return false;
throw m_programName + ": error opening '" + m_testFileName + '\'';
}
}

Expand Down Expand Up @@ -460,8 +459,6 @@ bool Tester::run(std::string copyrightStatement)
}
}
}

return true;
}


Expand Down
17 changes: 4 additions & 13 deletions test_ibcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Interactive BASIC Compiler Project
// File: test_ibcp.h - tester class header file
// Copyright (C) 2012-2013 Thunder422
// Copyright (C) 2012-2014 Thunder422
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -45,20 +45,12 @@ class Tester
explicit Tester(const std::string &programName,
const std::list<std::string> &args, std::ostream &cout);

static std::string options(void);
bool run(std::string copyrightStatement);
bool hasOption(void) const // has a test option been specified?
static std::string options();
void operator()(std::string copyrightStatement);
bool hasOption() const // has a test option been specified?
{
return m_option != Option{};
}
bool hasError(void) const // does test arguments contain an error?
{
return !m_errorMessage.empty();
}
std::string errorMessage(void) const // message of error
{
return m_errorMessage;
}

private:
enum class Option {
Expand Down Expand Up @@ -91,7 +83,6 @@ class Tester
std::ostream &m_cout; // reference to output stream
std::unique_ptr<Translator> m_translator; // translator instance
std::unique_ptr<ProgramModel> m_programUnit; // program unit
std::string m_errorMessage; // message if error occurred
};


Expand Down

0 comments on commit 738eba0

Please sign in to comment.