From 6cc975dbb85de7db25d77306410374bf3acfe678 Mon Sep 17 00:00:00 2001 From: Roland Fredenhagen Date: Mon, 7 Aug 2023 15:31:46 +0700 Subject: [PATCH] test rune_derive --- crates/rune/src/tests.rs | 1 + crates/rune/src/tests/rune_derive.rs | 65 +++++++++++++++++++++++++ crates/rune/tests/ui/rune_derive.rs | 13 +++++ crates/rune/tests/ui/rune_derive.stderr | 57 ++++++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 crates/rune/src/tests/rune_derive.rs create mode 100644 crates/rune/tests/ui/rune_derive.rs create mode 100644 crates/rune/tests/ui/rune_derive.stderr diff --git a/crates/rune/src/tests.rs b/crates/rune/src/tests.rs index a2e8d4eac..b46362b74 100644 --- a/crates/rune/src/tests.rs +++ b/crates/rune/src/tests.rs @@ -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; diff --git a/crates/rune/src/tests/rune_derive.rs b/crates/rune/src/tests/rune_derive.rs new file mode 100644 index 000000000..89173ab07 --- /dev/null +++ b/crates/rune/src/tests/rune_derive.rs @@ -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::()?; + 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(()) +} diff --git a/crates/rune/tests/ui/rune_derive.rs b/crates/rune/tests/ui/rune_derive.rs new file mode 100644 index 000000000..a9074b653 --- /dev/null +++ b/crates/rune/tests/ui/rune_derive.rs @@ -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() { +} diff --git a/crates/rune/tests/ui/rune_derive.stderr b/crates/rune/tests/ui/rune_derive.stderr new file mode 100644 index 000000000..36f7e1249 --- /dev/null +++ b/crates/rune/tests/ui/rune_derive.stderr @@ -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 { + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +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; + |