1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
# frozen_string_literal: true
require "generators/generators_test_helper"
require "rails/generators/rails/benchmark/benchmark_generator"
module Rails
module Generators
class BenchmarkGeneratorTest < Rails::Generators::TestCase
include GeneratorsTestHelper
setup do
copy_gemfile
end
test "generate benchmark" do
run_generator ["my_benchmark"]
assert_file("Gemfile") do |content|
assert_match "gem \"benchmark-ips\"", content
end
assert_file("script/benchmarks/my_benchmark.rb") do |content|
assert_equal <<~RUBY, content
# frozen_string_literal: true
require_relative "../../config/environment"
# Any benchmarking setup goes here...
Benchmark.ips do |x|
x.report("before") { }
x.report("after") { }
x.compare!
end
RUBY
end
end
test "generate benchmark with no name" do
output = capture(:stderr) do
run_generator []
end
assert_equal <<~MSG, output
No value provided for required arguments 'name'
MSG
end
test "generate benchmark with reports" do
run_generator ["my_benchmark", "with_patch", "without_patch"]
assert_file("script/benchmarks/my_benchmark.rb") do |content|
assert_equal <<~RUBY, content
# frozen_string_literal: true
require_relative "../../config/environment"
# Any benchmarking setup goes here...
Benchmark.ips do |x|
x.report("with_patch") { }
x.report("without_patch") { }
x.compare!
end
RUBY
end
end
test "generate benchmark twice only adds ips gem once" do
run_generator ["my_benchmark"]
run_generator ["my_benchmark"]
assert_file("Gemfile") do |content|
occurrences = content.scan("gem \"benchmark-ips\"").count
assert_equal 1, occurrences, "Should only have benchmark-ips present once"
end
end
end
end
end
|