From 5c60ef49a1067d112ca75f41f0c19ba9cc3d271d Mon Sep 17 00:00:00 2001 From: Sebastian Schneider Date: Mon, 25 Dec 2023 18:22:34 +0100 Subject: [PATCH] Add Test Case for Supported UMI --- cli/tests/umi.rs | 136 +++++++++++++++++++++++++++++++++++++++--- render/src/umi/mod.rs | 30 +++++----- 2 files changed, 144 insertions(+), 22 deletions(-) diff --git a/cli/tests/umi.rs b/cli/tests/umi.rs index 7e57a69f..0950756e 100644 --- a/cli/tests/umi.rs +++ b/cli/tests/umi.rs @@ -1,6 +1,11 @@ use std::path::PathBuf; -use unimarkup_core::{commons::config::Config, Unimarkup}; +use unimarkup_core::{ + commons::config::Config, + inline::element::{Inline, InlineElement}, + parser::{document::Document, elements::blocks::Block}, + Unimarkup, +}; fn compile_um(config: Config) -> Option { let source = std::fs::read_to_string(&config.input).ok()?; @@ -8,8 +13,125 @@ fn compile_um(config: Config) -> Option { Some(Unimarkup::parse(&source, config)) } +fn equals_inlines_output(input: &Vec, output: &Vec) -> bool { + assert_eq!( + input.len(), + output.len(), + "Parsed Inlines does not have the same number of elements" + ); + + let mut i = 0; + while i < output.len() { + assert_eq!( + input[i].as_unimarkup(), + output[i].as_unimarkup(), + "Inline contains wrong content" + ); + i += 1; + } + true +} + +fn equals_blocks_output(input: &Vec, output: &Vec) -> bool { + assert_eq!( + input.len(), + output.len(), + "Parsed Blocks does not have the same length as the input" + ); + + let mut i = 0; + while i < input.len() { + assert_eq!( + input[i].variant_str(), + output[i].variant_str(), + "Blocks did not match up at Index" + ); + let block_in = input[i].clone(); + let block_out = output[i].clone(); + match (block_in, block_out) { + (Block::Heading(block_in), Block::Heading(block_out)) => { + assert_eq!(block_in.id, block_out.id, "Heading ids do not match!"); + assert_eq!( + block_in.level, block_out.level, + "Heading Levels do not match!" + ); + assert!(equals_inlines_output(&block_in.content, &block_out.content)); + assert_eq!( + block_in.attributes, block_out.attributes, + "Heading Attributes do not match!" + ); + } + (Block::Paragraph(block_in), Block::Paragraph(block_out)) => { + assert!(equals_inlines_output(&block_in.content, &block_out.content)); + } + (Block::VerbatimBlock(block_in), Block::VerbatimBlock(block_out)) => { + assert_eq!( + block_in.content, block_out.content, + "Verbatim Content does not match" + ); + assert_eq!( + block_in.data_lang, block_out.data_lang, + "Verbatim Data_Lang does not match" + ); + assert_eq!( + block_in.attributes, block_out.attributes, + "Verbatim Attributes do not match" + ); + assert_eq!( + block_in.implicit_closed, block_out.implicit_closed, + "Verbatim Implicit_Closed does not match" + ); + assert_eq!( + block_in.tick_len, block_out.tick_len, + "Verbatim Tick-Len does not match" + ); + } + (Block::BulletList(block_in), Block::BulletList(block_out)) => { + assert_eq!( + block_in.entries.len(), + block_out.entries.len(), + "Bullet List entry count does not match" + ); + + let mut j = 0; + while j < block_in.entries.len() { + assert_eq!( + block_in.entries[j].keyword, block_out.entries[j].keyword, + "Bullet List Entry Keyword does not match" + ); + assert!(equals_inlines_output( + &block_in.entries[j].heading, + &block_out.entries[j].heading + )); + assert!(equals_blocks_output( + &block_in.entries[j].body, + &block_out.entries[j].body + )); + j += 1; + } + } + _ => return false, + } + + i += 1; + } + + true +} + +fn equals_umi_output(input: &Document, output: &Document) -> bool { + /* + assert_eq!( + input.config, output.config, + "Parsed UMI Config differs from original Config" + ); + */ + + equals_blocks_output(&input.blocks, &output.blocks) +} + #[test] -fn umi_loop() { +fn umi_supported() { let mut config = Config::default(); let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) .canonicalize() @@ -21,11 +143,11 @@ fn umi_loop() { let mut umi = um.render_umi().unwrap(); let workbook = umi.create_workbook(); - let looped_doc = workbook.create_um().map_err(|_| panic!()).unwrap(); + let looped_doc = &workbook.create_um().map_err(|_| panic!()).unwrap(); + let input = um.get_document(); - assert_eq!( - looped_doc.blocks.len(), - um.get_document().blocks.len(), - "Parsed UMI file differs from original UM." + assert!( + equals_umi_output(input, looped_doc), + "Output does not equal the Input" ); } diff --git a/render/src/umi/mod.rs b/render/src/umi/mod.rs index 9415e3a7..7aa78192 100644 --- a/render/src/umi/mod.rs +++ b/render/src/umi/mod.rs @@ -4,7 +4,9 @@ use std::path::PathBuf; use crate::render::OutputFormat; use crate::log_id::ParserError; -use spreadsheet_ods::{read_ods_buf, write_ods_buf_uncompressed, Sheet, WorkBook}; +use spreadsheet_ods::{ + read_ods_buf, write_ods_buf_uncompressed, Sheet, Value, ValueType, WorkBook, +}; use unimarkup_commons::config::output::Output; use unimarkup_commons::config::{Config, MergingConfig}; use unimarkup_commons::lexer::{ @@ -28,6 +30,14 @@ use unimarkup_parser::{ pub mod render; +fn unpack_content_safe(value: Value) -> String { + if value.value_type() == ValueType::Text { + value.as_str_or("").into() + } else { + value.as_cow_str_or("").into() + } +} + #[derive(Debug, Default, Clone)] pub struct UmiRow { position: u8, @@ -105,7 +115,7 @@ impl Umi { sheet.set_value(1, 2, "Preamble"); sheet.set_value( 1, - 4, + 5, serde_yaml::to_string(&self.config.preamble).unwrap_or_default(), ); @@ -167,7 +177,7 @@ impl Umi { current_line.position, )))?, content: self.read_inlines(current_line.content.clone()), - attributes: Some(current_line.attributes), + attributes: Some(current_line.attributes).filter(|s| !s.is_empty()), start: Position::new(1, 1), end: Position::new(1, 1), }; @@ -183,7 +193,7 @@ impl Umi { let verbatim = VerbatimBlock { content: current_line.content.clone(), data_lang: properties.get("data_lang").cloned(), - attributes: Some(current_line.attributes), + attributes: Some(current_line.attributes).filter(|s| !s.is_empty()), implicit_closed: properties .get("implicit_closed") .ok_or(ParserError::MissingProperty(( @@ -225,8 +235,6 @@ impl Umi { _ => break, }; bullet_list.entries.append(&mut vec![bullet_list_entry]); - } else { - break; } current_line_index += 1; @@ -279,8 +287,6 @@ impl Umi { // Append Element to Bullet List Entry Body let block = self.read_row(current_line_index)?; bullet_list_entry.body.append(&mut vec![block]); - } else { - break; } current_line_index += 1; @@ -341,13 +347,7 @@ impl Umi { .value .as_u8_opt() .unwrap_or(0), - sheet - .cell(row_index, 5) - .unwrap_or_default() - .value - .as_str_opt() - .unwrap_or_default() - .to_string(), + unpack_content_safe(sheet.cell(row_index, 5).unwrap_or_default().value), sheet .cell(row_index, 6) .unwrap_or_default()