test: add unit test to catch missing distribution to subsystems faster (#2495)

* test: add unit test to catch missing distribution to subsystems faster

* add a simple count

* introduce proc macro to generate dispatch type

* refactor

* refactor

* chore: add license

* fixup unit test

* fixup merge

* better errors

* better fmt

* fix error spans

* better docs

* better error messages

* ui test foo

* Update node/subsystem/dispatch-gen/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update node/network/bridge/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update node/subsystem/Cargo.toml

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update node/subsystem/dispatch-gen/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update node/subsystem/dispatch-gen/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update node/network/bridge/src/lib.rs

Co-authored-by: Andronik Ordian <write@reusable.software>

* fix compilation

* use find_map

* drop the silly 2, use _inner instead

* Update node/network/bridge/src/lib.rs

Co-authored-by: Andronik Ordian <write@reusable.software>

* Update node/subsystem/dispatch-gen/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* nail deps down

* more into()

* flatten

* missing use statement

* fix messages order

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: Andronik Ordian <write@reusable.software>
This commit is contained in:
Bernhard Schuster
2021-02-26 09:10:41 +01:00
committed by GitHub
parent 69734bb8ed
commit 31327eb0c7
14 changed files with 500 additions and 69 deletions
@@ -0,0 +1,37 @@
#![allow(dead_code)]
use polkadot_procmacro_subsystem_dispatch_gen::subsystem_dispatch_gen;
/// The event type in question.
#[derive(Clone, Copy)]
enum Event {
Smth,
Else,
}
impl Event {
fn focus(&self) -> std::result::Result<Inner, ()> {
unimplemented!("foo")
}
}
/// This should have a `From<Event>` impl but does not.
#[derive(Clone)]
enum Inner {
Foo,
Bar(Event),
}
#[subsystem_dispatch_gen(Event)]
#[derive(Clone)]
enum AllMessages {
/// Foo
Vvvvvv(Inner),
/// Missing a `#[skip]` annotation
Uuuuu,
}
fn main() {
let _x = AllMessages::dispatch_iter(Event::Else);
}
@@ -0,0 +1,14 @@
error: Must be annotated with #[skip].
--> $DIR/err-01-missing-skip.rs:32:5
|
32 | Uuuuu,
| ^^^^^
error[E0599]: no variant or associated item named `dispatch_iter` found for enum `AllMessages` in the current scope
--> $DIR/err-01-missing-skip.rs:36:27
|
27 | enum AllMessages {
| ---------------- variant or associated item `dispatch_iter` not found here
...
36 | let _x = AllMessages::dispatch_iter(Event::Else);
| ^^^^^^^^^^^^^ variant or associated item not found in `AllMessages`
@@ -0,0 +1,41 @@
#![allow(dead_code)]
use polkadot_procmacro_subsystem_dispatch_gen::subsystem_dispatch_gen;
/// The event type in question.
#[derive(Clone, Copy, Debug)]
enum Event {
Smth,
Else,
}
impl Event {
fn focus(&self) -> std::result::Result<Intermediate, ()> {
Ok(Intermediate(self.clone()))
}
}
#[derive(Debug, Clone)]
struct Intermediate(Event);
/// This should have a `From<Event>` impl but does not.
#[derive(Debug, Clone)]
enum Inner {
Foo,
Bar(Intermediate),
}
#[subsystem_dispatch_gen(Event)]
#[derive(Clone)]
enum AllMessages {
/// Foo
Vvvvvv(Inner),
#[skip]
Uuuuu,
}
fn main() {
let _x = AllMessages::dispatch_iter(Event::Else);
}
@@ -0,0 +1,10 @@
error[E0308]: mismatched types
--> $DIR/err-02-missing-from.rs:29:1
|
29 | #[subsystem_dispatch_gen(Event)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| expected enum `Inner`, found struct `Intermediate`
| help: try using a variant of the expected enum: `Inner::Bar(#[subsystem_dispatch_gen(Event)])`
|
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -0,0 +1,48 @@
#![allow(dead_code)]
use polkadot_procmacro_subsystem_dispatch_gen::subsystem_dispatch_gen;
/// The event type in question.
#[derive(Clone, Copy, Debug)]
enum Event {
Smth,
Else,
}
impl Event {
fn focus(&self) -> std::result::Result<Intermediate, ()> {
Ok(Intermediate(self.clone()))
}
}
#[derive(Debug, Clone)]
struct Intermediate(Event);
/// This should have a `From<Event>` impl but does not.
#[derive(Clone, Debug)]
enum Inner {
Foo,
Bar(Intermediate),
}
impl From<Intermediate> for Inner {
fn from(src: Intermediate) -> Self {
Inner::Bar(src)
}
}
#[subsystem_dispatch_gen(Event)]
#[derive(Clone)]
enum AllMessages {
/// Foo
Vvvvvv(Inner),
#[skip]
Uuuuu,
}
fn main() {
let _x = AllMessages::dispatch_iter(Event::Else);
}