use crate::*;
#[test]
fn count_zero_empty() {
let parser = is("hello");
let tokens: Vec<&str> = vec![];
let mut input = Input::new(tokens);
let parser = count(&parser, 0);
let output = parser(&mut input);
assert_eq!(output, Ok(vec![]));
}
#[test]
fn count_0_not_empty() {
let parser = is("hello");
let tokens: Vec<&str> = vec!["other"];
let mut input = Input::new(tokens);
let parser = count(&parser, 0);
let output = parser(&mut input);
assert_eq!(output, Ok(vec![]));
}
#[test]
fn count_all_same() {
let parser = is("hello");
let repeat_count: usize = 500;
let tokens: Vec<_> = std::iter::repeat_n("hello", repeat_count).collect();
let mut input = Input::new(tokens);
let parser = count(&parser, repeat_count);
let output = parser(&mut input).unwrap();
assert_eq!(output.len(), repeat_count); assert!(output.iter().all(|x| *x == "hello")); assert!(end_of_input()(&mut input).is_ok()); }
#[test]
fn count_one_different() {
let parser = is("hello");
let repeat_count: usize = 500;
let mut tokens: Vec<_> = std::iter::repeat_n("hello", repeat_count).collect();
tokens.push("other");
let mut tail: Vec<_> = std::iter::repeat_n("hello", repeat_count).collect();
tokens.append(&mut tail);
let mut input = Input::new(tokens);
let parser = count(&parser, repeat_count);
let output = parser(&mut input).unwrap();
assert!(output.iter().all(|x| *x == "hello")); assert_eq!(input.consumed_count(), repeat_count); assert_eq!(input.next_token(), Some("other")); }
#[test]
fn count_not_enough() {
let parser = is("hello");
let mut tokens: Vec<_> = std::iter::repeat_n("hello", 3).collect();
tokens.push("other");
tokens.push("hello");
let mut input = Input::new(tokens);
let parser = count(&parser, 4); let output = parser(&mut input);
assert_eq!(output, Err(Error::UnexpectedToken));
}