File: benchmark_generator_test.rb

package info (click to toggle)
rails 2%3A7.2.2.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,348 kB
  • sloc: ruby: 349,797; javascript: 30,703; yacc: 46; sql: 43; sh: 29; makefile: 27
file content (86 lines) | stat: -rw-r--r-- 2,157 bytes parent folder | download | duplicates (2)
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