Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RubyFastererBear.py: Add fasterer #2595

Merged
merged 1 commit into from
Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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", "~>0.4.0", 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 @@ -6,6 +6,8 @@ gem_requirements:
version: ~>4.1.1
csvlint:
version: ~>0.4.0
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