mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 16:57:58 +00:00
25eaa95fbf
This PR is related to [this](https://github.com/paritytech/polkadot-sdk/issues/154) issue. The idea is to add `OrdNoBound` and `PartialOrdNoBound` macros to the substrate `*NoBound` macros. closes #2198 ✄ ----------------------------------------------------------------------------- Thank you for your Pull Request! 🙏 Please make sure it follows the contribution guidelines outlined in [this document](https://github.com/paritytech/polkadot-sdk/blob/master/docs/CONTRIBUTING.md) and fill out the sections below. Once you're ready to submit your PR for review, please delete this section and leave only the text under the "Description" heading. # Description *Please include a summary of the changes and the related issue. Please also include relevant motivation and context, including:* - What does this PR do? - Why are these changes needed? - How were these changes implemented and what do they affect? *Use [Github semantic linking](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword) to address any open issues this PR relates to or closes.* Fixes # (issue number, *if applicable*) Closes # (issue number, *if applicable*) # Checklist - [ ] My PR includes a detailed description as outlined in the "Description" section above - [ ] My PR follows the [labeling requirements](CONTRIBUTING.md#Process) of this project (at minimum one label for `T` required) - [ ] I have made corresponding changes to the documentation (if applicable) - [ ] I have added tests that prove my fix is effective or that my feature works (if applicable) You can remove the "Checklist" section once all have been checked. Thank you for your contribution! ✄ ----------------------------------------------------------------------------- --------- Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> Co-authored-by: command-bot <>
344 lines
9.2 KiB
Rust
344 lines
9.2 KiB
Rust
// This file is part of Substrate.
|
|
|
|
// Copyright (C) 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.
|
|
|
|
//! Tests for DebugNoBound, CloneNoBound, EqNoBound, PartialEqNoBound, DefaultNoBound,
|
|
//! RuntimeDebugNoBound, PartialOrdNoBound and OrdNoBound
|
|
|
|
use frame_support::{
|
|
CloneNoBound, DebugNoBound, DefaultNoBound, EqNoBound, OrdNoBound, PartialEqNoBound,
|
|
PartialOrdNoBound, RuntimeDebugNoBound,
|
|
};
|
|
|
|
#[derive(RuntimeDebugNoBound)]
|
|
struct Unnamed(u64);
|
|
|
|
#[test]
|
|
fn runtime_debug_no_bound_display_correctly() {
|
|
// This test is not executed without std
|
|
assert_eq!(format!("{:?}", Unnamed(1)), "Unnamed(1)");
|
|
}
|
|
|
|
trait Config {
|
|
type C: std::fmt::Debug + Clone + Eq + PartialEq + Default + PartialOrd + Ord;
|
|
}
|
|
|
|
struct Runtime;
|
|
struct ImplNone;
|
|
|
|
impl Config for Runtime {
|
|
type C = u32;
|
|
}
|
|
|
|
#[derive(
|
|
DebugNoBound,
|
|
CloneNoBound,
|
|
EqNoBound,
|
|
PartialEqNoBound,
|
|
DefaultNoBound,
|
|
PartialOrdNoBound,
|
|
OrdNoBound,
|
|
)]
|
|
struct StructNamed<T: Config, U, V> {
|
|
a: u32,
|
|
b: u64,
|
|
c: T::C,
|
|
phantom: core::marker::PhantomData<(U, V)>,
|
|
}
|
|
|
|
#[rustversion::attr(not(stable), ignore)]
|
|
#[cfg(not(feature = "disable-ui-tests"))]
|
|
#[test]
|
|
fn test_struct_named_debug_print() {
|
|
let a_1 = StructNamed::<Runtime, ImplNone, ImplNone> {
|
|
a: 1,
|
|
b: 2,
|
|
c: 3,
|
|
phantom: Default::default(),
|
|
};
|
|
|
|
assert_eq!(
|
|
format!("{:?}", a_1),
|
|
String::from("StructNamed { a: 1, b: 2, c: 3, phantom: PhantomData<(derive_no_bound::ImplNone, derive_no_bound::ImplNone)> }")
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn test_struct_named() {
|
|
let a_1 = StructNamed::<Runtime, ImplNone, ImplNone> {
|
|
a: 1,
|
|
b: 2,
|
|
c: 3,
|
|
phantom: Default::default(),
|
|
};
|
|
|
|
let a_default: StructNamed<Runtime, ImplNone, ImplNone> = Default::default();
|
|
assert_eq!(a_default.a, 0);
|
|
assert_eq!(a_default.b, 0);
|
|
assert_eq!(a_default.c, 0);
|
|
assert_eq!(a_default.phantom, Default::default());
|
|
|
|
let a_2 = a_1.clone();
|
|
assert_eq!(a_2.a, 1);
|
|
assert_eq!(a_2.b, 2);
|
|
assert_eq!(a_2.c, 3);
|
|
assert_eq!(a_2, a_1);
|
|
|
|
let b = StructNamed::<Runtime, ImplNone, ImplNone> {
|
|
a: 1,
|
|
b: 2,
|
|
c: 4,
|
|
phantom: Default::default(),
|
|
};
|
|
|
|
assert!(b != a_1);
|
|
assert!(b > a_1);
|
|
}
|
|
|
|
#[derive(
|
|
DebugNoBound,
|
|
CloneNoBound,
|
|
EqNoBound,
|
|
PartialEqNoBound,
|
|
DefaultNoBound,
|
|
PartialOrdNoBound,
|
|
OrdNoBound,
|
|
)]
|
|
struct StructUnnamed<T: Config, U, V>(u32, u64, T::C, core::marker::PhantomData<(U, V)>);
|
|
|
|
#[rustversion::attr(not(stable), ignore)]
|
|
#[cfg(not(feature = "disable-ui-tests"))]
|
|
#[test]
|
|
fn test_struct_unnamed_debug_print() {
|
|
let a_1 = StructUnnamed::<Runtime, ImplNone, ImplNone>(1, 2, 3, Default::default());
|
|
assert_eq!(format!("{:?}", a_1), String::from("StructUnnamed(1, 2, 3, PhantomData<(derive_no_bound::ImplNone, derive_no_bound::ImplNone)>)"));
|
|
}
|
|
|
|
#[test]
|
|
fn test_struct_unnamed() {
|
|
let a_1 = StructUnnamed::<Runtime, ImplNone, ImplNone>(1, 2, 3, Default::default());
|
|
|
|
let a_default: StructUnnamed<Runtime, ImplNone, ImplNone> = Default::default();
|
|
assert_eq!(a_default.0, 0);
|
|
assert_eq!(a_default.1, 0);
|
|
assert_eq!(a_default.2, 0);
|
|
assert_eq!(a_default.3, Default::default());
|
|
|
|
let a_2 = a_1.clone();
|
|
assert_eq!(a_2.0, 1);
|
|
assert_eq!(a_2.1, 2);
|
|
assert_eq!(a_2.2, 3);
|
|
assert_eq!(a_2, a_1);
|
|
|
|
let b = StructUnnamed::<Runtime, ImplNone, ImplNone>(1, 2, 4, Default::default());
|
|
|
|
assert!(b != a_1);
|
|
assert!(b > a_1);
|
|
}
|
|
|
|
#[derive(
|
|
DebugNoBound,
|
|
CloneNoBound,
|
|
EqNoBound,
|
|
PartialEqNoBound,
|
|
DefaultNoBound,
|
|
PartialOrdNoBound,
|
|
OrdNoBound,
|
|
)]
|
|
struct StructNoGenerics {
|
|
field1: u32,
|
|
field2: u64,
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
#[derive(DebugNoBound, CloneNoBound, EqNoBound, PartialEqNoBound, DefaultNoBound)]
|
|
enum EnumNoGenerics {
|
|
#[default]
|
|
VariantUnnamed(u32, u64),
|
|
VariantNamed {
|
|
a: u32,
|
|
b: u64,
|
|
},
|
|
VariantUnit,
|
|
}
|
|
|
|
#[derive(DebugNoBound, CloneNoBound, EqNoBound, PartialEqNoBound, DefaultNoBound)]
|
|
enum Enum<T: Config, U, V> {
|
|
#[default]
|
|
VariantUnnamed(u32, u64, T::C, core::marker::PhantomData<(U, V)>),
|
|
VariantNamed {
|
|
a: u32,
|
|
b: u64,
|
|
c: T::C,
|
|
phantom: core::marker::PhantomData<(U, V)>,
|
|
},
|
|
VariantUnit,
|
|
VariantUnit2,
|
|
}
|
|
|
|
// enum that will have a named default.
|
|
#[allow(dead_code)]
|
|
#[derive(DebugNoBound, CloneNoBound, EqNoBound, PartialEqNoBound, DefaultNoBound)]
|
|
enum Enum2<T: Config> {
|
|
#[default]
|
|
VariantNamed {
|
|
a: u32,
|
|
b: u64,
|
|
c: T::C,
|
|
},
|
|
VariantUnnamed(u32, u64, T::C),
|
|
VariantUnit,
|
|
VariantUnit2,
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
// enum that will have a unit default.
|
|
#[derive(DebugNoBound, CloneNoBound, EqNoBound, PartialEqNoBound, DefaultNoBound)]
|
|
enum Enum3<T: Config> {
|
|
#[default]
|
|
VariantUnit,
|
|
VariantNamed {
|
|
a: u32,
|
|
b: u64,
|
|
c: T::C,
|
|
},
|
|
VariantUnnamed(u32, u64, T::C),
|
|
VariantUnit2,
|
|
}
|
|
|
|
#[rustversion::attr(not(stable), ignore)]
|
|
#[cfg(not(feature = "disable-ui-tests"))]
|
|
#[test]
|
|
fn test_enum_debug_print() {
|
|
type TestEnum = Enum<Runtime, ImplNone, ImplNone>;
|
|
let variant_0 = TestEnum::VariantUnnamed(1, 2, 3, Default::default());
|
|
let variant_1 = TestEnum::VariantNamed { a: 1, b: 2, c: 3, phantom: Default::default() };
|
|
let variant_2 = TestEnum::VariantUnit;
|
|
let variant_3 = TestEnum::VariantUnit2;
|
|
|
|
assert_eq!(
|
|
format!("{:?}", variant_0),
|
|
String::from("Enum::VariantUnnamed(1, 2, 3, PhantomData<(derive_no_bound::ImplNone, derive_no_bound::ImplNone)>)"),
|
|
);
|
|
assert_eq!(
|
|
format!("{:?}", variant_1),
|
|
String::from("Enum::VariantNamed { a: 1, b: 2, c: 3, phantom: PhantomData<(derive_no_bound::ImplNone, derive_no_bound::ImplNone)> }"),
|
|
);
|
|
assert_eq!(format!("{:?}", variant_2), String::from("Enum::VariantUnit"));
|
|
assert_eq!(format!("{:?}", variant_3), String::from("Enum::VariantUnit2"));
|
|
}
|
|
|
|
#[test]
|
|
fn test_enum() {
|
|
type TestEnum = Enum<Runtime, ImplNone, ImplNone>;
|
|
let variant_0 = TestEnum::VariantUnnamed(1, 2, 3, Default::default());
|
|
let variant_0_bis = TestEnum::VariantUnnamed(1, 2, 4, Default::default());
|
|
let variant_1 = TestEnum::VariantNamed { a: 1, b: 2, c: 3, phantom: Default::default() };
|
|
let variant_1_bis = TestEnum::VariantNamed { a: 1, b: 2, c: 4, phantom: Default::default() };
|
|
let variant_2 = TestEnum::VariantUnit;
|
|
let variant_3 = TestEnum::VariantUnit2;
|
|
|
|
let default: TestEnum = Default::default();
|
|
assert_eq!(
|
|
default,
|
|
// first variant is default.
|
|
TestEnum::VariantUnnamed(0, 0, 0, Default::default())
|
|
);
|
|
|
|
assert_eq!(Enum2::<Runtime>::default(), Enum2::<Runtime>::VariantNamed { a: 0, b: 0, c: 0 });
|
|
assert_eq!(Enum3::<Runtime>::default(), Enum3::<Runtime>::VariantUnit);
|
|
|
|
assert!(variant_0 != variant_0_bis);
|
|
assert!(variant_1 != variant_1_bis);
|
|
assert!(variant_0 != variant_1);
|
|
assert!(variant_0 != variant_2);
|
|
assert!(variant_0 != variant_3);
|
|
assert!(variant_1 != variant_0);
|
|
assert!(variant_1 != variant_2);
|
|
assert!(variant_1 != variant_3);
|
|
assert!(variant_2 != variant_0);
|
|
assert!(variant_2 != variant_1);
|
|
assert!(variant_2 != variant_3);
|
|
assert!(variant_3 != variant_0);
|
|
assert!(variant_3 != variant_1);
|
|
assert!(variant_3 != variant_2);
|
|
|
|
assert!(variant_0.clone() == variant_0);
|
|
assert!(variant_1.clone() == variant_1);
|
|
assert!(variant_2.clone() == variant_2);
|
|
assert!(variant_3.clone() == variant_3);
|
|
}
|
|
|
|
// Return all the combinations of (0, 0, 0), (0, 0, 1), (0, 1, 0), ...
|
|
// Used to test all the possible struct orderings
|
|
fn combinations() -> Vec<(u32, u64, u32)> {
|
|
let mut v = vec![];
|
|
|
|
for a in 0..=1 {
|
|
for b in 0..=1 {
|
|
for c in 0..=1 {
|
|
v.push((a, b, c));
|
|
}
|
|
}
|
|
}
|
|
|
|
v
|
|
}
|
|
|
|
// Ensure that the PartialOrdNoBound follows the same rules as the native PartialOrd
|
|
#[derive(Debug, Clone, Eq, PartialEq, Default, PartialOrd, Ord)]
|
|
struct StructNamedRust {
|
|
a: u32,
|
|
b: u64,
|
|
c: u32,
|
|
phantom: core::marker::PhantomData<(ImplNone, ImplNone)>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, Default, PartialOrd, Ord)]
|
|
struct StructUnnamedRust(u32, u64, u32, core::marker::PhantomData<(ImplNone, ImplNone)>);
|
|
|
|
#[test]
|
|
fn struct_named_same_as_native_rust() {
|
|
for (a, b, c) in combinations() {
|
|
let a_1 =
|
|
StructNamed::<Runtime, ImplNone, ImplNone> { a, b, c, phantom: Default::default() };
|
|
let b_1 = StructNamedRust { a, b, c, phantom: Default::default() };
|
|
for (a, b, c) in combinations() {
|
|
let a_2 =
|
|
StructNamed::<Runtime, ImplNone, ImplNone> { a, b, c, phantom: Default::default() };
|
|
let b_2 = StructNamedRust { a, b, c, phantom: Default::default() };
|
|
assert_eq!(a_1.partial_cmp(&a_2), b_1.partial_cmp(&b_2));
|
|
assert_eq!(a_1.cmp(&a_2), b_1.cmp(&b_2));
|
|
assert_eq!(a_1.eq(&a_2), b_1.eq(&b_2));
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn struct_unnamed_same_as_native_rust() {
|
|
for (a, b, c) in combinations() {
|
|
let a_1 = StructUnnamed::<Runtime, ImplNone, ImplNone>(a, b, c, Default::default());
|
|
let b_1 = StructUnnamedRust(a, b, c, Default::default());
|
|
for (a, b, c) in combinations() {
|
|
let a_2 = StructUnnamed::<Runtime, ImplNone, ImplNone>(a, b, c, Default::default());
|
|
let b_2 = StructUnnamedRust(a, b, c, Default::default());
|
|
assert_eq!(a_1.partial_cmp(&a_2), b_1.partial_cmp(&b_2));
|
|
assert_eq!(a_1.cmp(&a_2), b_1.cmp(&b_2));
|
|
assert_eq!(a_1.eq(&a_2), b_1.eq(&b_2));
|
|
}
|
|
}
|
|
}
|