File: memory_prof_spec.rb

package info (click to toggle)
ruby-test-prof 1.6.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,448 kB
  • sloc: ruby: 13,093; sh: 4; makefile: 4
file content (180 lines) | stat: -rw-r--r-- 4,209 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# frozen_string_literal: true

describe TestProf::MemoryProf do
  subject { described_class }

  describe ".config" do
    let(:config) { subject.config }

    it "returns an instance of TestProf::MemoryProf::Configuration" do
      expect(config).to be_kind_of(TestProf::MemoryProf::Configuration)
    end

    describe "#mode=" do
      let(:set_mode) { config.mode = value }

      context "when value is alloc" do
        let(:value) { "alloc" }

        it "sets mode to alloc" do
          set_mode

          expect(config.mode).to eq(:alloc)
        end
      end

      context "when value is rss" do
        let(:value) { "rss" }

        it "sets mode to rss" do
          set_mode

          expect(config.mode).to eq(:rss)
        end
      end

      context "when value is neither alloc or rss" do
        let(:value) { "invalid" }

        it "sets mode to alloc" do
          set_mode

          expect(config.mode).to eq(:rss)
        end
      end
    end

    describe "#top_count=" do
      let(:set_top_count) { config.top_count = value }

      context "when value is an integer" do
        let(:value) { 5 }

        it "sets top_count to the value" do
          set_top_count

          expect(config.top_count).to eq(5)
        end
      end

      context "when value is a float" do
        let(:value) { 5.7 }

        it "transforms the value to an integer" do
          set_top_count

          expect(config.top_count).to eq(5)
        end
      end

      context "when value is 0" do
        let(:value) { 0 }

        it "sets top_count to 5" do
          set_top_count

          expect(config.top_count).to eq(5)
        end
      end

      context "when value is negative" do
        let(:value) { -7 }

        it "sets top_count to 5" do
          set_top_count

          expect(config.top_count).to eq(5)
        end
      end

      context "when value is nil" do
        let(:value) { nil }

        it "sets top_count to 5" do
          set_top_count

          expect(config.top_count).to eq(5)
        end
      end

      context "when value is a text" do
        let(:value) { "invalid" }

        it "sets top_count to 5" do
          set_top_count

          expect(config.top_count).to eq(5)
        end
      end
    end
  end

  describe ".tracker" do
    let(:tracker) { subject.tracker }

    context "when mode is alloc" do
      before { described_class.config.mode = "alloc" }

      if RUBY_ENGINE == "jruby"
        it "raises an error" do
          expect { tracker }.to raise_error("Your Ruby Engine or OS is not supported")
        end
      else
        it "returns an instance of AllocTracker" do
          expect(tracker).to be_kind_of(TestProf::MemoryProf::AllocTracker)
        end

        it "sets tracker.top_count to config.top_count" do
          expect(tracker.top_count).to eq(5)
        end
      end
    end

    context "when mode is rss" do
      before { described_class.config.mode = "rss" }

      it "returns an instance of RssTracker" do
        expect(tracker).to be_kind_of(TestProf::MemoryProf::RssTracker)
      end

      it "sets tracker.top_count to config.top_count" do
        expect(tracker.top_count).to eq(5)
      end
    end
  end

  describe ".printer" do
    let(:printer) { subject.printer(tracker) }
    let(:tracker) { subject.tracker }

    context "when mode is alloc" do
      before { described_class.config.mode = "alloc" }

      if RUBY_ENGINE == "jruby"
        it "raises an error" do
          expect { printer }.to raise_error("Your Ruby Engine or OS is not supported")
        end
      else
        it "returns an instance of AllocPrinter" do
          expect(printer).to be_kind_of(TestProf::MemoryProf::AllocPrinter)
        end

        it "sets printer.tracker" do
          expect(printer.send(:tracker)).to eq(tracker)
        end
      end
    end

    context "when mode is rss" do
      before { described_class.config.mode = "rss" }

      it "returns an instance of RssPrinter" do
        expect(printer).to be_kind_of(TestProf::MemoryProf::RssPrinter)
      end

      it "sets printer.tracker" do
        expect(printer.send(:tracker)).to eq(tracker)
      end
    end
  end
end