Implement first trybuild tests
and use a dedicated InvalidEvent constructor fn in EventContent derives
This commit is contained in:
parent
0c0dafa2c5
commit
7ec162678e
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,2 +1,5 @@
|
|||||||
Cargo.lock
|
Cargo.lock
|
||||||
target
|
target
|
||||||
|
|
||||||
|
# trybuild generates a `wip` folder when creating or updating a test
|
||||||
|
wip
|
||||||
|
@ -26,6 +26,7 @@ strum = { version = "0.18.0", features = ["derive"] }
|
|||||||
maplit = "1.0.2"
|
maplit = "1.0.2"
|
||||||
matches = "0.1.8"
|
matches = "0.1.8"
|
||||||
ruma-identifiers = { version = "0.16.2", features = ["rand"] }
|
ruma-identifiers = { version = "0.16.2", features = ["rand"] }
|
||||||
|
trybuild = "1.0.27"
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
members = [
|
members = [
|
||||||
|
@ -58,10 +58,9 @@ fn expand_room_event(input: DeriveInput) -> syn::Result<TokenStream> {
|
|||||||
content: Box<::serde_json::value::RawValue>
|
content: Box<::serde_json::value::RawValue>
|
||||||
) -> Result<Self, ::ruma_events::InvalidEvent> {
|
) -> Result<Self, ::ruma_events::InvalidEvent> {
|
||||||
if ev_type != #event_type {
|
if ev_type != #event_type {
|
||||||
return Err(::ruma_events::InvalidEvent {
|
return Err(
|
||||||
kind: ::ruma_events::error::InvalidEventKind::Deserialization,
|
::ruma_events::InvalidEvent::wrong_event_type(#event_type, ev_type)
|
||||||
message: format!("expected `{}` found {}", #event_type, ev_type),
|
);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let ev_json = ::ruma_events::EventJson::from(content);
|
let ev_json = ::ruma_events::EventJson::from(content);
|
||||||
|
17
src/error.rs
17
src/error.rs
@ -11,17 +11,32 @@ use std::{
|
|||||||
/// message and a flag for which type of error was encountered.
|
/// message and a flag for which type of error was encountered.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct InvalidEvent {
|
pub struct InvalidEvent {
|
||||||
|
/// A description of the error that occurred.
|
||||||
pub(crate) message: String,
|
pub(crate) message: String,
|
||||||
|
/// The kind of error that occurred.
|
||||||
pub(crate) kind: InvalidEventKind,
|
pub(crate) kind: InvalidEventKind,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The kind of error that occurred.
|
||||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||||
pub enum InvalidEventKind {
|
pub(crate) enum InvalidEventKind {
|
||||||
|
/// A deserialization error from malformed input.
|
||||||
Deserialization,
|
Deserialization,
|
||||||
|
/// An error occurred validating input according the the matrix spec.
|
||||||
Validation,
|
Validation,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InvalidEvent {
|
impl InvalidEvent {
|
||||||
|
/// Constructor used in the event content macros.
|
||||||
|
///
|
||||||
|
/// This has to be public to allow the macros to be used outside of ruma-events.
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub fn wrong_event_type(expected: &str, found: &str) -> Self {
|
||||||
|
Self {
|
||||||
|
message: format!("expected `{}` found {}", expected, found),
|
||||||
|
kind: InvalidEventKind::Deserialization,
|
||||||
|
}
|
||||||
|
}
|
||||||
/// A message describing why the event is invalid.
|
/// A message describing why the event is invalid.
|
||||||
pub fn message(&self) -> String {
|
pub fn message(&self) -> String {
|
||||||
self.message.clone()
|
self.message.clone()
|
||||||
|
7
tests/event_content.rs
Normal file
7
tests/event_content.rs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#[test]
|
||||||
|
fn ui() {
|
||||||
|
let t = trybuild::TestCases::new();
|
||||||
|
t.pass("tests/ui/01-sanity-check.rs");
|
||||||
|
t.compile_fail("tests/ui/02-no-event-type.rs");
|
||||||
|
t.compile_fail("tests/ui/03-invalid-event-type.rs");
|
||||||
|
}
|
10
tests/ui/01-sanity-check.rs
Normal file
10
tests/ui/01-sanity-check.rs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
use ruma_events_macros::{FromRaw, StateEventContent};
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, FromRaw, StateEventContent)]
|
||||||
|
#[ruma_event(type = "m.macro.test")]
|
||||||
|
pub struct MacroTest {
|
||||||
|
pub url: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
9
tests/ui/02-no-event-type.rs
Normal file
9
tests/ui/02-no-event-type.rs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
use ruma_events_macros::{FromRaw, StateEventContent};
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, FromRaw, StateEventContent)]
|
||||||
|
pub struct MacroTest {
|
||||||
|
pub url: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
7
tests/ui/02-no-event-type.stderr
Normal file
7
tests/ui/02-no-event-type.stderr
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
error: no event type attribute found, add `#[ruma_events(type = "any.room.event")]` below the event content derive
|
||||||
|
--> $DIR/02-no-event-type.rs:4:44
|
||||||
|
|
|
||||||
|
4 | #[derive(Clone, Debug, Serialize, FromRaw, StateEventContent)]
|
||||||
|
| ^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
|
16
tests/ui/03-invalid-event-type.rs
Normal file
16
tests/ui/03-invalid-event-type.rs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
use ruma_events_macros::{FromRaw, StateEventContent};
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, FromRaw, StateEventContent)]
|
||||||
|
#[not_ruma_event(type = "m.macro.test")]
|
||||||
|
pub struct MacroTest {
|
||||||
|
pub test: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, StateEventContent)]
|
||||||
|
#[ruma_event(event = "m.macro.test")]
|
||||||
|
pub struct MoreMacroTest {
|
||||||
|
pub test: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
19
tests/ui/03-invalid-event-type.stderr
Normal file
19
tests/ui/03-invalid-event-type.stderr
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
error: expected `type`
|
||||||
|
--> $DIR/03-invalid-event-type.rs:11:14
|
||||||
|
|
|
||||||
|
11 | #[ruma_event(event = "m.macro.test")]
|
||||||
|
| ^^^^^
|
||||||
|
|
||||||
|
error: no event type attribute found, add `#[ruma_events(type = "any.room.event")]` below the event content derive
|
||||||
|
--> $DIR/03-invalid-event-type.rs:4:44
|
||||||
|
|
|
||||||
|
4 | #[derive(Clone, Debug, Serialize, FromRaw, StateEventContent)]
|
||||||
|
| ^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
|
error: cannot find attribute `not_ruma_event` in this scope
|
||||||
|
--> $DIR/03-invalid-event-type.rs:5:3
|
||||||
|
|
|
||||||
|
5 | #[not_ruma_event(type = "m.macro.test")]
|
||||||
|
| ^^^^^^^^^^^^^^ help: a derive helper attribute with a similar name exists: `ruma_event`
|
Loading…
x
Reference in New Issue
Block a user