blob: 10e8a2a9450b865ab0cde1a7c5ffd33c95b443ac [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");
Elliott Hughes031d9c42021-09-30 18:12:52 -070030
31 ASSERT_MATCH(L"foobar", LR"(fo+baz?r)");
32 EXPECT_FATAL_FAILURE(ASSERT_MATCH(L"foobar", LR"(foobaz)"), "regex mismatch");
Josh Gao8f1fa002017-04-27 19:48:44 -070033}
34
35TEST(TestUtilsTest, AssertNotMatch) {
36 ASSERT_NOT_MATCH("foobar", R"(foobaz)");
37 EXPECT_FATAL_FAILURE(ASSERT_NOT_MATCH("foobar", R"(foobar)"), "regex mismatch");
Elliott Hughes031d9c42021-09-30 18:12:52 -070038
39 ASSERT_NOT_MATCH(L"foobar", LR"(foobaz)");
40 EXPECT_FATAL_FAILURE(ASSERT_NOT_MATCH(L"foobar", LR"(foobar)"), "regex mismatch");
Josh Gao8f1fa002017-04-27 19:48:44 -070041}
42
43TEST(TestUtilsTest, ExpectMatch) {
44 EXPECT_MATCH("foobar", R"(fo+baz?r)");
45 EXPECT_NONFATAL_FAILURE(EXPECT_MATCH("foobar", R"(foobaz)"), "regex mismatch");
Elliott Hughes031d9c42021-09-30 18:12:52 -070046
47 EXPECT_MATCH(L"foobar", LR"(fo+baz?r)");
48 EXPECT_NONFATAL_FAILURE(EXPECT_MATCH(L"foobar", LR"(foobaz)"), "regex mismatch");
Josh Gao8f1fa002017-04-27 19:48:44 -070049}
50
51TEST(TestUtilsTest, ExpectNotMatch) {
52 EXPECT_NOT_MATCH("foobar", R"(foobaz)");
53 EXPECT_NONFATAL_FAILURE(EXPECT_NOT_MATCH("foobar", R"(foobar)"), "regex mismatch");
Elliott Hughes031d9c42021-09-30 18:12:52 -070054
55 EXPECT_NOT_MATCH(L"foobar", LR"(foobaz)");
56 EXPECT_NONFATAL_FAILURE(EXPECT_NOT_MATCH(L"foobar", LR"(foobar)"), "regex mismatch");
Josh Gao8f1fa002017-04-27 19:48:44 -070057}
58
Christopher Ferrisa6f0b672018-08-30 13:31:45 -070059TEST(TestUtilsTest, CaptureStdout_smoke) {
60 CapturedStdout cap;
61 printf("This should be captured.\n");
Krzysztof Kosińskie61c65c2020-08-04 17:40:17 -070062 fflush(stdout);
Christopher Ferrisa6f0b672018-08-30 13:31:45 -070063 cap.Stop();
64 printf("This will not be captured.\n");
Krzysztof Kosińskie61c65c2020-08-04 17:40:17 -070065 fflush(stdout);
Christopher Ferrisa6f0b672018-08-30 13:31:45 -070066 ASSERT_EQ("This should be captured.\n", cap.str());
67
68 cap.Start();
69 printf("And this text should be captured too.\n");
Krzysztof Kosińskie61c65c2020-08-04 17:40:17 -070070 fflush(stdout);
Christopher Ferrisa6f0b672018-08-30 13:31:45 -070071 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");
Krzysztof Kosińskie61c65c2020-08-04 17:40:17 -070075 fflush(stdout);
Christopher Ferrisa6f0b672018-08-30 13:31:45 -070076 cap.Reset();
77 cap.Start();
78 printf("Only this will be captured.\n");
Krzysztof Kosińskie61c65c2020-08-04 17:40:17 -070079 fflush(stdout);
Christopher Ferrisa6f0b672018-08-30 13:31:45 -070080 ASSERT_EQ("Only this will be captured.\n", cap.str());
81}
82
83TEST(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
Josh Gao8f1fa002017-04-27 19:48:44 -0700102} // namespace base
103} // namespace android