Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Working options_tests
  • Loading branch information
eyal0 committed Jan 11, 2019
1 parent b839386 commit c4276a9
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -75,4 +75,4 @@ testing/gerbv_example/*/*.svg
.directory

#gerberimporter_tests outputs
/*.xpm
/*.gbr.png
3 changes: 2 additions & 1 deletion Makefile.am
Expand Up @@ -62,7 +62,7 @@ LIBS = $(glibmm_LIBS) $(gdkmm_LIBS) $(gerbv_LIBS) $(BOOST_PROGRAM_OPTIONS_LIBS)
EXTRA_DIST = millproject

check_PROGRAMS = voronoi_tests eulerian_paths_tests segmentize_tests tsp_solver_tests units_tests \
available_drills_tests gerberimporter_tests
available_drills_tests gerberimporter_tests options_tests
voronoi_tests_SOURCES = voronoi.hpp voronoi.cpp voronoi_tests.cpp
eulerian_paths_tests_SOURCES = eulerian_paths_tests.cpp
segmentize_tests_SOURCES = segmentize_tests.cpp
Expand All @@ -71,5 +71,6 @@ units_tests_SOURCES = units_tests.cpp units.hpp
available_drills_tests_SOURCES = available_drills_tests.cpp available_drills.hpp
gerberimporter_tests_SOURCES = gerberimporter.hpp gerberimporter.cpp gerberimporter_tests.cpp
gerberimporter_tests_LDFLAGS = $(rsvg_LIBS)
options_tests_SOURCES = options_tests.cpp options.hpp options.cpp

TESTS = $(check_PROGRAMS)
18 changes: 9 additions & 9 deletions main.cpp
Expand Up @@ -52,7 +52,7 @@ using Glib::build_filename;
#include <boost/algorithm/string.hpp>
#include <boost/version.hpp>

void do_pcb2gcode(int argc, char* argv[]) {
void do_pcb2gcode(int argc, const char* argv[]) {
Glib::init();
Gdk::wrap_init();

Expand Down Expand Up @@ -179,7 +179,7 @@ void do_pcb2gcode(int argc, char* argv[]) {
fstream in(name.c_str(), fstream::in);

if (!in.good()) {
options::maybe_throw("Cannot read preamble-text file \"" + name + "\"", EXIT_FAILURE);
options::maybe_throw("Cannot read preamble-text file \"" + name + "\"", ERR_INVALIDPARAMETER);
}

string line;
Expand Down Expand Up @@ -211,7 +211,7 @@ void do_pcb2gcode(int argc, char* argv[]) {
fstream in(name.c_str(), fstream::in);

if (!in.good()) {
options::maybe_throw("Cannot read preamble file \"" + name + "\"", EXIT_FAILURE);
options::maybe_throw("Cannot read preamble file \"" + name + "\"", ERR_INVALIDPARAMETER);
}

string tmp((std::istreambuf_iterator<char>(in)),
Expand All @@ -230,7 +230,7 @@ void do_pcb2gcode(int argc, char* argv[]) {
fstream in(name.c_str(), fstream::in);

if (!in.good()) {
options::maybe_throw("Cannot read postamble file \"" + name + "\"", EXIT_FAILURE);
options::maybe_throw("Cannot read postamble file \"" + name + "\"", ERR_INVALIDPARAMETER);
}

string tmp((std::istreambuf_iterator<char>(in)),
Expand Down Expand Up @@ -276,7 +276,7 @@ void do_pcb2gcode(int argc, char* argv[]) {
board->prepareLayer("front", importer, isolator, false);
cout << "DONE.\n";
} catch (import_exception& i) {
options::maybe_throw("ERROR.", EXIT_FAILURE);
options::maybe_throw("ERROR.", ERR_INVALIDPARAMETER);
}
} else {
cout << "not specified.\n";
Expand All @@ -295,7 +295,7 @@ void do_pcb2gcode(int argc, char* argv[]) {
cout << "DONE.\n";
}
catch (import_exception& i) {
options::maybe_throw("ERROR.", EXIT_FAILURE);
options::maybe_throw("ERROR.", ERR_INVALIDPARAMETER);
}
} else {
cout << "not specified.\n";
Expand All @@ -312,7 +312,7 @@ void do_pcb2gcode(int argc, char* argv[]) {
board->prepareLayer("outline", importer, cutter, !workSide(vm, "cut"));
cout << "DONE.\n";
} catch (import_exception& i) {
options::maybe_throw("ERROR.", EXIT_FAILURE);
options::maybe_throw("ERROR.", ERR_INVALIDPARAMETER);
}
} else {
cout << "not specified.\n";
Expand Down Expand Up @@ -434,7 +434,7 @@ void do_pcb2gcode(int argc, char* argv[]) {

}
catch (const drill_exception& e) {
options::maybe_throw("ERROR: drill_exception", EXIT_FAILURE);
options::maybe_throw("ERROR: drill_exception", ERR_INVALIDPARAMETER);
}
} else {
cout << "not specified.\n";
Expand All @@ -444,7 +444,7 @@ void do_pcb2gcode(int argc, char* argv[]) {

}

int main(int argc, char* argv[]) {
int main(int argc, const char* argv[]) {
try {
do_pcb2gcode(argc, argv);
} catch (pcb2gcode_parse_exception e) {
Expand Down
2 changes: 1 addition & 1 deletion ngc_exporter.cpp
Expand Up @@ -204,7 +204,7 @@ void NGC_Exporter::export_layer(shared_ptr<Layer> layer, string of_name, boost::
if(!leveller->prepareWorkarea(toolpaths)) {
options::maybe_throw(std::string("Required number of probe points (") + std::to_string(leveller->requiredProbePoints()) +
") exceeds the maximum number (" + std::to_string(leveller->maxProbePoints()) + "). "
"Reduce either al-x or al-y.", EXIT_FAILURE);
"Reduce either al-x or al-y.", ERR_INVALIDPARAMETER);
}

leveller->header(of);
Expand Down
6 changes: 3 additions & 3 deletions options.cpp
Expand Up @@ -45,7 +45,7 @@ options::instance()
return singleton;
}

void options::maybe_throw(const std::string& what, int error_code) {
void options::maybe_throw(const std::string& what, ErrorCodes error_code) {
if (instance().vm["ignore-warnings"].as<bool>()) {
cerr << "Ignoring error code " << error_code << ": " << what << endl;
} else {
Expand All @@ -56,7 +56,7 @@ void options::maybe_throw(const std::string& what, int error_code) {
/* parse options, both command line and from the millproject file if it exists.
* Throws on error.
*/
void options::parse(int argc, char** argv) {
void options::parse(int argc, const char** argv) {
// guessing causes problems when one option is the start of another
// (--drill, --drill-diameter); see bug 3089930
int style = po::command_line_style::default_style
Expand Down Expand Up @@ -158,7 +158,7 @@ void options::parse_files()
instance().vm);
} catch (std::exception& e) {
maybe_throw("Error parsing configuration file \"" + file + "\": " +
e.what(), EXIT_FAILURE);
e.what(), ERR_INVALIDPARAMETER);
}

po::notify(instance().vm);
Expand Down
45 changes: 23 additions & 22 deletions options.hpp
Expand Up @@ -36,26 +36,8 @@ namespace po = boost::program_options;
using std::string;
using std::to_string;

class pcb2gcode_parse_exception : public std::exception {
public:
pcb2gcode_parse_exception(const std::string& what, int error_code) {
what_string = what;
this->error_code = error_code;
}
virtual const char* what() const throw() {
return what_string.c_str();
}
virtual int code() const throw() {
return error_code;
}

private:
std::string what_string;
int error_code;
};

enum ErrorCodes
{
enum ErrorCodes {
ERR_OK = 0,
ERR_NOZWORK = 1,
ERR_NOCUTTERDIAMETER = 2,
ERR_NOZSAFE = 3,
Expand Down Expand Up @@ -113,6 +95,25 @@ enum ErrorCodes
ERR_UNKNOWNPARAMETER = 101
};

class pcb2gcode_parse_exception : public std::exception {
public:
pcb2gcode_parse_exception(const std::string& what, ErrorCodes error_code) {
what_string = what;
this->error_code = error_code;
}
virtual const char* what() const throw() {
return what_string.c_str();
}
virtual ErrorCodes code() const throw() {
return error_code;
}

private:
std::string what_string;
ErrorCodes error_code;
};


/******************************************************************************/
/*
*/
Expand All @@ -121,7 +122,7 @@ class options: boost::noncopyable
{

public:
static void parse(int argc, char** argv);
static void parse(int argc, const char** argv);
static void parse_files();
static void check_parameters();
static po::variables_map& get_vm()
Expand All @@ -131,7 +132,7 @@ class options: boost::noncopyable
;
static string help();

static void maybe_throw(const std::string& what, int error_code);
static void maybe_throw(const std::string& what, ErrorCodes error_code);
private:
options();
po::variables_map vm;
Expand Down
33 changes: 33 additions & 0 deletions options_tests.cpp
@@ -0,0 +1,33 @@
#define BOOST_TEST_MODULE options tests
#include <boost/test/included/unit_test.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/program_options.hpp>
namespace po = boost::program_options;

#include "options.hpp"

using namespace std;

BOOST_AUTO_TEST_SUITE(options_tests);

ErrorCodes get_error_code(const std::string& args) {
std::vector<std::string> words;
boost::split(words, args, boost::is_any_of(" "), boost::token_compress_on);
const char* argc[words.size()];
int argv = words.size();
for (unsigned int i = 0; i < words.size(); i++) {
argc[i] = words[i].c_str();
}
try {
options::parse(argv, argc);
} catch (pcb2gcode_parse_exception e) {
return e.code();
}
return ERR_OK;
}

BOOST_AUTO_TEST_CASE(options) {
BOOST_CHECK_EQUAL(get_error_code("pcb2gcode --foo"), 101);
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit c4276a9

Please sign in to comment.