Skip to content

Commit a79dc96

Browse files
committed
Added unit tests for rustdoc rules
1 parent 6630fd5 commit a79dc96

File tree

7 files changed

+242
-0
lines changed

7 files changed

+242
-0
lines changed

test/unit/rustdoc/BUILD.bazel

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
load(":rustdoc_unit_test.bzl", "rustdoc_test_suite")
2+
3+
rustdoc_test_suite(
4+
name = "rustdoc_test_suite",
5+
)

test/unit/rustdoc/rustdoc.cc

Whitespace-only changes.

test/unit/rustdoc/rustdoc.h

Whitespace-only changes.

test/unit/rustdoc/rustdoc_bin.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn main() {}

test/unit/rustdoc/rustdoc_lib.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#[cfg(feature = "with_proc_macro")]
2+
use rustdoc_proc_macro::make_answer;
3+
4+
#[cfg(feature = "with_proc_macro")]
5+
make_answer!();
6+
7+
/// The answer to the ultimate question
8+
/// ```
9+
/// fn answer() -> u32 { 42 }
10+
/// assert_eq!(answer(), 42);
11+
/// ```
12+
#[cfg(not(feature = "with_proc_macro"))]
13+
pub fn answer() -> u32 {
14+
42
15+
}
16+
17+
#[cfg(test)]
18+
mod test {
19+
use super::*;
20+
21+
#[test]
22+
fn test_answer() {
23+
assert_eq!(answer(), 42);
24+
}
25+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use proc_macro::TokenStream;
2+
3+
#[proc_macro]
4+
pub fn make_answer(_item: TokenStream) -> TokenStream {
5+
"\
6+
/// A procedural macro example:
7+
/// ```
8+
/// fn answer() -> u32 { 42 }
9+
/// assert_eq!(answer(), 42);
10+
/// ```
11+
pub fn answer() -> u32 { 42 }"
12+
.parse()
13+
.unwrap()
14+
}
+197
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
"""Unittest to verify properties of rustdoc rules"""
2+
3+
load("@bazel_skylib//lib:unittest.bzl", "analysistest")
4+
load("@rules_cc//cc:defs.bzl", "cc_library")
5+
load("//rust:defs.bzl", "rust_binary", "rust_doc", "rust_doc_test", "rust_library", "rust_proc_macro", "rust_test")
6+
load(
7+
"//test/unit:common.bzl",
8+
"assert_action_mnemonic",
9+
"assert_argv_contains_prefix_not",
10+
)
11+
12+
def _common_rustdoc_checks(env, tut):
13+
actions = tut.actions
14+
action = actions[0]
15+
assert_action_mnemonic(env, action, "Rustdoc")
16+
17+
# These flags, while required for `Rustc` actions, should be omitted for
18+
# `Rustdoc` actions
19+
assert_argv_contains_prefix_not(env, action, "--remap-path-prefix")
20+
assert_argv_contains_prefix_not(env, action, "--emit")
21+
22+
def _rustdoc_for_lib_test_impl(ctx):
23+
env = analysistest.begin(ctx)
24+
tut = analysistest.target_under_test(env)
25+
26+
_common_rustdoc_checks(env, tut)
27+
28+
return analysistest.end(env)
29+
30+
def _rustdoc_for_bin_test_impl(ctx):
31+
env = analysistest.begin(ctx)
32+
tut = analysistest.target_under_test(env)
33+
34+
_common_rustdoc_checks(env, tut)
35+
36+
return analysistest.end(env)
37+
38+
def _rustdoc_for_proc_macro_test_impl(ctx):
39+
env = analysistest.begin(ctx)
40+
tut = analysistest.target_under_test(env)
41+
42+
_common_rustdoc_checks(env, tut)
43+
44+
return analysistest.end(env)
45+
46+
def _rustdoc_for_lib_with_proc_macro_test_impl(ctx):
47+
env = analysistest.begin(ctx)
48+
tut = analysistest.target_under_test(env)
49+
50+
_common_rustdoc_checks(env, tut)
51+
52+
return analysistest.end(env)
53+
54+
def _rustdoc_for_bin_with_transitive_proc_macro_test_impl(ctx):
55+
env = analysistest.begin(ctx)
56+
tut = analysistest.target_under_test(env)
57+
58+
_common_rustdoc_checks(env, tut)
59+
60+
return analysistest.end(env)
61+
62+
def _rustdoc_for_lib_with_cc_lib_test_impl(ctx):
63+
env = analysistest.begin(ctx)
64+
tut = analysistest.target_under_test(env)
65+
66+
_common_rustdoc_checks(env, tut)
67+
68+
return analysistest.end(env)
69+
70+
rustdoc_for_lib_test = analysistest.make(_rustdoc_for_lib_test_impl)
71+
rustdoc_for_bin_test = analysistest.make(_rustdoc_for_bin_test_impl)
72+
rustdoc_for_proc_macro_test = analysistest.make(_rustdoc_for_proc_macro_test_impl)
73+
rustdoc_for_lib_with_proc_macro_test = analysistest.make(_rustdoc_for_lib_with_proc_macro_test_impl)
74+
rustdoc_for_bin_with_transitive_proc_macro_test = analysistest.make(_rustdoc_for_bin_with_transitive_proc_macro_test_impl)
75+
rustdoc_for_lib_with_cc_lib_test = analysistest.make(_rustdoc_for_lib_with_cc_lib_test_impl)
76+
77+
def _target_maker(rule_fn, name, **kwargs):
78+
rule_fn(
79+
name = name,
80+
**kwargs
81+
)
82+
83+
rust_test(
84+
name = "{}_test".format(name),
85+
crate = ":{}".format(name),
86+
)
87+
88+
rust_doc(
89+
name = "{}_doc".format(name),
90+
crate = ":{}".format(name),
91+
)
92+
93+
rust_doc_test(
94+
name = "{}_doctest".format(name),
95+
crate = ":{}".format(name),
96+
)
97+
98+
def _define_targets():
99+
_target_maker(
100+
rust_binary,
101+
name = "bin",
102+
srcs = ["rustdoc_bin.rs"],
103+
)
104+
105+
_target_maker(
106+
rust_library,
107+
name = "lib",
108+
srcs = ["rustdoc_lib.rs"],
109+
)
110+
111+
_target_maker(
112+
rust_proc_macro,
113+
name = "rustdoc_proc_macro",
114+
srcs = ["rustdoc_proc_macro.rs"],
115+
edition = "2018",
116+
)
117+
118+
_target_maker(
119+
rust_library,
120+
name = "lib_with_proc_macro",
121+
srcs = ["rustdoc_lib.rs"],
122+
proc_macro_deps = [":rustdoc_proc_macro"],
123+
crate_features = ["with_proc_macro"],
124+
)
125+
126+
_target_maker(
127+
rust_binary,
128+
name = "bin_with_transitive_proc_macro",
129+
srcs = ["rustdoc_bin.rs"],
130+
deps = [":lib_with_proc_macro"],
131+
proc_macro_deps = [":rustdoc_proc_macro"],
132+
crate_features = ["with_proc_macro"],
133+
)
134+
135+
cc_library(
136+
name = "cc_lib",
137+
hdrs = ["rustdoc.h"],
138+
srcs = ["rustdoc.cc"],
139+
)
140+
141+
_target_maker(
142+
rust_library,
143+
name = "lib_with_cc",
144+
srcs = ["rustdoc_lib.rs"],
145+
crate_features = ["with_cc"],
146+
deps = [":cc_lib"],
147+
)
148+
149+
def rustdoc_test_suite(name):
150+
"""Entry-point macro called from the BUILD file.
151+
152+
Args:
153+
name (str): Name of the macro.
154+
"""
155+
156+
_define_targets()
157+
158+
rustdoc_for_lib_test(
159+
name = "rustdoc_for_lib_test",
160+
target_under_test = ":lib_doc",
161+
)
162+
163+
rustdoc_for_bin_test(
164+
name = "rustdoc_for_bin_test",
165+
target_under_test = ":bin_doc",
166+
)
167+
168+
rustdoc_for_proc_macro_test(
169+
name = "rustdoc_for_proc_macro_test",
170+
target_under_test = ":rustdoc_proc_macro_doc",
171+
)
172+
173+
rustdoc_for_lib_with_proc_macro_test(
174+
name = "rustdoc_for_lib_with_proc_macro_test",
175+
target_under_test = ":lib_with_proc_macro_doc",
176+
)
177+
178+
rustdoc_for_bin_with_transitive_proc_macro_test(
179+
name = "rustdoc_for_bin_with_transitive_proc_macro_test",
180+
target_under_test = ":bin_with_transitive_proc_macro_doc",
181+
)
182+
183+
rustdoc_for_lib_with_cc_lib_test(
184+
name = "rustdoc_for_lib_with_cc_lib_test",
185+
target_under_test = ":lib_with_cc_doc",
186+
)
187+
188+
native.test_suite(
189+
name = name,
190+
tests = [
191+
":rustdoc_for_lib_test",
192+
":rustdoc_for_bin_test",
193+
":rustdoc_for_proc_macro_test",
194+
":rustdoc_for_lib_with_proc_macro_test",
195+
":rustdoc_for_lib_with_cc_lib_test",
196+
],
197+
)

0 commit comments

Comments
 (0)