Options class for test

This commit is contained in:
Christopher Dunn 2015-01-23 11:27:19 -06:00
parent 632c9b5032
commit 79211e1aeb

View File

@ -132,7 +132,8 @@ static int parseAndSaveValueTree(const std::string& input,
const std::string& kind, const std::string& kind,
const Json::Features& features, const Json::Features& features,
bool parseOnly, bool parseOnly,
Json::Value* root) { Json::Value* root)
{
Json::Reader reader(features); Json::Reader reader(features);
bool parsingSuccessful = reader.parse(input, *root); bool parsingSuccessful = reader.parse(input, *root);
if (!parsingSuccessful) { if (!parsingSuccessful) {
@ -153,16 +154,31 @@ static int parseAndSaveValueTree(const std::string& input,
} }
return 0; return 0;
} }
// static std::string useFastWriter(Json::Value const& root) {
static int rewriteValueTree(const std::string& rewritePath, // Json::FastWriter writer;
const Json::Value& root, // writer.enableYAMLCompatibility();
std::string* rewrite) { // return writer.write(root);
// Json::FastWriter writer; // }
// writer.enableYAMLCompatibility(); static std::string useStyledWriter(
Json::Value const& root)
{
Json::StyledWriter writer;
return writer.write(root);
}
static std::string useStyledStreamWriter(
Json::Value const& root)
{
Json::StyledStreamWriter writer; Json::StyledStreamWriter writer;
std::ostringstream sout; std::ostringstream sout;
writer.write(sout, root); writer.write(sout, root);
*rewrite = sout.str(); return sout.str();
}
static int rewriteValueTree(
const std::string& rewritePath,
const Json::Value& root,
std::string* rewrite)
{
*rewrite = useStyledWriter(root);
FILE* fout = fopen(rewritePath.c_str(), "wt"); FILE* fout = fopen(rewritePath.c_str(), "wt");
if (!fout) { if (!fout) {
printf("Failed to create rewrite file: %s\n", rewritePath.c_str()); printf("Failed to create rewrite file: %s\n", rewritePath.c_str());
@ -197,56 +213,56 @@ static int printUsage(const char* argv[]) {
return 3; return 3;
} }
int parseCommandLine(int argc, struct Options
const char* argv[], {
Json::Features& features, std::string path;
std::string& path, Json::Features features;
bool& parseOnly) { bool parseOnly;
parseOnly = false; };
static int parseCommandLine(
int argc, const char* argv[], Options* opts)
{
opts->parseOnly = false;
if (argc < 2) { if (argc < 2) {
return printUsage(argv); return printUsage(argv);
} }
int index = 1; int index = 1;
if (std::string(argv[1]) == "--json-checker") { if (std::string(argv[1]) == "--json-checker") {
features = Json::Features::strictMode(); opts->features = Json::Features::strictMode();
parseOnly = true; opts->parseOnly = true;
++index; ++index;
} }
if (std::string(argv[1]) == "--json-config") { if (std::string(argv[1]) == "--json-config") {
printConfig(); printConfig();
return 3; return 3;
} }
if (index == argc || index + 1 < argc) { if (index == argc || index + 1 < argc) {
return printUsage(argv); return printUsage(argv);
} }
opts->path = argv[index];
path = argv[index];
return 0; return 0;
} }
static void tryTest(Options const& opts)
{
}
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
std::string path; Options opts;
Json::Features features; int exitCode = parseCommandLine(argc, argv, &opts);
bool parseOnly;
int exitCode = parseCommandLine(argc, argv, features, path, parseOnly);
if (exitCode != 0) { if (exitCode != 0) {
return exitCode; return exitCode;
} }
try { try {
std::string input = readInputTestFile(path.c_str()); std::string input = readInputTestFile(opts.path.c_str());
if (input.empty()) { if (input.empty()) {
printf("Failed to read input or empty input: %s\n", path.c_str()); printf("Failed to read input or empty input: %s\n", opts.path.c_str());
return 3; return 3;
} }
std::string basePath = removeSuffix(argv[1], ".json"); std::string basePath = removeSuffix(argv[1], ".json");
if (!parseOnly && basePath.empty()) { if (!opts.parseOnly && basePath.empty()) {
printf("Bad input path. Path does not end with '.expected':\n%s\n", printf("Bad input path. Path does not end with '.expected':\n%s\n",
path.c_str()); opts.path.c_str());
return 3; return 3;
} }
@ -256,8 +272,9 @@ int main(int argc, const char* argv[]) {
Json::Value root; Json::Value root;
exitCode = parseAndSaveValueTree( exitCode = parseAndSaveValueTree(
input, actualPath, "input", features, parseOnly, &root); input, actualPath, "input",
if (exitCode || parseOnly) { opts.features, opts.parseOnly, &root);
if (exitCode || opts.parseOnly) {
return exitCode; return exitCode;
} }
std::string rewrite; std::string rewrite;
@ -267,7 +284,8 @@ int main(int argc, const char* argv[]) {
} }
Json::Value rewriteRoot; Json::Value rewriteRoot;
exitCode = parseAndSaveValueTree( exitCode = parseAndSaveValueTree(
rewrite, rewriteActualPath, "rewrite", features, parseOnly, &rewriteRoot); rewrite, rewriteActualPath, "rewrite",
opts.features, opts.parseOnly, &rewriteRoot);
if (exitCode) { if (exitCode) {
return exitCode; return exitCode;
} }