tagua_parser/rules/whitespaces.rs
1// Tagua VM
2//
3//
4// New BSD License
5//
6// Copyright © 2016-2016, Ivan Enderlin.
7// All rights reserved.
8//
9// Redistribution and use in source and binary forms, with or without
10// modification, are permitted provided that the following conditions are met:
11// * Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// * Redistributions in binary form must reproduce the above copyright
14// notice, this list of conditions and the following disclaimer in the
15// documentation and/or other materials provided with the distribution.
16// * Neither the name of the Hoa nor the names of its contributors may be
17// used to endorse or promote products derived from this software without
18// specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
24// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30// POSSIBILITY OF SUCH DAMAGE.
31
32//! Group of white space rules.
33//!
34//! The list of all white spaces is provided by the PHP Language Specification
35//! in the [Grammar chapter, White Space
36//! section](https://github.com/php/php-langspec/blob/master/spec/19-grammar.md#white-space).
37
38named!(
39 pub whitespace,
40 is_a!(" \t\n\r")
41);
42
43
44#[cfg(test)]
45mod tests {
46 use super::super::super::internal::{
47 Error,
48 ErrorKind,
49 Result
50 };
51 use super::whitespace;
52
53 #[test]
54 fn case_whitespace_space() {
55 assert_eq!(whitespace(b" "), Result::Done(&b""[..], &b" "[..]));
56 }
57
58 #[test]
59 fn case_whitespace_horizontal_tabulation() {
60 assert_eq!(whitespace(b"\t\t\t"), Result::Done(&b""[..], &b"\t\t\t"[..]));
61 }
62
63 #[test]
64 fn case_whitespace_carriage_return_line_feed() {
65 assert_eq!(whitespace(b"\r\n\r\n\r\n"), Result::Done(&b""[..], &b"\r\n\r\n\r\n"[..]));
66 }
67
68 #[test]
69 fn case_whitespace_carriage_return() {
70 assert_eq!(whitespace(b"\r\r\r"), Result::Done(&b""[..], &b"\r\r\r"[..]));
71 }
72
73 #[test]
74 fn case_whitespace_line_feed() {
75 assert_eq!(whitespace(b"\n\n\n"), Result::Done(&b""[..], &b"\n\n\n"[..]));
76 }
77
78 #[test]
79 fn case_whitespace_mixed() {
80 assert_eq!(whitespace(b"\n \n \r\t \t\r\n\t \t\t"), Result::Done(&b""[..], &b"\n \n \r\t \t\r\n\t \t\t"[..]));
81 }
82
83 #[test]
84 fn case_whitespace_with_a_tail() {
85 assert_eq!(whitespace(b"\n \n \r\t \t\r\n\t \t\tabc "), Result::Done(&b"abc "[..], &b"\n \n \r\t \t\r\n\t \t\t"[..]));
86 }
87
88 #[test]
89 fn case_whitespace_too_short() {
90 assert_eq!(whitespace(b""), Result::Done(&b""[..], &b""[..]));
91 }
92
93 #[test]
94 fn case_invalid_whitespace_not_a_valid_whitespace() {
95 assert_eq!(whitespace(b"\xa0 "), Result::Error(Error::Position(ErrorKind::IsA, &b"\xa0 "[..])));
96 }
97
98 #[test]
99 fn case_invalid_whitespace_not_a_valid_character() {
100 assert_eq!(whitespace(b"abc\n \t"), Result::Error(Error::Position(ErrorKind::IsA, &b"abc\n \t"[..])));
101 }
102}