Skip to content

Commit 359eb85

Browse files
authored
Merge pull request #1322 from stan-dev/improve-diagnose-tests
Helper for diagnose tests
2 parents 98b4e01 + 33c30db commit 359eb85

File tree

3 files changed

+49
-37
lines changed

3 files changed

+49
-37
lines changed

src/test/interface/csv_header_consistency_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <stan/mcmc/chains.hpp>
1+
#include <stan/mcmc/chainset.hpp>
22
#include <stan/services/error_codes.hpp>
33
#include <test/utility.hpp>
44
#include <gtest/gtest.h>
@@ -28,7 +28,7 @@ TEST(interface, csv_header_consistency) {
2828

2929
std::ifstream ifstream;
3030
ifstream.open(samples.c_str());
31-
stan::mcmc::chains<> chains(
31+
stan::mcmc::chainset chains(
3232
stan::io::stan_csv_reader::parse(ifstream, &std::cout));
3333
ifstream.close();
3434

src/test/interface/diagnose_test.cpp

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
#include <fstream>
44
#include <sstream>
55

6+
using cmdstan::test::compare_to_stored_output;
67
using cmdstan::test::count_matches;
78
using cmdstan::test::get_path_separator;
89
using cmdstan::test::run_command;
9-
using cmdstan::test::run_command_output;
1010

1111
TEST(CommandDiagnose, corr_gauss) {
1212
std::string path_separator;
@@ -16,14 +16,11 @@ TEST(CommandDiagnose, corr_gauss) {
1616
+ "interface" + path_separator + "example_output"
1717
+ path_separator + "corr_gauss_output.csv";
1818

19-
run_command_output out = run_command(command + " " + csv_file);
19+
auto out = run_command(command + " " + csv_file);
2020
ASSERT_FALSE(out.hasError) << "\"" << out.command << "\" quit with an error";
2121

22-
std::ifstream expected_output(
23-
"src/test/interface/example_output/corr_gauss.nom");
24-
std::stringstream ss;
25-
ss << expected_output.rdbuf();
26-
EXPECT_EQ(1, count_matches(ss.str(), out.output));
22+
compare_to_stored_output(out.output,
23+
"src/test/interface/example_output/corr_gauss.nom");
2724
}
2825

2926
TEST(CommandDiagnose, corr_gauss_depth8) {
@@ -34,14 +31,11 @@ TEST(CommandDiagnose, corr_gauss_depth8) {
3431
+ "interface" + path_separator + "example_output"
3532
+ path_separator + "corr_gauss_output_depth8.csv";
3633

37-
run_command_output out = run_command(command + " " + csv_file);
34+
auto out = run_command(command + " " + csv_file);
3835
ASSERT_FALSE(out.hasError) << "\"" << out.command << "\" quit with an error";
3936

40-
std::ifstream expected_output(
41-
"src/test/interface/example_output/corr_gauss_depth8.nom");
42-
std::stringstream ss;
43-
ss << expected_output.rdbuf();
44-
EXPECT_EQ(1, count_matches(ss.str(), out.output));
37+
compare_to_stored_output(
38+
out.output, "src/test/interface/example_output/corr_gauss_depth8.nom");
4539
}
4640

4741
TEST(CommandDiagnose, corr_gauss_depth15) {
@@ -52,14 +46,11 @@ TEST(CommandDiagnose, corr_gauss_depth15) {
5246
+ "interface" + path_separator + "example_output"
5347
+ path_separator + "corr_gauss_output_depth15.csv";
5448

55-
run_command_output out = run_command(command + " " + csv_file);
49+
auto out = run_command(command + " " + csv_file);
5650
ASSERT_FALSE(out.hasError) << "\"" << out.command << "\" quit with an error";
5751

58-
std::ifstream expected_output(
59-
"src/test/interface/example_output/corr_gauss_depth15.nom");
60-
std::stringstream ss;
61-
ss << expected_output.rdbuf();
62-
EXPECT_EQ(1, count_matches(ss.str(), out.output));
52+
compare_to_stored_output(
53+
out.output, "src/test/interface/example_output/corr_gauss_depth15.nom");
6354
}
6455

6556
TEST(CommandDiagnose, eight_schools) {
@@ -70,14 +61,11 @@ TEST(CommandDiagnose, eight_schools) {
7061
+ "interface" + path_separator + "example_output"
7162
+ path_separator + "eight_schools_output.csv";
7263

73-
run_command_output out = run_command(command + " " + csv_file);
64+
auto out = run_command(command + " " + csv_file);
7465
ASSERT_FALSE(out.hasError) << "\"" << out.command << "\" quit with an error";
7566

76-
std::ifstream expected_output(
77-
"src/test/interface/example_output/eight_schools.nom");
78-
std::stringstream ss;
79-
ss << expected_output.rdbuf();
80-
EXPECT_EQ(1, count_matches(ss.str(), out.output));
67+
compare_to_stored_output(
68+
out.output, "src/test/interface/example_output/eight_schools.nom");
8169
}
8270

8371
TEST(CommandDiagnose, mix) {
@@ -88,13 +76,11 @@ TEST(CommandDiagnose, mix) {
8876
+ "interface" + path_separator + "example_output"
8977
+ path_separator + "mix_output.*";
9078

91-
run_command_output out = run_command(command + " " + csv_file);
79+
auto out = run_command(command + " " + csv_file);
9280
ASSERT_FALSE(out.hasError) << "\"" << out.command << "\" quit with an error";
9381

94-
std::ifstream expected_output("src/test/interface/example_output/mix.nom");
95-
std::stringstream ss;
96-
ss << expected_output.rdbuf();
97-
EXPECT_EQ(1, count_matches(ss.str(), out.output));
82+
compare_to_stored_output(out.output,
83+
"src/test/interface/example_output/mix.nom");
9884
}
9985

10086
TEST(CommandDiagnose, divergences) {
@@ -105,11 +91,9 @@ TEST(CommandDiagnose, divergences) {
10591
+ "interface" + path_separator + "example_output"
10692
+ path_separator + "div_output*.csv";
10793

108-
run_command_output out = run_command(command + " " + csv_file);
94+
auto out = run_command(command + " " + csv_file);
10995
ASSERT_FALSE(out.hasError) << "\"" << out.command << "\" quit with an error";
11096

111-
std::ifstream expected_output("src/test/interface/example_output/div.nom");
112-
std::stringstream ss;
113-
ss << expected_output.rdbuf();
114-
EXPECT_EQ(1, count_matches(ss.str(), out.output));
97+
compare_to_stored_output(out.output,
98+
"src/test/interface/example_output/div.nom");
11599
}

src/test/utility.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,34 @@ struct temporary_unwritable_file {
364364
}
365365
};
366366

367+
void compare_to_stored_output(const std::string &output,
368+
const std::string &expected_path) {
369+
if (getenv("CMDSTAN_UPDATE_EXPECTED_OUTPUT")) {
370+
std::ofstream expected_output_file(expected_path);
371+
ASSERT_TRUE(expected_output_file.good())
372+
<< "Could not open expected output file: " << expected_path;
373+
expected_output_file << output;
374+
expected_output_file.close();
375+
std::cout << "Updated expected output file: " << expected_path << std::endl;
376+
return;
377+
}
378+
379+
std::ifstream expected_output_file(expected_path);
380+
ASSERT_TRUE(expected_output_file.good())
381+
<< "Could not open expected output file: " << expected_path;
382+
std::stringstream ss;
383+
ss << expected_output_file.rdbuf();
384+
expected_output_file.close();
385+
std::string expected_output = ss.str();
386+
387+
EXPECT_EQ(output, expected_output)
388+
<< "Output does not match expected output. Expected:\n--------\n"
389+
<< expected_output << "\n--------\nActual:\n--------\n"
390+
<< output
391+
<< "\n--------\n\nSet CMDSTAN_UPDATE_EXPECTED_OUTPUT=1 to update the "
392+
"expected output.\n";
393+
}
394+
367395
} // namespace test
368396
} // namespace cmdstan
369397
#endif

0 commit comments

Comments
 (0)