Skip to content

Commit

Permalink
RubyFastererBear.py: Add fasterer
Browse files Browse the repository at this point in the history
This adds a `Local Bear` for `Ruby`, wrapping
`fasterer`.

Closes #444
  • Loading branch information
sangamcse committed Jul 8, 2018
1 parent ff6dab7 commit 6738084
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ source 'https://rubygems.org'

gem "brakeman", "~>4.1.1", require: false
gem "csvlint", require: false
gem "fasterer", "~>0.4.1", require: false
gem "haml_lint", "~>0.27.0", require: false
gem "puppet-lint", "~>2.1.1", require: false
gem "reek", "~>4.6", require: false
Expand Down
2 changes: 2 additions & 0 deletions bear-requirements.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ gem_requirements:
brakeman:
version: ~>4.1.1
csvlint: true
fasterer:
version: ~>0.4.1
haml_lint:
version: ~>0.27.0
puppet-lint:
Expand Down
27 changes: 27 additions & 0 deletions bears/ruby/RubyFastererBear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from coalib.bearlib.abstractions.Linter import linter
from dependency_management.requirements.GemRequirement import GemRequirement


@linter(executable='fasterer',
output_format='regex',
output_regex=r'(?P<message>.*\.).*:\s(?P<line>\d+)')
class RubyFastererBear:
"""
The ``RubyFastererBear`` will suggest some speed improvements which you
can check in details at the <https://github.com/JuanitoFatas/fast-ruby>.
It uses ``fasterer``. See <https://www.rubydoc.info/gems/fasterer/0.4.1>
for more info.
"""

LANGUAGES = {'Ruby'}
REQUIREMENTS = {GemRequirement('fasterer', '0.4.1')}
AUTHORS = {'The coala developers'}
AUTHORS_EMAILS = {'[email protected]'}
LICENSE = 'AGPL-3.0'
CAN_DETECT = {'Complexity'}
SEE_MORE = 'https://github.com/DamirSvrtan/fasterer'

@staticmethod
def create_arguments(filename, file, config_file):
return filename,
53 changes: 53 additions & 0 deletions tests/ruby/RubyFastererBearTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import os
from queue import Queue

from coalib.results.Result import Result
from coalib.settings.Section import Section
from coalib.testing.BearTestHelper import generate_skip_decorator
from coalib.testing.LocalBearTestHelper import LocalBearTestHelper

from bears.ruby.RubyFastererBear import RubyFastererBear


def get_testfile_path(name):
return os.path.join(os.path.dirname(__file__),
'fasterer_test_files',
name)


def load_testfile(name):
with open(get_testfile_path(name)) as f:
return f.readlines()


@generate_skip_decorator(RubyFastererBear)
class RubyFastererBearTest(LocalBearTestHelper):

def setUp(self):
self.uut = RubyFastererBear(Section('name'), Queue())

def test_module_eval_vs_define_method(self):
filename = 'module_eval.rb'
file_contents = load_testfile(filename)
self.check_results(
self.uut,
file_contents,
[Result.from_values('RubyFastererBear',
message='Using module_eval is slower than '
'define_method.',
file=get_testfile_path(filename),
line=3)],
filename=get_testfile_path(filename))

def test_sort_vs_sort_by(self):
filename = 'sort_vs_sort_by.rb'
file_contents = load_testfile(filename)
self.check_results(
self.uut,
file_contents,
[Result.from_values('RubyFastererBear',
message='Enumerable#sort is slower than '
'Enumerable#sort_by.',
file=get_testfile_path(filename),
line=6)],
filename=get_testfile_path(filename))
15 changes: 15 additions & 0 deletions tests/ruby/fasterer_test_files/module_eval.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Hihi
class << self
module_eval %{
def hello
puts "win"
end
}
end
end

Hihi.hello

Hihi.module_eval %{
puts @foo
}
9 changes: 9 additions & 0 deletions tests/ruby/fasterer_test_files/sort_vs_sort_by.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
User = Struct.new(:name)
ARRAY = Array.new(3) do
User.new(sprintf("%010d"), rand(1_000_000_000))
end

ARRAY.sort { |a, b| a.name <=> b.name }
ARRAY.sort_by(&:name)

ARRAY.sort

0 comments on commit 6738084

Please sign in to comment.