Skip to content

Commit cd0ae0b

Browse files
committed
Keep benchmark cases titles consistent
Before, we would call "capitalize" on the title of the benchmark case, which would change the original string the user provided. This fixes it, so the report always uses the original title.
1 parent b84e3fd commit cd0ae0b

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

CHANGELOG.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,19 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
9+
910
---
1011

12+
## [0.3.2] - 2021-09-10
13+
14+
### Changed
15+
16+
- Keep benchmark cases titles consistent
17+
18+
Before, we would call "capitalize" on the title of the benchmark case, which
19+
would change the original string the user provided. This fixes it, so the report
20+
always uses the original title.
21+
1122
## [0.3.1] - 2021-09-10
1223

1324
### Changed
@@ -48,6 +59,7 @@ end
4859
## [0.2.0] - 2020-10-23
4960

5061
### Added
62+
5163
- Support for `Benchmark.memory` via [benchmark-memory](https://github.com/michaelherold/benchmark-memory).
5264

5365
[unreleased]: https://github.com/MatheusRich/benchable/compare/v0.2.0...HEAD
@@ -56,10 +68,11 @@ end
5668
## [0.1.0] - 2020-08-16
5769

5870
### Added
71+
5972
- Support for `Benchmark.bm`, `Benchmark.bmbm` and `Benchmark.ips`.
6073

6174
[unreleased]: https://github.com/MatheusRich/benchable/compare/v0.3.1...HEAD
6275
[0.1.0]: https://github.com/MatheusRich/benchable/releases/tag/v0.1.0
6376
[0.2.0]: https://github.com/MatheusRich/benchable/releases/tag/v0.2.0
6477
[0.3.0]: https://github.com/MatheusRich/benchable/releases/tag/v0.3.0
65-
[0.3.1]: https://github.com/MatheusRich/benchable/releases/tag/v0.3.1
78+
[0.3.1]: https://github.com/MatheusRich/benchable/releases/tag/v0.3.1

lib/benchable/benchmark.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def self.setup(&block)
2424
end
2525

2626
def self.bench(name, &block)
27-
define_method(method_name_for(name), &block)
27+
define_method(:"bench_#{name}", &block)
2828
end
2929

3030
def setup
@@ -39,10 +39,6 @@ def cases
3939
public_methods.grep(/\Abench_/)
4040
end
4141

42-
private_class_method def self.method_name_for(name)
43-
"bench_#{name.to_s.tr(" ", "_").downcase}"
44-
end
45-
4642
private
4743

4844
attr_reader :benchmark_type, :options
@@ -56,7 +52,7 @@ def run_benchmark
5652
b.config(options) if benchmark_type == :ips
5753

5854
cases.each do |benchmark_case|
59-
b.report(name_for(benchmark_case)) do
55+
b.report(title_for(benchmark_case)) do
6056
method(benchmark_case).call
6157
end
6258
end
@@ -65,8 +61,8 @@ def run_benchmark
6561
end
6662
end
6763

68-
def name_for(benchmark_case)
69-
benchmark_case.to_s.gsub("bench_", "").tr("_", " ").capitalize
64+
def title_for(benchmark_case)
65+
benchmark_case.to_s.delete_prefix("bench_")
7066
end
7167

7268
def benchmark(&block)

spec/benchable/benchmark_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
end
5656

5757
it "creates a new bench method" do
58-
expect { klass.new(:bm).bench_the_puts_method }.to output("Hello world!\n").to_stdout
58+
expect { klass.new(:bm).public_send("bench_The puts method") }.to output("Hello world!\n").to_stdout
5959
end
6060
end
6161

spec/benchable_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ def an_invalid_bench_method
6262
before do
6363
allow(bench_mock).to receive(:config)
6464
allow(bench_mock).to receive(:compare!)
65-
allow(bench_mock).to receive(:report).with("Sum").and_yield
66-
allow(bench_mock).to receive(:report).with("Multiplication").and_yield
65+
allow(bench_mock).to receive(:report).with("sum").and_yield
66+
allow(bench_mock).to receive(:report).with("multiplication").and_yield
6767
allow(Benchmark).to receive(:ips).and_yield(bench_mock)
6868
end
6969

@@ -76,8 +76,8 @@ def an_invalid_bench_method
7676
it "builds and runs a benchmark", :aggregate_failures do
7777
run_benchmark
7878

79-
expect(bench_mock).to have_received(:report).with("Sum")
80-
expect(bench_mock).to have_received(:report).with("Multiplication")
79+
expect(bench_mock).to have_received(:report).with("sum")
80+
expect(bench_mock).to have_received(:report).with("multiplication")
8181
expect(bench_mock).to have_received(:compare!)
8282
end
8383

0 commit comments

Comments
 (0)