stanc_helper_test.cpp
Go to the documentation of this file.
1 // TODO(carpenter): move this into test/unit/command
2 // it's here now because it won't compile there
3 
4 #include <gtest/gtest.h>
5 #include <stan/lang/compiler.hpp>
7 #include <test/unit/util.hpp>
8 #include <fstream>
9 #include <sstream>
10 
12  EXPECT_TRUE(str.find(target) != std::string::npos)
13  << str << " does not contain " << target << std::endl;
14 }
15 
16 TEST(commandStancHelper, printVersion) {
17  std::stringstream ss;
18  print_version(&ss);
19  expect_find(ss.str(), "stanc version 2.");
20 }
21 
22 TEST(commandStancHelper, printStancHelp) {
23  std::stringstream ss;
24  print_stanc_help(&ss);
25  expect_find(ss.str(), "USAGE: stanc [options] <model_file>");
26  expect_find(ss.str(), "OPTIONS:");
27 }
28 
29 bool create_test_file(const std::string& path, const std::string& program) {
30  std::string cmd = "echo ";
31  cmd += "\"";
32  cmd += program;
33  cmd += "\"";
34  cmd += " > ";
35  cmd += path;
36  int return_code = system(cmd.c_str());
37  return return_code == 0;
38 }
39 
41  std::string program
42  = "parameters { real y; } model { y ~ normal(0, 1); }";
43  return create_test_file("test/test-models/temp-bar.stan", program);
44 }
45 
46 TEST(commandStancHelper, deleteFile) {
47  if (!create_test_file()) return;
48  std::stringstream ss;
49  delete_file(&ss, "test/test-models/temp-bar.stan");
50  // test there's no error message
51  EXPECT_EQ(0, ss.str().size());
52  // and then test the file stream can't be opened
53  std::fstream fs;
54  fs.open("test/test-models/temp-bar.stan", std::fstream::in);
55  EXPECT_FALSE(fs.is_open());
56  fs.close();
57 }
58 
60  std::ostream& out, std::ostream& err) {
61  int argc = 2;
62  std::vector<const char*> argv_vec;
63  argv_vec.push_back("main");
64  argv_vec.push_back(path.c_str());
65  const char** argv = &argv_vec[0];
66  return stanc_helper(argc, argv, &out, &err);
67 }
68 
69 TEST(commandStancHelper, readOnlyOK) {
70  std::stringstream out;
71  std::stringstream err;
72  int rc = run_helper("src/test/test-models/good/stanc_helper.stan", out, err);
73  EXPECT_EQ(0, rc)
74  << "out=" << out.str() << std::endl << "err=" << err.str() << std::endl;
75  expect_find(out.str(), "Model name=stanc_helper_model");
76  expect_find(out.str(), "Input file=src/test/test-models/good/stanc_helper.stan");
77  expect_find(out.str(), "Output file=stanc_helper_model.cpp");
78  delete_file(&err, "stanc_helper_model.cpp");
79  EXPECT_EQ(0, err.str().size())
80  << "error=" << err.str() << std::endl;
81 }
82 
83 TEST(commandStancHelper, failRC) {
84  std::stringstream out;
85  std::stringstream err;
86  int rc = run_helper("src/test/test-models/bad/stanc_helper.stan", out, err);
87 
88  // TODO(carpenter): This should be -2 but it's -3 so
89  // I only tested that it's != 0 to contrast with earlier success
90  EXPECT_TRUE(rc != 0);
91 }
92 
93 TEST(commandStancHelper, noSuchFile) {
94  std::stringstream out;
95  std::stringstream err;
96  int argc = 2;
97  std::vector<const char*> argv_vec;
98  argv_vec.push_back("main");
99  argv_vec.push_back("src/test/test-models/good/nosuchfile.stan");
100  const char** argv = &argv_vec[0];
101  int rc = stanc_helper(argc, argv, &out, &err);
102  EXPECT_GT(err.str().size(), 10)
103  << "error=" << err.str() << std::endl;
104  expect_find(err.str(), "Failed to open model file");
105  EXPECT_TRUE(rc != 0);
106 }
107 
108 TEST(commandStancHelper, readOnlyDirReadFile) {
109  std::stringstream out;
110  std::stringstream err;
111  int argc = 4;
112  std::vector<const char*> argv_vec;
113  argv_vec.push_back("main");
114  argv_vec.push_back("--name=m1");
115  argv_vec.push_back("--o=src/test/test-models/m1.cpp");
116  argv_vec.push_back("src/test/test-models/bad/read_only/m1.stan");
117  const char** argv = &argv_vec[0];
118  int rc = stanc_helper(argc, argv, &out, &err);
119  EXPECT_TRUE(rc == 0);
120  delete_file(&err, "src/test/test-models/m1.cpp");
121  EXPECT_EQ(0, err.str().size())
122  << "error=" << err.str() << std::endl;
123 }
124 
125 TEST(commandStancHelper, readOnlyDirWriteFile) {
126  std::stringstream out;
127  std::stringstream err;
128  int argc = 4;
129  std::vector<const char*> argv_vec;
130  argv_vec.push_back("main");
131  argv_vec.push_back("--name=m1");
132  argv_vec.push_back("--o=src/test/test-models/read_only/m1.cpp");
133  argv_vec.push_back("src/test/test-models/bad/read_only/m1.stan");
134  const char** argv = &argv_vec[0];
135  int rc = stanc_helper(argc, argv, &out, &err);
136  EXPECT_TRUE(rc != 0);
137 }
138 
139 TEST(commandStancHelper, readOnlyDirBadFile) {
140  std::stringstream out;
141  std::stringstream err;
142  int argc = 2;
143  std::vector<const char*> argv_vec;
144  argv_vec.push_back("main");
145  argv_vec.push_back("src/test/test-models/bad/read_only/nosuchfile.stan");
146  const char** argv = &argv_vec[0];
147  int rc = stanc_helper(argc, argv, &out, &err);
148  EXPECT_TRUE(rc != 0);
149 }
150 
system("rm -rf microbeam.root")
const XML_Char * target
Definition: expat.h:268
Float_t ss
Definition: plot.C:24
void delete_file(std::ostream *err_stream, const std::string &file_name)
void print_version(std::ostream *out_stream)
int run_helper(const std::string &path, std::ostream &out, std::ostream &err)
int stanc_helper(int argc, const char *argv[], std::ostream *out_stream, std::ostream *err_stream)
string cmd
Definition: run_hadd.py:52
TEST(commandStancHelper, printVersion)
void expect_find(const std::string &str, const std::string &target)
const std::string path
Definition: plot_BEN.C:43
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
ifstream in
Definition: comparison.C:7
void print_stanc_help(std::ostream *out_stream)
bool create_test_file(const std::string &path, const std::string &program)