tstCommandLine.cc 3.45 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
 * File:   tstCommandLine.cc
 * Author: Jordan P. Lefebvre, lefebvrejp@ornl.gov
 *
 * Created on January 11, 2017, 11:09 AM
 */

#include "gtest/gtest.h"
#include "radixbug/bug.hh"
#include "radixcommand/commandline.hh"
#include <iostream>

using namespace std;
using namespace radix;
15
TEST(radixcommand, mixed)
16
{
17
18
19
20
21
22
23
24
    int fake_argc = 8;
    char * fake_argv[] = {(char*)("/Users/jap/build/release/dummy"),
                          (char*)"-T=temp",
                          (char*)"-N=16",
                          (char*)"inputA",
                          (char*)"-M=machine.txt",
                          (char*)"-load=3.4",
                          (char*)"-flag",
25
26
                          (char*)"inputB"
                         };
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    CommandLine line(fake_argc, fake_argv);
    line.addOption("o"
                   , "Output filepath, whitespace must be escaped."
                   , true);
    line.addOption("flag"
                   , "Print warnings."
                   , false);
    line.printParsedLine(std::cout);
    line.help(std::cout);
    EXPECT_TRUE(line.exists("flag"));
    EXPECT_EQ(16, line.get<int>("N"));
    EXPECT_EQ(32, line.get("Z", 32));
    EXPECT_DOUBLE_EQ(32., line.get("A", 32.));
    EXPECT_ANY_THROW(line.get<int>("F"));
    EXPECT_EQ("out.radix", line.get<std::string>("o", "out.radix"));
    std::vector<std::string> errors;
    EXPECT_FALSE(line.validate(errors));
    std::cout << "Errors:" << std::endl;
    for(size_t i = 0; i < errors.size(); ++i)
46
    {
47
        std::cout << "\t" <<errors[i] << std::endl;
48
    }
49
50
51
52
53
}
TEST(radixcommand, declared)
{
    int fake_argc = 2;
    char * fake_argv[] = {(char*)("/Users/jap/build/release/dummy"),
54
55
                          (char*)"-o=path/to a/file"
                         };
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    CommandLine line(fake_argc, fake_argv);
    line.declareArgument("input", "Path to input file.");
    line.addOption("o"
                   , "Output filepath, whitespace must be escaped."
                   , true);
    line.printParsedLine(std::cout);
    EXPECT_EQ("path/to a/file", line.get<std::string>("o", ""));
    std::vector<std::string> errors;
    EXPECT_FALSE(line.validate(errors));
    std::cout << "Errors:" << std::endl;
    for(size_t i = 0; i < errors.size(); ++i)
    {
        std::cout << "\t" <<errors[i] << std::endl;
    }
    //test multiple missing arguments
    line.declareArgument("output", "Path to output file w/o extension.");
    EXPECT_FALSE(line.validate(errors));
    std::cout << "Errors:" << std::endl;
    for(size_t i = 0; i < errors.size(); ++i)
75
    {
76
        std::cout << "\t" <<errors[i] << std::endl;
77
    }
78
79
80
81
82
83
84
85
86
87
88
    line.help(std::cout);
}
TEST(radixcommand, args)
{
    int fake_argc = 7;
    char * fake_argv[] = {(char*)("/Users/jap/build/release/dummy"),
                          (char*)"temp",
                          (char*)"16",
                          (char*)"inputA",
                          (char*)"machine.txt",
                          (char*)"3.4",
89
90
                          (char*)"input 6"
                         };
91
92
93
94
95
96
97
98
99
100
101
102
103
104

    CommandLine line(fake_argc, fake_argv);
    std::string a0 = line.arg<std::string>(0);
    EXPECT_EQ("temp", a0);
    int a1 = line.arg<int>(1);
    EXPECT_EQ(16, a1);
    std::string a2 = line.arg<std::string>(2);
    EXPECT_EQ("inputA", a2);
    std::string a3 = line.arg<std::string>(3);
    EXPECT_EQ("machine.txt", a3);
    double a4 = line.arg<double>(4);
    EXPECT_DOUBLE_EQ(3.4, a4);
    std::string a5 = line.arg<std::string>(5);
    EXPECT_EQ("input 6", a5);
105
106
}