Skip to content

Commit

Permalink
Add Test Case for Supported UMI
Browse files Browse the repository at this point in the history
  • Loading branch information
SeppFS committed Dec 25, 2023
1 parent 076adb3 commit 5c60ef4
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 22 deletions.
136 changes: 129 additions & 7 deletions cli/tests/umi.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,137 @@
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<Unimarkup> {
let source = std::fs::read_to_string(&config.input).ok()?;

Some(Unimarkup::parse(&source, config))
}

fn equals_inlines_output(input: &Vec<Inline>, output: &Vec<Inline>) -> 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<Block>, output: &Vec<Block>) -> 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()
Expand All @@ -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"
);
}
30 changes: 15 additions & 15 deletions render/src/umi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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,
Expand Down Expand Up @@ -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(),
);

Expand Down Expand Up @@ -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),
};
Expand All @@ -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((
Expand Down Expand Up @@ -225,8 +235,6 @@ impl Umi {
_ => break,
};
bullet_list.entries.append(&mut vec![bullet_list_entry]);
} else {
break;
}

current_line_index += 1;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 5c60ef4

Please sign in to comment.