Custom matcher

This commit is contained in:
Vitalii Elenhaupt 2017-11-01 22:05:41 +02:00
parent 67506fc643
commit c9f229c3f1
No known key found for this signature in database
GPG key ID: 7558EF3A4056C706
12 changed files with 71 additions and 46 deletions

View file

@ -11,7 +11,7 @@ module Ameba
describe "#name" do describe "#name" do
it "returns name of the rule" do it "returns name of the rule" do
DummyRule.new.name.should eq "DummyRule" DummyRule.new.name.should eq "Ameba::DummyRule"
end end
end end

View file

@ -33,7 +33,7 @@ module Ameba::Rules
:not_ok :not_ok
end end
) )
subject.catch(source).valid?.should be_true subject.catch(source).should be_valid
end end
context "boolean on the right" do context "boolean on the right" do
@ -43,7 +43,7 @@ module Ameba::Rules
:ok :ok
end end
) )
subject.catch(source).valid?.should be_false subject.catch(source).should_not be_valid
end end
it "fails if there is != comparison to boolean" do it "fails if there is != comparison to boolean" do
@ -52,14 +52,14 @@ module Ameba::Rules
:ok :ok
end end
) )
subject.catch(source).valid?.should be_false subject.catch(source).should_not be_valid
end end
it "fails if there is case comparison to boolean" do it "fails if there is case comparison to boolean" do
source = Source.new %( source = Source.new %(
a === true a === true
) )
subject.catch(source).valid?.should be_false subject.catch(source).should_not be_valid
end end
it "reports rule, pos and message" do it "reports rule, pos and message" do
@ -80,7 +80,7 @@ module Ameba::Rules
:ok :ok
end end
) )
subject.catch(source).valid?.should be_false subject.catch(source).should_not be_valid
end end
it "fails if there is != comparison to boolean" do it "fails if there is != comparison to boolean" do
@ -89,19 +89,19 @@ module Ameba::Rules
:ok :ok
end end
) )
subject.catch(source).valid?.should be_false subject.catch(source).should_not be_valid
end end
it "fails if there is case comparison to boolean" do it "fails if there is case comparison to boolean" do
source = Source.new %( source = Source.new %(
true === a true === a
) )
subject.catch(source).valid?.should be_false subject.catch(source).should_not be_valid
end end
it "reports rule, pos and message" do it "reports rule, pos and message" do
source = Source.new "true != a" source = Source.new "true != a"
subject.catch(source) subject.catch(source).should_not be_valid
error = source.errors.first error = source.errors.first
error.rule.should_not be_nil error.rule.should_not be_nil

View file

@ -19,7 +19,7 @@ module Ameba::Rules
end end
A.new.debugger A.new.debugger
) )
subject.catch(s).valid?.should be_true subject.catch(s).should be_valid
end end
it "fails if there is a debugger statement" do it "fails if there is a debugger statement" do
@ -28,12 +28,12 @@ module Ameba::Rules
debugger debugger
a = a + 1 a = a + 1
) )
subject.catch(s).valid?.should be_false subject.catch(s).should_not be_valid
end end
it "reports rule, pos and message" do it "reports rule, pos and message" do
s = Source.new "debugger" s = Source.new "debugger"
subject.catch(s).valid?.should be_false subject.catch(s).should_not be_valid
error = s.errors.first error = s.errors.first
error.rule.should_not be_nil error.rule.should_not be_nil

View file

@ -7,22 +7,22 @@ module Ameba::Rules
describe LineLength do describe LineLength do
it "passes if all lines are shorter than 80 symbols" do it "passes if all lines are shorter than 80 symbols" do
source = Source.new "short line" source = Source.new "short line"
subject.catch(source).valid?.should be_true subject.catch(source).should be_valid
end end
it "passes if line consists of 79 symbols" do it "passes if line consists of 79 symbols" do
source = Source.new "*" * 79 source = Source.new "*" * 79
subject.catch(source).valid?.should be_true subject.catch(source).should be_valid
end end
it "fails if there is at least one line longer than 79 symbols" do it "fails if there is at least one line longer than 79 symbols" do
source = Source.new long_line source = Source.new long_line
subject.catch(source).valid?.should be_false subject.catch(source).should_not be_valid
end end
it "reports rule, pos and message" do it "reports rule, pos and message" do
source = Source.new long_line source = Source.new long_line
subject.catch(source).valid?.should be_false subject.catch(source).should_not be_valid
error = source.errors.first error = source.errors.first
error.rule.should eq subject error.rule.should eq subject

View file

@ -23,7 +23,7 @@ module Ameba::Rules
:ok :ok
end end
) )
subject.catch(s).valid?.should be_true subject.catch(s).should be_valid
end end
it "fails if there is a predicate in if conditional" do it "fails if there is a predicate in if conditional" do
@ -32,7 +32,7 @@ module Ameba::Rules
:ok :ok
end end
) )
subject.catch(s).valid?.should be_false subject.catch(s).should_not be_valid
end end
it "fails if there is a predicate in unless conditional" do it "fails if there is a predicate in unless conditional" do
@ -41,7 +41,7 @@ module Ameba::Rules
:ok :ok
end end
) )
subject.catch(s).valid?.should be_false subject.catch(s).should_not be_valid
end end
it "fails if there is a predicate in case conditional" do it "fails if there is a predicate in case conditional" do
@ -53,14 +53,14 @@ module Ameba::Rules
:also_ok :also_ok
end end
) )
subject.catch(s).valid?.should be_false subject.catch(s).should_not be_valid
end end
it "reports rule, pos and message" do it "reports rule, pos and message" do
s = Source.new %( s = Source.new %(
puts "hello" if true puts "hello" if true
) )
subject.catch(s) subject.catch(s).should_not be_valid
s.errors.size.should eq 1 s.errors.size.should eq 1
error = s.errors.first error = s.errors.first

View file

@ -13,7 +13,7 @@ module Ameba::Rules
"Name size: #{name.size}" "Name size: #{name.size}"
) )
subject.catch(s).valid?.should be_true subject.catch(s).should be_valid
end end
it "fails if there is useless interpolation" do it "fails if there is useless interpolation" do
@ -24,13 +24,13 @@ module Ameba::Rules
%q("#{false}"), %q("#{false}"),
%q("here are #{4} cats"), %q("here are #{4} cats"),
].each do |str| ].each do |str|
subject.catch(Source.new str).valid?.should be_false subject.catch(Source.new str).should_not be_valid
end end
end end
it "reports rule, pos and message" do it "reports rule, pos and message" do
s = Source.new %q("#{4}") s = Source.new %q("#{4}")
subject.catch(s).valid?.should be_false subject.catch(s).should_not be_valid
error = s.errors.first error = s.errors.first
error.rule.should_not be_nil error.rule.should_not be_nil

View file

@ -16,7 +16,7 @@ module Ameba::Rules
:ok :ok
end end
) )
subject.catch(s).valid?.should be_true subject.catch(s).should be_valid
end end
it "fails if there is a negated condition in unless" do it "fails if there is a negated condition in unless" do
@ -25,7 +25,7 @@ module Ameba::Rules
:nok :nok
end end
) )
subject.catch(s).valid?.should be_false subject.catch(s).should_not be_valid
end end
it "fails if one of AND conditions is negated" do it "fails if one of AND conditions is negated" do
@ -34,7 +34,7 @@ module Ameba::Rules
:nok :nok
end end
) )
subject.catch(s).valid?.should be_false subject.catch(s).should_not be_valid
end end
it "fails if one of OR conditions is negated" do it "fails if one of OR conditions is negated" do
@ -43,7 +43,7 @@ module Ameba::Rules
:nok :nok
end end
) )
subject.catch(s).valid?.should be_false subject.catch(s).should_not be_valid
end end
it "fails if one of inner conditions is negated" do it "fails if one of inner conditions is negated" do
@ -52,12 +52,12 @@ module Ameba::Rules
:nok :nok
end end
) )
subject.catch(s).valid?.should be_false subject.catch(s).should_not be_valid
end end
it "reports rule, pos and message" do it "reports rule, pos and message" do
s = Source.new ":nok unless !s.empty?" s = Source.new ":nok unless !s.empty?"
subject.catch(s).valid?.should be_false subject.catch(s).should_not be_valid
error = s.errors.first error = s.errors.first
error.rule.should_not be_nil error.rule.should_not be_nil

View file

@ -14,7 +14,7 @@ module Ameba::Rules
end end
end end
) )
subject.catch(s).valid?.should be_true subject.catch(s).should be_valid
end end
it "fails if predicate name is wrong" do it "fails if predicate name is wrong" do
@ -22,7 +22,7 @@ module Ameba::Rules
def is_valid?(x) def is_valid?(x)
end end
) )
subject.catch(s).valid?.should be_false subject.catch(s).should_not be_valid
end end
it "reports rule, pos and message" do it "reports rule, pos and message" do
@ -33,7 +33,7 @@ module Ameba::Rules
end end
end end
) )
subject.catch(s).valid?.should be_false subject.catch(s).should_not be_valid
error = s.errors.first error = s.errors.first
error.rule.should_not be_nil error.rule.should_not be_nil

View file

@ -6,27 +6,27 @@ module Ameba::Rules
describe TrailingBlankLines do describe TrailingBlankLines do
it "passes if there is no blank lines at the end" do it "passes if there is no blank lines at the end" do
source = Source.new "no-blankline" source = Source.new "no-blankline"
subject.catch(source).valid?.should be_true subject.catch(source).should be_valid
end end
it "fails if there is a blank line at the end of a source" do it "fails if there is a blank line at the end of a source" do
source = Source.new "a = 1\n \n " source = Source.new "a = 1\n \n "
subject.catch(source).valid?.should be_false subject.catch(source).should_not be_valid
end end
it "passes if source is empty" do it "passes if source is empty" do
source = Source.new "" source = Source.new ""
subject.catch(source).valid?.should be_true subject.catch(source).should be_valid
end end
it "passes if last line is not blank" do it "passes if last line is not blank" do
source = Source.new "\n\n\n puts 22" source = Source.new "\n\n\n puts 22"
subject.catch(source).valid?.should be_true subject.catch(source).should be_valid
end end
it "reports rule, pos and message" do it "reports rule, pos and message" do
source = Source.new "a = 1\n\n " source = Source.new "a = 1\n\n "
subject.catch(source) subject.catch(source).should_not be_valid
error = source.errors.first error = source.errors.first
error.rule.should_not be_nil error.rule.should_not be_nil

View file

@ -6,17 +6,17 @@ module Ameba::Rules
describe TrailingWhitespace do describe TrailingWhitespace do
it "passes if all lines do not have trailing whitespace" do it "passes if all lines do not have trailing whitespace" do
source = Source.new "no-whispace" source = Source.new "no-whispace"
subject.catch(source).valid?.should be_true subject.catch(source).should be_valid
end end
it "fails if there is a line with trailing whitespace" do it "fails if there is a line with trailing whitespace" do
source = Source.new "whitespace at the end " source = Source.new "whitespace at the end "
subject.catch(source).valid?.should be_false subject.catch(source).should_not be_valid
end end
it "reports rule, pos and message" do it "reports rule, pos and message" do
source = Source.new "a = 1\n b = 2 " source = Source.new "a = 1\n b = 2 "
subject.catch(source).valid?.should be_false subject.catch(source).should_not be_valid
error = source.errors.first error = source.errors.first
error.rule.should_not be_nil error.rule.should_not be_nil

View file

@ -10,7 +10,7 @@ module Ameba::Rules
:ok :ok
end end
) )
subject.catch(s).valid?.should be_true subject.catch(s).should be_valid
end end
it "fails if unless has else" do it "fails if unless has else" do
@ -21,7 +21,7 @@ module Ameba::Rules
:two :two
end end
) )
subject.catch(s).valid?.should be_false subject.catch(s).should_not be_valid
end end
it "reports rule, pos and message" do it "reports rule, pos and message" do
@ -32,7 +32,7 @@ module Ameba::Rules
:two :two
end end
) )
subject.catch(s) subject.catch(s).should_not be_valid
error = s.errors.first error = s.errors.first
error.should_not be_nil error.should_not be_nil

View file

@ -1,7 +1,32 @@
require "spec" require "spec"
require "../src/ameba" require "../src/ameba"
struct DummyRule < Ameba::Rule module Ameba
struct DummyRule < Ameba::Rule
def test(source) def test(source)
end end
end
struct BeValidExpectation
def match(source)
source.valid?
end
def failure_message(source)
String.build do |str|
str << "Source expected to be valid, but there are errors:\n\n"
source.errors.each do |e|
str << " * #{e.rule.name}:#{e.pos} #{e.message}\n"
end
end
end
def negative_failure_message(source)
"Source expected to be invalid, but it is valid."
end
end
end
def be_valid
Ameba::BeValidExpectation.new
end end