Skip to content

Commit d4b5e97

Browse files
committed
tests: use raw string literals for checking output contains certain Rust code
This should be much easier to read.
1 parent a9a4f96 commit d4b5e97

File tree

2 files changed

+150
-22
lines changed

2 files changed

+150
-22
lines changed

tests/items/src/test_fn_attrs.rs

Lines changed: 129 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,42 +28,152 @@ pub fn test_fn_attrs() {
2828
// static __attribute__((always_inline)) void always_inline_static(void) {}
2929
// static __attribute__((__noinline__)) void noinline_static(void) {}
3030
// static void inline inline_static(void) {}
31-
assert!(src.contains("#[inline(always)]\nunsafe extern \"C\" fn rust_always_inline_static"));
32-
assert!(src.contains("#[inline(never)]\nunsafe extern \"C\" fn rust_noinline_static"));
33-
assert!(src.contains("#[inline]\nunsafe extern \"C\" fn rust_inline_static"));
34-
assert!(src.contains("#[inline]\nunsafe extern \"C\" fn rust_alt_kw_inline_static"));
35-
assert!(src.contains("#[inline]\nunsafe extern \"C\" fn rust_gnu_inline_static"));
36-
assert!(src.contains("#[cold]\nunsafe extern \"C\" fn rust_cold_used_attrs"));
31+
assert!(src.contains(
32+
r#"
33+
#[inline(always)]
34+
unsafe extern "C" fn rust_always_inline_static
35+
"#
36+
.trim(),
37+
));
38+
assert!(src.contains(
39+
r#"
40+
#[inline(never)]
41+
unsafe extern "C" fn rust_noinline_static
42+
"#
43+
.trim(),
44+
));
45+
assert!(src.contains(
46+
r#"
47+
#[inline]
48+
unsafe extern "C" fn rust_inline_static
49+
"#
50+
.trim(),
51+
));
52+
assert!(src.contains(
53+
r#"
54+
#[inline]
55+
unsafe extern "C" fn rust_alt_kw_inline_static
56+
"#
57+
.trim(),
58+
));
59+
assert!(src.contains(
60+
r#"
61+
#[inline]
62+
unsafe extern "C" fn rust_gnu_inline_static
63+
"#
64+
.trim(),
65+
));
66+
assert!(src.contains(
67+
r#"
68+
#[cold]
69+
unsafe extern "C" fn rust_cold_used_attrs
70+
"#
71+
.trim(),
72+
));
3773

3874
// __attribute__((__always_inline__)) void always_inline_nonstatic(void) {}
3975
// __attribute__((noinline)) void noinline_nonstatic(void) {}
4076
// void inline inline_nonstatic(void) {}
41-
assert!(src.contains("#[inline(always)]\nunsafe extern \"C\" fn rust_always_inline_nonstatic"));
42-
assert!(src.contains("#[inline(never)]\npub unsafe extern \"C\" fn rust_noinline_nonstatic"));
43-
assert!(src.contains("#[inline]\nunsafe extern \"C\" fn rust_inline_nonstatic"));
44-
assert!(src.contains("#[inline]\nunsafe extern \"C\" fn rust_alt_kw_inline_nonstatic"));
45-
assert!(src.contains("#[inline]\nunsafe extern \"C\" fn rust_gnu_inline_nonstatic"));
77+
assert!(src.contains(
78+
r#"
79+
#[inline(always)]
80+
unsafe extern "C" fn rust_always_inline_nonstatic
81+
"#
82+
.trim(),
83+
));
84+
assert!(src.contains(
85+
r#"
86+
#[inline(never)]
87+
pub unsafe extern "C" fn rust_noinline_nonstatic
88+
"#
89+
.trim(),
90+
));
91+
assert!(src.contains(
92+
r#"
93+
#[inline]
94+
unsafe extern "C" fn rust_inline_nonstatic
95+
"#
96+
.trim(),
97+
));
98+
assert!(src.contains(
99+
r#"
100+
#[inline]
101+
unsafe extern "C" fn rust_alt_kw_inline_nonstatic
102+
"#
103+
.trim(),
104+
));
105+
assert!(src.contains(
106+
r#"
107+
#[inline]
108+
unsafe extern "C" fn rust_gnu_inline_nonstatic
109+
"#
110+
.trim(),
111+
));
46112

47113
// extern void inline inline_extern(void) {}
48114
// extern void inline __attribute__((always_inline)) always_inline_extern(void) {}
49115
// extern void inline __attribute__((__gnu_inline__)) gnu_inline_extern(void) {}
50116
// extern void inline __attribute__((gnu_inline, always_inline)) always_inline_gnu_inline_extern(void) {}
51117
assert!(src.contains(
52-
"#[inline]\n#[linkage = \"external\"]\npub unsafe extern \"C\" fn rust_inline_extern"
118+
r#"
119+
#[inline]
120+
#[linkage = "external"]
121+
pub unsafe extern "C" fn rust_inline_extern
122+
"#
123+
.trim(),
124+
));
125+
assert!(src.contains(
126+
r#"
127+
#[inline]
128+
#[linkage = "external"]
129+
pub unsafe extern "C" fn rust_alt_kw_inline_extern
130+
"#
131+
.trim(),
132+
));
133+
assert!(src.contains(
134+
r#"
135+
#[inline(always)]
136+
pub unsafe extern "C" fn rust_always_inline_extern
137+
"#
138+
.trim(),
139+
));
140+
assert!(src.contains(
141+
r#"
142+
#[inline]
143+
unsafe extern "C" fn rust_gnu_inline_extern
144+
"#
145+
.trim(),
53146
));
54-
assert!(src.contains("#[inline]\n#[linkage = \"external\"]\npub unsafe extern \"C\" fn rust_alt_kw_inline_extern"));
55-
assert!(src.contains("#[inline(always)]\npub unsafe extern \"C\" fn rust_always_inline_extern"));
56-
assert!(src.contains("#[inline]\nunsafe extern \"C\" fn rust_gnu_inline_extern"));
57147
assert!(src.contains(
58-
"#[inline(always)]\nunsafe extern \"C\" fn rust_always_inline_gnu_inline_extern"
148+
r#"
149+
#[inline(always)]
150+
unsafe extern "C" fn rust_always_inline_gnu_inline_extern
151+
"#
152+
.trim(),
59153
));
60154
assert!(src.contains(
61-
"#[inline]\nunsafe extern \"C\" fn rust_gnu_inline_non_canonical_definition_extern"
155+
r#"
156+
#[inline]
157+
unsafe extern "C" fn rust_gnu_inline_non_canonical_definition_extern
158+
"#
159+
.trim(),
62160
));
63161

64162
if cfg!(not(target_os = "macos")) {
65163
// aliased_fn is aliased to the inline_extern function
66-
let aliased_fn_syntax = |public| format!("extern \"C\" {{\n #[link_name = \"inline_extern\"]\n {}fn aliased_fn();", public);
67-
assert!(src.contains(&aliased_fn_syntax("")) || src.contains(&aliased_fn_syntax("pub ")));
164+
let aliased_fn_syntax = |public| {
165+
format!(
166+
r#"
167+
extern "C" {{
168+
#[link_name = "inline_extern"]
169+
{}fn aliased_fn();
170+
"#,
171+
public
172+
)
173+
};
174+
assert!(
175+
src.contains(aliased_fn_syntax("").trim())
176+
|| src.contains(aliased_fn_syntax("pub ").trim())
177+
);
68178
}
69179
}

tests/statics/src/test_sections.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,27 @@ pub fn test_sectioned_used_static() {
5656
);
5757

5858
// This static is pub, but we want to ensure it has attributes applied
59-
assert!(src.contains("#[link_section = \"fb\"]\npub static mut rust_initialized_extern: ::core::ffi::c_int = 1 as ::core::ffi::c_int;"));
59+
assert!(src.contains(
60+
r#"
61+
#[link_section = "fb"]
62+
pub static mut rust_initialized_extern: ::core::ffi::c_int = 1 as ::core::ffi::c_int;
63+
"#
64+
.trim(),
65+
));
6066
// This static is pub only with --reorganize-definitions
61-
let aliased_static_syntax = |public| format!("extern \"C\" {{\n #[link_name = \"no_attrs\"]\n {}static mut rust_aliased_static: ::core::ffi::c_int;", public);
62-
assert!(src.contains(&aliased_static_syntax("")) || src.contains(&aliased_static_syntax("pub ")))
67+
let aliased_static_syntax = |public| {
68+
format!(
69+
r#"
70+
extern "C" {{
71+
#[link_name = "no_attrs"]
72+
{}static mut rust_aliased_static: ::core::ffi::c_int;
73+
"#,
74+
public
75+
)
76+
};
77+
assert!(
78+
src.contains(aliased_static_syntax("").trim())
79+
|| src.contains(aliased_static_syntax("pub ").trim())
80+
)
6381
}
6482
}

0 commit comments

Comments
 (0)