Skip to content

Commit 73acc29

Browse files
committed
Added normalize_newlines adapter predicate
Created normalize file and added normalize-line-endings as default dependency Created normalize trim predicate Issue assert-rs#45 Normalize newlines - Insert newline in adapter.rs for readability - Add example code in adapter.rs for usability - Add self to mod.rs - Change to NormalizedPredicate in normalize.rs to avoid ambiguity Normalize newlines syntax clean-up Make comments "///" instead of "//" for consistency Improve readability by making one commented block for function in adapter.rs Delete extraneous whitespace for readability Normalize NewLines Add copyright notice to adapters.rs and normalize.rs Amend doc tests for passing CI Normalize NewLines passes all tests Struct documentation added for NormalizedPredicate Assert tests fixed in adapters.rs (string assertions did not match)
1 parent 43c613f commit 73acc29

File tree

5 files changed

+54
-17
lines changed

5 files changed

+54
-17
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ regex = { version="1.0", optional = true }
2424
float-cmp = { version="0.4", optional = true }
2525

2626
[features]
27-
default = ["difference", "regex", "float-cmp"]
27+
default = ["difference", "regex", "float-cmp", "normalize-line-endings"]
2828
unstable = []

src/lib.rs

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

src/str/adapters.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
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;
4-
use self::normalized::NormalizedTrimPredicate;
12+
513
use Predicate;
14+
use str::normalize::NormalizedPredicate;
615

716
/// Predicate adaper that trims the variable being tested.
817
///
@@ -111,10 +120,25 @@ where
111120
fn from_utf8(self) -> Utf8Predicate<Self> {
112121
Utf8Predicate { p: self }
113122
}
114-
/// Returns a `NormalizedTrimPredicate` that ensures the data passed
115-
/// to `Self` is trimmed and Normalized.
116-
fn normalized_trim(self) -> NormalizedTrimPredicate<Self> {
117-
NormalizedTrimPredicate { p: self }
123+
124+
/// Returns a `NormalizedPredicate` that ensures
125+
/// the newlines within the data passed to `Self` is normalised.
126+
///
127+
/// # Examples
128+
///
129+
/// ```
130+
/// use predicates::prelude::*;
131+
///
132+
/// let predicate_fn = predicate::eq("Hello World!\n").normalize();
133+
/// assert_eq!(true, predicate_fn.eval("Hello World!\n"));
134+
/// assert_eq!(true, predicate_fn.eval("Hello World!\r"));
135+
/// assert_eq!(true, predicate_fn.eval("Hello World!\r\n"));
136+
/// assert_eq!(false, predicate_fn.eval("Goodbye"));
137+
/// ```
138+
///
139+
#[cfg(feature = "normalize-line-endings")]
140+
fn normalize(self) -> NormalizedPredicate<Self> {
141+
NormalizedPredicate { p: self }
118142
}
119143

120144
}

src/str/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ pub use self::adapters::*;
1919
mod difference;
2020
#[cfg(feature = "difference")]
2121
pub use self::difference::{diff, similar, DifferencePredicate};
22-
// need to double check this part
23-
#[cfg(feature = "normalized-line-endings")]
22+
#[cfg(feature = "normalize-line-endings")]
2423
mod normalize;
25-
#[cfg(feature = "normalized-line-endings")]
26-
pub use normalized_line_endings::normalized;
24+
#[cfg(feature = "normalize-line-endings")]
25+
pub use self::normalize::NormalizedPredicate;
2726

2827
#[cfg(feature = "regex")]
2928
mod regex;

src/str/normalize.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,38 @@
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::fmt;
210
use Predicate;
311

12+
use normalize_line_endings::normalized;
13+
use std::iter::FromIterator;
14+
415
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5-
pub struct NormalizedTrimPredicate<P>
16+
/// Predicate adapter that normalizes the newlines contained in the variable being tested.
17+
///
18+
/// This is created by `pred.normalized()`.
19+
pub struct NormalizedPredicate<P>
620
where
721
P: Predicate<str>,
822
{
9-
p: P,
23+
pub(crate) p: P,
1024
}
1125

12-
impl<P> Predicate<str> for NormalizedTrimPredicate<P>
26+
impl<P> Predicate<str> for NormalizedPredicate<P>
1327
where
1428
P: Predicate<str>,
1529
{
1630
fn eval(&self, variable: &str) -> bool {
17-
self.p.eval(variable.normalised())
31+
self.p.eval(&String::from_iter(normalized(variable.chars())))
1832
}
1933
}
2034

21-
impl<P> fmt::Display for NormalizedTrimPredicate<P>
35+
impl<P> fmt::Display for NormalizedPredicate<P>
2236
where
2337
P: Predicate<str>,
2438
{

0 commit comments

Comments
 (0)