Josh Gao | 8f1fa00 | 2017-04-27 19:48:44 -0700 | [diff] [blame] | 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 | |
Christopher Ferris | a6f0b67 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | |
Josh Gao | 8f1fa00 | 2017-04-27 19:48:44 -0700 | [diff] [blame] | 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
Christopher Ferris | a6f0b67 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 47 | TEST(TestUtilsTest, CaptureStdout_smoke) { |
| 48 | CapturedStdout cap; |
| 49 | printf("This should be captured.\n"); |
Krzysztof Kosiński | e61c65c | 2020-08-04 17:40:17 -0700 | [diff] [blame] | 50 | fflush(stdout); |
Christopher Ferris | a6f0b67 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 51 | cap.Stop(); |
| 52 | printf("This will not be captured.\n"); |
Krzysztof Kosiński | e61c65c | 2020-08-04 17:40:17 -0700 | [diff] [blame] | 53 | fflush(stdout); |
Christopher Ferris | a6f0b67 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 54 | 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ński | e61c65c | 2020-08-04 17:40:17 -0700 | [diff] [blame] | 58 | fflush(stdout); |
Christopher Ferris | a6f0b67 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 59 | 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ński | e61c65c | 2020-08-04 17:40:17 -0700 | [diff] [blame] | 63 | fflush(stdout); |
Christopher Ferris | a6f0b67 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 64 | cap.Reset(); |
| 65 | cap.Start(); |
| 66 | printf("Only this will be captured.\n"); |
Krzysztof Kosiński | e61c65c | 2020-08-04 17:40:17 -0700 | [diff] [blame] | 67 | fflush(stdout); |
Christopher Ferris | a6f0b67 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 68 | ASSERT_EQ("Only this will be captured.\n", cap.str()); |
| 69 | } |
| 70 | |
| 71 | TEST(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 Gao | 8f1fa00 | 2017-04-27 19:48:44 -0700 | [diff] [blame] | 90 | } // namespace base |
| 91 | } // namespace android |