From c9f229c3f1d05a086d4bc417f4ef3698efa819fa Mon Sep 17 00:00:00 2001 From: Vitalii Elenhaupt Date: Wed, 1 Nov 2017 22:05:41 +0200 Subject: [PATCH] Custom matcher --- spec/ameba/rule_spec.cr | 2 +- .../ameba/rules/comparison_to_boolean_spec.cr | 16 +++++----- spec/ameba/rules/debugger_statement_spec.cr | 6 ++-- spec/ameba/rules/line_length_spec.cr | 8 ++--- spec/ameba/rules/literal_in_condition_spec.cr | 10 +++---- .../rules/literal_in_interpolation_spec.cr | 6 ++-- .../negated_conditions_in_unless_spec.cr | 12 ++++---- spec/ameba/rules/predicate_name_spec.cr | 6 ++-- spec/ameba/rules/trailing_blank_lines_spec.cr | 10 +++---- spec/ameba/rules/trailing_whitespace_spec.cr | 6 ++-- spec/ameba/rules/unless_else_spec.cr | 6 ++-- spec/spec_helper.cr | 29 +++++++++++++++++-- 12 files changed, 71 insertions(+), 46 deletions(-) diff --git a/spec/ameba/rule_spec.cr b/spec/ameba/rule_spec.cr index 91513784..cc86580a 100644 --- a/spec/ameba/rule_spec.cr +++ b/spec/ameba/rule_spec.cr @@ -11,7 +11,7 @@ module Ameba describe "#name" do it "returns name of the rule" do - DummyRule.new.name.should eq "DummyRule" + DummyRule.new.name.should eq "Ameba::DummyRule" end end diff --git a/spec/ameba/rules/comparison_to_boolean_spec.cr b/spec/ameba/rules/comparison_to_boolean_spec.cr index 0e068930..34d938dd 100644 --- a/spec/ameba/rules/comparison_to_boolean_spec.cr +++ b/spec/ameba/rules/comparison_to_boolean_spec.cr @@ -33,7 +33,7 @@ module Ameba::Rules :not_ok end ) - subject.catch(source).valid?.should be_true + subject.catch(source).should be_valid end context "boolean on the right" do @@ -43,7 +43,7 @@ module Ameba::Rules :ok end ) - subject.catch(source).valid?.should be_false + subject.catch(source).should_not be_valid end it "fails if there is != comparison to boolean" do @@ -52,14 +52,14 @@ module Ameba::Rules :ok end ) - subject.catch(source).valid?.should be_false + subject.catch(source).should_not be_valid end it "fails if there is case comparison to boolean" do source = Source.new %( a === true ) - subject.catch(source).valid?.should be_false + subject.catch(source).should_not be_valid end it "reports rule, pos and message" do @@ -80,7 +80,7 @@ module Ameba::Rules :ok end ) - subject.catch(source).valid?.should be_false + subject.catch(source).should_not be_valid end it "fails if there is != comparison to boolean" do @@ -89,19 +89,19 @@ module Ameba::Rules :ok end ) - subject.catch(source).valid?.should be_false + subject.catch(source).should_not be_valid end it "fails if there is case comparison to boolean" do source = Source.new %( true === a ) - subject.catch(source).valid?.should be_false + subject.catch(source).should_not be_valid end it "reports rule, pos and message" do source = Source.new "true != a" - subject.catch(source) + subject.catch(source).should_not be_valid error = source.errors.first error.rule.should_not be_nil diff --git a/spec/ameba/rules/debugger_statement_spec.cr b/spec/ameba/rules/debugger_statement_spec.cr index 85e8b132..2b14f7ac 100644 --- a/spec/ameba/rules/debugger_statement_spec.cr +++ b/spec/ameba/rules/debugger_statement_spec.cr @@ -19,7 +19,7 @@ module Ameba::Rules end A.new.debugger ) - subject.catch(s).valid?.should be_true + subject.catch(s).should be_valid end it "fails if there is a debugger statement" do @@ -28,12 +28,12 @@ module Ameba::Rules debugger a = a + 1 ) - subject.catch(s).valid?.should be_false + subject.catch(s).should_not be_valid end it "reports rule, pos and message" do s = Source.new "debugger" - subject.catch(s).valid?.should be_false + subject.catch(s).should_not be_valid error = s.errors.first error.rule.should_not be_nil diff --git a/spec/ameba/rules/line_length_spec.cr b/spec/ameba/rules/line_length_spec.cr index 23b25098..b34bde5c 100644 --- a/spec/ameba/rules/line_length_spec.cr +++ b/spec/ameba/rules/line_length_spec.cr @@ -7,22 +7,22 @@ module Ameba::Rules describe LineLength do it "passes if all lines are shorter than 80 symbols" do source = Source.new "short line" - subject.catch(source).valid?.should be_true + subject.catch(source).should be_valid end it "passes if line consists of 79 symbols" do source = Source.new "*" * 79 - subject.catch(source).valid?.should be_true + subject.catch(source).should be_valid end it "fails if there is at least one line longer than 79 symbols" do source = Source.new long_line - subject.catch(source).valid?.should be_false + subject.catch(source).should_not be_valid end it "reports rule, pos and message" do source = Source.new long_line - subject.catch(source).valid?.should be_false + subject.catch(source).should_not be_valid error = source.errors.first error.rule.should eq subject diff --git a/spec/ameba/rules/literal_in_condition_spec.cr b/spec/ameba/rules/literal_in_condition_spec.cr index a8fb0e09..c993fcb5 100644 --- a/spec/ameba/rules/literal_in_condition_spec.cr +++ b/spec/ameba/rules/literal_in_condition_spec.cr @@ -23,7 +23,7 @@ module Ameba::Rules :ok end ) - subject.catch(s).valid?.should be_true + subject.catch(s).should be_valid end it "fails if there is a predicate in if conditional" do @@ -32,7 +32,7 @@ module Ameba::Rules :ok end ) - subject.catch(s).valid?.should be_false + subject.catch(s).should_not be_valid end it "fails if there is a predicate in unless conditional" do @@ -41,7 +41,7 @@ module Ameba::Rules :ok end ) - subject.catch(s).valid?.should be_false + subject.catch(s).should_not be_valid end it "fails if there is a predicate in case conditional" do @@ -53,14 +53,14 @@ module Ameba::Rules :also_ok end ) - subject.catch(s).valid?.should be_false + subject.catch(s).should_not be_valid end it "reports rule, pos and message" do s = Source.new %( puts "hello" if true ) - subject.catch(s) + subject.catch(s).should_not be_valid s.errors.size.should eq 1 error = s.errors.first diff --git a/spec/ameba/rules/literal_in_interpolation_spec.cr b/spec/ameba/rules/literal_in_interpolation_spec.cr index 2977845e..1438ec42 100644 --- a/spec/ameba/rules/literal_in_interpolation_spec.cr +++ b/spec/ameba/rules/literal_in_interpolation_spec.cr @@ -13,7 +13,7 @@ module Ameba::Rules "Name size: #{name.size}" ) - subject.catch(s).valid?.should be_true + subject.catch(s).should be_valid end it "fails if there is useless interpolation" do @@ -24,13 +24,13 @@ module Ameba::Rules %q("#{false}"), %q("here are #{4} cats"), ].each do |str| - subject.catch(Source.new str).valid?.should be_false + subject.catch(Source.new str).should_not be_valid end end it "reports rule, pos and message" do s = Source.new %q("#{4}") - subject.catch(s).valid?.should be_false + subject.catch(s).should_not be_valid error = s.errors.first error.rule.should_not be_nil diff --git a/spec/ameba/rules/negated_conditions_in_unless_spec.cr b/spec/ameba/rules/negated_conditions_in_unless_spec.cr index 5c29ae49..4ea3474c 100644 --- a/spec/ameba/rules/negated_conditions_in_unless_spec.cr +++ b/spec/ameba/rules/negated_conditions_in_unless_spec.cr @@ -16,7 +16,7 @@ module Ameba::Rules :ok end ) - subject.catch(s).valid?.should be_true + subject.catch(s).should be_valid end it "fails if there is a negated condition in unless" do @@ -25,7 +25,7 @@ module Ameba::Rules :nok end ) - subject.catch(s).valid?.should be_false + subject.catch(s).should_not be_valid end it "fails if one of AND conditions is negated" do @@ -34,7 +34,7 @@ module Ameba::Rules :nok end ) - subject.catch(s).valid?.should be_false + subject.catch(s).should_not be_valid end it "fails if one of OR conditions is negated" do @@ -43,7 +43,7 @@ module Ameba::Rules :nok end ) - subject.catch(s).valid?.should be_false + subject.catch(s).should_not be_valid end it "fails if one of inner conditions is negated" do @@ -52,12 +52,12 @@ module Ameba::Rules :nok end ) - subject.catch(s).valid?.should be_false + subject.catch(s).should_not be_valid end it "reports rule, pos and message" do 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.rule.should_not be_nil diff --git a/spec/ameba/rules/predicate_name_spec.cr b/spec/ameba/rules/predicate_name_spec.cr index 7e7ab3c6..70902a20 100644 --- a/spec/ameba/rules/predicate_name_spec.cr +++ b/spec/ameba/rules/predicate_name_spec.cr @@ -14,7 +14,7 @@ module Ameba::Rules end end ) - subject.catch(s).valid?.should be_true + subject.catch(s).should be_valid end it "fails if predicate name is wrong" do @@ -22,7 +22,7 @@ module Ameba::Rules def is_valid?(x) end ) - subject.catch(s).valid?.should be_false + subject.catch(s).should_not be_valid end it "reports rule, pos and message" do @@ -33,7 +33,7 @@ module Ameba::Rules end end ) - subject.catch(s).valid?.should be_false + subject.catch(s).should_not be_valid error = s.errors.first error.rule.should_not be_nil diff --git a/spec/ameba/rules/trailing_blank_lines_spec.cr b/spec/ameba/rules/trailing_blank_lines_spec.cr index 1c7e5572..abb1ef16 100644 --- a/spec/ameba/rules/trailing_blank_lines_spec.cr +++ b/spec/ameba/rules/trailing_blank_lines_spec.cr @@ -6,27 +6,27 @@ module Ameba::Rules describe TrailingBlankLines do it "passes if there is no blank lines at the end" do source = Source.new "no-blankline" - subject.catch(source).valid?.should be_true + subject.catch(source).should be_valid end it "fails if there is a blank line at the end of a source" do source = Source.new "a = 1\n \n " - subject.catch(source).valid?.should be_false + subject.catch(source).should_not be_valid end it "passes if source is empty" do source = Source.new "" - subject.catch(source).valid?.should be_true + subject.catch(source).should be_valid end it "passes if last line is not blank" do source = Source.new "\n\n\n puts 22" - subject.catch(source).valid?.should be_true + subject.catch(source).should be_valid end it "reports rule, pos and message" do source = Source.new "a = 1\n\n " - subject.catch(source) + subject.catch(source).should_not be_valid error = source.errors.first error.rule.should_not be_nil diff --git a/spec/ameba/rules/trailing_whitespace_spec.cr b/spec/ameba/rules/trailing_whitespace_spec.cr index c88d0868..2bebc298 100644 --- a/spec/ameba/rules/trailing_whitespace_spec.cr +++ b/spec/ameba/rules/trailing_whitespace_spec.cr @@ -6,17 +6,17 @@ module Ameba::Rules describe TrailingWhitespace do it "passes if all lines do not have trailing whitespace" do source = Source.new "no-whispace" - subject.catch(source).valid?.should be_true + subject.catch(source).should be_valid end it "fails if there is a line with trailing whitespace" do source = Source.new "whitespace at the end " - subject.catch(source).valid?.should be_false + subject.catch(source).should_not be_valid end it "reports rule, pos and message" do 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.rule.should_not be_nil diff --git a/spec/ameba/rules/unless_else_spec.cr b/spec/ameba/rules/unless_else_spec.cr index ebd40066..0dead8e2 100644 --- a/spec/ameba/rules/unless_else_spec.cr +++ b/spec/ameba/rules/unless_else_spec.cr @@ -10,7 +10,7 @@ module Ameba::Rules :ok end ) - subject.catch(s).valid?.should be_true + subject.catch(s).should be_valid end it "fails if unless has else" do @@ -21,7 +21,7 @@ module Ameba::Rules :two end ) - subject.catch(s).valid?.should be_false + subject.catch(s).should_not be_valid end it "reports rule, pos and message" do @@ -32,7 +32,7 @@ module Ameba::Rules :two end ) - subject.catch(s) + subject.catch(s).should_not be_valid error = s.errors.first error.should_not be_nil diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 519c1729..19b5e278 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -1,7 +1,32 @@ require "spec" require "../src/ameba" -struct DummyRule < Ameba::Rule - def test(source) +module Ameba + struct DummyRule < Ameba::Rule + def test(source) + 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