1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
//! Macros for testing `nom` parsers //! //! Often when I'm testing `nom` parsers, I end up defining a lot of little //! macros like this, so I thought I would bundle them all up into a crate //! so I didn't have to define them over and over //! #![deny(missing_docs)] // <- this doesn't appear to check macros? /// Instead of importing the helper macros individually, this can be /// star-imported to get all of them pub mod prelude { pub use crate::{ assert_done, assert_finished, assert_done_and_eq, assert_finished_and_eq, assert_error, assert_error_and_eq, assert_needed, assert_needs, }; } #[macro_export] /// This macro checks to make sure that the IResult it is /// passed is `Done` /// /// # Examples /// /// ``` /// use nom_test_helpers::assert_done; /// use nom::{named, tag}; /// /// # fn main() { /// named!(abcd<&str, &str>, tag!("abcd")); /// let r = abcd("abcd"); /// assert_done!(r); /// # } /// ``` macro_rules! assert_done { ($e:expr) => { { if let ::std::result::Result::Ok((_, _)) = $e { assert!(true); } else { assert!(false, "parser did not complete"); } } } } #[macro_export] /// Internal only, really, and not even necessary :P macro_rules! assert_empty { ($v:expr) => { assert!($v.is_empty()) } } #[macro_export] /// This does the same thing as `assert_done!`, except that /// this also asserts that the input slice is empty /// /// # Examples /// /// ``` /// use nom_test_helpers::assert_finished; /// /// # fn main() { /// let r: nom::IResult<&str, &str> = Ok(("", "efgh")); /// assert_finished!(r); /// # } /// ``` macro_rules! assert_finished { ($e:expr) => { { if let ::std::result::Result::Ok((i, _)) = $e { $crate::assert_empty!(i); } else { assert!(false, "parser did not complete"); } } } } #[macro_export] /// This checks that the `IResult` is `Done`, and lets you /// check that the value returned as the `O` type of the /// `IResult` is equal to the second parameter /// /// # Examples /// /// ``` /// use nom_test_helpers::assert_done_and_eq; /// /// # fn main() { /// let r: nom::IResult<&[u8], &[u8]> = Ok((b"abcd", b"efgh")); /// assert_done_and_eq!(r, b"efgh"); /// # } /// ``` macro_rules! assert_done_and_eq { ($e:expr, $a:expr) => { { if let ::std::result::Result::Ok((_, o)) = $e { assert_eq!(o, $a); } else { assert!(false, "parser did not complete"); } } } } #[macro_export] /// Same as `assert_done_and_eq!`, but asserts that /// the input slice is empty /// /// # Examples /// /// ``` /// use nom_test_helpers::assert_finished_and_eq; /// /// # fn main() { /// let r: nom::IResult<&str, &str> = Ok(("", "sup")); /// assert_finished_and_eq!(r, "sup"); /// # } /// ``` macro_rules! assert_finished_and_eq { ($r:expr, $o:expr) => { { if let ::std::result::Result::Ok((i, o)) = $r { $crate::assert_empty!(i); assert_eq!(o, $o); } else { assert!(false, "parser did not complete"); } } } } #[macro_export] /// This asserts that the `IResult` is an `Err` /// /// # Examples /// /// ``` /// use nom_test_helpers::assert_error; /// /// # fn main() { /// let r: nom::IResult<&[u8], &[u8]> = Err(nom::Err::Error(nom::error::Error::new(&b""[..], nom::error::ErrorKind::Count))); /// assert_error!(r); /// # } macro_rules! assert_error { ($e:expr) => { { if let ::std::result::Result::Err(_) = $e { assert!(true); } else { assert!(false, "parser did not error"); } } } } #[macro_export] /// This asserts that the `IResult` is an `Err` and that the error /// is what is expected /// /// # Examples /// /// ``` /// use nom_test_helpers::assert_error_and_eq; /// /// # fn main() { /// let r: nom::IResult<&[u8], &[u8]> = Err(nom::Err::Error(nom::error::Error::new(&b""[..], nom::error::ErrorKind::Count))); /// assert_error_and_eq!(r, nom::Err::Error(nom::error::Error::new(&b""[..], nom::error::ErrorKind::Count))); /// # } macro_rules! assert_error_and_eq { ($r:expr, $err:expr) => { { if let ::std::result::Result::Err(err) = $r { assert_eq!(err, $err); } else { assert!(false, "parser did not error"); } } } } #[macro_export] /// This asserts that the `IResult` is an `Incomplete` /// /// # Examples /// /// ``` /// use std::num::NonZeroUsize; /// use nom_test_helpers::assert_needed; /// use nom::Needed; /// /// # fn main() { /// let r: nom::IResult<&[u8], &[u8]> = Err(nom::Err::Incomplete(Needed::Size(NonZeroUsize::new(1).unwrap()))); /// assert_needed!(r); /// # } /// ``` macro_rules! assert_needed { ($e:expr) => { { if let ::std::result::Result::Err(::nom::Err::Incomplete(..)) = $e { assert!(true); } else { assert!(false, "parser is not incomplete"); } } } } #[macro_export] /// This lets the user specify how much input the parser should need /// /// # Examples /// /// ``` /// use nom_test_helpers::assert_needs; /// use nom::Needed; /// /// # fn main() { /// let r: nom::IResult<&[u8], &[u8]> = Err(nom::Err::Incomplete(Needed::Unknown)); /// assert_needs!(r, ?); /// # } /// ``` /// /// ``` /// use std::num::NonZeroUsize; /// use nom_test_helpers::assert_needs; /// use nom::Needed; /// /// # fn main() { /// let r: nom::IResult<&[u8], &[u8]> = Err(nom::Err::Incomplete(Needed::Size(NonZeroUsize::new(2).unwrap()))); /// assert_needs!(r, 2usize); /// # } /// ``` macro_rules! assert_needs { ($e:expr, ? ) => { { if let ::std::result::Result::Err(::nom::Err::Incomplete(e)) = $e { if let ::nom::Needed::Unknown = e { assert!(true); } else { assert!(false, "parser is incomplete, but Needed is known"); } } else { assert!(false, "parser is not incomplete"); } } }; ($e:expr, $i:expr) => { { if let ::std::result::Result::Err(::nom::Err::Incomplete(e)) = $e { if let ::nom::Needed::Size(i) = e { assert_eq!($i, i.into()); } else { assert!(false, "parser is incomplete, but Needed is unknown"); } } else { assert!(false, "parser is not incomplete"); } } } }