Skip to content

Commit 8bb1cc1

Browse files
committed
misc
1 parent d85252d commit 8bb1cc1

File tree

6 files changed

+101
-52
lines changed

6 files changed

+101
-52
lines changed

ortools/algorithms/python/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ load("@pybind11_bazel//:build_defs.bzl", "pybind_extension")
1818
load("@rules_cc//cc:cc_library.bzl", "cc_library")
1919
load("@rules_python//python:py_test.bzl", "py_test")
2020

21+
package(default_visibility = ["//visibility:public"])
22+
2123
# knapsack_solver
2224
cc_library(
2325
name = "knapsack_solver_doc",

ortools/algorithms/samples/knapsack.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
"""A simple knapsack problem."""
16+
1617
# [START program]
1718
# [START import]
1819
from ortools.algorithms.python import knapsack_solver

ortools/port/BUILD.bazel

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,12 @@ cc_library(
6969
"@abseil-cpp//absl/log:check",
7070
],
7171
)
72+
73+
cc_test(
74+
name = "scoped_std_stream_capture_test",
75+
srcs = ["scoped_std_stream_capture_test.cc"],
76+
deps = [
77+
":scoped_std_stream_capture",
78+
"//ortools/base:gmock_main",
79+
],
80+
)

ortools/port/scoped_std_stream_capture.cc

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,51 +13,23 @@
1313

1414
#include "ortools/port/scoped_std_stream_capture.h"
1515

16-
#ifdef OPERATIONS_RESEARCH_OUTPUT_CAPTURE_SUPPORTED
17-
1816
#include <string>
1917

2018
#include "absl/log/check.h"
21-
#include "testing/base/public/googletest.h"
2219

2320
namespace operations_research {
2421

22+
const bool ScopedStdStreamCapture::kIsSupported = false;
23+
2524
ScopedStdStreamCapture::ScopedStdStreamCapture(const CapturedStream stream)
26-
: stream_(stream) {
27-
switch (stream) {
28-
case CapturedStream::kStdout:
29-
CaptureTestStdout();
30-
break;
31-
case CapturedStream::kStderr:
32-
CaptureTestStderr();
33-
break;
34-
}
35-
}
25+
: stream_(stream) {}
3626

37-
ScopedStdStreamCapture::~ScopedStdStreamCapture() {
38-
if (!capture_released_) {
39-
switch (stream_) {
40-
case CapturedStream::kStdout:
41-
GetCapturedTestStdout();
42-
break;
43-
case CapturedStream::kStderr:
44-
GetCapturedTestStderr();
45-
break;
46-
}
47-
}
48-
}
27+
ScopedStdStreamCapture::~ScopedStdStreamCapture() {}
4928

5029
std::string ScopedStdStreamCapture::StopCaptureAndReturnContents() && {
5130
CHECK(!capture_released_) << "Can't call this function twice.";
5231
capture_released_ = true;
53-
switch (stream_) {
54-
case CapturedStream::kStdout:
55-
return GetCapturedTestStdout();
56-
case CapturedStream::kStderr:
57-
return GetCapturedTestStderr();
58-
}
32+
return {};
5933
}
6034

6135
} // namespace operations_research
62-
63-
#endif // OPERATIONS_RESEARCH_OUTPUT_CAPTURE_SUPPORTED

ortools/port/scoped_std_stream_capture.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,64 @@
1414
#ifndef ORTOOLS_PORT_SCOPED_STD_STREAM_CAPTURE_H_
1515
#define ORTOOLS_PORT_SCOPED_STD_STREAM_CAPTURE_H_
1616

17+
#include <string>
18+
19+
namespace operations_research {
20+
21+
// The stream to capture for
22+
enum class CapturedStream { kStdout, kStderr };
23+
24+
// RAII class that captures stdout & stderr in tests until either
25+
// StopCaptureAndReturnContents() is called or it is destroyed.
26+
//
27+
// This capture is not available in all configurations, thus the `kIsSupported`
28+
// constant should be used in tests to disable the checks on the captured
29+
// result.
30+
//
31+
// Usage:
32+
//
33+
// TEST(MyTest, Something) {
34+
// ...
35+
// ScopedStdStreamCapture stdout_capture(CapturedStream::kStdout);
36+
// ... some code that may return ...
37+
// if (ScopedStdStreamCapture::kIsSupported) {
38+
// EXPECT_THAT(std::move(stdout_capture).StopCaptureAndReturnContents(),
39+
// HasSubstr("..."));
40+
// }
41+
// }
42+
class ScopedStdStreamCapture {
43+
public:
44+
// True if the capture is supported by the configuration.
45+
//
46+
// When false, no capture occurs and StopCaptureAndReturnContents() returns an
47+
// empty string.
48+
static const bool kIsSupported;
49+
50+
// Starts the capture.
51+
explicit ScopedStdStreamCapture(CapturedStream stream);
52+
53+
ScopedStdStreamCapture(const ScopedStdStreamCapture&) = delete;
54+
ScopedStdStreamCapture& operator=(const ScopedStdStreamCapture&) = delete;
55+
ScopedStdStreamCapture& operator=(ScopedStdStreamCapture&&) = delete;
56+
ScopedStdStreamCapture(ScopedStdStreamCapture&& other) = delete;
57+
58+
// Releases the capture if needed.
59+
~ScopedStdStreamCapture();
60+
61+
// Stops the capture and returns the captured data.
62+
//
63+
// Returns an empty string when kIsSupported is false.
64+
//
65+
// This function can only be called once. This function requires moving *this
66+
// so that the user gets a ClangTidy error (bugprone-use-after-move) when they
67+
// try to use this function more than once.
68+
std::string StopCaptureAndReturnContents() &&;
69+
70+
private:
71+
const CapturedStream stream_;
72+
bool capture_released_ = false;
73+
};
74+
75+
} // namespace operations_research
76+
1777
#endif // ORTOOLS_PORT_SCOPED_STD_STREAM_CAPTURE_H_

ortools/port/scoped_std_stream_capture_test.cc

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,24 @@
2121
namespace operations_research {
2222
namespace {
2323

24-
#ifdef OPERATIONS_RESEARCH_OUTPUT_CAPTURE_SUPPORTED
25-
2624
TEST(ScopedStdStreamCaptureTest, CaptureStdout) {
2725
ScopedStdStreamCapture capture(CapturedStream::kStdout);
2826
std::cout << "something";
29-
EXPECT_EQ(std::move(capture).StopCaptureAndReturnContents(), "something");
27+
if (ScopedStdStreamCapture::kIsSupported) {
28+
EXPECT_EQ(std::move(capture).StopCaptureAndReturnContents(), "something");
29+
} else {
30+
EXPECT_EQ(std::move(capture).StopCaptureAndReturnContents(), "");
31+
}
3032
}
3133

3234
TEST(ScopedStdStreamCaptureTest, CaptureStderr) {
3335
ScopedStdStreamCapture capture(CapturedStream::kStderr);
3436
std::cerr << "something";
35-
EXPECT_EQ(std::move(capture).StopCaptureAndReturnContents(), "something");
37+
if (ScopedStdStreamCapture::kIsSupported) {
38+
EXPECT_EQ(std::move(capture).StopCaptureAndReturnContents(), "something");
39+
} else {
40+
EXPECT_EQ(std::move(capture).StopCaptureAndReturnContents(), "");
41+
}
3642
}
3743

3844
TEST(ScopedStdStreamCaptureTest, EarlyExitThenCaptureStdout) {
@@ -43,8 +49,10 @@ TEST(ScopedStdStreamCaptureTest, EarlyExitThenCaptureStdout) {
4349
{
4450
ScopedStdStreamCapture capture(CapturedStream::kStdout);
4551
std::cout << "another thing";
46-
EXPECT_EQ(std::move(capture).StopCaptureAndReturnContents(),
47-
"another thing");
52+
if (ScopedStdStreamCapture::kIsSupported) {
53+
EXPECT_EQ(std::move(capture).StopCaptureAndReturnContents(),
54+
"another thing");
55+
}
4856
}
4957
}
5058

@@ -56,8 +64,10 @@ TEST(ScopedStdStreamCaptureTest, EarlyExitThenCaptureStderr) {
5664
{
5765
ScopedStdStreamCapture capture(CapturedStream::kStderr);
5866
std::cerr << "another thing";
59-
EXPECT_EQ(std::move(capture).StopCaptureAndReturnContents(),
60-
"another thing");
67+
if (ScopedStdStreamCapture::kIsSupported) {
68+
EXPECT_EQ(std::move(capture).StopCaptureAndReturnContents(),
69+
"another thing");
70+
}
6171
}
6272
}
6373

@@ -68,8 +78,12 @@ TEST(ScopedStdStreamCaptureTest, CaptureStdoutAndStderr) {
6878
std::cout << "stdout";
6979
std::cerr << "stderr";
7080

71-
EXPECT_EQ(std::move(stdout_capture).StopCaptureAndReturnContents(), "stdout");
72-
EXPECT_EQ(std::move(stderr_capture).StopCaptureAndReturnContents(), "stderr");
81+
if (ScopedStdStreamCapture::kIsSupported) {
82+
EXPECT_EQ(std::move(stdout_capture).StopCaptureAndReturnContents(),
83+
"stdout");
84+
EXPECT_EQ(std::move(stderr_capture).StopCaptureAndReturnContents(),
85+
"stderr");
86+
}
7387
}
7488

7589
TEST(ScopedStdStreamCaptureDeathTest, TwoCalls) {
@@ -89,14 +103,5 @@ TEST(ScopedStdStreamCaptureDeathTest, TwoCalls) {
89103
"twice");
90104
}
91105

92-
#else // OPERATIONS_RESEARCH_OUTPUT_CAPTURE_SUPPORTED
93-
94-
TEST(ScopedStdStreamCaptureTest, NotSupported) {
95-
// This unit test is intentionally empty since iOS test fails when zero tests
96-
// are executed.
97-
}
98-
99-
#endif // OPERATIONS_RESEARCH_OUTPUT_CAPTURE_SUPPORTED
100-
101106
} // namespace
102107
} // namespace operations_research

0 commit comments

Comments
 (0)