minitest-ruby_golf_metrics 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in minitest-ruby_golf_metrics.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Thomas Jachmann
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # Minitest::RubyGolfMetrics
2
+
3
+ This is a [Minitest](https://github.com/seattlerb/minitest) plugin that can be
4
+ used to play [Ruby Golf](http://www.sitepoint.com/ruby-golf/).
5
+
6
+ It makes some assumptions which make it unusable for you if you don't play the
7
+ game after its rules:
8
+
9
+ * your tasks are tested in the minitest class ```RubyGolfTest```
10
+ * the contestants implement their solutions as module methods in the module
11
+ ```RubyGolf```
12
+
13
+ That's it.
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ gem 'minitest-ruby_golf_metrics'
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ ## Usage
26
+
27
+ Run your minitests and check your results.
28
+
29
+ You can provide the parameter --nocolor to prevent the metrics output form being
30
+ colorized.
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,8 @@
1
+ require "minitest/ruby_golf_metrics/version"
2
+ require "minitest/ruby_golf_metrics/reporter"
3
+
4
+ module Minitest
5
+ module RubyGolfMetrics
6
+ # Your code goes here...
7
+ end
8
+ end
@@ -0,0 +1,37 @@
1
+ require "sourcify"
2
+ require "minitest/ruby_golf_metrics/method_parser"
3
+
4
+ module Minitest
5
+
6
+ module RubyGolfMetrics
7
+
8
+ class CharacterCounter
9
+
10
+ def initialize(method_name, previous_method_stack = [])
11
+ method_stack = previous_method_stack + [method_name]
12
+ @method_name = method_name
13
+ source = RubyGolf.method(method_name).to_raw_source(strip_enclosure: true)
14
+ @size = source.strip.gsub(/\s+/, "").size
15
+ parser = MethodParser.new(source)
16
+ @children = (RubyGolf.methods(false) - method_stack).map do |method|
17
+ CharacterCounter.new(method, method_stack) if parser.is_method_called?(method)
18
+ end.compact
19
+ end
20
+
21
+ def cumulative_size
22
+ @size + @children.map(&:cumulative_size).inject(0, &:+)
23
+ end
24
+
25
+ def self_contained?
26
+ @children.empty?
27
+ end
28
+
29
+ def called_method_sizes
30
+ ["#{@method_name}: #{@size}", @children.map(&:called_method_sizes)].flatten.compact
31
+ end
32
+
33
+ end
34
+
35
+ end
36
+
37
+ end
@@ -0,0 +1,46 @@
1
+ # taken from http://stackoverflow.com/questions/16917902/parsing-ruby-code-for-certain-function-calls
2
+ require "ripper"
3
+
4
+ module Minitest
5
+
6
+ module RubyGolfMetrics
7
+
8
+ class MethodParser
9
+
10
+ def initialize(source)
11
+ @ast = Ripper.sexp(source)
12
+ end
13
+
14
+ def is_method_called?(method_name)
15
+ search_ast_for_method(@ast, method_name)
16
+ end
17
+
18
+ private
19
+
20
+ def is_top_level_method_call(ast, method_name)
21
+ # firstly check if possible command block
22
+ unless ast.is_a?(Array) && ast.length > 1 && ast[1].is_a?(Array)
23
+ return false
24
+ end
25
+ # now check if it is a function call or command, and check the method name
26
+ if [:command, :fcall].include? ast[0]
27
+ ast[1].include?(method_name.to_s)
28
+ elsif ast[0] == :command_call
29
+ ast[1][0] == :var_ref && ast[1][1].include?("RubyGolf")
30
+ ast[3].include?(method_name.to_s)
31
+ else
32
+ false
33
+ end
34
+ end
35
+
36
+ def search_ast_for_method(ast, method_name)
37
+ return true if is_top_level_method_call(ast, method_name)
38
+ return false unless ast.is_a? Array
39
+ ast.any? { |e| search_ast_for_method(e, method_name) }
40
+ end
41
+
42
+ end
43
+
44
+ end
45
+
46
+ end
@@ -0,0 +1,47 @@
1
+ require "minitest/ruby_golf_metrics/character_counter"
2
+
3
+ module Minitest
4
+
5
+ module RubyGolfMetrics
6
+
7
+ class Reporter < ::Minitest::Reporter
8
+
9
+ Erg = Struct.new(:method_name, :passed)
10
+
11
+ def start
12
+ @ergs = []
13
+ end
14
+
15
+ def record(erg)
16
+ @ergs << Erg.new(erg.location.gsub("RubyGolfTest#test_", "").gsub(/ .*/, ""), erg.passed?)
17
+ end
18
+
19
+ def report
20
+ io.puts "\nRuby Golf Metrics"
21
+ @ergs.sort_by(&:method_name).each do |erg|
22
+ if erg.passed
23
+ begin
24
+ counter = CharacterCounter.new(erg.method_name)
25
+ msg = " #{colorize(erg.method_name, 32)}: #{counter.cumulative_size} characters"
26
+ msg << " (#{counter.called_method_sizes.join(", ")})" if !counter.self_contained?
27
+ io.puts msg
28
+ rescue NoMethodError
29
+ io.puts " #{colorize(erg.method_name, 31)}: UNDEFINED"
30
+ end
31
+ else
32
+ io.puts " #{colorize(erg.method_name, 31)}: FAILED"
33
+ end
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def colorize(text, color)
40
+ options[:nocolor] ? text : "\e[#{color}m#{text}\e[0m"
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+
47
+ end
@@ -0,0 +1,5 @@
1
+ module Minitest
2
+ module RubyGolfMetrics
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ require "minitest/ruby_golf_metrics"
2
+
3
+ module Minitest
4
+
5
+ def self.plugin_ruby_golf_metrics_options(opts, options)
6
+ opts.on "--nocolor", "Report metrics without color" do
7
+ options[:nocolor] = true
8
+ end
9
+ end
10
+
11
+ def self.plugin_ruby_golf_metrics_init(options)
12
+ self.reporter << RubyGolfMetrics::Reporter.new(options[:io], options)
13
+ end
14
+
15
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'minitest/ruby_golf_metrics/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "minitest-ruby_golf_metrics"
8
+ spec.version = Minitest::RubyGolfMetrics::VERSION
9
+ spec.authors = ["Thomas Jachmann"]
10
+ spec.email = ["self@thomasjachmann.com"]
11
+ spec.description = %q{Provide metrics for evaluating ruby golf solutions concerning code length when running minitests.}
12
+ spec.summary = %q{Ruby Golf Metrics Plugin for Minitest}
13
+ spec.homepage = "https://github.com/thomasjachmann/minitest-ruby_golf_metrics"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "sourcify", "0.6.0.rc4"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minitest-ruby_golf_metrics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Thomas Jachmann
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sourcify
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - '='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.6.0.rc4
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - '='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.6.0.rc4
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Provide metrics for evaluating ruby golf solutions concerning code length
63
+ when running minitests.
64
+ email:
65
+ - self@thomasjachmann.com
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - lib/minitest/ruby_golf_metrics.rb
76
+ - lib/minitest/ruby_golf_metrics/character_counter.rb
77
+ - lib/minitest/ruby_golf_metrics/method_parser.rb
78
+ - lib/minitest/ruby_golf_metrics/reporter.rb
79
+ - lib/minitest/ruby_golf_metrics/version.rb
80
+ - lib/minitest/ruby_golf_metrics_plugin.rb
81
+ - minitest-ruby_golf_metrics.gemspec
82
+ homepage: https://github.com/thomasjachmann/minitest-ruby_golf_metrics
83
+ licenses:
84
+ - MIT
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ segments:
96
+ - 0
97
+ hash: 3528417271052105563
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ segments:
105
+ - 0
106
+ hash: 3528417271052105563
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 1.8.23
110
+ signing_key:
111
+ specification_version: 3
112
+ summary: Ruby Golf Metrics Plugin for Minitest
113
+ test_files: []