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"); |
Elliott Hughes | 031d9c4 | 2021-09-30 18:12:52 -0700 | [diff] [blame] | 30 | |
| 31 | ASSERT_MATCH(L"foobar", LR"(fo+baz?r)"); |
| 32 | EXPECT_FATAL_FAILURE(ASSERT_MATCH(L"foobar", LR"(foobaz)"), "regex mismatch"); |
Josh Gao | 8f1fa00 | 2017-04-27 19:48:44 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | TEST(TestUtilsTest, AssertNotMatch) { |
| 36 | ASSERT_NOT_MATCH("foobar", R"(foobaz)"); |
| 37 | EXPECT_FATAL_FAILURE(ASSERT_NOT_MATCH("foobar", R"(foobar)"), "regex mismatch"); |
Elliott Hughes | 031d9c4 | 2021-09-30 18:12:52 -0700 | [diff] [blame] | 38 | |
| 39 | ASSERT_NOT_MATCH(L"foobar", LR"(foobaz)"); |
| 40 | EXPECT_FATAL_FAILURE(ASSERT_NOT_MATCH(L"foobar", LR"(foobar)"), "regex mismatch"); |
Josh Gao | 8f1fa00 | 2017-04-27 19:48:44 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | TEST(TestUtilsTest, ExpectMatch) { |
| 44 | EXPECT_MATCH("foobar", R"(fo+baz?r)"); |
| 45 | EXPECT_NONFATAL_FAILURE(EXPECT_MATCH("foobar", R"(foobaz)"), "regex mismatch"); |
Elliott Hughes | 031d9c4 | 2021-09-30 18:12:52 -0700 | [diff] [blame] | 46 | |
| 47 | EXPECT_MATCH(L"foobar", LR"(fo+baz?r)"); |
| 48 | EXPECT_NONFATAL_FAILURE(EXPECT_MATCH(L"foobar", LR"(foobaz)"), "regex mismatch"); |
Josh Gao | 8f1fa00 | 2017-04-27 19:48:44 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | TEST(TestUtilsTest, ExpectNotMatch) { |
| 52 | EXPECT_NOT_MATCH("foobar", R"(foobaz)"); |
| 53 | EXPECT_NONFATAL_FAILURE(EXPECT_NOT_MATCH("foobar", R"(foobar)"), "regex mismatch"); |
Elliott Hughes | 031d9c4 | 2021-09-30 18:12:52 -0700 | [diff] [blame] | 54 | |
| 55 | EXPECT_NOT_MATCH(L"foobar", LR"(foobaz)"); |
| 56 | EXPECT_NONFATAL_FAILURE(EXPECT_NOT_MATCH(L"foobar", LR"(foobar)"), "regex mismatch"); |
Josh Gao | 8f1fa00 | 2017-04-27 19:48:44 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Christopher Ferris | a6f0b67 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 59 | TEST(TestUtilsTest, CaptureStdout_smoke) { |
| 60 | CapturedStdout cap; |
| 61 | printf("This should be captured.\n"); |
Krzysztof Kosiński | e61c65c | 2020-08-04 17:40:17 -0700 | [diff] [blame] | 62 | fflush(stdout); |
Christopher Ferris | a6f0b67 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 63 | cap.Stop(); |
| 64 | printf("This will not be captured.\n"); |
Krzysztof Kosiński | e61c65c | 2020-08-04 17:40:17 -0700 | [diff] [blame] | 65 | fflush(stdout); |
Christopher Ferris | a6f0b67 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 66 | 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ński | e61c65c | 2020-08-04 17:40:17 -0700 | [diff] [blame] | 70 | fflush(stdout); |
Christopher Ferris | a6f0b67 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 71 | 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ński | e61c65c | 2020-08-04 17:40:17 -0700 | [diff] [blame] | 75 | fflush(stdout); |
Christopher Ferris | a6f0b67 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 76 | cap.Reset(); |
| 77 | cap.Start(); |
| 78 | printf("Only this will be captured.\n"); |
Krzysztof Kosiński | e61c65c | 2020-08-04 17:40:17 -0700 | [diff] [blame] | 79 | fflush(stdout); |
Christopher Ferris | a6f0b67 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 80 | ASSERT_EQ("Only this will be captured.\n", cap.str()); |
| 81 | } |
| 82 | |
| 83 | TEST(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 Gao | 8f1fa00 | 2017-04-27 19:48:44 -0700 | [diff] [blame] | 102 | } // namespace base |
| 103 | } // namespace android |