minitest-metz 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.ruby-version +1 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/README.md +86 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/minitest/metz.rb +11 -0
- data/lib/minitest/metz/assertions.rb +29 -0
- data/lib/minitest/metz/scan_results.rb +36 -0
- data/lib/minitest/metz/scanner.rb +21 -0
- data/lib/minitest/metz/stats_reporter.rb +45 -0
- data/lib/minitest/metz/version.rb +5 -0
- data/lib/minitest/metz/where.rb +53 -0
- data/lib/minitest/metz_plugin.rb +16 -0
- data/minitest-metz.gemspec +31 -0
- metadata +122 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 765abed06e6131561c6fe86e6707efb7d6f954e5
|
4
|
+
data.tar.gz: 301ff1aa8dd6f203f71f2dbe271cd2151ac2f97c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5d3a0314b9bde3e44bac776e106dfb47a26e5b38e5ffff5e6412978369a614d70a69cdfe729fa7af24eb4808ca38b537fc2ed196a9b6b5b8c8fe731c0ada76ba
|
7
|
+
data.tar.gz: 912f0756dfb7c1f691e889fe5cdfd870c0ce5bea73875ff106205a92baaf8eb3630610af12882140b7ae6229ed6ca528e386126628cd577600093ee2b2afef4f
|
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.2.2
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# Minitest::Metz
|
2
|
+
|
3
|
+
Minitest::Metz is a Minitest plugin that under it's hood hides the [SandiMeter](https://github.com/makaroni4/sandi_meter).
|
4
|
+
It allows you to easily apply Sandi Metz's [four rules for developers](https://robots.thoughtbot.com/sandi-metz-rules-for-developers)
|
5
|
+
on your tests.
|
6
|
+
|
7
|
+
## Why?
|
8
|
+
|
9
|
+
Because tests are code. Especially in Minitest, where each test file is a subclass
|
10
|
+
of `Minitest::Test`.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'minitest-metz'
|
18
|
+
```
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install minitest-metz
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
There two ways that you can use this plugin:
|
31
|
+
|
32
|
+
### `--metz`
|
33
|
+
|
34
|
+
You can apply the `--metz` flag when you are running your tests. If present, the
|
35
|
+
plugin will run the `sandi_meter` for the test that ran.
|
36
|
+
|
37
|
+
### `assert_obey_metz`
|
38
|
+
|
39
|
+
If you are willing of writing tests around the four rules, you can assert on
|
40
|
+
them. The assertions works on basically any type of class, whether it is a test
|
41
|
+
class or a production class. If you want to test the test class for the four
|
42
|
+
rules:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
class PersonTest < Minitest::Test
|
46
|
+
def test_sandi_four_rules
|
47
|
+
assert_obey_metz(self.class)
|
48
|
+
# Or..
|
49
|
+
assert_obey_metz(PersonTest)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
If you want to test another class:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
class PersonTest < Minitest::Test
|
58
|
+
def test_sandi_four_rules
|
59
|
+
assert_obey_metz(Person)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
Also, there's the option on asserting on a file path:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
class PersonTest < Minitest::Test
|
68
|
+
def test_sandi_four_rules
|
69
|
+
assert_obey_metz("lib/person/person.rb")
|
70
|
+
end
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
And, of course, you can use `refute_obey_metz`, although I have no idea why
|
75
|
+
would you. But anyway, it's available for use. Have fun.
|
76
|
+
|
77
|
+
## Development
|
78
|
+
|
79
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
80
|
+
|
81
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
82
|
+
|
83
|
+
## Contributing
|
84
|
+
|
85
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/fteem/minitest-metz.
|
86
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "minitest/metz"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require "minitest/assertions"
|
2
|
+
|
3
|
+
module Minitest::Assertions
|
4
|
+
def assert_obey_metz(str)
|
5
|
+
file_path = full_file_path(str)
|
6
|
+
res = Minitest::Metz::Scanner.scan(file_path)
|
7
|
+
assert(res.all_valid?, "The class does not obey Sandi Metz' four rules.")
|
8
|
+
end
|
9
|
+
|
10
|
+
def refute_obey_metz(str)
|
11
|
+
file_path = full_file_path(str)
|
12
|
+
res = Minitest::Metz::Scanner.scan(file_path)
|
13
|
+
refute(res.all_valid?, "The class obeys Sandi Metz' four rules.")
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def full_file_path(s)
|
19
|
+
if s.is_a?(Class)
|
20
|
+
where_is(s).first
|
21
|
+
else
|
22
|
+
s
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def where_is(klass)
|
27
|
+
Minitest::Metz::Where.is_class_primarily(klass)
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Minitest
|
2
|
+
module Metz
|
3
|
+
|
4
|
+
class ScanResults
|
5
|
+
attr_reader :first_rule, :misindentation, :second_rule, :third_rule
|
6
|
+
|
7
|
+
def initialize(results)
|
8
|
+
@first_rule = results[:first_rule][:total_classes_amount] - results[:first_rule][:small_classes_amount]
|
9
|
+
@misindentation = results[:first_rule][:misindented_classes_amount]
|
10
|
+
@second_rule = results[:second_rule][:total_methods_amount] - results[:second_rule][:small_methods_amount]
|
11
|
+
@third_rule = results[:third_rule][:total_method_calls] - results[:third_rule][:proper_method_calls]
|
12
|
+
end
|
13
|
+
|
14
|
+
def all_valid?
|
15
|
+
first_rule_valid? && misidentation_valid? && second_rule_valid? && third_rule_valid?
|
16
|
+
end
|
17
|
+
|
18
|
+
def first_rule_valid?
|
19
|
+
first_rule.zero?
|
20
|
+
end
|
21
|
+
|
22
|
+
def misidentation_valid?
|
23
|
+
misindentation.zero?
|
24
|
+
end
|
25
|
+
|
26
|
+
def second_rule_valid?
|
27
|
+
second_rule.zero?
|
28
|
+
end
|
29
|
+
|
30
|
+
def third_rule_valid?
|
31
|
+
third_rule.zero?
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "sandi_meter/file_scanner"
|
2
|
+
|
3
|
+
module Minitest
|
4
|
+
module Metz
|
5
|
+
class Scanner
|
6
|
+
def self.scan(file_path)
|
7
|
+
new.scan(file_path)
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@scanner = SandiMeter::FileScanner.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def scan(file_path)
|
15
|
+
r = @scanner.scan(file_path)
|
16
|
+
Minitest::Metz::ScanResults.new(r)
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "minitest"
|
2
|
+
|
3
|
+
module Minitest
|
4
|
+
module Metz
|
5
|
+
|
6
|
+
class StatsReporter < Minitest::Reporter
|
7
|
+
def initialize
|
8
|
+
@scanner = Minitest::Metz::Scanner.new
|
9
|
+
@results = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def record(result)
|
13
|
+
file_path, = result.class.instance_method(result.name).source_location
|
14
|
+
unless @results[file_path]
|
15
|
+
scan_result = Minitest::Metz::Scanner.scan(file_path)
|
16
|
+
@results[file_path] = build_results_string(file_path, scan_result)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def report
|
21
|
+
@results.each do |key, output|
|
22
|
+
puts output
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def build_results_string(file_path, result)
|
29
|
+
str = "\nSandi Meter Rules results:"
|
30
|
+
if result.all_valid?
|
31
|
+
str << " All valid."
|
32
|
+
else
|
33
|
+
str << "\n#{file_path}"
|
34
|
+
str << "\n #{result.first_rule} class(es) over 100 lines." unless result.first_rule_valid?
|
35
|
+
str << "\n #{result.misidentation} misindented class(es)." unless result.misidentation_valid?
|
36
|
+
str << "\n #{result.second_rule} method(s) over 5 lines." unless result.second_rule_valid?
|
37
|
+
str << "\n #{result.third_rule} method call(s) accepted are more than 4 parameters." unless result.third_rule_valid?
|
38
|
+
end
|
39
|
+
|
40
|
+
str
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Minitest
|
2
|
+
module Metz
|
3
|
+
module Where
|
4
|
+
class << self
|
5
|
+
def is_class(klass)
|
6
|
+
methods = defined_methods(klass)
|
7
|
+
file_groups = methods.group_by{|sl| sl[0]}
|
8
|
+
file_counts = file_groups.map do |file, sls|
|
9
|
+
lines = sls.map{|sl| sl[1]}
|
10
|
+
count = lines.size
|
11
|
+
line = lines.min
|
12
|
+
{file: file, count: count, line: line}
|
13
|
+
end
|
14
|
+
file_counts.sort_by!{|fc| fc[:count]}
|
15
|
+
source_locations = file_counts.map{|fc| [fc[:file], fc[:line]]}
|
16
|
+
source_locations
|
17
|
+
end
|
18
|
+
|
19
|
+
# Raises ArgumentError if klass does not have any Ruby methods defined in it.
|
20
|
+
def is_class_primarily(klass)
|
21
|
+
source_locations = is_class(klass)
|
22
|
+
if source_locations.empty?
|
23
|
+
methods = defined_methods(klass)
|
24
|
+
raise ArgumentError, (methods.empty? ?
|
25
|
+
"#{klass} has no methods" :
|
26
|
+
"#{klass} only has built-in methods (#{methods.size} in total)"
|
27
|
+
)
|
28
|
+
end
|
29
|
+
source_locations[0]
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def source_location(method)
|
35
|
+
method.source_location || (
|
36
|
+
method.to_s =~ /: (.*)>/
|
37
|
+
$1
|
38
|
+
)
|
39
|
+
end
|
40
|
+
|
41
|
+
def defined_methods(klass)
|
42
|
+
methods = klass.methods(false).map{|m| klass.method(m)} +
|
43
|
+
klass.instance_methods(false).map{|m| klass.instance_method(m)}
|
44
|
+
methods.map!(&:source_location)
|
45
|
+
methods.compact!
|
46
|
+
methods
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'pp'
|
2
|
+
require "minitest"
|
3
|
+
|
4
|
+
module Minitest
|
5
|
+
def self.plugin_metz_options(opts, options)
|
6
|
+
opts.on "-z", "--metz", "Check if your code obeys Sandi Metz' four rules for developers" do |z|
|
7
|
+
options[:metz] = z
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.plugin_metz_init(options)
|
12
|
+
if options[:metz]
|
13
|
+
self.reporter << Minitest::Metz::StatsReporter.new
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'minitest/metz/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "minitest-metz"
|
8
|
+
spec.version = Minitest::Metz::VERSION
|
9
|
+
spec.authors = ["Ile Eftimov"]
|
10
|
+
spec.email = ["ileeftimov@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Make sure your code (production and tests) obey Sandi
|
13
|
+
Metz' four rules for developers.}
|
14
|
+
spec.description = %q{Minitest::Metz is a Minitest plugin that under it's
|
15
|
+
hood hides the SandiMeter. It allows you to easily
|
16
|
+
apply Sandi Metz's four rules for developers on your
|
17
|
+
tests.}
|
18
|
+
spec.homepage = "https://github.com/fteem/minitest-metz"
|
19
|
+
|
20
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_dependency "minitest", "~> 5.8"
|
26
|
+
spec.add_dependency "sandi_meter", "~> 1.2"
|
27
|
+
|
28
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
29
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
30
|
+
end
|
31
|
+
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: minitest-metz
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ile Eftimov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: minitest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.8'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sandi_meter
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.10'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.10'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
description: |-
|
70
|
+
Minitest::Metz is a Minitest plugin that under it's
|
71
|
+
hood hides the SandiMeter. It allows you to easily
|
72
|
+
apply Sandi Metz's four rules for developers on your
|
73
|
+
tests.
|
74
|
+
email:
|
75
|
+
- ileeftimov@gmail.com
|
76
|
+
executables: []
|
77
|
+
extensions: []
|
78
|
+
extra_rdoc_files: []
|
79
|
+
files:
|
80
|
+
- ".gitignore"
|
81
|
+
- ".ruby-version"
|
82
|
+
- ".travis.yml"
|
83
|
+
- Gemfile
|
84
|
+
- README.md
|
85
|
+
- Rakefile
|
86
|
+
- bin/console
|
87
|
+
- bin/setup
|
88
|
+
- lib/minitest/metz.rb
|
89
|
+
- lib/minitest/metz/assertions.rb
|
90
|
+
- lib/minitest/metz/scan_results.rb
|
91
|
+
- lib/minitest/metz/scanner.rb
|
92
|
+
- lib/minitest/metz/stats_reporter.rb
|
93
|
+
- lib/minitest/metz/version.rb
|
94
|
+
- lib/minitest/metz/where.rb
|
95
|
+
- lib/minitest/metz_plugin.rb
|
96
|
+
- minitest-metz.gemspec
|
97
|
+
homepage: https://github.com/fteem/minitest-metz
|
98
|
+
licenses: []
|
99
|
+
metadata: {}
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 2.4.8
|
117
|
+
signing_key:
|
118
|
+
specification_version: 4
|
119
|
+
summary: Make sure your code (production and tests) obey Sandi Metz' four rules for
|
120
|
+
developers.
|
121
|
+
test_files: []
|
122
|
+
has_rdoc:
|