Skip to content

Commit

Permalink
feat: Readable diffs (#4600)
Browse files Browse the repository at this point in the history
# Motivation
When a `std::assert_eq!()` fails, it can be quite hard to see the
difference.

# Changes
* Use `pretty_assertions::assert_eq()` instead of `std::assert_eq()`

# Tests
* Library security and code quality: I had a quick look through the
library and its dependencies. It has few dependencies (2) and both
dependencies are minimal.
* Licencing: The license is the standard BSD/apache dual license used in
most Rust projects.
* `pretty_assertions` is already used extensively in the `dfinity/ic`
repository.

# Todos

- [x] Add entry to changelog (if necessary).
  • Loading branch information
bitdivine authored Mar 11, 2024
1 parent f01b88f commit 5bfa5e2
Show file tree
Hide file tree
Showing 16 changed files with 29 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-Nns-Dapp-unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ proposal is successful, the changes it released will be moved from this file to
#### Changed

* Refactored `icpAccountsStore` to be derived from separate stores.
* More readable error messages if `assert_eq` fails in tests.
* Wording changes for ineligible neurons description.

#### Deprecated
Expand Down
28 changes: 5 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
disallowed-macros = [
"std::print",
"std::println",
"std::eprint",
"std::eprintln",
{ path = "std::print", reason = "Use ic_cdk::print instead" },
{ path = "std::println", reason = "Use ic_cdk::println instead" },
{ path = "std::eprint", reason = "Use ic_cdk::eprint instead" },
{ path = "std::eprintln", reason = "Use ic_cdk::eprintln instead" },
{ path = "std::assert_eq", reason = "Use pretty_assertions::assert_eq instead" },
]
disallowed-methods = [
"dfn_core::api::print",
{ path = "dfn_core::api::print", reason = "Use ic_cdk::print or ic_cdk::println instead" },
]
1 change: 1 addition & 0 deletions rs/backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ proposals = { path = "../proposals" }
[dev-dependencies]
anyhow = "1.0.75"
maplit = "1.0.2"
pretty_assertions = "1.4.0"
proptest = "1.0.0"
rand = "0.8.5"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use super::*;
use crate::accounts_store::schema::SchemaLabel;
use pretty_assertions::assert_eq;
use strum::IntoEnumIterator;

#[test]
Expand Down
1 change: 1 addition & 0 deletions rs/backend/src/accounts_store/schema/proxy/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use ic_stable_structures::memory_manager;
use pretty_assertions::assert_eq;
use proptest::proptest;
use rand::seq::IteratorRandom;
use rand::{Rng, SeedableRng};
Expand Down
1 change: 1 addition & 0 deletions rs/backend/src/accounts_store/schema/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use super::super::{AccountIdentifier, CanisterId, NamedCanister, PrincipalId};
use super::*;
use pretty_assertions::assert_eq;
use std::collections::{BTreeMap, BTreeSet, HashMap};

/// Creates a toy canister.
Expand Down
1 change: 1 addition & 0 deletions rs/backend/src/accounts_store/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use super::*;
use crate::accounts_store::toy_data::{toy_account, ToyAccountSize};
use crate::multi_part_transactions_processor::MultiPartTransactionToBeProcessed;
use icp_ledger::Tokens;
use pretty_assertions::assert_eq;
use std::str::FromStr;

const TEST_ACCOUNT_1: &str = "h4a5i-5vcfo-5rusv-fmb6m-vrkia-mjnkc-jpoow-h5mam-nthnm-ldqlr-bqe";
Expand Down
3 changes: 3 additions & 0 deletions rs/backend/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ use std::collections::HashMap;
use std::io::prelude::*;
use std::io::Read;

#[cfg(test)]
use pretty_assertions::assert_eq;

type HeaderField = (String, String);

#[derive(Clone, Debug, CandidType, Deserialize)]
Expand Down
1 change: 1 addition & 0 deletions rs/backend/src/multi_part_transactions_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl MultiPartTransactionsProcessor {
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
use std::str::FromStr;

const TEST_ACCOUNT_1: &str = "h4a5i-5vcfo-5rusv-fmb6m-vrkia-mjnkc-jpoow-h5mam-nthnm-ldqlr-bqe";
Expand Down
1 change: 1 addition & 0 deletions rs/backend/src/perf/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::PerformanceCounts;
use crate::state::StableState;
use pretty_assertions::assert_eq;

/// Given valid performance data, it should serialize and be parsed back to the original.
#[test]
Expand Down
1 change: 1 addition & 0 deletions rs/backend/src/state/partitions/schemas/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use super::*;
#[cfg(test)]
use ic_stable_structures::memory_manager::MemoryManager;
use pretty_assertions::assert_eq;
#[cfg(test)]
use std::rc::Rc;
use strum::IntoEnumIterator;
Expand Down
1 change: 1 addition & 0 deletions rs/backend/src/state/partitions/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
};
use ic_cdk::api::stable::WASM_PAGE_SIZE_IN_BYTES;
use ic_crypto_sha::Sha256;
use pretty_assertions::assert_eq;
use std::rc::Rc;

#[test]
Expand Down
1 change: 1 addition & 0 deletions rs/backend/src/state/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
},
};
use ic_stable_structures::{DefaultMemoryImpl, VectorMemory};
use pretty_assertions::assert_eq;
use proptest::proptest;
use std::cell::RefCell;
use strum::IntoEnumIterator;
Expand Down
1 change: 1 addition & 0 deletions rs/proposals/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ idl2json = "0.8.7"

[dev-dependencies]
anyhow = "1.0.75"
pretty_assertions = "1.4.0"
3 changes: 3 additions & 0 deletions rs/sns_aggregator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@ serde_derive = "1.0.126"
serde_json = "1.0.108"
sha2 = "0.10.8"

[dev-dependencies]
pretty_assertions = "1.4.0"

[features]
reconfigurable = []

0 comments on commit 5bfa5e2

Please sign in to comment.