cmd_line_test.cpp
Go to the documentation of this file.
1 #include <stan/io/cmd_line.hpp>
2 #include <gtest/gtest.h>
3 #include <test/unit/util.hpp>
4 
5 TEST(io_cmd_line, cmd_line_0) {
6  int argc = 0;
7  const char* argv[1];
8  argv[0] = "foo";
9  stan::io::cmd_line cl(argc,argv);
10  EXPECT_FALSE(cl.has_key("bar"));
11  std::string y;
12  EXPECT_FALSE(cl.val<std::string>("bar",y));
13 }
14 
15 TEST(io_cmd_line, cmd_line_string) {
16  int argc = 2;
17  const char* argv[2];
18  argv[0] = "prog";
19  argv[1] = "--foo=bar";
20  stan::io::cmd_line cl(argc,argv);
21  std::string x;
22  EXPECT_TRUE(cl.val<std::string>("foo",x));
23  EXPECT_EQ("bar",x);
24 }
25 
26 TEST(io_cmd_line, cmd_line_int) {
27  int argc = 6;
28  const char* argv[6];
29  argv[0] = "prog.exe";
30  argv[1] = "--foo=bar";
31  argv[2] = "--bz";
32  argv[3] = "--bing=3";
33  argv[4] = "ahoy";
34  argv[5] = "17";
35  stan::io::cmd_line cl(argc,argv);
36  EXPECT_EQ("prog.exe",cl.command());
37  std::string x;
38  EXPECT_TRUE(cl.val<std::string>("foo",x));
39  EXPECT_EQ("bar",x);
40  unsigned int y;
41  EXPECT_TRUE(cl.val<unsigned int>("bing",y));
42  EXPECT_EQ(3U,y);
43  EXPECT_TRUE(cl.has_flag("bz"));
44  EXPECT_FALSE(cl.has_flag("bza"));
45  EXPECT_FALSE(cl.val<unsigned int>("bongo",y));
46  EXPECT_EQ(2U, cl.bare_size());
47  std::string z;
48  EXPECT_TRUE(cl.bare<std::string>(0,z));
49  EXPECT_EQ("ahoy", z);
50  EXPECT_TRUE(cl.bare<unsigned int>(1,y));
51  EXPECT_EQ(17U, y);
52  double aaa;
53  EXPECT_FALSE(cl.bare<double>(2,aaa));
54 }
55 
56 TEST(io_cmd_line, spaces) {
57  int argc = 3;
58  const char* argv[3];
59  argv[0] = "prog";
60  argv[1] = "arg 1";
61  argv[2] = "--foo=arg 2";
62  stan::io::cmd_line cl(argc,argv);
63  std::string x;
64  EXPECT_EQ(1U, cl.bare_size());
65  EXPECT_TRUE(cl.bare<std::string>(0,x));
66  EXPECT_EQ("arg 1", x);
67 
68  std::string y;
69  EXPECT_TRUE(cl.val<std::string>("foo",y));
70  EXPECT_EQ("arg 2",y);
71 }
72 
73 TEST(io_cmd_line, pad_help_option) {
75  EXPECT_NO_THROW(stan::io::pad_help_option(0, "foo", 2));
76 
77  std::stringstream out;
78  EXPECT_NO_THROW(stan::io::pad_help_option(&out, "foo", 2));
79  EXPECT_EQ(" foo\n ", out.str());
81 
82  EXPECT_EQ("", stan::test::cout_ss.str());
83  EXPECT_EQ("", stan::test::cerr_ss.str());
84 }
void pad_help_option(std::ostream *o, const std::string &option="", unsigned int width=20)
Definition: cmd_line.hpp:27
bool val(const std::string &key, T &x) const
Definition: cmd_line.hpp:190
bool has_key(const std::string &key) const
Definition: cmd_line.hpp:166
void reset_std_streams()
Definition: util.hpp:47
bool has_flag(const std::string &flag) const
Definition: cmd_line.hpp:204
TEST(io_cmd_line, cmd_line_0)
z
Definition: test.py:28
size_t bare_size() const
Definition: cmd_line.hpp:213
void capture_std_streams()
Definition: util.hpp:36
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
bool bare(size_t n, T &x) const
Definition: cmd_line.hpp:231
std::stringstream cerr_ss
Definition: util.hpp:34
std::string command()
Definition: cmd_line.hpp:156
std::stringstream cout_ss
Definition: util.hpp:33