-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add assert_str_eq
macro for comparing "raw" strings
#88
Conversation
Hi @x3ro - thanks for your contribution, and your well thought out discussion in #24 as well. I actually really like your proposed solution. I think previously I've felt that proposed solutions (both others and my own) have been too generic/complex and would need to be split out to separate crates. This feels actually like you're simplifying the functionality - we're literally skipping a step (where we turn whatever type into I don't have any inline feedback on your implementation as is, it looks like it does the job for
I've pushed a commit that makes suggestions for both these points. Please take a look and let me know if you want to discuss anything further - otherwise I'm happy to get this merged in and released. |
Hey @tommilligan :) Thanks for the feedback and great suggestions. I had also played around with The only potential issue I see is that one can instantiate let c = StrComparison::new(&6, &6); # works fine
# breaks
println!("{}", c);
^ the trait `AsRef<str>` is not implemented for `{integer}` It's hard for me to judge whether this would actually be a problem, since this would only impact people who are using pub struct StrComparison<'a, TLeft, TRight>
where
TLeft: AsRef<str> + ?Sized,
TRight: AsRef<str> + ?Sized,
{
left: &'a TLeft,
right: &'a TRight,
}
impl<'a, TLeft, TRight> StrComparison<'a, TLeft, TRight>
where
TLeft: AsRef<str> + ?Sized,
TRight: AsRef<str> + ?Sized,
{
/// Store two values to be compared in future.
///
/// Expensive diffing is deferred until calling `Debug::fmt`.
pub fn new(left: &'a TLeft, right: &'a TRight) -> StrComparison<'a, TLeft, TRight> {
StrComparison { left, right }
}
} Do you see any problems with that approach? |
Ah good spot. Yes, I think adding the bound to the constructor is sensible, if for some reason we want to relax it later we can make it more permissive. If you'd like to add that and then squash everything together, I think we're good to go |
As discussed in rust-pretty-assertions#24, pretty_assertion's `assert_eq` will escape line breaks in multi-line strings, making diffs for them much harder to read. This commit introduces another variant, `assert_str_eq`, and the associated `StrComparison`, which compares the raw strings, where the newlines are not escaped. Fixes rust-pretty-assertions#24 Co-authored-by: Tom Milligan <tom@reinfer.io>
Done! |
Codecov Report
@@ Coverage Diff @@
## main #88 +/- ##
==========================================
- Coverage 96.35% 95.83% -0.53%
==========================================
Files 3 3
Lines 192 216 +24
==========================================
+ Hits 185 207 +22
- Misses 7 9 +2
Continue to review full report at Codecov.
|
As discussed in #24, pretty_assertion's
assert_eq
will escape linebreaks in multi-line strings, making diffs for them much harder to
read.
This commit introduces another variant,
assert_str_eq
, and theassociated
StrComparison
, which compares the raw strings, wherethe newlines are not escaped.
Fixes #24