rust_gtest_interop_unittest.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. use rust_gtest_interop::prelude::*;
  5. use std::pin::Pin;
  6. #[gtest(Test, InTopModule)]
  7. fn test() {
  8. expect_true!(true);
  9. }
  10. #[gtest(Test, WithCustomMessage)]
  11. fn test() {
  12. expect_true!(true, "foo");
  13. expect_true!(true, "foo {}", 1);
  14. expect_eq!(5, 5, "math stopped working");
  15. expect_eq!(5 + 5, 10, "uh {}", "oh");
  16. }
  17. mod module1 {
  18. use super::*;
  19. #[gtest(Test, InChildModule)]
  20. fn test() {
  21. expect_true!(true);
  22. }
  23. mod module2 {
  24. use super::*;
  25. #[gtest(Test, InGrandChildModule)]
  26. fn test() {
  27. expect_true!(true);
  28. }
  29. }
  30. }
  31. #[allow(dead_code)]
  32. fn bar() {
  33. #[gtest(Test, InFunctionBody)]
  34. fn test() {
  35. expect_true!(true);
  36. }
  37. }
  38. mod module3 {
  39. use super::*;
  40. #[allow(dead_code)]
  41. fn bar() {
  42. #[gtest(Test, InFunctionBodyInChildModule)]
  43. fn test() {
  44. expect_true!(true);
  45. }
  46. }
  47. }
  48. #[gtest(ExactSuite, ExactTest)]
  49. fn test() {}
  50. #[gtest(Test, WithResultType)]
  51. fn test() -> std::io::Result<()> {
  52. expect_true!(true);
  53. Ok(())
  54. }
  55. #[gtest(Test, WithBoxResultType)]
  56. fn test() -> std::result::Result<(), Box<dyn std::error::Error>> {
  57. expect_true!(true);
  58. Ok(())
  59. }
  60. // This test intentionally fails due to returning Err, and displays the message "uhoh."
  61. #[gtest(Test, DISABLED_WithError)]
  62. fn test() -> std::result::Result<(), Box<dyn std::error::Error>> {
  63. expect_true!(true);
  64. Err("uhoh".into())
  65. }
  66. // TODO(danakj): It would be nice to test expect macros, but we would need to hook up
  67. // EXPECT_NONFATAL_FAILURE to do so. There's no way to fail a test in a way that we accept, the bots
  68. // see the failure even if the process returns 0.
  69. // #[gtest(ExpectFailTest, Failures)]
  70. // fn test() {
  71. // expect_eq!(1 + 1, 1 + 2);
  72. // expect_ne!(2 + 3, 3 + 2);
  73. // expect_lt!(1 + 1, 1 + 0);
  74. // expect_gt!(1 + 0, 1 + 1);
  75. // expect_le!(1 + 1, 1 + 0);
  76. // expect_ge!(1 + 0, 1 + 1);
  77. // expect_true!(true && false);
  78. // expect_false!(true || false);
  79. // unsafe { COUNTER += 1 };
  80. // }
  81. #[gtest(Test, WithTestSubclassAsTestSuite)]
  82. #[gtest_suite(rust_gtest_interop_test_support::TestSubclass)]
  83. fn test(mut suite: Pin<&mut rust_gtest_interop_test_support::TestSubclass>) {
  84. expect_eq!(0, suite.as_ref().num_calls());
  85. expect_true!(suite.as_mut().get_true());
  86. expect_eq!(1, suite.as_ref().num_calls());
  87. expect_false!(suite.as_mut().get_false());
  88. expect_eq!(2, suite.as_ref().num_calls());
  89. }
  90. #[gtest(Test, WithCustomTemplateTestSuite)]
  91. #[gtest_suite(rust_gtest_interop_test_support::TestSubclassWithCustomTemplate)]
  92. fn test(mut suite: Pin<&mut rust_gtest_interop_test_support::TestSubclassWithCustomTemplate>) {
  93. expect_eq!(0, suite.as_ref().num_calls());
  94. expect_eq!(3, suite.as_mut().get_three());
  95. expect_eq!(1, suite.as_ref().num_calls());
  96. expect_eq!(4, suite.as_mut().get_four());
  97. expect_eq!(2, suite.as_ref().num_calls());
  98. }