From 6de30893f888e2f0352ce0d73273b4fe8032f948 Mon Sep 17 00:00:00 2001 From: Eric Scouten Date: Fri, 29 Dec 2023 09:12:46 -0800 Subject: [PATCH 1/2] Use pretty_assertions_sorted --- Cargo.toml | 3 +++ src/tests/blocks/block.rs | 1 + src/tests/blocks/simple.rs | 1 + src/tests/document.rs | 2 ++ src/tests/fixtures/blocks/simple.rs | 15 ++++++++++++++- src/tests/fixtures/document.rs | 16 ++++++++++++++-- src/tests/fixtures/span.rs | 19 +++++++++++++++++-- src/tests/primitives/line.rs | 8 ++++++++ 8 files changed, 60 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ea0f9cf..781e2dc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,6 @@ rust-version = "1.72.0" nom = "7.1" nom-span = "0.1.1" thiserror = "1.0.50" + +[dev-dependencies] +pretty_assertions_sorted = "1.2" diff --git a/src/tests/blocks/block.rs b/src/tests/blocks/block.rs index 9429dbd..4f39768 100644 --- a/src/tests/blocks/block.rs +++ b/src/tests/blocks/block.rs @@ -3,6 +3,7 @@ mod simple { error::{Error, ErrorKind}, Err, }; + use pretty_assertions_sorted::assert_eq; use crate::{ blocks::Block, diff --git a/src/tests/blocks/simple.rs b/src/tests/blocks/simple.rs index 63fa5ac..f3a835c 100644 --- a/src/tests/blocks/simple.rs +++ b/src/tests/blocks/simple.rs @@ -2,6 +2,7 @@ use nom::{ error::{Error, ErrorKind}, Err, }; +use pretty_assertions_sorted::assert_eq; use crate::{ blocks::SimpleBlock, diff --git a/src/tests/document.rs b/src/tests/document.rs index 46a6ec6..5cdc719 100644 --- a/src/tests/document.rs +++ b/src/tests/document.rs @@ -1,3 +1,5 @@ +use pretty_assertions_sorted::assert_eq; + use crate::{ tests::fixtures::{ blocks::{TBlock, TSimpleBlock}, diff --git a/src/tests/fixtures/blocks/simple.rs b/src/tests/fixtures/blocks/simple.rs index c12bc9b..75235d2 100644 --- a/src/tests/fixtures/blocks/simple.rs +++ b/src/tests/fixtures/blocks/simple.rs @@ -1,10 +1,23 @@ +use std::fmt; + use crate::{blocks::SimpleBlock, tests::fixtures::TSpan}; -#[derive(Debug, Eq, PartialEq)] +#[derive(Eq, PartialEq)] pub(crate) struct TSimpleBlock { pub inlines: Vec, } +impl fmt::Debug for TSimpleBlock { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + // Intentionally mimic the output of nom_span::Spanned + // so diffs point the unit test author to the important + // differences. + f.debug_struct("SimpleBlock") + .field("inlines", &self.inlines) + .finish() + } +} + impl<'a> PartialEq> for TSimpleBlock { fn eq(&self, other: &SimpleBlock<'a>) -> bool { tsimple_block_eq(self, other) diff --git a/src/tests/fixtures/document.rs b/src/tests/fixtures/document.rs index 95bb0c9..762a00c 100644 --- a/src/tests/fixtures/document.rs +++ b/src/tests/fixtures/document.rs @@ -1,4 +1,4 @@ -use std::cmp::PartialEq; +use std::{cmp::PartialEq, fmt}; use crate::{ tests::fixtures::{blocks::TBlock, TSpan}, @@ -10,12 +10,24 @@ use crate::{ // // Primary difference is that the data members are public // so we can declare them inline. -#[derive(Debug, Eq, PartialEq)] +#[derive(Eq, PartialEq)] pub(crate) struct TDocument { pub blocks: Vec, pub source: TSpan, } +impl fmt::Debug for TDocument { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + // Intentionally mimic the output of nom_span::Spanned + // so diffs point the unit test author to the important + // differences. + f.debug_struct("Document") + .field("blocks", &self.blocks) + .field("source", &self.source) + .finish() + } +} + impl<'a> PartialEq> for TDocument { fn eq(&self, other: &Document<'a>) -> bool { tdocument_eq(self, other) diff --git a/src/tests/fixtures/span.rs b/src/tests/fixtures/span.rs index 62aca43..d33c194 100644 --- a/src/tests/fixtures/span.rs +++ b/src/tests/fixtures/span.rs @@ -1,4 +1,4 @@ -use std::cmp::PartialEq; +use std::{cmp::PartialEq, fmt}; use crate::Span; @@ -7,7 +7,7 @@ use crate::Span; // // Primary difference is that the data members are public // so we can declare them inline. -#[derive(Debug, Eq, PartialEq)] +#[derive(Eq, PartialEq)] pub(crate) struct TSpan { pub data: &'static str, pub line: usize, @@ -15,6 +15,21 @@ pub(crate) struct TSpan { pub offset: usize, } +impl fmt::Debug for TSpan { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + // Intentionally mimic the output of nom_span::Spanned + // so diffs point the unit test author to the important + // differences. + f.debug_struct("Spanned") + .field("data", &self.data) + .field("line", &self.line) + .field("col", &self.col) + .field("offset", &self.offset) + .field("handle_utf8", &true) + .finish() + } +} + impl<'a> PartialEq> for TSpan { fn eq(&self, other: &Span<'a>) -> bool { tspan_eq(self, other) diff --git a/src/tests/primitives/line.rs b/src/tests/primitives/line.rs index a602139..39e7bec 100644 --- a/src/tests/primitives/line.rs +++ b/src/tests/primitives/line.rs @@ -1,4 +1,6 @@ mod fn_line { + use pretty_assertions_sorted::assert_eq; + use crate::{primitives::line, tests::fixtures::TSpan, Span}; #[test] @@ -186,6 +188,8 @@ mod fn_line { } mod normalized_line { + use pretty_assertions_sorted::assert_eq; + use crate::{primitives::normalized_line, tests::fixtures::TSpan, Span}; #[test] @@ -402,6 +406,7 @@ mod non_empty_line { error::{Error, ErrorKind}, Err, }; + use pretty_assertions_sorted::assert_eq; use crate::{primitives::non_empty_line, tests::fixtures::TSpan, Span}; @@ -589,6 +594,7 @@ mod empty_line { error::{Error, ErrorKind}, Err, }; + use pretty_assertions_sorted::assert_eq; use crate::{primitives::empty_line, tests::fixtures::TSpan, Span}; @@ -786,6 +792,8 @@ mod empty_line { } mod consume_empty_lines { + use pretty_assertions_sorted::assert_eq; + use crate::{primitives::consume_empty_lines, tests::fixtures::TSpan, Span}; #[test] From b746bd4d2d1dce206c1411642a6d97f32e2fcc1a Mon Sep 17 00:00:00 2001 From: Eric Scouten Date: Fri, 29 Dec 2023 09:20:15 -0800 Subject: [PATCH 2/2] Looks like we need 1.2.3 of pretty_assertions_sorted --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 781e2dc..b0919fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,4 +15,4 @@ nom-span = "0.1.1" thiserror = "1.0.50" [dev-dependencies] -pretty_assertions_sorted = "1.2" +pretty_assertions_sorted = "1.2.3"