Skip to content

Commit b4659e5

Browse files
authored
Merge pull request #61 from kammitama5/normalize_newlines
Normalize newlines
2 parents 53ef88a + a418eef commit b4659e5

File tree

5 files changed

+81
-1
lines changed

5 files changed

+81
-1
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ appveyor = { repository = "assert-rs/predicates-rs" }
1919

2020
[dependencies]
2121
difference = { version = "2.0", optional = true }
22+
normalize-line-endings = { version = "0.2.2", optional = true }
2223
regex = { version="1.0", optional = true }
2324
float-cmp = { version="0.4", optional = true }
2425

2526
[features]
26-
default = ["difference", "regex", "float-cmp"]
27+
default = ["difference", "regex", "float-cmp", "normalize-line-endings"]
2728
unstable = []

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@
9595
extern crate difference;
9696
#[cfg(feature = "float-cmp")]
9797
extern crate float_cmp;
98+
#[cfg(feature = "normalize-line-endings")]
99+
extern crate normalize_line_endings;
98100
#[cfg(feature = "regex")]
99101
extern crate regex;
100102

src/str/adapters.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1+
// Copyright (c) 2018 The predicates-rs Project Developers.
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// http://www.apache.org/license/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
19
use std::ffi;
210
use std::fmt;
311
use std::str;
412

513
use Predicate;
14+
#[cfg(feature = "normalize-line-endings")]
15+
use str::normalize::NormalizedPredicate;
616

717
/// Predicate adaper that trims the variable being tested.
818
///
@@ -111,6 +121,27 @@ where
111121
fn from_utf8(self) -> Utf8Predicate<Self> {
112122
Utf8Predicate { p: self }
113123
}
124+
125+
/// Returns a `NormalizedPredicate` that ensures
126+
/// the newlines within the data passed to `Self` is normalised.
127+
///
128+
/// # Examples
129+
///
130+
/// ```
131+
/// use predicates::prelude::*;
132+
///
133+
/// let predicate_fn = predicate::eq("Hello World!\n").normalize();
134+
/// assert_eq!(true, predicate_fn.eval("Hello World!\n"));
135+
/// assert_eq!(true, predicate_fn.eval("Hello World!\r"));
136+
/// assert_eq!(true, predicate_fn.eval("Hello World!\r\n"));
137+
/// assert_eq!(false, predicate_fn.eval("Goodbye"));
138+
/// ```
139+
///
140+
#[cfg(feature = "normalize-line-endings")]
141+
fn normalize(self) -> NormalizedPredicate<Self> {
142+
NormalizedPredicate { p: self }
143+
}
144+
114145
}
115146

116147
impl<P> PredicateStrExt for P

src/str/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ pub use self::adapters::*;
1919
mod difference;
2020
#[cfg(feature = "difference")]
2121
pub use self::difference::{diff, similar, DifferencePredicate};
22+
#[cfg(feature = "normalize-line-endings")]
23+
mod normalize;
24+
#[cfg(feature = "normalize-line-endings")]
25+
pub use self::normalize::NormalizedPredicate;
2226

2327
#[cfg(feature = "regex")]
2428
mod regex;

src/str/normalize.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) 2018 The predicates-rs Project Developers.
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// http://www.apache.org/license/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
use std::fmt;
10+
use Predicate;
11+
12+
use normalize_line_endings::normalized;
13+
use std::iter::FromIterator;
14+
15+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16+
/// Predicate adapter that normalizes the newlines contained in the variable being tested.
17+
///
18+
/// This is created by `pred.normalize()`.
19+
pub struct NormalizedPredicate<P>
20+
where
21+
P: Predicate<str>,
22+
{
23+
pub(crate) p: P,
24+
}
25+
26+
impl<P> Predicate<str> for NormalizedPredicate<P>
27+
where
28+
P: Predicate<str>,
29+
{
30+
fn eval(&self, variable: &str) -> bool {
31+
self.p.eval(&String::from_iter(normalized(variable.chars())))
32+
}
33+
}
34+
35+
impl<P> fmt::Display for NormalizedPredicate<P>
36+
where
37+
P: Predicate<str>,
38+
{
39+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40+
write!(f, "{}", self.p)
41+
}
42+
}

0 commit comments

Comments
 (0)