minitest-memory 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +13 -0
- data/LICENSE.txt +21 -0
- data/README.md +72 -0
- data/lib/minitest/memory/version.rb +8 -0
- data/lib/minitest/memory.rb +60 -0
- metadata +85 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 2ebf51378aad65a92a5563045128674d105040e0c8c6a923d8f94aea5a47099c
|
|
4
|
+
data.tar.gz: 4c3ac6868a00e6b00e47e4ccd96bb0d69ad275e3aa7d7287fdd0036c7d4c6f8d
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 23abb466ddad6de511d47c7d77a84a61c60f362cb815115a711f120ae59ba2920129644beee0d29fc4943ec55170aa152335a796444d4bd18f203c2ab95a4ed6
|
|
7
|
+
data.tar.gz: dda3dd7ebefb5aafe8f1225ea9760a194fb4cd951921c0566a0d32ef6c5acb65ee2372d2de4a388cc624da22c3eaf503aa40fd35dbfa0b916faa3c51e24ee26a
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [1.0.0] - 2026-03-06
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- `Minitest::Memory::AllocationCounter.count` — counts object allocations by class within a block
|
|
13
|
+
- `assert_allocations` — fails if any class exceeds its allocation limit within a block
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Erik Berlin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# minitest-memory
|
|
2
|
+
|
|
3
|
+
Minitest assertions for tracking memory allocations. Verify that your code
|
|
4
|
+
allocates only the expected number of objects.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
Add to your Gemfile:
|
|
9
|
+
|
|
10
|
+
```ruby
|
|
11
|
+
gem "minitest-memory"
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
Include `Minitest::Memory` in your test class, then use `assert_allocations`
|
|
17
|
+
to set upper bounds on object allocations within a block:
|
|
18
|
+
|
|
19
|
+
```ruby
|
|
20
|
+
require "minitest/autorun"
|
|
21
|
+
require "minitest/memory"
|
|
22
|
+
|
|
23
|
+
class MyTest < Minitest::Test
|
|
24
|
+
include Minitest::Memory
|
|
25
|
+
|
|
26
|
+
def test_no_string_allocations
|
|
27
|
+
assert_allocations(String => 0) do
|
|
28
|
+
# code that should not allocate strings
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def test_limited_allocations
|
|
33
|
+
assert_allocations(String => 2, Array => 1) do
|
|
34
|
+
# code that should allocate at most 2 Strings and 1 Array
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
It also works with `Minitest::Spec`:
|
|
41
|
+
|
|
42
|
+
```ruby
|
|
43
|
+
require "minitest/autorun"
|
|
44
|
+
require "minitest/memory"
|
|
45
|
+
|
|
46
|
+
class Minitest::Spec
|
|
47
|
+
include Minitest::Memory
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
describe MyClass do
|
|
51
|
+
it "does not allocate strings" do
|
|
52
|
+
assert_allocations(String => 0) do
|
|
53
|
+
# code under test
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## How It Works
|
|
60
|
+
|
|
61
|
+
`assert_allocations` uses `ObjectSpace.trace_object_allocations` to track
|
|
62
|
+
every object allocated during the block's execution. It then compares the
|
|
63
|
+
counts per class against the limits you provide. If any class exceeds its
|
|
64
|
+
limit, the assertion fails with a message like:
|
|
65
|
+
|
|
66
|
+
```
|
|
67
|
+
Expected at most 0 String allocations, got 3
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## License
|
|
71
|
+
|
|
72
|
+
MIT
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
require "objspace"
|
|
2
|
+
require "minitest/strict"
|
|
3
|
+
require_relative "memory/version"
|
|
4
|
+
|
|
5
|
+
##
|
|
6
|
+
# Minitest extensions for memory allocation assertions.
|
|
7
|
+
|
|
8
|
+
module Minitest
|
|
9
|
+
##
|
|
10
|
+
# Provides assertions for tracking memory allocations in tests.
|
|
11
|
+
# Include this module in your test class to use +assert_allocations+.
|
|
12
|
+
module Memory
|
|
13
|
+
##
|
|
14
|
+
# Counts object allocations within a block using ObjectSpace.
|
|
15
|
+
class AllocationCounter
|
|
16
|
+
##
|
|
17
|
+
# Counts allocations by class within a block. Returns a Hash
|
|
18
|
+
# mapping each class to its allocation count. Temporarily
|
|
19
|
+
# disables GC during counting.
|
|
20
|
+
|
|
21
|
+
def self.count(&)
|
|
22
|
+
GC.start
|
|
23
|
+
GC.disable
|
|
24
|
+
generation = GC.count
|
|
25
|
+
ObjectSpace.trace_object_allocations(&)
|
|
26
|
+
count_allocations generation
|
|
27
|
+
ensure
|
|
28
|
+
GC.enable
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
##
|
|
32
|
+
# Returns a Hash of allocations from the given +generation+.
|
|
33
|
+
|
|
34
|
+
def self.count_allocations generation
|
|
35
|
+
allocations = Hash.new(0)
|
|
36
|
+
ObjectSpace.each_object do |obj|
|
|
37
|
+
allocations[obj.class] += 1 if ObjectSpace.allocation_generation(obj) == generation
|
|
38
|
+
end
|
|
39
|
+
allocations
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
##
|
|
44
|
+
# Fails if any class in +limits+ exceeds its allocation count
|
|
45
|
+
# within a block. +limits+ is a Hash mapping classes to maximum
|
|
46
|
+
# allowed allocations. Eg:
|
|
47
|
+
#
|
|
48
|
+
# assert_allocations(String => 1) { "hello" }
|
|
49
|
+
|
|
50
|
+
def assert_allocations(limits, &)
|
|
51
|
+
actual = AllocationCounter.count(&)
|
|
52
|
+
|
|
53
|
+
limits.each do |klass, max_count|
|
|
54
|
+
count = actual[klass]
|
|
55
|
+
msg = "Expected at most #{max_count} #{klass} allocations, got #{count}"
|
|
56
|
+
assert_operator max_count, :>=, count, msg
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: minitest-memory
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Erik Berlin
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: minitest
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '5.21'
|
|
19
|
+
- - "<"
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '7'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
requirements:
|
|
26
|
+
- - ">="
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: '5.21'
|
|
29
|
+
- - "<"
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: '7'
|
|
32
|
+
- !ruby/object:Gem::Dependency
|
|
33
|
+
name: minitest-strict
|
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - ">="
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '1.0'
|
|
39
|
+
type: :runtime
|
|
40
|
+
prerelease: false
|
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - ">="
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '1.0'
|
|
46
|
+
description: Provides assert_allocations to verify object allocation counts within
|
|
47
|
+
a block.
|
|
48
|
+
email:
|
|
49
|
+
- sferik@gmail.com
|
|
50
|
+
executables: []
|
|
51
|
+
extensions: []
|
|
52
|
+
extra_rdoc_files: []
|
|
53
|
+
files:
|
|
54
|
+
- CHANGELOG.md
|
|
55
|
+
- LICENSE.txt
|
|
56
|
+
- README.md
|
|
57
|
+
- lib/minitest/memory.rb
|
|
58
|
+
- lib/minitest/memory/version.rb
|
|
59
|
+
homepage: https://sferik.github.io/minitest-memory
|
|
60
|
+
licenses:
|
|
61
|
+
- MIT
|
|
62
|
+
metadata:
|
|
63
|
+
rubygems_mfa_required: 'true'
|
|
64
|
+
source_code_uri: https://github.com/sferik/minitest-memory
|
|
65
|
+
bug_tracker_uri: https://github.com/sferik/minitest-memory/issues
|
|
66
|
+
changelog_uri: https://github.com/sferik/minitest-memory/blob/main/CHANGELOG.md
|
|
67
|
+
documentation_uri: https://www.rubydoc.info/gems/minitest-memory
|
|
68
|
+
rdoc_options: []
|
|
69
|
+
require_paths:
|
|
70
|
+
- lib
|
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '3.2'
|
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
|
+
requirements:
|
|
78
|
+
- - ">="
|
|
79
|
+
- !ruby/object:Gem::Version
|
|
80
|
+
version: '0'
|
|
81
|
+
requirements: []
|
|
82
|
+
rubygems_version: 4.0.7
|
|
83
|
+
specification_version: 4
|
|
84
|
+
summary: Memory allocation assertions for Minitest
|
|
85
|
+
test_files: []
|