Skip to content

Commit

Permalink
Merge pull request #64 from dukaev/by_lines
Browse files Browse the repository at this point in the history
Add output by lines
  • Loading branch information
DamirSvrtan authored Mar 11, 2019
2 parents 308051b + e04bd76 commit ad10551
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/fasterer/file_traverser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,13 @@ def root_dir
end

def output(analyzer)
puts analyzer.file_path.colorize(:red)

offenses_grouped_by_type(analyzer).each do |error_group_name, error_occurences|
puts "#{Fasterer::Offense::EXPLANATIONS[error_group_name]}."\
" Occurred at lines: #{error_occurences.map(&:line_number).join(', ')}."
error_occurences.map(&:line_number).each do |line|
print "#{analyzer.file_path}:#{line} #{Fasterer::Offense::EXPLANATIONS[error_group_name]}.\n"
end
end

puts
print "\n"
end

def offenses_grouped_by_type(analyzer)
Expand Down
20 changes: 20 additions & 0 deletions spec/lib/fasterer/file_traverser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,24 @@
.to match_array(['user.rb - RubyParser::SyntaxError - unterminated string meets end of file. near line 1: ""'])
end
end

describe 'output' do
let(:test_file_path) { RSpec.root.join('support', 'output', 'sample_code.rb') }
let(:analyzer) { Fasterer::Analyzer.new(test_file_path) }
let(:file_traverser) { Fasterer::FileTraverser.new('.') }

before do
analyzer.scan
end

context "when print offenses" do
let(:explanation) { Fasterer::Offense::EXPLANATIONS[:for_loop_vs_each] }

it 'should print offense' do
match = "#{test_file_path}:1 #{explanation}.\n\n"

expect { file_traverser.send(:output, analyzer) }.to output(match).to_stdout
end
end
end
end
3 changes: 3 additions & 0 deletions spec/support/output/sample_code.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
for number in [*1..100] do
number
end

0 comments on commit ad10551

Please sign in to comment.