From 64efd0898a11e7bcf9f5a78f8d6f87ed331bab39 Mon Sep 17 00:00:00 2001 From: Chris Punches Date: Tue, 20 Jun 2017 02:06:33 -0400 Subject: [PATCH] rework of suite and unit --- src/loaders/Suite.cpp | 6 ++++-- src/loaders/Suite.h | 4 ++-- src/loaders/Unit.cpp | 49 +++++++++++++++++++++++++++++++++---------- src/loaders/Unit.h | 36 +++++++++++++++++++++++++------ 4 files changed, 74 insertions(+), 21 deletions(-) diff --git a/src/loaders/Suite.cpp b/src/loaders/Suite.cpp index bef541e..82a6992 100644 --- a/src/loaders/Suite.cpp +++ b/src/loaders/Suite.cpp @@ -10,13 +10,15 @@ void Suite::load_units_file( std::string filename, bool verbose ) // will use json_root staging buffer on each run to append to this->units vector as valid units are found. this->load_json_file( filename, verbose ); + Json::Value jbuff; // refill the json_root buffer with a json object in the - if ( this->get_serialized( Json::Value jbuf, "units", verbose ) != 0) + if ( this->get_serialized( jbuff, "units", verbose ) != 0) { - this->json_root = jbuf; + this->json_root = jbuff; } + // assemble the units } diff --git a/src/loaders/Suite.h b/src/loaders/Suite.h index ef0b412..d6f1af6 100644 --- a/src/loaders/Suite.h +++ b/src/loaders/Suite.h @@ -17,10 +17,10 @@ class Suite: public JSON_Loader Suite(); // load a unit definitions file and add valid unit definitions to this->units - void load_units_file( std::string filename ); + void load_units_file( std::string filename, bool verbose ); // returns the unit identified by name - void get_unit(Unit & result, std::string provided_name); + // void get_unit(Unit & result, std::string provided_name); }; #endif //FTESTS_UNITS_H diff --git a/src/loaders/Unit.cpp b/src/loaders/Unit.cpp index 5271c7a..614c42b 100644 --- a/src/loaders/Unit.cpp +++ b/src/loaders/Unit.cpp @@ -1,14 +1,9 @@ #include "Unit.h" - +#include +#include +#include Unit::Unit() {} -Unit::Unit( Json::Value loader_root ) -/* - * Constructor for Unit type. Receives a UnitHolder loader_root. - */ -{ - this->load_root( loader_root ); -} int Unit::load_root(Json::Value loader_root) { @@ -22,9 +17,41 @@ int Unit::load_root(Json::Value loader_root) return EXIT_SUCCESS; } -/* - * getters for Unit type. - */ + +// TODO CHANGE HOW THIS WORKS +int Unit::load_string(std::string json_val) +{ + // reads from a string into a Json::Value type. + Json::Reader json_reader; + + // the deserialized json type to contain what's read by the reader + Json::Value serialized; + + // create the ifstream file handle for the parser method to consume + std::ifstream json_file_ifstream( json_val.c_str(), std::ifstream::binary ); + + // use the reader to parse the ifstream to the local property + bool parsingSuccessful = json_reader.parse( json_file_ifstream, serialized ); + + if (! parsingSuccessful ) + { + std::cerr << "Failed to parse adhoc JSON value." << std::endl << json_val << std::endl << std::endl << json_reader.getFormattedErrorMessages(); + throw JSON_Loader_InvalidJSON(); + + } else { + // if in verbose mode, give the user an "it worked" message + if ( verbose ) + { + std::cout << "Successfully parsed JSON string with " << this->json_root.size() << " elements. Value:" << std::endl; + std::cout << json_val << std::endl << std::endl; + } + } + // exit successfully + this->populated = true; +} + + +// getters for Unit type. std::string Unit::get_name() { return this->name; diff --git a/src/loaders/Unit.h b/src/loaders/Unit.h index ae2a0dc..9c2247d 100644 --- a/src/loaders/Unit.h +++ b/src/loaders/Unit.h @@ -1,29 +1,53 @@ -// -// Created by phanes on 4/29/17. -// - +/* Unit.h + * Unit is a type that represents a safely deserialized JSON object which defines what actions are taken as Examplar + * iterates through it's Tasks in it's given Plan. They only define the behaviour on execution, while the tasks define + * which Units are executed and in what order (and which Units a given Task depends on. + */ #ifndef FTESTS_UNIT_H #define FTESTS_UNIT_H #include #include "../json/json.h" +#include "JSON_Loader.h" -class Unit +class Unit: JSON_Loader { private: + // the name of the test std::string name; + + // the path of the executable this test executes when run std::string target; + + // the desired output + // poll: would an empty value be good to indicate to rely solely on zero/non-zero exit code? std::string output; + + // the path of the executable this test runs when the target executable fails to produce output or a 0 exit code. std::string rectifier; + + // an indicator of whether the test is active or not + // this is used as a way to give definers a way to force executors to edit arbitrary fields or prevent + // execution of potentially dangerous or intrusive tests std::string active; + + // an indicator of whether or not this test is required to pass. + // intended to be used as a flag to halt execution of further tests on failure std::string required; + + // indicator of whether the rectifier executable should be run on test failures. + // if rectifier exits on non-zero return code, it should be trigger the behaviour indicated by required std::string rectify; public: Unit(); - Unit( Json::Value loader_root ); + // loads a serialized jason::value object as a unit int load_root( Json::Value loader_root ); + // loads a string as a json string and deserializes that. + int load_string( std::string json_val ); + + // getters std::string get_name(); std::string get_target(); std::string get_output();