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   ASSERT_MATCH(L"foobar", LR"(fo+baz?r)");
32   EXPECT_FATAL_FAILURE(ASSERT_MATCH(L"foobar", LR"(foobaz)"), "regex mismatch");
33 }
34 
TEST(TestUtilsTest,AssertNotMatch)35 TEST(TestUtilsTest, AssertNotMatch) {
36   ASSERT_NOT_MATCH("foobar", R"(foobaz)");
37   EXPECT_FATAL_FAILURE(ASSERT_NOT_MATCH("foobar", R"(foobar)"), "regex mismatch");
38 
39   ASSERT_NOT_MATCH(L"foobar", LR"(foobaz)");
40   EXPECT_FATAL_FAILURE(ASSERT_NOT_MATCH(L"foobar", LR"(foobar)"), "regex mismatch");
41 }
42 
TEST(TestUtilsTest,ExpectMatch)43 TEST(TestUtilsTest, ExpectMatch) {
44   EXPECT_MATCH("foobar", R"(fo+baz?r)");
45   EXPECT_NONFATAL_FAILURE(EXPECT_MATCH("foobar", R"(foobaz)"), "regex mismatch");
46 
47   EXPECT_MATCH(L"foobar", LR"(fo+baz?r)");
48   EXPECT_NONFATAL_FAILURE(EXPECT_MATCH(L"foobar", LR"(foobaz)"), "regex mismatch");
49 }
50 
TEST(TestUtilsTest,ExpectNotMatch)51 TEST(TestUtilsTest, ExpectNotMatch) {
52   EXPECT_NOT_MATCH("foobar", R"(foobaz)");
53   EXPECT_NONFATAL_FAILURE(EXPECT_NOT_MATCH("foobar", R"(foobar)"), "regex mismatch");
54 
55   EXPECT_NOT_MATCH(L"foobar", LR"(foobaz)");
56   EXPECT_NONFATAL_FAILURE(EXPECT_NOT_MATCH(L"foobar", LR"(foobar)"), "regex mismatch");
57 }
58 
TEST(TestUtilsTest,CaptureStdout_smoke)59 TEST(TestUtilsTest, CaptureStdout_smoke) {
60   CapturedStdout cap;
61   printf("This should be captured.\n");
62   fflush(stdout);
63   cap.Stop();
64   printf("This will not be captured.\n");
65   fflush(stdout);
66   ASSERT_EQ("This should be captured.\n", cap.str());
67 
68   cap.Start();
69   printf("And this text should be captured too.\n");
70   fflush(stdout);
71   cap.Stop();
72   ASSERT_EQ("This should be captured.\nAnd this text should be captured too.\n", cap.str());
73 
74   printf("Still not going to be captured.\n");
75   fflush(stdout);
76   cap.Reset();
77   cap.Start();
78   printf("Only this will be captured.\n");
79   fflush(stdout);
80   ASSERT_EQ("Only this will be captured.\n", cap.str());
81 }
82 
TEST(TestUtilsTest,CaptureStderr_smoke)83 TEST(TestUtilsTest, CaptureStderr_smoke) {
84   CapturedStderr cap;
85   fprintf(stderr, "This should be captured.\n");
86   cap.Stop();
87   fprintf(stderr, "This will not be captured.\n");
88   ASSERT_EQ("This should be captured.\n", cap.str());
89 
90   cap.Start();
91   fprintf(stderr, "And this text should be captured too.\n");
92   cap.Stop();
93   ASSERT_EQ("This should be captured.\nAnd this text should be captured too.\n", cap.str());
94 
95   fprintf(stderr, "Still not going to be captured.\n");
96   cap.Reset();
97   cap.Start();
98   fprintf(stderr, "Only this will be captured.\n");
99   ASSERT_EQ("Only this will be captured.\n", cap.str());
100 }
101 
102 }  // namespace base
103 }  // namespace android
104