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 use std::path::PathBuf;
7
8 #[gtest(Test, InTopModule)]
test()9 fn test() {
10 expect_true!(true);
11 }
12
13 #[gtest(Test, WithCustomMessage)]
test()14 fn test() {
15 expect_true!(true, "foo");
16 expect_true!(true, "foo {}", 1);
17 expect_eq!(5, 5, "math stopped working");
18 expect_eq!(5 + 5, 10, "uh {}", "oh");
19 }
20
21 mod module1 {
22 use super::*;
23
24 #[gtest(Test, InChildModule)]
test()25 fn test() {
26 expect_true!(true);
27 }
28
29 mod module2 {
30 use super::*;
31
32 #[gtest(Test, InGrandChildModule)]
test()33 fn test() {
34 expect_true!(true);
35 }
36 }
37 }
38
39 #[allow(dead_code)]
bar()40 fn bar() {
41 #[gtest(Test, InFunctionBody)]
42 fn test() {
43 expect_true!(true);
44 }
45 }
46
47 mod module3 {
48 use super::*;
49
50 #[allow(dead_code)]
bar()51 fn bar() {
52 #[gtest(Test, InFunctionBodyInChildModule)]
53 fn test() {
54 expect_true!(true);
55 }
56 }
57 }
58
59 #[gtest(ExactSuite, ExactTest)]
test()60 fn test() {}
61
62 #[gtest(Test, WithResultType)]
test() -> std::io::Result<()>63 fn test() -> std::io::Result<()> {
64 expect_true!(true);
65 Ok(())
66 }
67
68 #[gtest(Test, WithBoxResultType)]
test() -> std::result::Result<(), Box<dyn std::error::Error>>69 fn test() -> std::result::Result<(), Box<dyn std::error::Error>> {
70 expect_true!(true);
71 Ok(())
72 }
73
74 // This test intentionally fails due to returning Err, and displays the message
75 // "uhoh."
76 #[gtest(Test, DISABLED_WithError)]
test() -> std::result::Result<(), Box<dyn std::error::Error>>77 fn test() -> std::result::Result<(), Box<dyn std::error::Error>> {
78 expect_true!(true);
79 Err("uhoh".into())
80 }
81
82 // TODO(danakj): It would be nice to test expect macros, but we would need to
83 // hook up EXPECT_NONFATAL_FAILURE to do so. There's no way to fail a test in a
84 // way that we accept, the bots see the failure even if the process returns 0.
85 // #[gtest(ExpectFailTest, Failures)]
86 // fn test() {
87 // expect_eq!(1 + 1, 1 + 2);
88 // expect_ne!(2 + 3, 3 + 2);
89 // expect_lt!(1 + 1, 1 + 0);
90 // expect_gt!(1 + 0, 1 + 1);
91 // expect_le!(1 + 1, 1 + 0);
92 // expect_ge!(1 + 0, 1 + 1);
93 // expect_true!(true && false);
94 // expect_false!(true || false);
95 // unsafe { COUNTER += 1 };
96 // }
97
98 #[gtest(Test, Paths)]
test()99 fn test() {
100 let expected_result =
101 ["foo", "bar.rs"].iter().collect::<PathBuf>().to_string_lossy().to_string();
102
103 expect_eq!(
104 rust_gtest_interop::__private::make_canonical_file_path("foo/bar.rs"),
105 expected_result
106 );
107 expect_eq!(
108 rust_gtest_interop::__private::make_canonical_file_path("../foo/bar.rs"),
109 expected_result
110 );
111 expect_eq!(
112 rust_gtest_interop::__private::make_canonical_file_path("../../foo/bar.rs"),
113 expected_result
114 );
115 expect_eq!(
116 rust_gtest_interop::__private::make_canonical_file_path("a/../foo/bar.rs"),
117 expected_result
118 );
119 expect_eq!(
120 rust_gtest_interop::__private::make_canonical_file_path("a/../../../foo/bar.rs"),
121 expected_result
122 );
123 expect_eq!(
124 rust_gtest_interop::__private::make_canonical_file_path("a/../b/../../foo/bar.rs"),
125 expected_result
126 );
127
128 #[cfg(windows)]
129 {
130 expect_eq!(
131 rust_gtest_interop::__private::make_canonical_file_path(r"foo\bar.rs"),
132 r"foo\bar.rs"
133 );
134
135 expect_eq!(
136 rust_gtest_interop::__private::make_canonical_file_path(r"..\foo\bar.rs"),
137 r"foo\bar.rs"
138 );
139
140 expect_eq!(
141 rust_gtest_interop::__private::make_canonical_file_path(r"..\..\foo\bar.rs"),
142 r"foo\bar.rs"
143 );
144
145 expect_eq!(
146 rust_gtest_interop::__private::make_canonical_file_path(r"a\..\foo\bar.rs"),
147 r"foo\bar.rs"
148 );
149
150 expect_eq!(
151 rust_gtest_interop::__private::make_canonical_file_path(r"a\..\..\..\foo\bar.rs"),
152 r"foo\bar.rs"
153 );
154
155 expect_eq!(
156 rust_gtest_interop::__private::make_canonical_file_path(r"a\..\b\..\..\foo\bar.rs"),
157 expected_result
158 );
159 }
160 }
161