File: job_spec.rb

package info (click to toggle)
ruby-benchmark-memory 0.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 300 kB
  • sloc: ruby: 1,121; makefile: 7; sh: 4
file content (110 lines) | stat: -rw-r--r-- 2,968 bytes parent folder | download
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# frozen_string_literal: true

require 'spec_helper'

# rubocop:disable Lint/EmptyBlock
RSpec.describe Benchmark::Memory::Job do
  describe '#report' do
    it 'raises an ArgumentError when no block is specified' do
      job = create_job

      expect { job.report('riddle me this') }.to raise_error(ArgumentError)
    end

    it 'adds a task to the list of tasks in the job' do
      job = create_job

      expect { job.report('riddle me that') {} }.to(
        change(job.tasks, :count).by(1)
      )
    end
  end

  describe '#run' do
    it 'adds an entry to the report for each task' do
      job = create_job
      job.report('with you') {}
      job.report('my brown eyed girl') {}

      expect { job.run }.to change(job.full_report.entries, :count).by(2)
    end

    context 'holding' do
      it 'only executes one task for each call to run' do
        hold_buffer = StringIO.new
        job, output = create_job_and_output
        job.report('with you') {}
        job.report('my brown eyed girl') {}
        job.hold!(hold_buffer)

        expect { job.run }.to change(job.full_report.entries, :count).by(1)
        output_string = output.string
        expect(output_string).to match(/with you/)
        expect(output_string).not_to match(/brown eyed girl/)
        expect(output_string).to match(/Pausing/)

        # Reset for the second run
        hold_buffer.rewind
        job, output = create_job_and_output
        job.report('with you') {}
        job.report('my brown eyed girl') {}
        job.hold!(hold_buffer)

        expect { job.run }.to change(job.full_report.entries, :count).by(2)
        output_string = output.string
        expect(output_string).not_to match(/with you/)
        expect(output_string).to match(/brown eyed girl/)
        expect(output_string).not_to match(/Pausing/)
      end
    end
  end

  describe '#run_comparison' do
    it 'does not run if there are no entries' do
      job, output = create_job_and_output

      job.run
      job.run_comparison

      expect(output.string).not_to match(/Comparison/)
    end

    it 'runs when there are entries and the job is configured to compare' do
      job, output = create_job_and_output
      job.report('with you') {}
      job.report('my brown eyed girl') {}
      job.compare!

      job.run
      job.run_comparison

      expect(output.string).to match(/Comparison/)
    end
  end

  describe '#quiet?' do
    it 'prevents any output from being written' do
      job, output = create_job_and_output(quiet: true)
      job.report('with you') {}
      job.report('my brown eyed girl') {}
      job.compare!

      job.run
      job.run_comparison

      expect(output.string).to be_empty
    end
  end

  def create_job_and_output(quiet: false)
    output = StringIO.new
    job = Benchmark::Memory::Job.new(output: output, quiet: quiet)

    [job, output]
  end

  def create_job
    create_job_and_output.first
  end
end
# rubocop:enable Lint/EmptyBlock