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

Add output by lines #64

Merged
merged 1 commit into from
Mar 11, 2019
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
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