Skip to content
Myr

orgmode.rs

This file

Path

src/export/orgmode.rs

169 lines · 4.8 KiB · Rust

Last changed by

Adapt to new markup exporter changesDavid Peterbefore adoptioneb0ccc8

History of this file

Take it

1use super::markup::Alignment;2use crate::export::markup::MarkupExporter;34#[derive(Default)]5pub struct OrgmodeExporter {}67impl MarkupExporter for OrgmodeExporter {8    fn table_row(&self, cells: &[&str]) -> String {9        format!(10            "| {}  |  {} |\n",11            cells.first().unwrap(),12            &cells[1..].join(" |  ")13        )14    }1516    fn table_divider(&self, cell_aligmnents: &[Alignment]) -> String {17        format!("|{}--|\n", "--+".repeat(cell_aligmnents.len() - 1))18    }1920    fn command(&self, cmd: &str) -> String {21        format!("={}=", cmd)22    }23}2425/// Check Emacs org-mode data row formatting26#[test]27fn test_orgmode_formatter_table_data() {28    let exporter = OrgmodeExporter::default();2930    let actual = exporter.table_row(&["a", "b", "c"]);31    let expect = "| a  |  b |  c |\n";3233    assert_eq!(expect, actual);34}3536/// Check Emacs org-mode horizontal line formatting37#[test]38fn test_orgmode_formatter_table_line() {39    let exporter = OrgmodeExporter::default();4041    let actual = exporter.table_divider(&[42        Alignment::Left,43        Alignment::Left,44        Alignment::Left,45        Alignment::Left,46        Alignment::Left,47    ]);48    let expect = "|--+--+--+--+--|\n";4950    assert_eq!(expect, actual);51}5253/// Test helper function to create unit-based header and horizontal line54/// independently from the markup functionality for Emacs org-mode.55#[cfg(test)]56fn cfg_test_table_header(unit_short_name: String) -> String {57    format!(58        "| Command  |  Mean [{unit}] |  Min [{unit}] |  Max [{unit}] |  Relative |\n|--+--+--+--+--|\n",59        unit = unit_short_name60    )61}6263/// Ensure the Emacs org-mode output includes the table header and the multiple64/// benchmark results as a table. The list of actual times is not included65/// in the output.66///67/// This also demonstrates that the first entry's units (ms) are used to set68/// the units for all entries when the time unit is not given.69#[test]70fn test_orgmode_format_ms() {71    use super::Exporter;72    use crate::benchmark::benchmark_result::BenchmarkResult;73    use std::collections::BTreeMap;7475    let exporter = OrgmodeExporter::default();7677    let results = vec![78        BenchmarkResult {79            command: String::from("sleep 0.1"),80            mean: 0.1057,81            stddev: Some(0.0016),82            median: 0.1057,83            user: 0.0009,84            system: 0.0011,85            min: 0.1023,86            max: 0.1080,87            times: Some(vec![0.1, 0.1, 0.1]),88            exit_codes: vec![Some(0), Some(0), Some(0)],89            parameters: BTreeMap::new(),90        },91        BenchmarkResult {92            command: String::from("sleep 2"),93            mean: 2.0050,94            stddev: Some(0.0020),95            median: 2.0050,96            user: 0.0009,97            system: 0.0012,98            min: 2.0020,99            max: 2.0080,100            times: Some(vec![2.0, 2.0, 2.0]),101            exit_codes: vec![Some(0), Some(0), Some(0)],102            parameters: BTreeMap::new(),103        },104    ];105106    let actual = String::from_utf8(exporter.serialize(&results, None).unwrap()).unwrap();107    let expect = format!(108        "{}\109| =sleep 0.1=  |  105.7 ± 1.6 |  102.3 |  108.0 |  1.00 |110| =sleep 2=  |  2005.0 ± 2.0 |  2002.0 |  2008.0 |  18.97 ± 0.29 |111",112        cfg_test_table_header("ms".to_string())113    );114115    assert_eq!(expect, actual);116}117118/// This test demonstrates that the given unit (s) is used to set119/// the units for all entries.120#[test]121fn test_orgmode_format_s() {122    use super::Exporter;123    use crate::benchmark::benchmark_result::BenchmarkResult;124    use crate::util::units::Unit;125    use std::collections::BTreeMap;126127    let exporter = OrgmodeExporter::default();128129    let results = vec![130        BenchmarkResult {131            command: String::from("sleep 2"),132            mean: 2.0050,133            stddev: Some(0.0020),134            median: 2.0050,135            user: 0.0009,136            system: 0.0012,137            min: 2.0020,138            max: 2.0080,139            times: Some(vec![2.0, 2.0, 2.0]),140            exit_codes: vec![Some(0), Some(0), Some(0)],141            parameters: BTreeMap::new(),142        },143        BenchmarkResult {144            command: String::from("sleep 0.1"),145            mean: 0.1057,146            stddev: Some(0.0016),147            median: 0.1057,148            user: 0.0009,149            system: 0.0011,150            min: 0.1023,151            max: 0.1080,152            times: Some(vec![0.1, 0.1, 0.1]),153            exit_codes: vec![Some(0), Some(0), Some(0)],154            parameters: BTreeMap::new(),155        },156    ];157158    let actual =159        String::from_utf8(exporter.serialize(&results, Some(Unit::Second)).unwrap()).unwrap();160    let expect = format!(161        "{}\162| =sleep 2=  |  2.005 ± 0.002 |  2.002 |  2.008 |  18.97 ± 0.29 |163| =sleep 0.1=  |  0.106 ± 0.002 |  0.102 |  0.108 |  1.00 |164",165        cfg_test_table_header("s".to_string())166    );167168    assert_eq!(expect, actual);169}