test_utils_test.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #include <stdio.h>
  17. #include "android-base/test_utils.h"
  18. #include <gtest/gtest-spi.h>
  19. #include <gtest/gtest.h>
  20. namespace android {
  21. namespace base {
  22. TEST(TestUtilsTest, AssertMatch) {
  23. ASSERT_MATCH("foobar", R"(fo+baz?r)");
  24. EXPECT_FATAL_FAILURE(ASSERT_MATCH("foobar", R"(foobaz)"), "regex mismatch");
  25. }
  26. TEST(TestUtilsTest, AssertNotMatch) {
  27. ASSERT_NOT_MATCH("foobar", R"(foobaz)");
  28. EXPECT_FATAL_FAILURE(ASSERT_NOT_MATCH("foobar", R"(foobar)"), "regex mismatch");
  29. }
  30. TEST(TestUtilsTest, ExpectMatch) {
  31. EXPECT_MATCH("foobar", R"(fo+baz?r)");
  32. EXPECT_NONFATAL_FAILURE(EXPECT_MATCH("foobar", R"(foobaz)"), "regex mismatch");
  33. }
  34. TEST(TestUtilsTest, ExpectNotMatch) {
  35. EXPECT_NOT_MATCH("foobar", R"(foobaz)");
  36. EXPECT_NONFATAL_FAILURE(EXPECT_NOT_MATCH("foobar", R"(foobar)"), "regex mismatch");
  37. }
  38. TEST(TestUtilsTest, CaptureStdout_smoke) {
  39. CapturedStdout cap;
  40. printf("This should be captured.\n");
  41. cap.Stop();
  42. printf("This will not be captured.\n");
  43. ASSERT_EQ("This should be captured.\n", cap.str());
  44. cap.Start();
  45. printf("And this text should be captured too.\n");
  46. cap.Stop();
  47. ASSERT_EQ("This should be captured.\nAnd this text should be captured too.\n", cap.str());
  48. printf("Still not going to be captured.\n");
  49. cap.Reset();
  50. cap.Start();
  51. printf("Only this will be captured.\n");
  52. ASSERT_EQ("Only this will be captured.\n", cap.str());
  53. }
  54. TEST(TestUtilsTest, CaptureStderr_smoke) {
  55. CapturedStderr cap;
  56. fprintf(stderr, "This should be captured.\n");
  57. cap.Stop();
  58. fprintf(stderr, "This will not be captured.\n");
  59. ASSERT_EQ("This should be captured.\n", cap.str());
  60. cap.Start();
  61. fprintf(stderr, "And this text should be captured too.\n");
  62. cap.Stop();
  63. ASSERT_EQ("This should be captured.\nAnd this text should be captured too.\n", cap.str());
  64. fprintf(stderr, "Still not going to be captured.\n");
  65. cap.Reset();
  66. cap.Start();
  67. fprintf(stderr, "Only this will be captured.\n");
  68. ASSERT_EQ("Only this will be captured.\n", cap.str());
  69. }
  70. } // namespace base
  71. } // namespace android