Skip to content

Normalize newlines #61

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

Merged
merged 1 commit into from
Jul 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ appveyor = { repository = "assert-rs/predicates-rs" }

[dependencies]
difference = { version = "2.0", optional = true }
normalize-line-endings = { version = "0.2.2", optional = true }
regex = { version="1.0", optional = true }
float-cmp = { version="0.4", optional = true }

[features]
default = ["difference", "regex", "float-cmp"]
default = ["difference", "regex", "float-cmp", "normalize-line-endings"]
unstable = []
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@
extern crate difference;
#[cfg(feature = "float-cmp")]
extern crate float_cmp;
#[cfg(feature = "normalize-line-endings")]
extern crate normalize_line_endings;
#[cfg(feature = "regex")]
extern crate regex;

Expand Down
31 changes: 31 additions & 0 deletions src/str/adapters.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
// Copyright (c) 2018 The predicates-rs Project Developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/license/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::ffi;
use std::fmt;
use std::str;

use Predicate;
#[cfg(feature = "normalize-line-endings")]
use str::normalize::NormalizedPredicate;

/// Predicate adaper that trims the variable being tested.
///
Expand Down Expand Up @@ -111,6 +121,27 @@ where
fn from_utf8(self) -> Utf8Predicate<Self> {
Utf8Predicate { p: self }
}

/// Returns a `NormalizedPredicate` that ensures
/// the newlines within the data passed to `Self` is normalised.
///
/// # Examples
///
/// ```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not seeing this example in the test results
https://travis-ci.org/assert-rs/predicates-rs/jobs/408337538

Is it because its missing "rust" on the code fence?

/// use predicates::prelude::*;
///
/// let predicate_fn = predicate::eq("Hello World!\n").normalize();
/// assert_eq!(true, predicate_fn.eval("Hello World!\n"));
/// assert_eq!(true, predicate_fn.eval("Hello World!\r"));
/// assert_eq!(true, predicate_fn.eval("Hello World!\r\n"));
/// assert_eq!(false, predicate_fn.eval("Goodbye"));
/// ```
///
#[cfg(feature = "normalize-line-endings")]
fn normalize(self) -> NormalizedPredicate<Self> {
NormalizedPredicate { p: self }
}

}

impl<P> PredicateStrExt for P
Expand Down
4 changes: 4 additions & 0 deletions src/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ pub use self::adapters::*;
mod difference;
#[cfg(feature = "difference")]
pub use self::difference::{diff, similar, DifferencePredicate};
#[cfg(feature = "normalize-line-endings")]
mod normalize;
#[cfg(feature = "normalize-line-endings")]
pub use self::normalize::NormalizedPredicate;

#[cfg(feature = "regex")]
mod regex;
Expand Down
42 changes: 42 additions & 0 deletions src/str/normalize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2018 The predicates-rs Project Developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/license/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::fmt;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forgot to point out that the crate owner wants copyright headers on the files.

use Predicate;

use normalize_line_endings::normalized;
use std::iter::FromIterator;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// Predicate adapter that normalizes the newlines contained in the variable being tested.
///
/// This is created by `pred.normalize()`.
pub struct NormalizedPredicate<P>
where
P: Predicate<str>,
{
pub(crate) p: P,
}

impl<P> Predicate<str> for NormalizedPredicate<P>
where
P: Predicate<str>,
{
fn eval(&self, variable: &str) -> bool {
self.p.eval(&String::from_iter(normalized(variable.chars())))
}
}

impl<P> fmt::Display for NormalizedPredicate<P>
where
P: Predicate<str>,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.p)
}
}