Skip to content
Myr

tests.rs

This file

Path

src/export/tests.rs

319 lines · 9.9 KiB · Rust

Last changed by

Move more tests to instaDavid Peterbefore adoptionda399ab

History of this file

Take it

Use insta for exporter testsDavid Peterbefore adoption7a9d04d

lines 1–2why, one step back
1use super::Exporter;
2use crate::benchmark::benchmark_result::BenchmarkResult;

Move more tests to instaDavid Peterbefore adoptionda399ab

lines 3–4why, one step back
3use crate::export::asciidoc::AsciidocExporter;
4use crate::export::orgmode::OrgmodeExporter;

Use insta for exporter testsDavid Peterbefore adoption7a9d04d

lines 5–8why, one step back
5use crate::util::units::Unit;
6use crate::{export::markdown::MarkdownExporter, options::SortOrder};7use std::collections::BTreeMap;8

Move more tests to instaDavid Peterbefore adoptionda399ab

line 9why, one step back
9fn get_output<E: Exporter + Default>(

Use insta for exporter testsDavid Peterbefore adoption7a9d04d

lines 10–13why, one step back
10 results: &[BenchmarkResult],
11 unit: Option<Unit>,12 sort_order: SortOrder,13) -> String {

Move more tests to instaDavid Peterbefore adoptionda399ab

line 14why, one step back
14 let exporter = E::default();

Use insta for exporter testsDavid Peterbefore adoption7a9d04d

lines 15–17why, one step back
15 String::from_utf8(exporter.serialize(results, unit, sort_order).unwrap()).unwrap()
16}17

Move more tests to instaDavid Peterbefore adoptionda399ab

line 18why, one step back
18/// Ensure the makrup output includes the table header and the multiple

Use insta for exporter testsDavid Peterbefore adoption7a9d04d

lines 19–22why, one step back
19/// benchmark results as a table. The list of actual times is not included
20/// in the output.21///22/// This also demonstrates that the first entry's units (ms) are used to set

Move more tests to instaDavid Peterbefore adoptionda399ab

line 23why, one step back
23/// the units for all entries when the time unit is not specified.

Use insta for exporter testsDavid Peterbefore adoption7a9d04d

line 24why, one step back
24#[test]

Move more tests to instaDavid Peterbefore adoptionda399ab

line 25why, one step back
25fn test_markup_export_auto_ms() {

Use insta for exporter testsDavid Peterbefore adoption7a9d04d

lines 26–58why, one step back
26 let results = [
27 BenchmarkResult {28 command: String::from("sleep 0.1"),29 command_with_unused_parameters: String::from("sleep 0.1"),30 mean: 0.1057,31 stddev: Some(0.0016),32 median: 0.1057,33 user: 0.0009,34 system: 0.0011,35 min: 0.1023,36 max: 0.1080,37 times: Some(vec![0.1, 0.1, 0.1]),38 memory_usage_byte: None,39 exit_codes: vec![Some(0), Some(0), Some(0)],40 parameters: BTreeMap::new(),41 },42 BenchmarkResult {43 command: String::from("sleep 2"),44 command_with_unused_parameters: String::from("sleep 2"),45 mean: 2.0050,46 stddev: Some(0.0020),47 median: 2.0050,48 user: 0.0009,49 system: 0.0012,50 min: 2.0020,51 max: 2.0080,52 times: Some(vec![2.0, 2.0, 2.0]),53 memory_usage_byte: None,54 exit_codes: vec![Some(0), Some(0), Some(0)],55 parameters: BTreeMap::new(),56 },57 ];58

Move more tests to instaDavid Peterbefore adoptionda399ab

line 59why, one step back
59 insta::assert_snapshot!(get_output::<MarkdownExporter>(&results, None, SortOrder::Command), @r#"

Use insta for exporter testsDavid Peterbefore adoption7a9d04d

lines 60–64why, one step back
60 | Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
61 |:---|---:|---:|---:|---:|62 | `sleep 0.1` | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |63 | `sleep 2` | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |64 "#);

Move more tests to instaDavid Peterbefore adoptionda399ab

lines 65–94why, one step back
65
66 insta::assert_snapshot!(get_output::<AsciidocExporter>(&results, None, SortOrder::Command), @r#"67 [cols="<,>,>,>,>"]68 |===69 | Command 70 | Mean [ms] 71 | Min [ms] 72 | Max [ms] 73 | Relative 7475 | `sleep 0.1` 76 | 105.7 ± 1.6 77 | 102.3 78 | 108.0 79 | 1.00 8081 | `sleep 2` 82 | 2005.0 ± 2.0 83 | 2002.0 84 | 2008.0 85 | 18.97 ± 0.29 86 |===87 "#);8889 insta::assert_snapshot!(get_output::<OrgmodeExporter>(&results, None, SortOrder::Command), @r#"90 | Command | Mean [ms] | Min [ms] | Max [ms] | Relative |91 |--+--+--+--+--|92 | =sleep 0.1= | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |93 | =sleep 2= | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |94 "#);

Use insta for exporter testsDavid Peterbefore adoption7a9d04d

lines 95–99why, one step back
95}
9697/// This (again) demonstrates that the first entry's units (s) are used to set98/// the units for all entries when the time unit is not given.99#[test]

Move more tests to instaDavid Peterbefore adoptionda399ab

line 100why, one step back
100fn test_markup_export_auto_s() {

Use insta for exporter testsDavid Peterbefore adoption7a9d04d

lines 101–133why, one step back
101 let results = [
102 BenchmarkResult {103 command: String::from("sleep 2"),104 command_with_unused_parameters: String::from("sleep 2"),105 mean: 2.0050,106 stddev: Some(0.0020),107 median: 2.0050,108 user: 0.0009,109 system: 0.0012,110 min: 2.0020,111 max: 2.0080,112 times: Some(vec![2.0, 2.0, 2.0]),113 memory_usage_byte: None,114 exit_codes: vec![Some(0), Some(0), Some(0)],115 parameters: BTreeMap::new(),116 },117 BenchmarkResult {118 command: String::from("sleep 0.1"),119 command_with_unused_parameters: String::from("sleep 0.1"),120 mean: 0.1057,121 stddev: Some(0.0016),122 median: 0.1057,123 user: 0.0009,124 system: 0.0011,125 min: 0.1023,126 max: 0.1080,127 times: Some(vec![0.1, 0.1, 0.1]),128 memory_usage_byte: None,129 exit_codes: vec![Some(0), Some(0), Some(0)],130 parameters: BTreeMap::new(),131 },132 ];133

Move more tests to instaDavid Peterbefore adoptionda399ab

line 134why, one step back
134 insta::assert_snapshot!(get_output::<MarkdownExporter>(&results, None, SortOrder::Command), @r#"

Use insta for exporter testsDavid Peterbefore adoption7a9d04d

lines 135–139why, one step back
135 | Command | Mean [s] | Min [s] | Max [s] | Relative |
136 |:---|---:|---:|---:|---:|137 | `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |138 | `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |139 "#);

Move more tests to instaDavid Peterbefore adoptionda399ab

lines 140–169why, one step back
140
141 insta::assert_snapshot!(get_output::<AsciidocExporter>(&results, None, SortOrder::Command), @r#"142 [cols="<,>,>,>,>"]143 |===144 | Command 145 | Mean [s] 146 | Min [s] 147 | Max [s] 148 | Relative 149150 | `sleep 2` 151 | 2.005 ± 0.002 152 | 2.002 153 | 2.008 154 | 18.97 ± 0.29 155156 | `sleep 0.1` 157 | 0.106 ± 0.002 158 | 0.102 159 | 0.108 160 | 1.00 161 |===162 "#);163164 insta::assert_snapshot!(get_output::<OrgmodeExporter>(&results, None, SortOrder::Command), @r#"165 | Command | Mean [s] | Min [s] | Max [s] | Relative |166 |--+--+--+--+--|167 | =sleep 2= | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |168 | =sleep 0.1= | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |169 "#);

Use insta for exporter testsDavid Peterbefore adoption7a9d04d

lines 170–171why, one step back
170}
171

Move more tests to instaDavid Peterbefore adoptionda399ab

lines 172–173why, one step back
172/// This (again) demonstrates that the given time unit (ms) is used to set
173/// the units for all entries.

Use insta for exporter testsDavid Peterbefore adoption7a9d04d

line 174why, one step back
174#[test]

Move more tests to instaDavid Peterbefore adoptionda399ab

lines 175–176why, one step back
175fn test_markup_export_manual_ms() {
176 let timing_results = [

Use insta for exporter testsDavid Peterbefore adoption7a9d04d

lines 177–208why, one step back
177 BenchmarkResult {
178 command: String::from("sleep 2"),179 command_with_unused_parameters: String::from("sleep 2"),180 mean: 2.0050,181 stddev: Some(0.0020),182 median: 2.0050,183 user: 0.0009,184 system: 0.0012,185 min: 2.0020,186 max: 2.0080,187 times: Some(vec![2.0, 2.0, 2.0]),188 memory_usage_byte: None,189 exit_codes: vec![Some(0), Some(0), Some(0)],190 parameters: BTreeMap::new(),191 },192 BenchmarkResult {193 command: String::from("sleep 0.1"),194 command_with_unused_parameters: String::from("sleep 0.1"),195 mean: 0.1057,196 stddev: Some(0.0016),197 median: 0.1057,198 user: 0.0009,199 system: 0.0011,200 min: 0.1023,201 max: 0.1080,202 times: Some(vec![0.1, 0.1, 0.1]),203 memory_usage_byte: None,204 exit_codes: vec![Some(0), Some(0), Some(0)],205 parameters: BTreeMap::new(),206 },207 ];208

Move more tests to instaDavid Peterbefore adoptionda399ab

lines 209–214why, one step back
209 insta::assert_snapshot!(get_output::<MarkdownExporter>(&timing_results, Some(Unit::MilliSecond), SortOrder::Command), @r#"
210 | Command | Mean [ms] | Min [ms] | Max [ms] | Relative |211 |:---|---:|---:|---:|---:|212 | `sleep 2` | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |213 | `sleep 0.1` | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |214 "#);

Use insta for exporter testsDavid Peterbefore adoption7a9d04d

line 215why, one step back
215

Move more tests to instaDavid Peterbefore adoptionda399ab

lines 216–244why, one step back
216 insta::assert_snapshot!(get_output::<AsciidocExporter>(&timing_results, Some(Unit::MilliSecond), SortOrder::Command), @r#"
217 [cols="<,>,>,>,>"]218 |===219 | Command 220 | Mean [ms] 221 | Min [ms] 222 | Max [ms] 223 | Relative 224225 | `sleep 2` 226 | 2005.0 ± 2.0 227 | 2002.0 228 | 2008.0 229 | 18.97 ± 0.29 230231 | `sleep 0.1` 232 | 105.7 ± 1.6 233 | 102.3 234 | 108.0 235 | 1.00 236 |===237 "#);238239 insta::assert_snapshot!(get_output::<OrgmodeExporter>(&timing_results, Some(Unit::MilliSecond), SortOrder::Command), @r#"240 | Command | Mean [ms] | Min [ms] | Max [ms] | Relative |241 |--+--+--+--+--|242 | =sleep 2= | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |243 | =sleep 0.1= | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |244 "#);

Use insta for exporter testsDavid Peterbefore adoption7a9d04d

lines 245–246why, one step back
245}
246

Move more tests to instaDavid Peterbefore adoptionda399ab

line 247why, one step back
247/// The given time unit (s) is used to set the units for all entries.

Use insta for exporter testsDavid Peterbefore adoption7a9d04d

line 248why, one step back
248#[test]

Move more tests to instaDavid Peterbefore adoptionda399ab

lines 249–250why, one step back
249fn test_markup_export_manual_s() {
250 let results = [

Use insta for exporter testsDavid Peterbefore adoption7a9d04d

lines 251–282why, one step back
251 BenchmarkResult {
252 command: String::from("sleep 2"),253 command_with_unused_parameters: String::from("sleep 2"),254 mean: 2.0050,255 stddev: Some(0.0020),256 median: 2.0050,257 user: 0.0009,258 system: 0.0012,259 min: 2.0020,260 max: 2.0080,261 times: Some(vec![2.0, 2.0, 2.0]),262 memory_usage_byte: None,263 exit_codes: vec![Some(0), Some(0), Some(0)],264 parameters: BTreeMap::new(),265 },266 BenchmarkResult {267 command: String::from("sleep 0.1"),268 command_with_unused_parameters: String::from("sleep 0.1"),269 mean: 0.1057,270 stddev: Some(0.0016),271 median: 0.1057,272 user: 0.0009,273 system: 0.0011,274 min: 0.1023,275 max: 0.1080,276 times: Some(vec![0.1, 0.1, 0.1]),277 memory_usage_byte: None,278 exit_codes: vec![Some(0), Some(0), Some(0)],279 parameters: BTreeMap::new(),280 },281 ];282

Move more tests to instaDavid Peterbefore adoptionda399ab

lines 283–317why, one step back
283 insta::assert_snapshot!(get_output::<MarkdownExporter>(&results, Some(Unit::Second), SortOrder::Command), @r#"
284 | Command | Mean [s] | Min [s] | Max [s] | Relative |285 |:---|---:|---:|---:|---:|286 | `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |287 | `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |288 "#);289290 insta::assert_snapshot!(get_output::<MarkdownExporter>(&results, Some(Unit::Second), SortOrder::MeanTime), @r#"291 | Command | Mean [s] | Min [s] | Max [s] | Relative |292 |:---|---:|---:|---:|---:|293 | `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |294 | `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |295 "#);296297 insta::assert_snapshot!(get_output::<AsciidocExporter>(&results, Some(Unit::Second), SortOrder::Command), @r#"298 [cols="<,>,>,>,>"]299 |===300 | Command 301 | Mean [s] 302 | Min [s] 303 | Max [s] 304 | Relative 305306 | `sleep 2` 307 | 2.005 ± 0.002 308 | 2.002 309 | 2.008 310 | 18.97 ± 0.29 311312 | `sleep 0.1` 313 | 0.106 ± 0.002 314 | 0.102 315 | 0.108 316 | 1.00 317 |===

Use insta for exporter testsDavid Peterbefore adoption7a9d04d

lines 318–319why, one step back
318 "#);
319}