Skip to content

Commit

Permalink
Add test to assert macro hygiene for Encode and Decode derives (#293)
Browse files Browse the repository at this point in the history
* improve macro hygiene in Decode derive

* add UI tests with #![no_implicit_prelude] to assert macro hygiene

Add tests for Decode and Encode derive proc macros.

* fix compilation guard for max_encoded_len UI

* Update tests/scale_codec_ui.rs

Co-authored-by: Bastian Köcher <[email protected]>

* disable flaky test

Co-authored-by: Bastian Köcher <[email protected]>
  • Loading branch information
Robbepop and bkchr authored Sep 28, 2021
1 parent 4fa5ddf commit 7a845c2
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 2 deletions.
4 changes: 3 additions & 1 deletion derive/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ pub fn quote(
.map_err(|e| e.chain(#read_byte_err_msg))?
{
#( #recurse )*
_ => ::core::result::Result::Err(#invalid_variant_err_msg.into()),
_ => ::core::result::Result::Err(
<_ as ::core::convert::Into<_>>::into(#invalid_variant_err_msg)
),
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/bit_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ mod tests {
}

#[test]
// Flaky test due to:
// * https://github.com/bitvecto-rs/bitvec/issues/135
// * https://github.com/rust-lang/miri/issues/1866
#[cfg(not(miri))]
fn bitvec_u32() {
for v in &test_data!(u32) {
let encoded = v.encode();
Expand Down
2 changes: 1 addition & 1 deletion tests/max_encoded_len.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// limitations under the License.

//! Tests for MaxEncodedLen derive macro
#![cfg(feature = "derive")]
#![cfg(all(feature = "derive", feature = "max-encoded-len"))]

use parity_scale_codec::{MaxEncodedLen, Compact, Encode};

Expand Down
22 changes: 22 additions & 0 deletions tests/scale_codec_ui.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#[test]
#[cfg(feature = "derive")]
fn scale_codec_ui_tests() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/scale_codec_ui/*.rs");
t.pass("tests/scale_codec_ui/pass/*.rs");
}
23 changes: 23 additions & 0 deletions tests/scale_codec_ui/pass/decode-no-implicit-prelude.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#![no_implicit_prelude]

#[derive(::parity_scale_codec::Decode)]
pub struct Struct {
field_1: i8,
field_2: i16,
field_3: i32,
field_4: i64,
}

#[derive(::parity_scale_codec::Decode)]
pub enum Enum {
Variant1,
Variant2(i8, i16, i32, i64),
Variant3 {
field_1: i8,
field_2: i16,
field_3: i32,
field_4: i64,
}
}

fn main() {}
23 changes: 23 additions & 0 deletions tests/scale_codec_ui/pass/encode-no-implicit-prelude.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#![no_implicit_prelude]

#[derive(::parity_scale_codec::Encode)]
pub struct Struct {
field_1: i8,
field_2: i16,
field_3: i32,
field_4: i64,
}

#[derive(::parity_scale_codec::Encode)]
pub enum Enum {
Variant1,
Variant2(i8, i16, i32, i64),
Variant3 {
field_1: i8,
field_2: i16,
field_3: i32,
field_4: i64,
}
}

fn main() {}

0 comments on commit 7a845c2

Please sign in to comment.