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