Skip to content

Commit 6cc975d

Browse files
committed
test rune_derive
1 parent 269acc9 commit 6cc975d

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

crates/rune/src/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ mod quote;
398398
mod range;
399399
mod reference_error;
400400
mod result;
401+
mod rune_derive;
401402
mod stmt_reordering;
402403
mod tuple;
403404
mod type_name_native;

crates/rune/src/tests/rune_derive.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use core::fmt::{self, Display};
2+
use core::ops::Add;
3+
4+
use super::prelude::*;
5+
use crate::no_std::prelude::*;
6+
7+
#[derive(Any, Debug, PartialEq)]
8+
#[rune_derive(ADD, STRING_DEBUG, STRING_DISPLAY)]
9+
#[rune_functions(Self::new)]
10+
struct Struct(usize);
11+
12+
impl Struct {
13+
#[rune::function(path = Self::new)]
14+
fn new(it: usize) -> Self {
15+
Self(it)
16+
}
17+
}
18+
19+
impl Add for Struct {
20+
type Output = Self;
21+
22+
fn add(mut self, other: Self) -> Self {
23+
self.0 += other.0;
24+
self
25+
}
26+
}
27+
28+
impl Display for Struct {
29+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> fmt::Result {
30+
self.0.fmt(f)
31+
}
32+
}
33+
34+
#[test]
35+
fn rune_derive() -> Result<()> {
36+
let mut m = Module::new();
37+
m.ty::<Struct>()?;
38+
assert_eq!(
39+
rune_n! {
40+
&m,
41+
(),
42+
Struct => pub fn main() {Struct::new(1) + Struct::new(2)}
43+
},
44+
Struct(3)
45+
);
46+
47+
assert_eq!(
48+
rune_n! {
49+
&m,
50+
(),
51+
String => pub fn main() {format!("{}", Struct::new(1))}
52+
},
53+
"1"
54+
);
55+
56+
assert_eq!(
57+
rune_n! {
58+
&m,
59+
(),
60+
String => pub fn main() {format!("{:?}", Struct::new(1))}
61+
},
62+
"Struct(1)"
63+
);
64+
Ok(())
65+
}

crates/rune/tests/ui/rune_derive.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use rune::Any;
2+
3+
// Generates a warning that the path should no longer be using a string literal.
4+
#[derive(Any)]
5+
#[rune_derive(ADD, STRING_DISPLAY, STRING_DEBUG)]
6+
struct UnimplementedTraits;
7+
8+
#[derive(Any)]
9+
#[rune_derive(NON_EXISTENT)]
10+
struct NonExistingProtocol;
11+
12+
fn main() {
13+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
error: Rune protocol `NON_EXISTENT` cannot be derived
2+
--> tests/ui/rune_derive.rs:9:15
3+
|
4+
9 | #[rune_derive(NON_EXISTENT)]
5+
| ^^^^^^^^^^^^
6+
7+
error[E0369]: cannot add `UnimplementedTraits` to `UnimplementedTraits`
8+
--> tests/ui/rune_derive.rs:5:15
9+
|
10+
5 | #[rune_derive(ADD, STRING_DISPLAY, STRING_DEBUG)]
11+
| ^^^
12+
|
13+
note: an implementation of `std::ops::Add` might be missing for `UnimplementedTraits`
14+
--> tests/ui/rune_derive.rs:6:1
15+
|
16+
6 | struct UnimplementedTraits;
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ must implement `std::ops::Add`
18+
note: the trait `std::ops::Add` must be implemented
19+
--> $RUST/core/src/ops/arith.rs
20+
|
21+
| pub trait Add<Rhs = Self> {
22+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
23+
24+
error[E0277]: `UnimplementedTraits` doesn't implement `std::fmt::Display`
25+
--> tests/ui/rune_derive.rs:5:20
26+
|
27+
5 | #[rune_derive(ADD, STRING_DISPLAY, STRING_DEBUG)]
28+
| ^^^^^^^^^^^^^^ `UnimplementedTraits` cannot be formatted with the default formatter
29+
|
30+
::: $RUST/core/src/macros/mod.rs
31+
|
32+
| $dst.write_fmt($crate::format_args!($($arg)*))
33+
| ------------------------------ in this macro invocation
34+
|
35+
= help: the trait `std::fmt::Display` is not implemented for `UnimplementedTraits`
36+
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
37+
= note: this error originates in the macro `$crate::format_args` (in Nightly builds, run with -Z macro-backtrace for more info)
38+
39+
error[E0277]: `UnimplementedTraits` doesn't implement `std::fmt::Debug`
40+
--> tests/ui/rune_derive.rs:5:36
41+
|
42+
5 | #[rune_derive(ADD, STRING_DISPLAY, STRING_DEBUG)]
43+
| ^^^^^^^^^^^^ `UnimplementedTraits` cannot be formatted using `{:?}`
44+
|
45+
::: $RUST/core/src/macros/mod.rs
46+
|
47+
| $dst.write_fmt($crate::format_args!($($arg)*))
48+
| ------------------------------ in this macro invocation
49+
|
50+
= help: the trait `std::fmt::Debug` is not implemented for `UnimplementedTraits`
51+
= note: add `#[derive(Debug)]` to `UnimplementedTraits` or manually `impl std::fmt::Debug for UnimplementedTraits`
52+
= note: this error originates in the macro `$crate::format_args` (in Nightly builds, run with -Z macro-backtrace for more info)
53+
help: consider annotating `UnimplementedTraits` with `#[derive(Debug)]`
54+
|
55+
6 + #[derive(Debug)]
56+
7 | struct UnimplementedTraits;
57+
|

0 commit comments

Comments
 (0)