Add Start/Stop/Reset to CapturedStdFd.
Move the fd() function to be private since it should not have been
exposed in the first place.
Fix the way logging_test uses CapturedXXX.
Adding this because the new isolated testing doesn't print errors to
stderr so the ASSERT_ EXPECT_ messages can get swallowed. Also, it's easier
to reuse a CapturedXXX object in a test with these functions.
Test: New unit tests pass.
Change-Id: I38b113fc184146ce434802f80a9b7997fa83e78a
diff --git a/test_utils_test.cpp b/test_utils_test.cpp
index 597271a..15a79dd 100644
--- a/test_utils_test.cpp
+++ b/test_utils_test.cpp
@@ -14,6 +14,8 @@
* limitations under the License.
*/
+#include <stdio.h>
+
#include "android-base/test_utils.h"
#include <gtest/gtest-spi.h>
@@ -42,5 +44,43 @@
EXPECT_NONFATAL_FAILURE(EXPECT_NOT_MATCH("foobar", R"(foobar)"), "regex mismatch");
}
+TEST(TestUtilsTest, CaptureStdout_smoke) {
+ CapturedStdout cap;
+ printf("This should be captured.\n");
+ cap.Stop();
+ printf("This will not be captured.\n");
+ ASSERT_EQ("This should be captured.\n", cap.str());
+
+ cap.Start();
+ printf("And this text should be captured too.\n");
+ cap.Stop();
+ ASSERT_EQ("This should be captured.\nAnd this text should be captured too.\n", cap.str());
+
+ printf("Still not going to be captured.\n");
+ cap.Reset();
+ cap.Start();
+ printf("Only this will be captured.\n");
+ ASSERT_EQ("Only this will be captured.\n", cap.str());
+}
+
+TEST(TestUtilsTest, CaptureStderr_smoke) {
+ CapturedStderr cap;
+ fprintf(stderr, "This should be captured.\n");
+ cap.Stop();
+ fprintf(stderr, "This will not be captured.\n");
+ ASSERT_EQ("This should be captured.\n", cap.str());
+
+ cap.Start();
+ fprintf(stderr, "And this text should be captured too.\n");
+ cap.Stop();
+ ASSERT_EQ("This should be captured.\nAnd this text should be captured too.\n", cap.str());
+
+ fprintf(stderr, "Still not going to be captured.\n");
+ cap.Reset();
+ cap.Start();
+ fprintf(stderr, "Only this will be captured.\n");
+ ASSERT_EQ("Only this will be captured.\n", cap.str());
+}
+
} // namespace base
} // namespace android