From 4fd07b240e18f7a8b1c3af5546e21a252a1c70aa Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Fri, 23 Jun 2023 00:10:41 -0400 Subject: [PATCH] Add tests for collections --- go-sgf/src/lib.rs | 85 ++++++++++++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 34 deletions(-) diff --git a/go-sgf/src/lib.rs b/go-sgf/src/lib.rs index bc24212..d3fc4bb 100644 --- a/go-sgf/src/lib.rs +++ b/go-sgf/src/lib.rs @@ -187,35 +187,40 @@ enum PropValue { Stone, } -pub fn parse_sgf(input: &str) -> Result { - let (_, tree) = parse_tree(input).finish()?; +pub fn parse_sgf(input: &str) -> Result, ParseError> { + let (_, trees) = parse_collection(input).finish()?; - let file_format = match tree.sequence[0].find_prop("FF") { - Some(prop) => prop.values[0].parse::().unwrap(), - None => 4, - }; - let app = tree.sequence[0] - .find_prop("AP") - .map(|prop| prop.values[0].clone()); - let board_size = match tree.sequence[0].find_prop("SZ") { - Some(prop) => { - let (_, size) = parse_size(prop.values[0].as_str()).finish()?; - size - } - None => Size { - width: 19, - height: 19, - }, - }; + trees + .into_iter() + .map(|tree| { + let file_format = match tree.sequence[0].find_prop("FF") { + Some(prop) => prop.values[0].parse::().unwrap(), + None => 4, + }; + let app = tree.sequence[0] + .find_prop("AP") + .map(|prop| prop.values[0].clone()); + let board_size = match tree.sequence[0].find_prop("SZ") { + Some(prop) => { + let (_, size) = parse_size(prop.values[0].as_str()).finish()?; + size + } + None => Size { + width: 19, + height: 19, + }, + }; - Ok(GameTree { - file_format, + Ok(GameTree { + file_format, - app, - game_type: GameType::Go, - board_size, - text: input.to_owned(), - }) + app, + game_type: GameType::Go, + board_size, + text: input.to_owned(), + }) + }) + .collect() } #[derive(Debug, PartialEq)] @@ -282,11 +287,15 @@ impl ToString for Property { } } +fn parse_collection(input: &str) -> IResult<&str, Vec> { + separated_list1(multispace0, parse_tree)(input) +} + // note: must preserve unknown properties // note: must fix or preserve illegally formatted game-info properties // note: must correct or delete illegally foramtted properties, but display a warning fn parse_tree(input: &str) -> IResult<&str, Tree> { - println!("parse_tree: {}", input); + println!(":: parse_tree: {}", input); let (input, _) = multispace0(input)?; delimited(tag("("), parse_sequence, tag(")"))(input) } @@ -307,7 +316,7 @@ fn parse_sequence(input: &str) -> IResult<&str, Tree> { } fn parse_node(input: &str) -> IResult<&str, Node> { - println!("parse_node: {}", input); + println!(":: parse_node: {}", input); let (input, _) = multispace0(input)?; let (input, _) = tag(";")(input)?; let (input, properties) = many1(parse_property)(input)?; @@ -315,7 +324,7 @@ fn parse_node(input: &str) -> IResult<&str, Node> { } fn parse_property(input: &str) -> IResult<&str, Property> { - println!("parse_property: {}", input); + println!(":: parse_property: {}", input); let (input, _) = multispace0(input)?; let (input, ident) = alpha1(input)?; let (input, values) = many1(delimited(tag("["), take_until("]"), tag("]")))(input)?; @@ -537,7 +546,6 @@ mod tests { #[test] fn it_can_regenerate_the_tree() { let (_, tree1) = parse_tree(EXAMPLE).unwrap(); - println!("{}", tree1.to_string()); assert_eq!( tree1.to_string(), "(;FF[4]C[root](;C[a];C[b](;C[c])(;C[d];C[e]))(;C[f](;C[g];C[h];C[i])(;C[j])))" @@ -546,11 +554,11 @@ mod tests { assert_eq!(tree1, tree2); } - fn with_text(text: &str, f: impl FnOnce(GameTree)) { + fn with_text(text: &str, f: impl FnOnce(Vec)) { f(parse_sgf(text).unwrap()); } - fn with_file(path: &std::path::Path, f: impl FnOnce(GameTree)) { + fn with_file(path: &std::path::Path, f: impl FnOnce(Vec)) { let mut file = File::open(path).unwrap(); let mut text = String::new(); let _ = file.read_to_string(&mut text); @@ -559,7 +567,9 @@ mod tests { #[test] fn it_parses_game_root() { - with_text(EXAMPLE, |tree| { + with_text(EXAMPLE, |trees| { + assert_eq!(trees.len(), 1); + let tree = &trees[0]; assert_eq!(tree.file_format, 4); assert_eq!(tree.app, None); assert_eq!(tree.game_type, GameType::Go); @@ -573,7 +583,9 @@ mod tests { assert_eq!(tree.text, EXAMPLE.to_owned()); }); - with_file(std::path::Path::new("test_data/print1.sgf"), |tree| { + with_file(std::path::Path::new("test_data/print1.sgf"), |trees| { + assert_eq!(trees.len(), 1); + let tree = &trees[0]; assert_eq!(tree.file_format, 4); assert_eq!(tree.app, None); assert_eq!(tree.game_type, GameType::Go); @@ -586,4 +598,9 @@ mod tests { ); }); } + + #[test] + fn it_parses_ff4_ex() { + with_file(std::path::Path::new("test_data/ff4_ex.sgf"), |tree| {}); + } }