Skip to content

Commit

Permalink
add spec for RunnerDecorator
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaseger committed Aug 26, 2014
1 parent 7541b72 commit 4db1b6d
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 18 deletions.
2 changes: 0 additions & 2 deletions lib/codeqa.rb
Expand Up @@ -64,10 +64,8 @@ def register_checkers
require 'codeqa/version'
require 'codeqa/sourcefile'
require 'codeqa/checker'
require 'codeqa/check_errors'
require 'codeqa/runner'
require 'codeqa/runner_decorator'
require 'codeqa/utils/erb_sanitizer'

require 'codeqa/configuration'

Expand Down
1 change: 1 addition & 0 deletions lib/codeqa/checker.rb
@@ -1,6 +1,7 @@
require 'stringio'
require 'tempfile'
require 'forwardable'
require 'codeqa/utils/check_errors'

module Codeqa
class Checker
Expand Down
9 changes: 5 additions & 4 deletions lib/codeqa/runner_decorator.rb
Expand Up @@ -32,23 +32,24 @@ def to_s

private

# TODO: move this error formating into check error class
def error_details
msg = ''
@runner.failures.each do |checker|
msg << error("------- #{checker.name} -------\n")
msg << error("#{checker.hint}\n")
msg << error("------- #{checker.name} -------") << "\n"
msg << error("#{checker.hint}") << "\n"
checker.errors.details.each do |type, content|
case type
when :source
content.each_line.with_index do |l, i|
msg << yellow((i).to_s.rjust(3)) << '|' << l
msg << yellow((i + 1).to_s.rjust(3)) << '|' << l
end
when Integer
msg << info('Line: ') << yellow(type) << '|' << info(content)
when Array
msg << info('Pos: ') << yellow(type.join(',')) << '|' << info(content)
when nil
msg << info(content) << "\n"
msg << info(content)
end
msg << "\n"
end
Expand Down
File renamed without changes.
83 changes: 71 additions & 12 deletions spec/lib/codeqa/runner_decorator_spec.rb
@@ -1,19 +1,78 @@
require 'spec_helper'

describe Codeqa::RunnerDecorator do
it 'should run provide the errors if the checker failed' do
source = source_with('def syntax_error', 'ruby.rb')
runner = Codeqa::Runner.run(source)
expect(runner.success?).to be false
decorator = Codeqa::RunnerDecorator.new(runner)
expect(decorator.to_s).to match(/syntax/)
let(:errors){ Codeqa::CheckErrors.new }
let(:decorator) do
checker = double(Codeqa::Checker, :errors => errors,
:name => 'test',
:hint => 'testtest')
runner = double(Codeqa::Runner, :failures => [checker],
:sourcefile => Codeqa::Sourcefile.new('foo.rb', 'foo'),
:success? => false)
Codeqa::RunnerDecorator.new(runner)
end
it 'should run list the ran checkers' do
source = source_with('def foo; end', 'ruby.rb')
runner = Codeqa::Runner.run(source)
expect(runner.success?).to be true
decorator = Codeqa::RunnerDecorator.new(runner)
expect(decorator.to_s).to match(/Passed tests.+strange chars/)

it 'should format error as line if number given' do
errors.add(77, 'test message')
expect(decorator.details_to_s).to eq(<<-EOF)
\e[31m------- test -------\e[0m
\e[31mtesttest\e[0m
Line: \e[33m77\e[0m|test message
EOF
end

it 'should format error as position if array given' do
errors.add([22, 77], 'test message')
expect(decorator.details_to_s).to eq(<<-EOF)
\e[31m------- test -------\e[0m
\e[31mtesttest\e[0m
Pos: \e[33m22,77\e[0m|test message
EOF
end

it 'should simply print error content if no context is given' do
errors.add(nil, 'test message')
expect(decorator.details_to_s).to eq(<<-EOF)
\e[31m------- test -------\e[0m
\e[31mtesttest\e[0m
test message
EOF
end

it 'should format error as source if :source token given' do
errors.add(:source, 'test message')
expect(decorator.details_to_s).to eq(<<-EOF)
\e[31m------- test -------\e[0m
\e[31mtesttest\e[0m
\e[33m 1\e[0m|test message
EOF
end

it 'should correctly format multiline source' do
errors.add(:source, "test message\nline two\nthird\n\nfifth")
expect(decorator.details_to_s).to eq(<<-EOF)
\e[31m------- test -------\e[0m
\e[31mtesttest\e[0m
\e[33m 1\e[0m|test message
\e[33m 2\e[0m|line two
\e[33m 3\e[0m|third
\e[33m 4\e[0m|
\e[33m 5\e[0m|fifth
EOF
end
# it 'should run provide the errors if the checker failed' do
# source = source_with('def syntax_error', 'ruby.rb')
# runner = Codeqa::Runner.run(source)
# expect(runner.success?).to be false
# decorator = Codeqa::RunnerDecorator.new(runner)
# expect(decorator.to_s).to match(/syntax/)
# end
# it 'should run list the ran checkers' do
# source = source_with('def foo; end', 'ruby.rb')
# runner = Codeqa::Runner.run(source)
# expect(runner.success?).to be true
# decorator = Codeqa::RunnerDecorator.new(runner)
# expect(decorator.to_s).to match(/Passed tests.+strange chars/)
# end

end

0 comments on commit 4db1b6d

Please sign in to comment.