blob: d08d5d6e492bd02eb5bba05aae817d6ccbd266cf [file] [log] [blame]
Josh Gao8f1fa002017-04-27 19:48:44 -07001/*
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
Christopher Ferrisa6f0b672018-08-30 13:31:45 -070017#include <stdio.h>
18
Josh Gao8f1fa002017-04-27 19:48:44 -070019#include "android-base/test_utils.h"
20
21#include <gtest/gtest-spi.h>
22#include <gtest/gtest.h>
23
24namespace android {
25namespace base {
26
27TEST(TestUtilsTest, AssertMatch) {
28 ASSERT_MATCH("foobar", R"(fo+baz?r)");
29 EXPECT_FATAL_FAILURE(ASSERT_MATCH("foobar", R"(foobaz)"), "regex mismatch");
30}
31
32TEST(TestUtilsTest, AssertNotMatch) {
33 ASSERT_NOT_MATCH("foobar", R"(foobaz)");
34 EXPECT_FATAL_FAILURE(ASSERT_NOT_MATCH("foobar", R"(foobar)"), "regex mismatch");
35}
36
37TEST(TestUtilsTest, ExpectMatch) {
38 EXPECT_MATCH("foobar", R"(fo+baz?r)");
39 EXPECT_NONFATAL_FAILURE(EXPECT_MATCH("foobar", R"(foobaz)"), "regex mismatch");
40}
41
42TEST(TestUtilsTest, ExpectNotMatch) {
43 EXPECT_NOT_MATCH("foobar", R"(foobaz)");
44 EXPECT_NONFATAL_FAILURE(EXPECT_NOT_MATCH("foobar", R"(foobar)"), "regex mismatch");
45}
46
Christopher Ferrisa6f0b672018-08-30 13:31:45 -070047TEST(TestUtilsTest, CaptureStdout_smoke) {
48 CapturedStdout cap;
49 printf("This should be captured.\n");
Krzysztof Kosińskie61c65c2020-08-04 17:40:17 -070050 fflush(stdout);
Christopher Ferrisa6f0b672018-08-30 13:31:45 -070051 cap.Stop();
52 printf("This will not be captured.\n");
Krzysztof Kosińskie61c65c2020-08-04 17:40:17 -070053 fflush(stdout);
Christopher Ferrisa6f0b672018-08-30 13:31:45 -070054 ASSERT_EQ("This should be captured.\n", cap.str());
55
56 cap.Start();
57 printf("And this text should be captured too.\n");
Krzysztof Kosińskie61c65c2020-08-04 17:40:17 -070058 fflush(stdout);
Christopher Ferrisa6f0b672018-08-30 13:31:45 -070059 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");
Krzysztof Kosińskie61c65c2020-08-04 17:40:17 -070063 fflush(stdout);
Christopher Ferrisa6f0b672018-08-30 13:31:45 -070064 cap.Reset();
65 cap.Start();
66 printf("Only this will be captured.\n");
Krzysztof Kosińskie61c65c2020-08-04 17:40:17 -070067 fflush(stdout);
Christopher Ferrisa6f0b672018-08-30 13:31:45 -070068 ASSERT_EQ("Only this will be captured.\n", cap.str());
69}
70
71TEST(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
Josh Gao8f1fa002017-04-27 19:48:44 -070090} // namespace base
91} // namespace android