Skip to content

Commit

Permalink
Fix non-multiple match report_lint failure message
Browse files Browse the repository at this point in the history
  • Loading branch information
srawlins authored and sds committed Jul 23, 2015
1 parent 3d68519 commit 2062370
Show file tree
Hide file tree
Showing 4 changed files with 281 additions and 4 deletions.
2 changes: 1 addition & 1 deletion spec/scss_lint/linter/bang_format_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
SCSS

it 'does not loop forever' do
subject.should_not report_lint
should_not report_lint
end
end

Expand Down
268 changes: 268 additions & 0 deletions spec/scss_lint/report_lint_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
require 'spec_helper'
require 'rspec/matchers/fail_matchers'

RSpec.configure do |config|
config.include RSpec::Matchers::FailMatchers
end

RSpec::Matchers.define_negated_matcher :succeed, :raise_error

# In these tests, we cover all of the combinations of successful and failed
# matches, expecting and _not_ expecting lints, specified and unspecified line
# numbers, specified and unspecified lint counts, and singular and multiple
# lints.
describe 'report_lint' do
let(:subject) { SCSSLint::Linter::LeadingZero.new }

def run_with(scss)
subject.run(SCSSLint::Engine.new(code: scss), SCSSLint::Config.default.linter_options(subject))
end

context 'when expecting no lints' do
it 'matches zero expected lints' do
run_with('p { margin: .5em; }')

expect do
should_not report_lint
end.to succeed
end

it 'fails to match one lint with a meaningful message' do
run_with('p { margin: 0.5em; }')

expect do
should_not report_lint
end.to fail_with 'expected that a lint would not be reported'
end
end

context 'when expecting no lints on a certain line' do
it 'matches zero expected lints' do
run_with('p { margin: .5em; }')

expect do
should_not report_lint line: 1
end.to succeed
end

it 'matches zero expected lints on the specified line' do
run_with('p { margin: 0.5em; }')

expect do
should_not report_lint line: 10
end.to succeed
end

it 'fails to match one lint on the specified line with a meaningful message' do
run_with('p { margin: 0.5em; }')

expect do
should_not report_lint line: 1
end.to fail_with 'expected that a lint would not be reported'
end
end

context 'when expecting a lint' do
it 'matches one expected lint' do
run_with('p { margin: 0.5em; }')

expect do
should report_lint
end.to succeed
end

it 'matches multiple lints' do
run_with('p { margin: 0.5em 0.5em; }')

expect do
should report_lint
end.to succeed
end

it 'fails to match zero lints with a meaningful message' do
run_with('p { margin: .5em; }')

expect do
should report_lint
end.to fail_with 'expected that a lint would be reported'
end
end

context 'when expecting a lint on a certain line' do
it 'matches one expected lint' do
run_with('p { margin: 0.5em; }')

expect do
should report_lint line: 1
end.to succeed
end

it 'matches multiple lints' do
run_with('p { margin: 0.5em 0.5em; }')

expect do
should report_lint line: 1
end.to succeed
end

it 'fails to match zero lints with a meaningful message' do
run_with('p { margin: .5em; }')

expect do
should report_lint line: 1
end.to fail_with 'expected that a lint would be reported on line 1'
end

it 'fails to match lints on the wrong line with a meaningful message' do
run_with('p { margin: 0.5em; }')

expect do
should report_lint line: 10
end.to fail_with 'expected that a lint would be reported on line 10, ' \
'but one lint was reported on line 1'
end
end

context 'when expecting exactly one lint' do
it 'matches one expected lint' do
run_with('p { margin: 0.5em; }')

expect do
should report_lint count: 1
end.to succeed
end

it 'fails to match zero lints with a meaningful message' do
run_with('p { margin: .5em; }')

expect do
should report_lint count: 1
end.to fail_with 'expected that exactly 1 lint would be reported'
end

it 'fails to match multiple lints with a meaningful message' do
run_with('p { margin: 0.5em 0.5em; }')

expect do
should report_lint count: 1
end.to fail_with 'expected that exactly 1 lint would be reported, ' \
'but lints were reported on lines 1 and 1'
end

it 'fails to match lints on the wrong line with a meaningful message' do
run_with('p { margin: 0.5em; }')

expect do
should report_lint line: 10, count: 1
end.to fail_with 'expected that exactly 1 lint would be reported on ' \
'line 10, but one lint was reported on line 1'
end
end

context 'when expecting multiple lints' do
it 'matches multiple lints' do
run_with('p { margin: 0.5em 0.5em; }')

expect do
should report_lint count: 2
end.to succeed
end

it 'fails to match zero lints with a meaningful message' do
run_with('p { margin: .5em; }')

expect do
should report_lint count: 2
end.to fail_with 'expected that exactly 2 lints would be reported'
end

it 'fails to match one lint with a meaningful message' do
run_with('p { margin: .5em; }')

expect do
should report_lint count: 2
end.to fail_with 'expected that exactly 2 lints would be reported'
end

it 'fails to match lints on the wrong line with a meaningful message' do
run_with('p { margin: 0.5em 0.5em; }')

expect do
should report_lint line: 10, count: 2
end.to fail_with 'expected that exactly 2 lints would be reported on ' \
'line 10, but lints were reported on lines 1 and 1'
end
end

context 'when expecting exactly one lint on a certain line' do
it 'matches one expected lint' do
run_with('p { margin: 0.5em; }')

expect do
should report_lint line: 1, count: 1
end.to succeed
end

it 'fails to match zero lints with a meaningful message' do
run_with('p { margin: .5em; }')

expect do
should report_lint line: 1, count: 1
end.to fail_with 'expected that exactly 1 lint would be reported on line 1'
end

it 'fails to match multiple lints with a meaningful message' do
run_with('p { margin: 0.5em 0.5em; }')

expect do
should report_lint line: 1, count: 1
end.to fail_with 'expected that exactly 1 lint would be reported on ' \
'line 1, but lints were reported on lines 1 and 1'
end

it 'fails to match lints on the wrong line with a meaningful message' do
run_with('p { margin: 0.5em; }')

expect do
should report_lint line: 10, count: 1
end.to fail_with 'expected that exactly 1 lint would be reported on ' \
'line 10, but one lint was reported on line 1'
end
end

context 'when expecting multiple lints on a certain line' do
it 'matches one expected lint' do
run_with('p { margin: 0.5em 0.5em; }')

expect do
should report_lint line: 1, count: 2
end.to succeed
end

it 'fails to match zero lints with a meaningful message' do
run_with('p { margin: .5em; }')

expect do
should report_lint line: 1, count: 2
end.to fail_with 'expected that exactly 2 lints would be reported on line 1'
end

it 'fails to match one lint with a meaningful message' do
run_with('p { margin: 0.5em; }')

expect do
should report_lint line: 1, count: 2
end.to fail_with 'expected that exactly 2 lints would be reported on ' \
'line 1, but one lint was reported on line 1'
end

it 'fails to match lints on the wrong line with a meaningful message' do
run_with('p { margin: 0.5em; }')

expect do
should report_lint line: 10, count: 2
end.to fail_with 'expected that exactly 2 lints would be reported on ' \
'line 10, but one lint was reported on line 1'
end
end
end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# If running a linter spec, run the described linter against the CSS code
# for each example. This significantly DRYs up our linter specs to contain
# only tests, since all the setup code is now centralized here.
if described_class <= SCSSLint::Linter
if described_class && described_class <= SCSSLint::Linter
initial_indent = scss[/\A(\s*)/, 1]
normalized_css = scss.gsub(/^#{initial_indent}/, '')

Expand Down
13 changes: 11 additions & 2 deletions spec/support/matchers/report_lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@
end

failure_message do |linter|
'expected that a lint would be reported' +
expected_count =
if count.nil?
'a lint'
elsif count == 1
'exactly 1 lint'
else
"exactly #{count} lints"
end

"expected that #{expected_count} would be reported" +
(expected_line ? " on line #{expected_line}" : '') +
case linter.lints.count
when 0
''
when 1
", but was on line #{linter.lints.first.location.line}"
", but one lint was reported on line #{linter.lints.first.location.line}"
else
lines = lint_lines(linter)
", but lints were reported on lines #{lines[0...-1].join(', ')} and #{lines.last}"
Expand Down

0 comments on commit 2062370

Please sign in to comment.