Skip to content

Commit 0f018b6

Browse files
authored
Merge pull request #17 from YuhanLiin/add-tests
Add trybuild tests for macros
2 parents ca7d4e9 + cd211d9 commit 0f018b6

32 files changed

+334
-11
lines changed

.github/workflows/test-macro.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
on: [push, pull_request]
2+
3+
name: Run Macro Tests
4+
5+
jobs:
6+
testing:
7+
name: testing
8+
runs-on: ubuntu-18.04
9+
10+
steps:
11+
- name: Checkout sources
12+
uses: actions/checkout@v2
13+
14+
- name: Install toolchain
15+
uses: actions-rs/toolchain@v1
16+
with:
17+
profile: minimal
18+
toolchain: nightly
19+
override: true
20+
21+
- name: Test macro library
22+
uses: actions-rs/cargo@v1
23+
with:
24+
command: test
25+
args: -p msp430-rt-macros --features device

macros/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,15 @@ version = "0.8.4"
2626
features = ["extra-traits", "full"]
2727
version = "1.0.85"
2828

29+
[dev-dependencies]
30+
msp430 = "0.3.0"
31+
32+
[target.'cfg(not(target_os = "none"))'.dev-dependencies]
33+
trybuild = "1"
34+
2935
[features]
3036
device = []
37+
38+
[[test]]
39+
name = "compiletest"
40+
required-features = ["device"]

macros/src/lib.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ use syn::{
5858
///
5959
/// ``` no_run
6060
/// # #![no_main]
61-
/// # use msp430_macros::entry;
61+
/// # use msp430_rt_macros::entry;
6262
/// #[entry]
6363
/// fn main(_cs: CriticalSection) -> ! {
6464
/// static mut FOO: u32 = 0;
@@ -127,9 +127,9 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
127127
.collect::<Vec<_>>();
128128

129129
quote!(
130-
#[export_name = "main"]
130+
#[no_mangle]
131131
#(#attrs)*
132-
pub #unsafety fn #hash() -> ! {
132+
pub #unsafety fn main() -> ! {
133133
#unsafety fn #hash<'a>(#cs_param) -> ! {
134134
#(#vars)*
135135
#(#stmts)*
@@ -140,7 +140,7 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
140140
.into()
141141
} else {
142142
parse::Error::new(
143-
f.span(),
143+
f.sig.span(),
144144
"`#[entry]` function must have signature `[unsafe] fn([<ident> : CriticalSection]) -> !`",
145145
)
146146
.to_compile_error()
@@ -227,9 +227,8 @@ pub fn interrupt(args: TokenStream, input: TokenStream) -> TokenStream {
227227
.into();
228228
}
229229

230-
let fspan = f.span();
230+
let fspan = f.sig.span();
231231
let ident = f.sig.ident;
232-
let ident_s = ident.to_string();
233232

234233
let check = if ident == "DefaultHandler" {
235234
None
@@ -295,9 +294,9 @@ pub fn interrupt(args: TokenStream, input: TokenStream) -> TokenStream {
295294
let output = f.sig.output;
296295
let hash = random_ident();
297296
quote!(
298-
#[export_name = #ident_s]
297+
#[no_mangle]
299298
#(#attrs)*
300-
#unsafety extern "msp430-interrupt" fn #hash() {
299+
#unsafety extern "msp430-interrupt" fn #ident() {
301300
#check
302301

303302
#unsafety fn #hash<'a>(#cs_param) #output {
@@ -330,7 +329,7 @@ pub fn interrupt(args: TokenStream, input: TokenStream) -> TokenStream {
330329
/// # Examples
331330
///
332331
/// ```
333-
/// # use msp430_macros::pre_init;
332+
/// # use msp430_rt_macros::pre_init;
334333
/// #[pre_init]
335334
/// unsafe fn before_main() {
336335
/// // do something here
@@ -361,7 +360,7 @@ pub fn pre_init(args: TokenStream, input: TokenStream) -> TokenStream {
361360

362361
if !valid_signature {
363362
return parse::Error::new(
364-
f.span(),
363+
f.sig.span(),
365364
"`#[pre_init]` function must have signature `unsafe fn()`",
366365
)
367366
.to_compile_error()
@@ -420,7 +419,7 @@ fn extract_critical_section_arg(
420419
Type::Path(TypePath { qself: None, path }),
421420
_,
422421
[],
423-
) if path.segments.len() == 1 && attrs.len() == 0 => {
422+
) if path.segments.len() == 1 && attrs.is_empty() => {
424423
let seg = path.segments.first().unwrap();
425424
match seg {
426425
PathSegment {

macros/tests/compiletest.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#[test]
2+
fn ui() {
3+
let t = trybuild::TestCases::new();
4+
t.compile_fail("tests/ui/*.rs");
5+
}

macros/tests/ui/entry_args.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![no_main]
2+
3+
use msp430_rt_macros::entry;
4+
5+
#[entry(arg)]
6+
fn main() -> ! {
7+
loop {}
8+
}

macros/tests/ui/entry_args.stderr

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
error: This attribute accepts no arguments
2+
--> tests/ui/entry_args.rs:5:1
3+
|
4+
5 | #[entry(arg)]
5+
| ^^^^^^^^^^^^^
6+
|
7+
= note: this error originates in the attribute macro `entry` (in Nightly builds, run with -Z macro-backtrace for more info)

macros/tests/ui/entry_bad_param.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![no_main]
2+
3+
use msp430_rt_macros::entry;
4+
5+
#[entry]
6+
fn main(i: u32) -> ! {
7+
loop {}
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: `#[entry]` function must have signature `[unsafe] fn([<ident> : CriticalSection]) -> !`
2+
--> tests/ui/entry_bad_param.rs:6:1
3+
|
4+
6 | fn main(i: u32) -> ! {
5+
| ^^^^^^^^^^^^^^^^^^^^

macros/tests/ui/entry_bad_ret.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![no_main]
2+
3+
use msp430_rt_macros::entry;
4+
5+
#[entry]
6+
fn main() {}

macros/tests/ui/entry_bad_ret.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: `#[entry]` function must have signature `[unsafe] fn([<ident> : CriticalSection]) -> !`
2+
--> tests/ui/entry_bad_ret.rs:6:1
3+
|
4+
6 | fn main() {}
5+
| ^^^^^^^^^

0 commit comments

Comments
 (0)