Skip to content

Commit

Permalink
WIP tests behind alloc
Browse files Browse the repository at this point in the history
  • Loading branch information
kamarkiewicz committed Feb 5, 2018
1 parent 76f6048 commit 791a53c
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -916,21 +916,25 @@ mod tests {
);
);

#[cfg(feature = "alloc")]
#[derive(Debug, Clone, PartialEq)]
pub struct ErrorStr(String);

#[cfg(feature = "alloc")]
impl From<u32> for ErrorStr {
fn from(i: u32) -> Self {
ErrorStr(format!("custom error code: {}", i))
}
}

#[cfg(feature = "alloc")]
impl<'a> From<&'a str> for ErrorStr {
fn from(i: &'a str) -> Self {
ErrorStr(format!("custom error message: {}", i))
}
}

#[cfg(feature = "alloc")]
#[test]
fn alt() {
fn work(input: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> {
Expand Down
4 changes: 4 additions & 0 deletions src/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/// `separated_list!(I -> IResult<I,T>, I -> IResult<I,O>) => I -> IResult<I, Vec<O>>`
/// separated_list(sep, X) returns Vec<X> will return Incomplete if there may be more elements
#[cfg(feature = "alloc")]
#[macro_export]
macro_rules! separated_list(
($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => (
Expand Down Expand Up @@ -381,6 +382,7 @@ macro_rules! many1(
/// error_position!(&c[..], ErrorKind::Tag)))));
/// # }
/// ```
#[cfg(feature = "alloc")]
#[macro_export]
macro_rules! many_till(
(__impl $i:expr, $submac1:ident!( $($args1:tt)* ), $submac2:ident!( $($args2:tt)* )) => (
Expand Down Expand Up @@ -468,6 +470,7 @@ macro_rules! many_till(
/// assert_eq!(multi(&c[..]),Ok((&b"abcdefgh"[..], res2)));
/// # }
/// ```
#[cfg(feature = "alloc")]
#[macro_export]
macro_rules! many_m_n(
($i:expr, $m:expr, $n: expr, $submac:ident!( $($args:tt)* )) => (
Expand Down Expand Up @@ -557,6 +560,7 @@ macro_rules! many_m_n(
/// # }
/// ```
///
#[cfg(feature = "alloc")]
#[macro_export]
macro_rules! count(
($i:expr, $submac:ident!( $($args:tt)* ), $count: expr) => (
Expand Down
1 change: 1 addition & 0 deletions src/nom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,7 @@ mod tests {
use types::{CompleteByteSlice, CompleteStr};

#[test]
#[cfg(feature = "alloc")]
fn tag_closure() {
let x = tag_cl(&b"abcd"[..]);
let r = x(&b"abcdabcdefgh"[..]);
Expand Down
7 changes: 7 additions & 0 deletions src/whitespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ macro_rules! switch_sep (
);

#[doc(hidden)]
#[cfg(feature = "alloc")]
#[macro_export]
macro_rules! separated_list_sep (
($i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => (
Expand Down Expand Up @@ -1039,21 +1040,25 @@ mod tests {
assert_eq!(perm(e), Err(Err::Incomplete(Needed::Size(4))));
}

#[cfg(feature = "alloc")]
#[derive(Debug, Clone, PartialEq)]
pub struct ErrorStr(String);

#[cfg(feature = "alloc")]
impl From<u32> for ErrorStr {
fn from(i: u32) -> Self {
ErrorStr(format!("custom error code: {}", i))
}
}

#[cfg(feature = "alloc")]
impl<'a> From<&'a str> for ErrorStr {
fn from(i: &'a str) -> Self {
ErrorStr(format!("custom error message: {}", i))
}
}

#[cfg(feature = "alloc")]
#[test]
fn alt() {
fn work(input: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> {
Expand Down Expand Up @@ -1142,6 +1147,7 @@ mod tests {

// test whitespace parser generation for alt
named!(space, tag!(" "));
#[cfg(feature = "alloc")]
named!(pipeline_statement<&[u8], ()>,
ws!(
do_parse!(
Expand All @@ -1161,5 +1167,6 @@ mod tests {
)
);

#[cfg(feature = "alloc")]
named!(fail<&[u8]>, map!(many_till!(take!(1), ws!(tag!("."))), |(r, _)| r[0]));
}
1 change: 1 addition & 0 deletions tests/custom_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fn test3(input: &str) -> IResult<&str, &str, CustomError> {
verify!(input, test1, |s: &str| s.starts_with("abcd"))
}

#[cfg(feature = "alloc")]
fn test4(input: &str) -> IResult<&str, Vec<&str>, CustomError> {
count!(input, test1, 4)
}
4 changes: 4 additions & 0 deletions tests/inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#![allow(unused_comparisons)]
#![allow(unused_doc_comment)]
#![allow(unused_variables)]
#![allow(unused_imports)]

#[macro_use]
extern crate nom;
Expand All @@ -15,6 +16,7 @@ use nom::{is_digit, alpha};
named!(multi<&[u8], () >, fold_many0!( take_while1!( is_digit ), (), |_, _| {}));

// issue #561
#[cfg(feature = "alloc")]
named!(value<Vec<Vec<&str>>>,
do_parse!(
first_line: map_res!(is_not_s!("\n"), std::str::from_utf8) >>
Expand All @@ -26,6 +28,7 @@ named!(value<Vec<Vec<&str>>>,
);

// issue #534
#[cfg(feature = "alloc")]
fn wrap_suffix(input: &Option<Vec<&[u8]>>) -> Option<String> {
if input.is_some() {
///
Expand All @@ -38,6 +41,7 @@ fn wrap_suffix(input: &Option<Vec<&[u8]>>) -> Option<String> {
}
}

#[cfg(feature = "alloc")]
named!(parse_suffix<&[u8],Option<String>>,do_parse!(
u: opt!(many1!(alt_complete!(
tag!("%") | tag!("#") | tag!("@") | alpha
Expand Down
6 changes: 4 additions & 2 deletions tests/issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#[macro_use]
extern crate nom;

use nom::{Err, IResult, Needed, space, be_u16, le_u64};
#[cfg(feature = "alloc")]
use nom::le_u64;
use nom::{Err, IResult, Needed, space, be_u16};
use nom::types::CompleteByteSlice;

#[allow(dead_code)]
Expand Down Expand Up @@ -150,7 +152,7 @@ named!(issue_308(&str) -> bool,
) >>
(b) ));


#[cfg(feature = "alloc")]
fn issue_302(input: &[u8]) -> IResult<&[u8], Option<Vec<u64>>> {
do_parse!(input,
entries: cond!(true, count!(le_u64, 3)) >>
Expand Down
1 change: 1 addition & 0 deletions tests/json.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "alloc")]
//#![feature(trace_macros)]

#[macro_use]
Expand Down
7 changes: 7 additions & 0 deletions tests/overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ fn overflow_incomplete_tuple() {
}

#[test]
#[cfg(feature = "alloc")]
fn overflow_incomplete_length_bytes() {
named!(multi<&[u8], Vec<&[u8]> >, many0!( length_bytes!(be_u64) ) );

Expand All @@ -50,6 +51,7 @@ fn overflow_incomplete_length_bytes() {
}

#[test]
#[cfg(feature = "alloc")]
fn overflow_incomplete_many0() {
named!(multi<&[u8], Vec<&[u8]> >, many0!( length_bytes!(be_u64) ) );

Expand All @@ -69,6 +71,7 @@ fn overflow_incomplete_many1() {
}

#[test]
#[cfg(feature = "alloc")]
fn overflow_incomplete_many_till() {
named!(multi<&[u8], (Vec<&[u8]>, &[u8]) >, many_till!( length_bytes!(be_u64), tag!("abc") ) );

Expand All @@ -78,6 +81,7 @@ fn overflow_incomplete_many_till() {
}

#[test]
#[cfg(feature = "alloc")]
fn overflow_incomplete_many_m_n() {
named!(multi<&[u8], Vec<&[u8]> >, many_m_n!(2, 4, length_bytes!(be_u64) ) );

Expand All @@ -87,6 +91,7 @@ fn overflow_incomplete_many_m_n() {
}

#[test]
#[cfg(feature = "alloc")]
fn overflow_incomplete_count() {
named!(counter<&[u8], Vec<&[u8]> >, count!( length_bytes!(be_u64), 2 ) );

Expand All @@ -103,6 +108,7 @@ fn overflow_incomplete_count_fixed() {
}

#[test]
#[cfg(feature = "alloc")]
fn overflow_incomplete_length_count() {
named!(multi<&[u8], Vec<&[u8]> >, length_count!( be_u8, length_bytes!(be_u64) ) );

Expand All @@ -111,6 +117,7 @@ fn overflow_incomplete_length_count() {
}

#[test]
#[cfg(feature = "alloc")]
fn overflow_incomplete_length_data() {
named!(multi<&[u8], Vec<&[u8]> >, many0!( length_data!(be_u64) ) );

Expand Down

0 comments on commit 791a53c

Please sign in to comment.