Skip to content
Myr

Use insta for exporter tests

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Peter · committed by David Peter · parent 6556b2b · Browse files · 5 files+237 added−273 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+237 added−273 removed

  • other:+237 added−273 removed

5 files+237 added−273 removed

modified:src/export/markdown.rs-273 removed
26 unmodified lines
27 format!("`{cmd}`")28 }29}3031#[cfg(test)]32use crate::options::SortOrder;3331/// Check Markdown-based data row formatting32#[test]33fn test_markdown_formatter_table_data() {34 let formatter = MarkdownExporter::default();
8 unmodified lines
4344 let divider = formatter.table_divider(&[Alignment::Left, Alignment::Right, Alignment::Left]);45 assert_eq!(divider, "|:---|---:|:---|\n");46}5051/// Test helper function to create unit-based header and horizontal line52/// independently from the markup functionality for Markdown.53#[cfg(test)]54fn cfg_test_table_header(unit_short_name: String) -> String {55 format!(56 "| Command | Mean [{unit_short_name}] | Min [{unit_short_name}] | Max [{unit_short_name}] | Relative |\n|:---|---:|---:|---:|---:|\n"57 )58}5960/// Ensure the markdown output includes the table header and the multiple61/// benchmark results as a table. The list of actual times is not included62/// in the output.63///64/// This also demonstrates that the first entry's units (ms) are used to set65/// the units for all entries when the time unit is not given.66#[test]67fn test_markdown_format_ms() {68 use super::Exporter;69 use crate::benchmark::benchmark_result::BenchmarkResult;70 use std::collections::BTreeMap;71 let exporter = MarkdownExporter::default();7273 let timing_results = vec![74 BenchmarkResult {75 command: String::from("sleep 0.1"),76 command_with_unused_parameters: String::from("sleep 0.1"),77 mean: 0.1057,78 stddev: Some(0.0016),79 median: 0.1057,80 user: 0.0009,81 system: 0.0011,82 min: 0.1023,83 max: 0.1080,84 times: Some(vec![0.1, 0.1, 0.1]),85 memory_usage_byte: None,86 exit_codes: vec![Some(0), Some(0), Some(0)],87 parameters: BTreeMap::new(),88 },89 BenchmarkResult {90 command: String::from("sleep 2"),91 command_with_unused_parameters: String::from("sleep 2"),92 mean: 2.0050,93 stddev: Some(0.0020),94 median: 2.0050,95 user: 0.0009,96 system: 0.0012,97 min: 2.0020,98 max: 2.0080,99 times: Some(vec![2.0, 2.0, 2.0]),100 memory_usage_byte: None,101 exit_codes: vec![Some(0), Some(0), Some(0)],102 parameters: BTreeMap::new(),103 },104 ];105106 let actual = String::from_utf8(107 exporter108 .serialize(&timing_results, None, SortOrder::Command)109 .unwrap(),110 )111 .unwrap();112 let expect = format!(113 "{}\114| `sleep 0.1` | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |115| `sleep 2` | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |116",117 cfg_test_table_header("ms".to_string())118 );119120 assert_eq!(expect, actual);121}122123/// This (again) demonstrates that the first entry's units (s) are used to set124/// the units for all entries when the time unit is not given.125#[test]126fn test_markdown_format_s() {127 use super::Exporter;128 use crate::benchmark::benchmark_result::BenchmarkResult;129 use std::collections::BTreeMap;130 let exporter = MarkdownExporter::default();131132 let timing_results = vec![133 BenchmarkResult {134 command: String::from("sleep 2"),135 command_with_unused_parameters: String::from("sleep 2"),136 mean: 2.0050,137 stddev: Some(0.0020),138 median: 2.0050,139 user: 0.0009,140 system: 0.0012,141 min: 2.0020,142 max: 2.0080,143 times: Some(vec![2.0, 2.0, 2.0]),144 memory_usage_byte: None,145 exit_codes: vec![Some(0), Some(0), Some(0)],146 parameters: BTreeMap::new(),147 },148 BenchmarkResult {149 command: String::from("sleep 0.1"),150 command_with_unused_parameters: String::from("sleep 0.1"),151 mean: 0.1057,152 stddev: Some(0.0016),153 median: 0.1057,154 user: 0.0009,155 system: 0.0011,156 min: 0.1023,157 max: 0.1080,158 times: Some(vec![0.1, 0.1, 0.1]),159 memory_usage_byte: None,160 exit_codes: vec![Some(0), Some(0), Some(0)],161 parameters: BTreeMap::new(),162 },163 ];164165 let actual = String::from_utf8(166 exporter167 .serialize(&timing_results, None, SortOrder::Command)168 .unwrap(),169 )170 .unwrap();171 let expect = format!(172 "{}\173| `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |174| `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |175",176 cfg_test_table_header("s".to_string())177 );178179 assert_eq!(expect, actual);180}181182/// The given time unit (s) is used to set the units for all entries.183#[test]184fn test_markdown_format_time_unit_s() {185 use super::Exporter;186 use crate::benchmark::benchmark_result::BenchmarkResult;187 use crate::util::units::Unit;188 use std::collections::BTreeMap;189 let exporter = MarkdownExporter::default();190191 let timing_results = vec![192 BenchmarkResult {193 command: String::from("sleep 2"),194 command_with_unused_parameters: String::from("sleep 2"),195 mean: 2.0050,196 stddev: Some(0.0020),197 median: 2.0050,198 user: 0.0009,199 system: 0.0012,200 min: 2.0020,201 max: 2.0080,202 times: Some(vec![2.0, 2.0, 2.0]),203 memory_usage_byte: None,204 exit_codes: vec![Some(0), Some(0), Some(0)],205 parameters: BTreeMap::new(),206 },207 BenchmarkResult {208 command: String::from("sleep 0.1"),209 command_with_unused_parameters: String::from("sleep 0.1"),210 mean: 0.1057,211 stddev: Some(0.0016),212 median: 0.1057,213 user: 0.0009,214 system: 0.0011,215 min: 0.1023,216 max: 0.1080,217 times: Some(vec![0.1, 0.1, 0.1]),218 memory_usage_byte: None,219 exit_codes: vec![Some(0), Some(0), Some(0)],220 parameters: BTreeMap::new(),221 },222 ];223224 {225 let actual = String::from_utf8(226 exporter227 .serialize(&timing_results, Some(Unit::Second), SortOrder::Command)228 .unwrap(),229 )230 .unwrap();231 let expect = format!(232 "{}\233| `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |234| `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |235",236 cfg_test_table_header("s".to_string())237 );238239 assert_eq!(expect, actual);240 }241242 {243 let actual = String::from_utf8(244 exporter245 .serialize(&timing_results, Some(Unit::Second), SortOrder::MeanTime)246 .unwrap(),247 )248 .unwrap();249 let expect = format!(250 "{}\251| `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |252| `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |253",254 cfg_test_table_header("s".to_string())255 );256257 assert_eq!(expect, actual);258 }259}260261/// This (again) demonstrates that the given time unit (ms) is used to set262/// the units for all entries.263#[test]264fn test_markdown_format_time_unit_ms() {265 use super::Exporter;266 use crate::benchmark::benchmark_result::BenchmarkResult;267 use crate::util::units::Unit;268 use std::collections::BTreeMap;269 let exporter = MarkdownExporter::default();270271 let timing_results = vec![272 BenchmarkResult {273 command: String::from("sleep 2"),274 command_with_unused_parameters: String::from("sleep 2"),275 mean: 2.0050,276 stddev: Some(0.0020),277 median: 2.0050,278 user: 0.0009,279 system: 0.0012,280 min: 2.0020,281 max: 2.0080,282 times: Some(vec![2.0, 2.0, 2.0]),283 memory_usage_byte: None,284 exit_codes: vec![Some(0), Some(0), Some(0)],285 parameters: BTreeMap::new(),286 },287 BenchmarkResult {288 command: String::from("sleep 0.1"),289 command_with_unused_parameters: String::from("sleep 0.1"),290 mean: 0.1057,291 stddev: Some(0.0016),292 median: 0.1057,293 user: 0.0009,294 system: 0.0011,295 min: 0.1023,296 max: 0.1080,297 times: Some(vec![0.1, 0.1, 0.1]),298 memory_usage_byte: None,299 exit_codes: vec![Some(0), Some(0), Some(0)],300 parameters: BTreeMap::new(),301 },302 ];303304 let actual = String::from_utf8(305 exporter306 .serialize(&timing_results, Some(Unit::MilliSecond), SortOrder::Command)307 .unwrap(),308 )309 .unwrap();310 let expect = format!(311 "{}\312| `sleep 2` | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |313| `sleep 0.1` | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |314",315 cfg_test_table_header("ms".to_string())316 );317318 assert_eq!(expect, actual);319}
modified:src/export/mod.rs+2 added
5 unmodified lines
6mod json;7mod markdown;8mod markup;9mod orgmode;10#[cfg(test)]11mod tests;1213use self::asciidoc::AsciidocExporter;14use self::csv::CsvExporter;15use self::json::JsonExporter;
144 unmodified lines
added:src/export/tests.rs+204 added
1use super::Exporter;2use crate::benchmark::benchmark_result::BenchmarkResult;3use crate::util::units::Unit;4use crate::{export::markdown::MarkdownExporter, options::SortOrder};5use std::collections::BTreeMap;67fn get_markdown_output(8 results: &[BenchmarkResult],9 unit: Option<Unit>,10 sort_order: SortOrder,11) -> String {12 let exporter = MarkdownExporter::default();13 String::from_utf8(exporter.serialize(results, unit, sort_order).unwrap()).unwrap()14}1516/// Ensure the markdown output includes the table header and the multiple17/// benchmark results as a table. The list of actual times is not included18/// in the output.19///20/// This also demonstrates that the first entry's units (ms) are used to set21/// the units for all entries when the time unit is not given.22#[test]23fn test_markdown_format_ms() {24 let results = [25 BenchmarkResult {26 command: String::from("sleep 0.1"),27 command_with_unused_parameters: String::from("sleep 0.1"),28 mean: 0.1057,29 stddev: Some(0.0016),30 median: 0.1057,31 user: 0.0009,32 system: 0.0011,33 min: 0.1023,34 max: 0.1080,35 times: Some(vec![0.1, 0.1, 0.1]),36 memory_usage_byte: None,37 exit_codes: vec![Some(0), Some(0), Some(0)],38 parameters: BTreeMap::new(),39 },40 BenchmarkResult {41 command: String::from("sleep 2"),42 command_with_unused_parameters: String::from("sleep 2"),43 mean: 2.0050,44 stddev: Some(0.0020),45 median: 2.0050,46 user: 0.0009,47 system: 0.0012,48 min: 2.0020,49 max: 2.0080,50 times: Some(vec![2.0, 2.0, 2.0]),51 memory_usage_byte: None,52 exit_codes: vec![Some(0), Some(0), Some(0)],53 parameters: BTreeMap::new(),54 },55 ];5657 insta::assert_snapshot!(get_markdown_output(&results, None, SortOrder::Command), @r#"58 | Command | Mean [ms] | Min [ms] | Max [ms] | Relative |59 |:---|---:|---:|---:|---:|60 | `sleep 0.1` | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |61 | `sleep 2` | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |62 "#);63}6465/// This (again) demonstrates that the first entry's units (s) are used to set66/// the units for all entries when the time unit is not given.67#[test]68fn test_markdown_format_s() {69 let results = [70 BenchmarkResult {71 command: String::from("sleep 2"),72 command_with_unused_parameters: String::from("sleep 2"),73 mean: 2.0050,74 stddev: Some(0.0020),75 median: 2.0050,76 user: 0.0009,77 system: 0.0012,78 min: 2.0020,79 max: 2.0080,80 times: Some(vec![2.0, 2.0, 2.0]),81 memory_usage_byte: None,82 exit_codes: vec![Some(0), Some(0), Some(0)],83 parameters: BTreeMap::new(),84 },85 BenchmarkResult {86 command: String::from("sleep 0.1"),87 command_with_unused_parameters: String::from("sleep 0.1"),88 mean: 0.1057,89 stddev: Some(0.0016),90 median: 0.1057,91 user: 0.0009,92 system: 0.0011,93 min: 0.1023,94 max: 0.1080,95 times: Some(vec![0.1, 0.1, 0.1]),96 memory_usage_byte: None,97 exit_codes: vec![Some(0), Some(0), Some(0)],98 parameters: BTreeMap::new(),99 },100 ];101102 insta::assert_snapshot!(get_markdown_output(&results, None, SortOrder::Command), @r#"103 | Command | Mean [s] | Min [s] | Max [s] | Relative |104 |:---|---:|---:|---:|---:|105 | `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |106 | `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |107 "#);108}109110/// The given time unit (s) is used to set the units for all entries.111#[test]112fn test_markdown_format_time_unit_s() {113 let results = [114 BenchmarkResult {115 command: String::from("sleep 2"),116 command_with_unused_parameters: String::from("sleep 2"),117 mean: 2.0050,118 stddev: Some(0.0020),119 median: 2.0050,120 user: 0.0009,121 system: 0.0012,122 min: 2.0020,123 max: 2.0080,124 times: Some(vec![2.0, 2.0, 2.0]),125 memory_usage_byte: None,126 exit_codes: vec![Some(0), Some(0), Some(0)],127 parameters: BTreeMap::new(),128 },129 BenchmarkResult {130 command: String::from("sleep 0.1"),131 command_with_unused_parameters: String::from("sleep 0.1"),132 mean: 0.1057,133 stddev: Some(0.0016),134 median: 0.1057,135 user: 0.0009,136 system: 0.0011,137 min: 0.1023,138 max: 0.1080,139 times: Some(vec![0.1, 0.1, 0.1]),140 memory_usage_byte: None,141 exit_codes: vec![Some(0), Some(0), Some(0)],142 parameters: BTreeMap::new(),143 },144 ];145146 insta::assert_snapshot!(get_markdown_output(&results, Some(Unit::Second), SortOrder::Command), @r#"147 | Command | Mean [s] | Min [s] | Max [s] | Relative |148 |:---|---:|---:|---:|---:|149 | `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |150 | `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |151 "#);152153 insta::assert_snapshot!(get_markdown_output(&results, Some(Unit::Second), SortOrder::MeanTime), @r#"154 | Command | Mean [s] | Min [s] | Max [s] | Relative |155 |:---|---:|---:|---:|---:|156 | `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |157 | `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |158 "#);159}160161/// This (again) demonstrates that the given time unit (ms) is used to set162/// the units for all entries.163#[test]164fn test_markdown_format_time_unit_ms() {165 let timing_results = [166 BenchmarkResult {167 command: String::from("sleep 2"),168 command_with_unused_parameters: String::from("sleep 2"),169 mean: 2.0050,170 stddev: Some(0.0020),171 median: 2.0050,172 user: 0.0009,173 system: 0.0012,174 min: 2.0020,175 max: 2.0080,176 times: Some(vec![2.0, 2.0, 2.0]),177 memory_usage_byte: None,178 exit_codes: vec![Some(0), Some(0), Some(0)],179 parameters: BTreeMap::new(),180 },181 BenchmarkResult {182 command: String::from("sleep 0.1"),183 command_with_unused_parameters: String::from("sleep 0.1"),184 mean: 0.1057,185 stddev: Some(0.0016),186 median: 0.1057,187 user: 0.0009,188 system: 0.0011,189 min: 0.1023,190 max: 0.1080,191 times: Some(vec![0.1, 0.1, 0.1]),192 memory_usage_byte: None,193 exit_codes: vec![Some(0), Some(0), Some(0)],194 parameters: BTreeMap::new(),195 },196 ];197198 insta::assert_snapshot!(get_markdown_output(&timing_results, Some(Unit::MilliSecond), SortOrder::Command), @r#"199 | Command | Mean [ms] | Min [ms] | Max [ms] | Relative |200 |:---|---:|---:|---:|---:|201 | `sleep 2` | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |202 | `sleep 0.1` | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |203 "#);204}
modified:Cargo.lock+26 added
420 unmodified lines
421 "clap_complete",422 "colored",423 "csv",424 "indicatif",425 "insta",426 "libc",427 "nix",428 "once_cell",429 "predicates",
31 unmodified lines
461 "unicode-width",462]463464[[package]]465name = "insta"466version = "1.41.1"467source = "registry+https://github.com/rust-lang/crates.io-index"468checksum = "7e9ffc4d4892617c50a928c52b2961cb5174b6fc6ebf252b2fac9d21955c48b8"469dependencies = [470 "console",471 "lazy_static",472 "linked-hash-map",473 "serde",474 "similar",475]476477[[package]]478name = "instant"479version = "0.1.13"480source = "registry+https://github.com/rust-lang/crates.io-index"481checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222"
25 unmodified lines
507source = "registry+https://github.com/rust-lang/crates.io-index"508checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398"509510[[package]]511name = "linked-hash-map"512version = "0.5.6"513source = "registry+https://github.com/rust-lang/crates.io-index"514checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"515516[[package]]517name = "linux-raw-sys"518version = "0.4.14"519source = "registry+https://github.com/rust-lang/crates.io-index"520checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89"
505 unmodified lines
1026source = "registry+https://github.com/rust-lang/crates.io-index"1027checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e"10281029[[package]]1030name = "similar"1031version = "2.6.0"1032source = "registry+https://github.com/rust-lang/crates.io-index"1033checksum = "1de1d4f81173b03af4c0cbed3c898f6bff5b870e4a7f5d6f4057d62a7a4b686e"10341035[[package]]1036name = "statistical"1037version = "1.0.0"1038source = "registry+https://github.com/rust-lang/crates.io-index"1039checksum = "49d57902bb128e5e38b5218d3681215ae3e322d99f65d5420e9849730d2ea372"
373 unmodified lines
modified:Cargo.toml+5 added