diff --git a/Cargo.toml b/Cargo.toml index eab313a..7865d7f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 = [] diff --git a/src/lib.rs b/src/lib.rs index d0a55b7..733a2ed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/str/adapters.rs b/src/str/adapters.rs index d57af32..5ee9026 100644 --- a/src/str/adapters.rs +++ b/src/str/adapters.rs @@ -1,8 +1,18 @@ +// Copyright (c) 2018 The predicates-rs Project Developers. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , 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. /// @@ -111,6 +121,27 @@ where fn from_utf8(self) -> Utf8Predicate { Utf8Predicate { p: self } } + + /// Returns a `NormalizedPredicate` that ensures + /// the newlines within the data passed to `Self` is normalised. + /// + /// # Examples + /// + /// ``` + /// 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 { + NormalizedPredicate { p: self } + } + } impl

PredicateStrExt for P diff --git a/src/str/mod.rs b/src/str/mod.rs index 9336eec..d1f864e 100644 --- a/src/str/mod.rs +++ b/src/str/mod.rs @@ -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; diff --git a/src/str/normalize.rs b/src/str/normalize.rs new file mode 100644 index 0000000..bde7f4d --- /dev/null +++ b/src/str/normalize.rs @@ -0,0 +1,42 @@ +// Copyright (c) 2018 The predicates-rs Project Developers. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::fmt; +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

+where + P: Predicate, +{ + pub(crate) p: P, +} + +impl

Predicate for NormalizedPredicate

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

fmt::Display for NormalizedPredicate

+where + P: Predicate, +{ + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.p) + } +} \ No newline at end of file