Skip to content
Myr

markdown.rs

This file

Path

src/export/markdown.rs

271 lines · 8.0 KiB · Rust

Last changed by

Simplify testDavid Peterbefore adoptione3bc5b8

History of this file

Take it

1use crate::export::markup::MarkupExporter;23use super::markup::Alignment;45#[derive(Default)]6pub struct MarkdownExporter {}78impl MarkupExporter for MarkdownExporter {9    fn table_row(&self, cells: &[&str]) -> String {10        format!("| {} |\n", cells.join(" | "))11    }1213    fn table_divider(&self, cell_aligmnents: &[Alignment]) -> String {14        format!(15            "|{}\n",16            cell_aligmnents17                .iter()18                .map(|a| match a {19                    Alignment::Left => ":---|",20                    Alignment::Right => "---:|",21                })22                .collect::<String>()23        )24    }2526    fn command(&self, cmd: &str) -> String {27        format!("`{}`", cmd)28    }29}3031/// Check Markdown-based data row formatting32#[test]33fn test_markdown_formatter_table_data() {34    let formatter = MarkdownExporter::default();3536    assert_eq!(formatter.table_row(&["a", "b", "c"]), "| a | b | c |\n");37}3839/// Check Markdown-based horizontal line formatting40#[test]41fn test_markdown_formatter_table_divider() {42    let formatter = MarkdownExporter::default();4344    let divider = formatter.table_divider(&[Alignment::Left, Alignment::Right, Alignment::Left]);45    assert_eq!(divider, "|:---|---:|:---|\n");46}4748/// Test helper function to create unit-based header and horizontal line49/// independently from the markup functionality for Markdown.50#[cfg(test)]51fn cfg_test_table_header(unit_short_name: String) -> String {52    format!(53        "| Command | Mean [{unit}] | Min [{unit}] | Max [{unit}] | Relative |\n|:---|---:|---:|---:|---:|\n",54        unit = unit_short_name55    )56}5758/// Ensure the markdown output includes the table header and the multiple59/// benchmark results as a table. The list of actual times is not included60/// in the output.61///62/// This also demonstrates that the first entry's units (ms) are used to set63/// the units for all entries when the time unit is not given.64#[test]65fn test_markdown_format_ms() {66    use super::Exporter;67    use crate::benchmark::benchmark_result::BenchmarkResult;68    use std::collections::BTreeMap;69    let exporter = MarkdownExporter::default();7071    let timing_results = vec![72        BenchmarkResult {73            command: String::from("sleep 0.1"),74            mean: 0.1057,75            stddev: Some(0.0016),76            median: 0.1057,77            user: 0.0009,78            system: 0.0011,79            min: 0.1023,80            max: 0.1080,81            times: Some(vec![0.1, 0.1, 0.1]),82            exit_codes: vec![Some(0), Some(0), Some(0)],83            parameters: BTreeMap::new(),84        },85        BenchmarkResult {86            command: String::from("sleep 2"),87            mean: 2.0050,88            stddev: Some(0.0020),89            median: 2.0050,90            user: 0.0009,91            system: 0.0012,92            min: 2.0020,93            max: 2.0080,94            times: Some(vec![2.0, 2.0, 2.0]),95            exit_codes: vec![Some(0), Some(0), Some(0)],96            parameters: BTreeMap::new(),97        },98    ];99100    let actual = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();101    let expect = format!(102        "{}\103| `sleep 0.1` | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |104| `sleep 2` | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |105",106        cfg_test_table_header("ms".to_string())107    );108109    assert_eq!(expect, actual);110}111112/// This (again) demonstrates that the first entry's units (s) are used to set113/// the units for all entries when the time unit is not given.114#[test]115fn test_markdown_format_s() {116    use super::Exporter;117    use crate::benchmark::benchmark_result::BenchmarkResult;118    use std::collections::BTreeMap;119    let exporter = MarkdownExporter::default();120121    let timing_results = vec![122        BenchmarkResult {123            command: String::from("sleep 2"),124            mean: 2.0050,125            stddev: Some(0.0020),126            median: 2.0050,127            user: 0.0009,128            system: 0.0012,129            min: 2.0020,130            max: 2.0080,131            times: Some(vec![2.0, 2.0, 2.0]),132            exit_codes: vec![Some(0), Some(0), Some(0)],133            parameters: BTreeMap::new(),134        },135        BenchmarkResult {136            command: String::from("sleep 0.1"),137            mean: 0.1057,138            stddev: Some(0.0016),139            median: 0.1057,140            user: 0.0009,141            system: 0.0011,142            min: 0.1023,143            max: 0.1080,144            times: Some(vec![0.1, 0.1, 0.1]),145            exit_codes: vec![Some(0), Some(0), Some(0)],146            parameters: BTreeMap::new(),147        },148    ];149150    let actual = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();151    let expect = format!(152        "{}\153| `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |154| `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |155",156        cfg_test_table_header("s".to_string())157    );158159    assert_eq!(expect, actual);160}161162/// The given time unit (s) is used to set the units for all entries.163#[test]164fn test_markdown_format_time_unit_s() {165    use super::Exporter;166    use crate::benchmark::benchmark_result::BenchmarkResult;167    use crate::util::units::Unit;168    use std::collections::BTreeMap;169    let exporter = MarkdownExporter::default();170171    let timing_results = vec![172        BenchmarkResult {173            command: String::from("sleep 0.1"),174            mean: 0.1057,175            stddev: Some(0.0016),176            median: 0.1057,177            user: 0.0009,178            system: 0.0011,179            min: 0.1023,180            max: 0.1080,181            times: Some(vec![0.1, 0.1, 0.1]),182            exit_codes: vec![Some(0), Some(0), Some(0)],183            parameters: BTreeMap::new(),184        },185        BenchmarkResult {186            command: String::from("sleep 2"),187            mean: 2.0050,188            stddev: Some(0.0020),189            median: 2.0050,190            user: 0.0009,191            system: 0.0012,192            min: 2.0020,193            max: 2.0080,194            times: Some(vec![2.0, 2.0, 2.0]),195            exit_codes: vec![Some(0), Some(0), Some(0)],196            parameters: BTreeMap::new(),197        },198    ];199200    let actual = String::from_utf8(201        exporter202            .serialize(&timing_results, Some(Unit::Second))203            .unwrap(),204    )205    .unwrap();206    let expect = format!(207        "{}\208| `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |209| `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |210",211        cfg_test_table_header("s".to_string())212    );213214    assert_eq!(expect, actual);215}216217/// This (again) demonstrates that the given time unit (ms) is used to set218/// the units for all entries.219#[test]220fn test_markdown_format_time_unit_ms() {221    use super::Exporter;222    use crate::benchmark::benchmark_result::BenchmarkResult;223    use crate::util::units::Unit;224    use std::collections::BTreeMap;225    let exporter = MarkdownExporter::default();226227    let timing_results = vec![228        BenchmarkResult {229            command: String::from("sleep 2"),230            mean: 2.0050,231            stddev: Some(0.0020),232            median: 2.0050,233            user: 0.0009,234            system: 0.0012,235            min: 2.0020,236            max: 2.0080,237            times: Some(vec![2.0, 2.0, 2.0]),238            exit_codes: vec![Some(0), Some(0), Some(0)],239            parameters: BTreeMap::new(),240        },241        BenchmarkResult {242            command: String::from("sleep 0.1"),243            mean: 0.1057,244            stddev: Some(0.0016),245            median: 0.1057,246            user: 0.0009,247            system: 0.0011,248            min: 0.1023,249            max: 0.1080,250            times: Some(vec![0.1, 0.1, 0.1]),251            exit_codes: vec![Some(0), Some(0), Some(0)],252            parameters: BTreeMap::new(),253        },254    ];255256    let actual = String::from_utf8(257        exporter258            .serialize(&timing_results, Some(Unit::MilliSecond))259            .unwrap(),260    )261    .unwrap();262    let expect = format!(263        "{}\264| `sleep 2` | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |265| `sleep 0.1` | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |266",267        cfg_test_table_header("ms".to_string())268    );269270    assert_eq!(expect, actual);271}