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 cap.Stop();
51 printf("This will not be captured.\n");
52 ASSERT_EQ("This should be captured.\n", cap.str());
53
54 cap.Start();
55 printf("And this text should be captured too.\n");
56 cap.Stop();
57 ASSERT_EQ("This should be captured.\nAnd this text should be captured too.\n", cap.str());
58
59 printf("Still not going to be captured.\n");
60 cap.Reset();
61 cap.Start();
62 printf("Only this will be captured.\n");
63 ASSERT_EQ("Only this will be captured.\n", cap.str());
64 }
65
TEST(TestUtilsTest,CaptureStderr_smoke)66 TEST(TestUtilsTest, CaptureStderr_smoke) {
67 CapturedStderr cap;
68 fprintf(stderr, "This should be captured.\n");
69 cap.Stop();
70 fprintf(stderr, "This will not be captured.\n");
71 ASSERT_EQ("This should be captured.\n", cap.str());
72
73 cap.Start();
74 fprintf(stderr, "And this text should be captured too.\n");
75 cap.Stop();
76 ASSERT_EQ("This should be captured.\nAnd this text should be captured too.\n", cap.str());
77
78 fprintf(stderr, "Still not going to be captured.\n");
79 cap.Reset();
80 cap.Start();
81 fprintf(stderr, "Only this will be captured.\n");
82 ASSERT_EQ("Only this will be captured.\n", cap.str());
83 }
84
85 } // namespace base
86 } // namespace android
87