summaryrefslogtreecommitdiff
path: root/base/test_utils_test.cpp
diff options
context:
space:
mode:
authorChristopher Ferris <cferris@google.com>2018-08-30 13:31:45 -0700
committerChristopher Ferris <cferris@google.com>2018-09-04 14:29:23 -0700
commiteea85c9aabf38d39e0474716df3b10502ced9586 (patch)
treef8982eaa50950ff776df46f8b6cce211cdaf02bd /base/test_utils_test.cpp
parent640ceee567c7f7e3832f9de189580fd3e138e951 (diff)
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
Diffstat (limited to 'base/test_utils_test.cpp')
-rw-r--r--base/test_utils_test.cpp40
1 files changed, 40 insertions, 0 deletions
diff --git a/base/test_utils_test.cpp b/base/test_utils_test.cpp
index 597271a722..15a79dd9d2 100644
--- a/base/test_utils_test.cpp
+++ b/base/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 @@ TEST(TestUtilsTest, ExpectNotMatch) {
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