diff --git a/lib/fasterer/file_traverser.rb b/lib/fasterer/file_traverser.rb index eb2ede6..64bd879 100644 --- a/lib/fasterer/file_traverser.rb +++ b/lib/fasterer/file_traverser.rb @@ -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) diff --git a/spec/lib/fasterer/file_traverser_spec.rb b/spec/lib/fasterer/file_traverser_spec.rb index e8a4279..081e0a6 100644 --- a/spec/lib/fasterer/file_traverser_spec.rb +++ b/spec/lib/fasterer/file_traverser_spec.rb @@ -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 diff --git a/spec/support/output/sample_code.rb b/spec/support/output/sample_code.rb new file mode 100644 index 0000000..92e5a45 --- /dev/null +++ b/spec/support/output/sample_code.rb @@ -0,0 +1,3 @@ +for number in [*1..100] do + number +end \ No newline at end of file