1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdio.h>
18 
19 #include "android-base/test_utils.h"
20 
21 #include <gtest/gtest-spi.h>
22 #include <gtest/gtest.h>
23 
24 namespace android {
25 namespace base {
26 
TEST(TestUtilsTest,AssertMatch)27 TEST(TestUtilsTest, AssertMatch) {
28   ASSERT_MATCH("foobar", R"(fo+baz?r)");
29   EXPECT_FATAL_FAILURE(ASSERT_MATCH("foobar", R"(foobaz)"), "regex mismatch");
30 }
31 
TEST(TestUtilsTest,AssertNotMatch)32 TEST(TestUtilsTest, AssertNotMatch) {
33   ASSERT_NOT_MATCH("foobar", R"(foobaz)");
34   EXPECT_FATAL_FAILURE(ASSERT_NOT_MATCH("foobar", R"(foobar)"), "regex mismatch");
35 }
36 
TEST(TestUtilsTest,ExpectMatch)37 TEST(TestUtilsTest, ExpectMatch) {
38   EXPECT_MATCH("foobar", R"(fo+baz?r)");
39   EXPECT_NONFATAL_FAILURE(EXPECT_MATCH("foobar", R"(foobaz)"), "regex mismatch");
40 }
41 
TEST(TestUtilsTest,ExpectNotMatch)42 TEST(TestUtilsTest, ExpectNotMatch) {
43   EXPECT_NOT_MATCH("foobar", R"(foobaz)");
44   EXPECT_NONFATAL_FAILURE(EXPECT_NOT_MATCH("foobar", R"(foobar)"), "regex mismatch");
45 }
46 
TEST(TestUtilsTest,CaptureStdout_smoke)47 TEST(TestUtilsTest, CaptureStdout_smoke) {
48   CapturedStdout cap;
49   printf("This should be captured.\n");
50   fflush(stdout);
51   cap.Stop();
52   printf("This will not be captured.\n");
53   fflush(stdout);
54   ASSERT_EQ("This should be captured.\n", cap.str());
55 
56   cap.Start();
57   printf("And this text should be captured too.\n");
58   fflush(stdout);
59   cap.Stop();
60   ASSERT_EQ("This should be captured.\nAnd this text should be captured too.\n", cap.str());
61 
62   printf("Still not going to be captured.\n");
63   fflush(stdout);
64   cap.Reset();
65   cap.Start();
66   printf("Only this will be captured.\n");
67   fflush(stdout);
68   ASSERT_EQ("Only this will be captured.\n", cap.str());
69 }
70 
TEST(TestUtilsTest,CaptureStderr_smoke)71 TEST(TestUtilsTest, CaptureStderr_smoke) {
72   CapturedStderr cap;
73   fprintf(stderr, "This should be captured.\n");
74   cap.Stop();
75   fprintf(stderr, "This will not be captured.\n");
76   ASSERT_EQ("This should be captured.\n", cap.str());
77 
78   cap.Start();
79   fprintf(stderr, "And this text should be captured too.\n");
80   cap.Stop();
81   ASSERT_EQ("This should be captured.\nAnd this text should be captured too.\n", cap.str());
82 
83   fprintf(stderr, "Still not going to be captured.\n");
84   cap.Reset();
85   cap.Start();
86   fprintf(stderr, "Only this will be captured.\n");
87   ASSERT_EQ("Only this will be captured.\n", cap.str());
88 }
89 
90 }  // namespace base
91 }  // namespace android
92