Skip to content

Commit

Permalink
test rune_derive
Browse files Browse the repository at this point in the history
  • Loading branch information
ModProg committed Aug 7, 2023
1 parent c42024d commit a4bb337
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/rune/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ mod quote;
mod range;
mod reference_error;
mod result;
mod rune_derive;
mod stmt_reordering;
mod tuple;
mod type_name_native;
Expand Down
65 changes: 65 additions & 0 deletions crates/rune/src/tests/rune_derive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use core::fmt::{self, Display};
use core::ops::Add;

use super::prelude::*;
use crate::no_std::prelude::*;

#[derive(Any, Debug, PartialEq)]
#[rune_derive(ADD, STRING_DEBUG, STRING_DISPLAY)]
#[rune_functions(Self::new)]
struct Struct(usize);

impl Struct {
#[rune::function(path = Self::new)]
fn new(it: usize) -> Self {
Self(it)
}
}

impl Add for Struct {
type Output = Self;

fn add(mut self, other: Self) -> Self {
self.0 += other.0;
self
}
}

impl Display for Struct {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}

#[test]
fn rune_derive() -> Result<()> {
let mut m = Module::new();
m.ty::<Struct>()?;
assert_eq!(
rune_n! {
&m,
(),
Struct => pub fn main() {Struct::new(1) + Struct::new(2)}
},
Struct(3)
);

assert_eq!(
rune_n! {
&m,
(),
String => pub fn main() {format!("{}", Struct::new(1))}
},
"1"
);

assert_eq!(
rune_n! {
&m,
(),
String => pub fn main() {format!("{:?}", Struct::new(1))}
},
"Struct(1)"
);
Ok(())
}
13 changes: 13 additions & 0 deletions crates/rune/tests/ui/rune_derive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use rune::Any;

// Generates a warning that the path should no longer be using a string literal.
#[derive(Any)]
#[rune_derive(ADD, STRING_DISPLAY, STRING_DEBUG)]
struct UnimplementedTraits;

#[derive(Any)]
#[rune_derive(NON_EXISTENT)]
struct NonExistingProtocol;

fn main() {
}
57 changes: 57 additions & 0 deletions crates/rune/tests/ui/rune_derive.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
error: Rune protocol `NON_EXISTENT` cannot be derived
--> tests/ui/rune_derive.rs:9:15
|
9 | #[rune_derive(NON_EXISTENT)]
| ^^^^^^^^^^^^

error[E0369]: cannot add `UnimplementedTraits` to `UnimplementedTraits`
--> tests/ui/rune_derive.rs:5:15
|
5 | #[rune_derive(ADD, STRING_DISPLAY, STRING_DEBUG)]
| ^^^
|
note: an implementation of `std::ops::Add` might be missing for `UnimplementedTraits`
--> tests/ui/rune_derive.rs:6:1
|
6 | struct UnimplementedTraits;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ must implement `std::ops::Add`
note: the trait `std::ops::Add` must be implemented
--> $RUST/core/src/ops/arith.rs
|
| pub trait Add<Rhs = Self> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0277]: `UnimplementedTraits` doesn't implement `std::fmt::Display`
--> tests/ui/rune_derive.rs:5:20
|
5 | #[rune_derive(ADD, STRING_DISPLAY, STRING_DEBUG)]
| ^^^^^^^^^^^^^^ `UnimplementedTraits` cannot be formatted with the default formatter
|
::: $RUST/core/src/macros/mod.rs
|
| $dst.write_fmt($crate::format_args!($($arg)*))
| ------------------------------ in this macro invocation
|
= help: the trait `std::fmt::Display` is not implemented for `UnimplementedTraits`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
= note: this error originates in the macro `$crate::format_args` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: `UnimplementedTraits` doesn't implement `std::fmt::Debug`
--> tests/ui/rune_derive.rs:5:36
|
5 | #[rune_derive(ADD, STRING_DISPLAY, STRING_DEBUG)]
| ^^^^^^^^^^^^ `UnimplementedTraits` cannot be formatted using `{:?}`
|
::: $RUST/core/src/macros/mod.rs
|
| $dst.write_fmt($crate::format_args!($($arg)*))
| ------------------------------ in this macro invocation
|
= help: the trait `std::fmt::Debug` is not implemented for `UnimplementedTraits`
= note: add `#[derive(Debug)]` to `UnimplementedTraits` or manually `impl std::fmt::Debug for UnimplementedTraits`
= note: this error originates in the macro `$crate::format_args` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider annotating `UnimplementedTraits` with `#[derive(Debug)]`
|
6 + #[derive(Debug)]
7 | struct UnimplementedTraits;
|

0 comments on commit a4bb337

Please sign in to comment.