third_party_rust_nom/tests/issues.rs

237 lines
5.0 KiB
Rust
Raw Normal View History

//#![feature(trace_macros)]
2017-06-16 12:34:49 +00:00
#![allow(dead_code)]
2017-12-10 13:03:25 +00:00
#![cfg_attr(feature = "cargo-clippy", allow(redundant_closure))]
2017-06-16 12:34:49 +00:00
2015-08-04 07:07:55 +00:00
#[macro_use]
extern crate nom;
use nom::{space, Err, IResult, Needed, le_u64};
2018-01-24 15:36:09 +00:00
use nom::types::CompleteByteSlice;
2015-08-04 07:07:55 +00:00
#[allow(dead_code)]
struct Range {
start: char,
end: char,
2015-08-04 07:07:55 +00:00
}
pub fn take_char(input: &[u8]) -> IResult<&[u8], char> {
2017-12-10 13:03:25 +00:00
if !input.is_empty() {
Ok((&input[1..], input[0] as char))
2015-08-04 07:07:55 +00:00
} else {
Err(Err::Incomplete(Needed::Size(1)))
2015-08-04 07:07:55 +00:00
}
}
//trace_macros!(true);
#[allow(dead_code)]
named!(range<&[u8], Range>,
alt!(
do_parse!(
start: take_char >>
tag!("-") >>
end: take_char >>
(Range {
start: start,
end: end,
})
2015-08-04 07:07:55 +00:00
) |
map!(
take_char,
|c| {
Range {
start: c,
end: c,
2015-08-04 07:07:55 +00:00
}
}
)
)
);
#[allow(dead_code)]
named!(literal<&[u8], Vec<char> >,
map!(
many1!(take_char),
|cs| {
cs
}
)
);
#[test]
fn issue_58() {
2017-12-09 15:12:57 +00:00
let _ = range(&b"abcd"[..]);
let _ = literal(&b"abcd"[..]);
2015-08-04 07:07:55 +00:00
}
//trace_macros!(false);
#[cfg(feature = "std")]
mod parse_int {
use nom::HexDisplay;
2018-02-17 14:06:43 +00:00
use nom::{digit, space, IResult};
use std::str;
2018-02-17 14:06:43 +00:00
named!(parse_ints<Vec<i32>>, many0!(spaces_or_int));
2017-12-10 17:39:11 +00:00
fn spaces_or_int(input: &[u8]) -> IResult<&[u8], i32> {
println!("{}", input.to_hex(8));
2018-02-17 14:06:43 +00:00
do_parse!(
input,
opt!(complete!(space)) >> res: map!(complete!(digit), |x| {
println!("x: {:?}", x);
let result = str::from_utf8(x).unwrap();
println!("Result: {}", result);
println!("int is empty?: {}", x.is_empty());
2018-02-17 14:06:43 +00:00
match result.parse() {
Ok(i) => i,
2018-02-17 14:06:43 +00:00
Err(e) => panic!("UH OH! NOT A DIGIT! {:?}", e),
}
2018-02-17 14:06:43 +00:00
}) >> (res)
)
}
#[test]
2017-12-10 17:39:11 +00:00
fn issue_142() {
let subject = parse_ints(&b"12 34 5689a"[..]);
let expected = Ok((&b"a"[..], vec![12, 34, 5689]));
2017-12-10 17:39:11 +00:00
assert_eq!(subject, expected);
let subject = parse_ints(&b"12 34 5689 "[..]);
let expected = Ok((&b" "[..], vec![12, 34, 5689]));
assert_eq!(subject, expected)
}
}
2016-03-28 08:07:03 +00:00
#[test]
2017-12-10 17:39:11 +00:00
fn usize_length_bytes_issue() {
use nom::be_u16;
2017-12-10 17:39:11 +00:00
let _: IResult<&[u8], &[u8], u32> = length_bytes!(b"012346", be_u16);
2016-03-28 08:07:03 +00:00
}
/*
DOES NOT COMPILE
#[test]
fn issue_152() {
named!(take4, take!(4));
named!(xyz, tag!("XYZ"));
named!(abc, tag!("abc"));
named!(sw,
switch!(take4,
b"abcd" => xyz |
b"efgh" => abc
)
);
}
*/
#[test]
fn take_till_issue() {
2018-02-17 14:06:43 +00:00
named!(nothing, take_till!(call!(|_| true)));
assert_eq!(nothing(b""), Err(Err::Incomplete(Needed::Size(1))));
2017-12-10 17:39:11 +00:00
assert_eq!(nothing(b"abc"), Ok((&b"abc"[..], &b""[..])));
}
2018-02-17 14:06:43 +00:00
named!(
issue_498<Vec<&[u8]>>,
separated_nonempty_list!(opt!(space), tag!("abcd"))
);
named!(issue_308(&str) -> bool,
do_parse! (
tag_s! ("foo") >>
b: alt_complete! (
map! (tag_s! ("1"), |_: &str|->bool {true}) |
value! (false)
) >>
(b) ));
2018-02-05 22:18:40 +00:00
#[cfg(feature = "alloc")]
2017-12-10 17:39:11 +00:00
fn issue_302(input: &[u8]) -> IResult<&[u8], Option<Vec<u64>>> {
2018-02-17 14:06:43 +00:00
do_parse!(input, entries: cond!(true, count!(le_u64, 3)) >> (entries))
}
2018-01-15 11:07:51 +00:00
#[test]
fn issue_655() {
use nom::{line_ending, not_line_ending};
named!(twolines(&str) -> (&str, &str),
do_parse!(
l1 : not_line_ending >>
line_ending >>
l2 : not_line_ending >>
line_ending >>
((l1, l2))
)
);
assert_eq!(twolines("foo\nbar\n"), Ok(("", ("foo", "bar"))));
assert_eq!(twolines("féo\nbar\n"), Ok(("", ("féo", "bar"))));
assert_eq!(twolines("foé\nbar\n"), Ok(("", ("foé", "bar"))));
assert_eq!(twolines("foé\r\nbar\n"), Ok(("", ("foé", "bar"))));
}
2018-01-24 15:36:09 +00:00
#[cfg(feature = "std")]
2018-01-24 16:16:07 +00:00
named!(issue_666 <CompleteByteSlice, CompleteByteSlice>, dbg_dmp!(tag!("abc")));
#[test]
fn issue_667() {
use nom::alpha;
named!(foo <CompleteByteSlice, Vec<CompleteByteSlice>>,
many0!(
alt!(alpha | is_a!("_"))
)
);
2018-02-17 14:06:43 +00:00
assert_eq!(
foo(CompleteByteSlice(b"")),
Ok((CompleteByteSlice(b""), vec![]))
);
assert_eq!(
foo(CompleteByteSlice(b"loremipsum")),
Ok((
CompleteByteSlice(b""),
vec![CompleteByteSlice(b"loremipsum")]
))
);
assert_eq!(
foo(CompleteByteSlice(b"lorem_ipsum")),
Ok((
CompleteByteSlice(b""),
vec![
CompleteByteSlice(b"lorem"),
CompleteByteSlice(b"_"),
CompleteByteSlice(b"ipsum"),
]
))
);
assert_eq!(
foo(CompleteByteSlice(b"_lorem_ipsum")),
Ok((
CompleteByteSlice(b""),
vec![
CompleteByteSlice(b"_"),
CompleteByteSlice(b"lorem"),
CompleteByteSlice(b"_"),
CompleteByteSlice(b"ipsum"),
]
))
);
assert_eq!(
foo(CompleteByteSlice(b"!@#$")),
Ok((CompleteByteSlice(b"!@#$"), vec![]))
);
2018-01-24 16:16:07 +00:00
}
2018-03-21 11:03:49 +00:00
#[test]
fn issue_721() {
assert_eq!(parse_to!("1234", u16), Ok(("", 1234)));
assert_eq!(parse_to!("foo", String), Ok(("", "foo".to_string())));
2018-03-26 09:09:58 +00:00
}
named!(issue_717<&[u8], Vec<&[u8]> >,
separated_list!(tag!([0x0]), is_not!([0x0u8]))
);