Skip to content

Commit

Permalink
GH-94: Add tests to table
Browse files Browse the repository at this point in the history
  • Loading branch information
CMDJojo committed Mar 10, 2023
1 parent 6aa4785 commit b8c28e2
Show file tree
Hide file tree
Showing 18 changed files with 1,285 additions and 12 deletions.
6 changes: 0 additions & 6 deletions packages/table/src/example.json

This file was deleted.

27 changes: 21 additions & 6 deletions packages/table/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ impl TryFrom<&str> for Borders {
// The struct holding a table. Since the text contained within holds pointers to stdin, we need
// lifetimes to avoid copying
#[derive(Debug)]
#[allow(dead_code)]
struct Table<'a> {
width: usize,
height: usize,
Expand All @@ -255,6 +256,8 @@ impl Table<'_> {
let mut vec: Vec<Value> = vec![];

let col_key = if self.width == 0 {
// If we have an empty table, we need some alignment still since otherwise
// we get an latex error
"|l|".to_string()
} else if self.borders == Borders::All || self.borders == Borders::Vertical {
self.alignment.latex_str(self.width, true)
Expand All @@ -267,11 +270,14 @@ impl Table<'_> {
vec.push(raw!("\\begin{center}\n"));
vec.push(raw!(format!("\\begin{{tabular}} {{ {} }}\n", col_key)));

// Only "None" borders should not have top row
if self.borders != Borders::None {
vec.push(raw!("\\hline\n"));
}

// Loop though all rows
for (idx, row) in self.content.iter().enumerate() {
// Collect all inline_content values, if heading add bold tags **
let values = if idx == 0 && self.header {
row.iter()
.map(|c| format!("**{c}**"))
Expand All @@ -283,6 +289,7 @@ impl Table<'_> {
.collect::<Vec<Value>>()
};

// For each cell in the row, push it and add & between, and \\\n to the end
for (idx, val) in values.into_iter().enumerate() {
if idx != 0 {
vec.push(raw!(" & "));
Expand All @@ -291,13 +298,16 @@ impl Table<'_> {
}
vec.push(raw!(" \\\\\n"));

// If we should have a border in-between all rows, add it
if self.borders == Borders::All || self.borders == Borders::Horizontal {
vec.push(raw!("\\hline\n"));
}
}

if self.borders == Borders::Outer {
vec.push(raw!(r"\hline"))
// Both horizontal and all already added this line, so it only needs to be
// added on outer and vertical
if self.borders == Borders::Outer || self.borders == Borders::Vertical {
vec.push(raw!("\\hline\n"))
}
vec.push(raw!("\\end{tabular}\n"));
vec.push(raw!(r"\end{center}"));
Expand All @@ -307,27 +317,31 @@ impl Table<'_> {
// Turns this table to HTML and gets a JSON value (containing mostly raw stuff) to return
fn to_html(&self) -> Value {
let mut vec: Vec<Value> = vec![];
// Push opening tag, border style on table if outer borders
if self.borders == Borders::None {
vec.push(raw!("<table>"));
} else {
vec.push(raw!(
r#"<table style="border: 1px solid black; border-collapse: collapse;""#
r#"<table style="border: 1px solid black; border-collapse: collapse;">"#
));
}

// Here is the style for all th/td elements
let inside_border_style = if self.borders == Borders::All {
"border: 1px solid black; border-collapse: collapse;"
" border: 1px solid black; border-collapse: collapse;"
} else if self.borders == Borders::Vertical {
"border-left: 1px solid black; border-right: 1px solid black; border-collapse: collapse;"
" border-left: 1px solid black; border-right: 1px solid black; border-collapse: collapse;"
} else if self.borders == Borders::Horizontal {
"border-top: 1px solid black; border-bottom: 1px solid black; border-collapse: collapse;"
" border-top: 1px solid black; border-bottom: 1px solid black; border-collapse: collapse;"
} else {
""
};

// Loop though each row
for (idx, row) in self.content.iter().enumerate() {
vec.push(raw!("<tr>"));

// If it is the header, use th, else use td
if idx == 0 && self.header {
for (idx, elem) in row.iter().enumerate() {
let alignment = self.alignment.for_column(idx).html_style();
Expand Down Expand Up @@ -357,6 +371,7 @@ impl Table<'_> {
}

// Parses the JSON input to a table, if possible. Warnings/errors are printed out when running this.
// Many of the arguments uses try_into() to optionally get a Border, Alignment etc
fn parse_table(input: &Value) -> Option<Table> {
let delimiter = input["arguments"]["delimiter"].as_str().unwrap();
if delimiter.contains('\\') {
Expand Down
87 changes: 87 additions & 0 deletions packages/table/tests/test_all_border_html.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"name": "table",
"arguments": {
"delimiter": "|",
"header": "none",
"alignment": "left",
"borders": "all",
"strip_whitespace": "true"
},
"data": "a | b \n c|d",
"inline": false,
"__test_transform_to": "html",
"__test_expected_result": [
{
"data": "<table style=\"border: 1px solid black; border-collapse: collapse;\">",
"name": "raw"
},
{
"data": "<tr>",
"name": "raw"
},
{
"data": "<td style=\"text-align: left; border: 1px solid black; border-collapse: collapse;\">",
"name": "raw"
},
{
"data": "a",
"name": "inline_content"
},
{
"data": "</td>",
"name": "raw"
},
{
"data": "<td style=\"text-align: left; border: 1px solid black; border-collapse: collapse;\">",
"name": "raw"
},
{
"data": "b",
"name": "inline_content"
},
{
"data": "</td>",
"name": "raw"
},
{
"data": "</tr>",
"name": "raw"
},
{
"data": "<tr>",
"name": "raw"
},
{
"data": "<td style=\"text-align: left; border: 1px solid black; border-collapse: collapse;\">",
"name": "raw"
},
{
"data": "c",
"name": "inline_content"
},
{
"data": "</td>",
"name": "raw"
},
{
"data": "<td style=\"text-align: left; border: 1px solid black; border-collapse: collapse;\">",
"name": "raw"
},
{
"data": "d",
"name": "inline_content"
},
{
"data": "</td>",
"name": "raw"
},
{
"data": "</tr>",
"name": "raw"
},
{
"data": "</table>",
"name": "raw"
}
]
}
75 changes: 75 additions & 0 deletions packages/table/tests/test_all_border_latex.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"name": "table",
"arguments": {
"delimiter": "|",
"header": "none",
"alignment": "left",
"borders": "all",
"strip_whitespace": "true"
},
"data": "a | b \n c|d",
"inline": false,
"__test_transform_to": "latex",
"__test_expected_result": [
{
"data": "\\begin{center}\n",
"name": "raw"
},
{
"data": "\\begin{tabular} { |l|l| }\n",
"name": "raw"
},
{
"data": "\\hline\n",
"name": "raw"
},
{
"data": "a",
"name": "inline_content"
},
{
"data": " & ",
"name": "raw"
},
{
"data": "b",
"name": "inline_content"
},
{
"data": " \\\\\n",
"name": "raw"
},
{
"data": "\\hline\n",
"name": "raw"
},
{
"data": "c",
"name": "inline_content"
},
{
"data": " & ",
"name": "raw"
},
{
"data": "d",
"name": "inline_content"
},
{
"data": " \\\\\n",
"name": "raw"
},
{
"data": "\\hline\n",
"name": "raw"
},
{
"data": "\\end{tabular}\n",
"name": "raw"
},
{
"data": "\\end{center}",
"name": "raw"
}
]
}
87 changes: 87 additions & 0 deletions packages/table/tests/test_heading_html.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"name": "table",
"arguments": {
"delimiter": "|",
"header": "bold",
"alignment": "left",
"borders": "none",
"strip_whitespace": "true"
},
"data": "a | b \n c|d",
"inline": false,
"__test_transform_to": "html",
"__test_expected_result": [
{
"data": "<table>",
"name": "raw"
},
{
"data": "<tr>",
"name": "raw"
},
{
"data": "<th style=\"text-align: left;\">",
"name": "raw"
},
{
"data": "a",
"name": "inline_content"
},
{
"data": "</th>",
"name": "raw"
},
{
"data": "<th style=\"text-align: left;\">",
"name": "raw"
},
{
"data": "b",
"name": "inline_content"
},
{
"data": "</th>",
"name": "raw"
},
{
"data": "</tr>",
"name": "raw"
},
{
"data": "<tr>",
"name": "raw"
},
{
"data": "<td style=\"text-align: left;\">",
"name": "raw"
},
{
"data": "c",
"name": "inline_content"
},
{
"data": "</td>",
"name": "raw"
},
{
"data": "<td style=\"text-align: left;\">",
"name": "raw"
},
{
"data": "d",
"name": "inline_content"
},
{
"data": "</td>",
"name": "raw"
},
{
"data": "</tr>",
"name": "raw"
},
{
"data": "</table>",
"name": "raw"
}
]
}
Loading

0 comments on commit b8c28e2

Please sign in to comment.