[librsvg: 5/28] path_parser: test_parser(): Compare the expected results, both Ok and errors.
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 5/28] path_parser: test_parser(): Compare the expected results, both Ok and errors.
- Date: Tue, 23 Jan 2018 17:59:39 +0000 (UTC)
commit e2e86ac87b47cd88087d746f7b638389eed0c2da
Author: Federico Mena Quintero <federico gnome org>
Date: Tue Jan 23 09:43:55 2018 -0600
path_parser: test_parser(): Compare the expected results, both Ok and errors.
rust/src/path_parser.rs | 48 ++++++++++++++++++++++--------------------------
1 file changed, 22 insertions(+), 26 deletions(-)
---
diff --git a/rust/src/path_parser.rs b/rust/src/path_parser.rs
index 2e52c5d..094d2e0 100644
--- a/rust/src/path_parser.rs
+++ b/rust/src/path_parser.rs
@@ -878,13 +878,13 @@ fn char_to_digit (c: char) -> i32 {
c as i32 - '0' as i32
}
-#[derive(Debug)]
+#[derive(Debug, PartialEq)]
pub enum ErrorKind {
UnexpectedToken,
UnexpectedEof
}
-#[derive(Debug)]
+#[derive(Debug, PartialEq)]
pub struct ParseError {
pub position: usize,
pub kind: ErrorKind
@@ -915,39 +915,35 @@ pub fn parse_path_into_builder (path_str: &str, builder: &mut RsvgPathBuilder) -
mod tests {
use super::*;
- fn print_error (error: &ParseError, path_str: &str) {
- let prefix = "Error in \"";
-
- println! ("");
- println! ("{}{}\"", prefix, path_str);
-
- for _ in 0 .. (prefix.len() + error.position) {
- print! (" ");
- }
-
- println! ("^ {}", error);
+ fn find_error_pos(s: &str) -> Option<usize> {
+ s.find('^')
}
- fn parse_path (path_str: &str) -> RsvgPathBuilder {
- let mut builder = RsvgPathBuilder::new ();
-
- let result = parse_path_into_builder (path_str, &mut builder);
-
- if let Err (e) = result {
- print_error (&e, path_str);
+ fn make_parse_result(error_pos_str: &str, error_kind: Option<ErrorKind>) -> Result<(), ParseError> {
+ if let Some(pos) = find_error_pos(error_pos_str) {
+ Err(ParseError {
+ position: pos,
+ kind: error_kind.unwrap()
+ })
+ } else {
+ assert!(error_kind.is_none());
+ Ok(())
}
-
- builder
}
fn test_parser (path_str: &str,
- _error_pos_str: &str,
+ error_pos_str: &str,
expected_commands: &[PathCommand],
- _expected_error_kind: Option<ErrorKind>) {
- let builder = parse_path (path_str);
+ expected_error_kind: Option<ErrorKind>) {
+ let expected_result = make_parse_result(error_pos_str, expected_error_kind);
+
+ let mut builder = RsvgPathBuilder::new();
+ let result = parse_path_into_builder (path_str, &mut builder);
+
let commands = builder.get_path_commands ();
- assert_eq! (expected_commands, commands);
+ assert_eq!(expected_commands, commands);
+ assert_eq!(expected_result, result);
}
fn moveto (x: f64, y: f64) -> PathCommand {
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]