Skip to content

Commit

Permalink
replace macros in tests with functions
Browse files Browse the repository at this point in the history
The deleted tests/test1.rs didn't contain any additional tests and was
depending on an old `stream` feature. It should be covered by the
doctests of the used functions.
  • Loading branch information
nickelc authored and Geal committed Aug 3, 2021
1 parent e95faa4 commit f8661f3
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 355 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,6 @@ name = "overflow"
[[test]]
name = "reborrow_fold"

[[test]]
name = "test1"

[[test]]
name = "fnmut"
required-features = ["alloc"]
Expand Down
19 changes: 8 additions & 11 deletions src/character/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -936,24 +936,21 @@ mod tests {

#[test]
fn full_line_windows() {
//let not_line_ending = |i:&[u8]| take_while(|c| c != b'\r' && c != b'\n')(i);

named!(
take_full_line<(&[u8], &[u8])>,
tuple!(not_line_ending, line_ending)
);
use crate::sequence::pair;
fn take_full_line(i: &[u8]) -> IResult<&[u8], (&[u8], &[u8])> {
pair(not_line_ending, line_ending)(i)
}
let input = b"abc\r\n";
let output = take_full_line(input);
assert_eq!(output, Ok((&b""[..], (&b"abc"[..], &b"\r\n"[..]))));
}

#[test]
fn full_line_unix() {
//let not_line_ending = |i:&[u8]| take_while(|c| c != b'\n')(i);
named!(
take_full_line<(&[u8], &[u8])>,
tuple!(not_line_ending, line_ending)
);
use crate::sequence::pair;
fn take_full_line(i: &[u8]) -> IResult<&[u8], (&[u8], &[u8])> {
pair(not_line_ending, line_ending)(i)
}
let input = b"abc\n";
let output = take_full_line(input);
assert_eq!(output, Ok((&b""[..], (&b"abc"[..], &b"\n"[..]))));
Expand Down
15 changes: 7 additions & 8 deletions src/character/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ mod tests {
use super::*;
use crate::error::ErrorKind;
use crate::internal::{Err, Needed};
use crate::sequence::pair;

macro_rules! assert_parse(
($left: expr, $right: expr) => {
Expand Down Expand Up @@ -920,21 +921,19 @@ mod tests {

#[test]
fn full_line_windows() {
named!(
take_full_line<(&[u8], &[u8])>,
tuple!(not_line_ending, line_ending)
);
fn take_full_line(i: &[u8]) -> IResult<&[u8], (&[u8], &[u8])> {
pair(not_line_ending, line_ending)(i)
}
let input = b"abc\r\n";
let output = take_full_line(input);
assert_eq!(output, Ok((&b""[..], (&b"abc"[..], &b"\r\n"[..]))));
}

#[test]
fn full_line_unix() {
named!(
take_full_line<(&[u8], &[u8])>,
tuple!(not_line_ending, line_ending)
);
fn take_full_line(i: &[u8]) -> IResult<&[u8], (&[u8], &[u8])> {
pair(not_line_ending, line_ending)(i)
}
let input = b"abc\n";
let output = take_full_line(input);
assert_eq!(output, Ok((&b""[..], (&b"abc"[..], &b"\n"[..]))));
Expand Down
49 changes: 24 additions & 25 deletions tests/float.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
#[macro_use]
extern crate nom;

use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::character::streaming::digit1 as digit;
use nom::combinator::{map, map_res, opt, recognize};
use nom::sequence::{delimited, pair};
use nom::IResult;

use std::str;
use std::str::FromStr;

named!(
unsigned_float<f32>,
map_res!(
map_res!(
recognize!(alt!(
delimited!(digit, tag!("."), opt!(digit)) | delimited!(opt!(digit), tag!("."), digit)
)),
str::from_utf8
),
FromStr::from_str
)
);
fn unsigned_float(i: &[u8]) -> IResult<&[u8], f32> {
let float_bytes = recognize(alt((
delimited(digit, tag("."), opt(digit)),
delimited(opt(digit), tag("."), digit),
)));
let float_str = map_res(float_bytes, str::from_utf8);
map_res(float_str, FromStr::from_str)(i)
}

named!(
float<f32>,
map!(
pair!(opt!(alt!(tag!("+") | tag!("-"))), unsigned_float),
|(sign, value): (Option<&[u8]>, f32)| sign
.and_then(|s| if s[0] == b'-' { Some(-1f32) } else { None })
.unwrap_or(1f32)
* value
)
);
fn float(i: &[u8]) -> IResult<&[u8], f32> {
map(
pair(opt(alt((tag("+"), tag("-")))), unsigned_float),
|(sign, value)| {
sign
.and_then(|s| if s[0] == b'-' { Some(-1f32) } else { None })
.unwrap_or(1f32)
* value
},
)(i)
}

#[test]
fn unsigned_float_test() {
Expand Down
87 changes: 34 additions & 53 deletions tests/ini.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#[macro_use]
extern crate nom;

use nom::{
bytes::complete::take_while,
character::complete::{
alphanumeric1 as alphanumeric, char, multispace0 as multispace, space0 as space,
},
combinator::map_res,
sequence::delimited,
combinator::{map, map_res, opt},
multi::many0,
sequence::{delimited, pair, separated_pair, terminated, tuple},
IResult,
};

Expand All @@ -21,56 +19,39 @@ fn category(i: &[u8]) -> IResult<&[u8], &str> {
)(i)
}

fn complete_byte_slice_to_str<'a>(s: &'a [u8]) -> Result<&'a str, str::Utf8Error> {
str::from_utf8(s)
fn key_value(i: &[u8]) -> IResult<&[u8], (&str, &str)> {
let (i, key) = map_res(alphanumeric, str::from_utf8)(i)?;
let (i, _) = tuple((opt(space), char('='), opt(space)))(i)?;
let (i, val) = map_res(take_while(|c| c != b'\n' && c != b';'), str::from_utf8)(i)?;
let (i, _) = opt(pair(char(';'), take_while(|c| c != b'\n')))(i)?;
Ok((i, (key, val)))
}

fn keys_and_values(i: &[u8]) -> IResult<&[u8], HashMap<&str, &str>> {
map(many0(terminated(key_value, opt(multispace))), |vec| {
vec.into_iter().collect()
})(i)
}

named!(key_value <&[u8],(&str,&str)>,
do_parse!(
key: map_res!(alphanumeric, complete_byte_slice_to_str)
>> opt!(space)
>> char!('=')
>> opt!(space)
>> val: map_res!(
take_while!(call!(|c| c != b'\n' && c != b';')),
complete_byte_slice_to_str
)
>> opt!(pair!(char!(';'), take_while!(call!(|c| c != b'\n'))))
>> (key, val)
)
);

named!(keys_and_values<&[u8], HashMap<&str, &str> >,
map!(
many0!(terminated!(key_value, opt!(multispace))),
|vec: Vec<_>| vec.into_iter().collect()
)
);

named!(category_and_keys<&[u8],(&str,HashMap<&str,&str>)>,
do_parse!(
category: category >>
opt!(multispace) >>
keys: keys_and_values >>
(category, keys)
)
);

named!(categories<&[u8], HashMap<&str, HashMap<&str,&str> > >,
map!(
many0!(
separated_pair!(
category,
opt!(multispace),
map!(
many0!(terminated!(key_value, opt!(multispace))),
|vec: Vec<_>| vec.into_iter().collect()
)
)
),
|vec: Vec<_>| vec.into_iter().collect()
)
);
fn category_and_keys(i: &[u8]) -> IResult<&[u8], (&str, HashMap<&str, &str>)> {
let (i, category) = terminated(category, opt(multispace))(i)?;
let (i, keys) = keys_and_values(i)?;
Ok((i, (category, keys)))
}

fn categories(i: &[u8]) -> IResult<&[u8], HashMap<&str, HashMap<&str, &str>>> {
map(
many0(separated_pair(
category,
opt(multispace),
map(
many0(terminated(key_value, opt(multispace))),
|vec: Vec<_>| vec.into_iter().collect(),
),
)),
|vec: Vec<_>| vec.into_iter().collect(),
)(i)
}

#[test]
fn parse_category_test() {
Expand Down
45 changes: 22 additions & 23 deletions tests/ini_str.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#[macro_use]
extern crate nom;

use nom::{
bytes::complete::{is_a, take_while},
bytes::complete::{is_a, tag, take_till, take_while},
character::complete::{alphanumeric1 as alphanumeric, char, space0 as space},
combinator::opt,
sequence::{delimited, terminated},
multi::many0,
sequence::{delimited, pair, terminated, tuple},
IResult,
};

Expand All @@ -30,21 +28,20 @@ fn category(i: &str) -> IResult<&str, &str> {
)(i)
}

named!(key_value <&str,(&str,&str)>,
do_parse!(
key: alphanumeric >>
opt!(space) >>
tag!("=") >>
opt!(space) >>
val: take_till!(is_line_ending_or_comment) >>
opt!(space) >>
opt!(pair!(tag!(";"), not_line_ending)) >>
opt!(space_or_line_ending) >>
(key, val)
)
);
fn key_value(i: &str) -> IResult<&str, (&str, &str)> {
let (i, key) = alphanumeric(i)?;
let (i, _) = tuple((opt(space), tag("="), opt(space)))(i)?;
let (i, val) = take_till(is_line_ending_or_comment)(i)?;
let (i, _) = opt(space)(i)?;
let (i, _) = opt(pair(tag(";"), not_line_ending))(i)?;
let (i, _) = opt(space_or_line_ending)(i)?;

Ok((i, (key, val)))
}

named!(keys_and_values_aggregator<&str, Vec<(&str, &str)> >, many0!(key_value));
fn keys_and_values_aggregator(i: &str) -> IResult<&str, Vec<(&str, &str)>> {
many0(key_value)(i)
}

fn keys_and_values(input: &str) -> IResult<&str, HashMap<&str, &str>> {
match keys_and_values_aggregator(input) {
Expand All @@ -53,11 +50,13 @@ fn keys_and_values(input: &str) -> IResult<&str, HashMap<&str, &str>> {
}
}

named!(category_and_keys<&str,(&str,HashMap<&str,&str>)>,
pair!(category, keys_and_values)
);
fn category_and_keys(i: &str) -> IResult<&str, (&str, HashMap<&str, &str>)> {
pair(category, keys_and_values)(i)
}

named!(categories_aggregator<&str, Vec<(&str, HashMap<&str,&str>)> >, many0!(category_and_keys));
fn categories_aggregator(i: &str) -> IResult<&str, Vec<(&str, HashMap<&str, &str>)>> {
many0(category_and_keys)(i)
}

fn categories(input: &str) -> IResult<&str, HashMap<&str, HashMap<&str, &str>>> {
match categories_aggregator(input) {
Expand Down
Loading

0 comments on commit f8661f3

Please sign in to comment.