blob: 15a79dd9d24cb056be15e5197488e11f56db08e4 [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");
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
66TEST(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
Josh Gao8f1fa002017-04-27 19:48:44 -070085} // namespace base
86} // namespace android