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
Provenance
- Commit
7a9d04d- Tree
eb9a175- Author
David Peter <mail@david-peter.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
6556b2b
5 files changed+237 added−273 removed
modified:src/export/markdown.rs-273 removed
26 unmodified lines27 format!("`{cmd}`")28 }29 }30 31-#[cfg(test)]32-use crate::options::SortOrder;33-31 /// Check Markdown-based data row formatting32 #[test]33 fn test_markdown_formatter_table_data() {34 let formatter = MarkdownExporter::default();8 unmodified lines43 44 let divider = formatter.table_divider(&[Alignment::Left, Alignment::Right, Alignment::Left]);45 assert_eq!(divider, "|:---|---:|:---|\n");46 }50-51-/// Test helper function to create unit-based header and horizontal line52-/// independently from the markup functionality for Markdown.53-#[cfg(test)]54-fn 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-}59-60-/// 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]67-fn 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();72-73- 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- ];105-106- 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- );119-120- assert_eq!(expect, actual);121-}122-123-/// 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]126-fn 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();131-132- 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- ];164-165- 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- );178-179- assert_eq!(expect, actual);180-}181-182-/// The given time unit (s) is used to set the units for all entries.183-#[test]184-fn 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();190-191- 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- ];223-224- {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- );238-239- assert_eq!(expect, actual);240- }241-242- {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- );256-257- assert_eq!(expect, actual);258- }259-}260-261-/// This (again) demonstrates that the given time unit (ms) is used to set262-/// the units for all entries.263-#[test]264-fn 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();270-271- 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- ];303-304- 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- );317-318- assert_eq!(expect, actual);319-}modified:src/export/mod.rs+2 added
5 unmodified lines6 mod json;7 mod markdown;8 mod markup;9 mod orgmode;10+#[cfg(test)]11+mod tests;12 13 use self::asciidoc::AsciidocExporter;14 use self::csv::CsvExporter;15 use self::json::JsonExporter;144 unmodified linesadded:src/export/tests.rs+204 added
1+use super::Exporter;2+use crate::benchmark::benchmark_result::BenchmarkResult;3+use crate::util::units::Unit;4+use crate::{export::markdown::MarkdownExporter, options::SortOrder};5+use std::collections::BTreeMap;6+7+fn 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+}15+16+/// 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]23+fn 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+ ];56+57+ 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+}64+65+/// 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]68+fn 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+ ];101+102+ 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+}109+110+/// The given time unit (s) is used to set the units for all entries.111+#[test]112+fn 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+ ];145+146+ 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+ "#);152+153+ 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+}160+161+/// This (again) demonstrates that the given time unit (ms) is used to set162+/// the units for all entries.163+#[test]164+fn 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+ ];197+198+ 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 lines421 "clap_complete",422 "colored",423 "csv",424 "indicatif",425+ "insta",426 "libc",427 "nix",428 "once_cell",429 "predicates",31 unmodified lines461 "unicode-width",462 ]463 464 [[package]]465+name = "insta"466+version = "1.41.1"467+source = "registry+https://github.com/rust-lang/crates.io-index"468+checksum = "7e9ffc4d4892617c50a928c52b2961cb5174b6fc6ebf252b2fac9d21955c48b8"469+dependencies = [470+ "console",471+ "lazy_static",472+ "linked-hash-map",473+ "serde",474+ "similar",475+]476+477+[[package]]478 name = "instant"479 version = "0.1.13"480 source = "registry+https://github.com/rust-lang/crates.io-index"481 checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222"25 unmodified lines507 source = "registry+https://github.com/rust-lang/crates.io-index"508 checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398"509 510 [[package]]511+name = "linked-hash-map"512+version = "0.5.6"513+source = "registry+https://github.com/rust-lang/crates.io-index"514+checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"515+516+[[package]]517 name = "linux-raw-sys"518 version = "0.4.14"519 source = "registry+https://github.com/rust-lang/crates.io-index"520 checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89"505 unmodified lines1026 source = "registry+https://github.com/rust-lang/crates.io-index"1027 checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e"1028 1029 [[package]]1030+name = "similar"1031+version = "2.6.0"1032+source = "registry+https://github.com/rust-lang/crates.io-index"1033+checksum = "1de1d4f81173b03af4c0cbed3c898f6bff5b870e4a7f5d6f4057d62a7a4b686e"1034+1035+[[package]]1036 name = "statistical"1037 version = "1.0.0"1038 source = "registry+https://github.com/rust-lang/crates.io-index"1039 checksum = "49d57902bb128e5e38b5218d3681215ae3e322d99f65d5420e9849730d2ea372"373 unmodified linesmodified:Cargo.toml+5 added
61 unmodified lines626364656667686970717273747576774 unmodified lines61 unmodified lines
[dev-dependencies]approx = "0.5"assert_cmd = "2.0"insta = { version = "1.41.1", features = ["yaml"] }predicates = "3.0"tempfile = "3.14"
[profile.dev.package]insta.opt-level = 3similar.opt-level = 3
[build-dependencies]clap = "4.4.12"clap_complete = "4.2.1"
4 unmodified lines