mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Source#content -> Source#code, Source::Error#pos -> Source::Error#location
This commit is contained in:
parent
9036a7ca71
commit
e718c90f16
39 changed files with 91 additions and 90 deletions
|
@ -20,7 +20,7 @@ module Ameba::Formatter
|
|||
|
||||
it "writes invalid source" do
|
||||
s = Source.new ""
|
||||
s.error DummyRule.new, 3, "message"
|
||||
s.error DummyRule.new, nil, "message"
|
||||
subject.source_finished s
|
||||
output.to_s.should contain "F"
|
||||
end
|
||||
|
|
|
@ -63,12 +63,12 @@ module Ameba::Rules
|
|||
end
|
||||
|
||||
it "reports rule, pos and message" do
|
||||
source = Source.new "a != true"
|
||||
source = Source.new "a != true", "source.cr"
|
||||
subject.catch(source)
|
||||
|
||||
error = source.errors.first
|
||||
error.rule.should_not be_nil
|
||||
error.pos.should eq 1
|
||||
error.location.to_s.should eq "source.cr:1:1"
|
||||
error.message.should eq "Comparison to a boolean is pointless"
|
||||
end
|
||||
end
|
||||
|
@ -100,12 +100,12 @@ module Ameba::Rules
|
|||
end
|
||||
|
||||
it "reports rule, pos and message" do
|
||||
source = Source.new "true != a"
|
||||
source = Source.new "true != a", "source.cr"
|
||||
subject.catch(source).should_not be_valid
|
||||
|
||||
error = source.errors.first
|
||||
error.rule.should_not be_nil
|
||||
error.pos.should eq 1
|
||||
error.location.to_s.should eq "source.cr:1:1"
|
||||
error.message.should eq "Comparison to a boolean is pointless"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,9 +3,9 @@ require "../../spec_helper"
|
|||
module Ameba
|
||||
subject = Rules::ConstantNames.new
|
||||
|
||||
private def it_reports_constant(content, expected)
|
||||
private def it_reports_constant(code, expected)
|
||||
it "reports constant name #{expected}" do
|
||||
s = Source.new content
|
||||
s = Source.new code
|
||||
Rules::ConstantNames.new.catch(s).should_not be_valid
|
||||
s.errors.first.message.should contain expected
|
||||
end
|
||||
|
@ -38,11 +38,11 @@ module Ameba
|
|||
it "reports rule, pos and message" do
|
||||
s = Source.new %(
|
||||
Const = 1
|
||||
)
|
||||
), "source.cr"
|
||||
subject.catch(s).should_not be_valid
|
||||
error = s.errors.first
|
||||
error.rule.should_not be_nil
|
||||
error.pos.should eq 2
|
||||
error.location.to_s.should eq "source.cr:2:9"
|
||||
error.message.should eq(
|
||||
"Constant name should be screaming-cased: CONST, not Const"
|
||||
)
|
||||
|
|
|
@ -32,12 +32,12 @@ module Ameba::Rules
|
|||
end
|
||||
|
||||
it "reports rule, pos and message" do
|
||||
s = Source.new "debugger"
|
||||
s = Source.new "debugger", "source.cr"
|
||||
subject.catch(s).should_not be_valid
|
||||
|
||||
error = s.errors.first
|
||||
error.rule.should_not be_nil
|
||||
error.pos.should eq 1
|
||||
error.location.to_s.should eq "source.cr:1:1"
|
||||
error.message.should eq "Possible forgotten debugger statement detected"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -70,15 +70,15 @@ module Ameba
|
|||
end
|
||||
)
|
||||
|
||||
it "reports rule, pos and message" do
|
||||
it "reports rule, location and message" do
|
||||
s = Source.new %(
|
||||
if ()
|
||||
end
|
||||
)
|
||||
), "source.cr"
|
||||
subject.catch(s).should_not be_valid
|
||||
error = s.errors.first
|
||||
error.rule.should_not be_nil
|
||||
error.pos.should eq 2
|
||||
error.location.to_s.should eq "source.cr:2:12"
|
||||
error.message.should eq "Avoid empty expression '()'"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -107,11 +107,11 @@ module Ameba
|
|||
it "reports rule, pos and message" do
|
||||
s = Source.new %q(
|
||||
1200000
|
||||
)
|
||||
), "source.cr"
|
||||
subject.catch(s).should_not be_valid
|
||||
error = s.errors.first
|
||||
error.rule.should_not be_nil
|
||||
error.pos.should eq 2
|
||||
error.location.to_s.should eq "source.cr:2:10"
|
||||
error.message.should match /1_200_000/
|
||||
end
|
||||
end
|
||||
|
|
|
@ -21,13 +21,13 @@ module Ameba::Rules
|
|||
end
|
||||
|
||||
it "reports rule, pos and message" do
|
||||
source = Source.new long_line
|
||||
source = Source.new long_line, "source.cr"
|
||||
subject.catch(source).should_not be_valid
|
||||
|
||||
error = source.errors.first
|
||||
error.rule.should eq subject
|
||||
error.pos.should eq 1
|
||||
error.message.should eq "Line too long (81 symbols)"
|
||||
error.location.to_s.should eq "source.cr:1:81"
|
||||
error.message.should eq "Line too long"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -59,13 +59,13 @@ module Ameba::Rules
|
|||
it "reports rule, pos and message" do
|
||||
s = Source.new %(
|
||||
puts "hello" if true
|
||||
)
|
||||
), "source.cr"
|
||||
subject.catch(s).should_not be_valid
|
||||
|
||||
s.errors.size.should eq 1
|
||||
error = s.errors.first
|
||||
error.rule.should_not be_nil
|
||||
error.pos.should eq 2
|
||||
error.location.to_s.should eq "source.cr:2:9"
|
||||
error.message.should eq "Literal value found in conditional"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -29,12 +29,12 @@ module Ameba::Rules
|
|||
end
|
||||
|
||||
it "reports rule, pos and message" do
|
||||
s = Source.new %q("#{4}")
|
||||
s = Source.new %q("#{4}"), "source.cr"
|
||||
subject.catch(s).should_not be_valid
|
||||
|
||||
error = s.errors.first
|
||||
error.rule.should_not be_nil
|
||||
error.pos.should eq 1
|
||||
error.location.to_s.should eq "source.cr:1:1"
|
||||
error.message.should eq "Literal value found in interpolation"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,9 +3,9 @@ require "../../spec_helper"
|
|||
module Ameba
|
||||
subject = Rules::MethodNames.new
|
||||
|
||||
private def it_reports_method_name(content, expected)
|
||||
private def it_reports_method_name(code, expected)
|
||||
it "reports method name #{expected}" do
|
||||
s = Source.new content
|
||||
s = Source.new code
|
||||
Rules::MethodNames.new.catch(s).should_not be_valid
|
||||
s.errors.first.message.should contain expected
|
||||
end
|
||||
|
@ -42,11 +42,11 @@ module Ameba
|
|||
s = Source.new %(
|
||||
def bad_Name
|
||||
end
|
||||
)
|
||||
), "source.cr"
|
||||
subject.catch(s).should_not be_valid
|
||||
error = s.errors.first
|
||||
error.rule.should_not be_nil
|
||||
error.pos.should eq 2
|
||||
error.location.to_s.should eq "source.cr:2:9"
|
||||
error.message.should eq(
|
||||
"Method name should be underscore-cased: bad_name, not bad_Name"
|
||||
)
|
||||
|
|
|
@ -56,12 +56,12 @@ module Ameba::Rules
|
|||
end
|
||||
|
||||
it "reports rule, pos and message" do
|
||||
s = Source.new ":nok unless !s.empty?"
|
||||
s = Source.new ":nok unless !s.empty?", "source.cr"
|
||||
subject.catch(s).should_not be_valid
|
||||
|
||||
error = s.errors.first
|
||||
error.rule.should_not be_nil
|
||||
error.pos.should eq 1
|
||||
error.location.to_s.should eq "source.cr:1:1"
|
||||
error.message.should eq "Avoid negated conditions in unless blocks"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -32,12 +32,12 @@ module Ameba::Rules
|
|||
true
|
||||
end
|
||||
end
|
||||
)
|
||||
), "source.cr"
|
||||
subject.catch(s).should_not be_valid
|
||||
|
||||
error = s.errors.first
|
||||
error.rule.should_not be_nil
|
||||
error.pos.should eq 3
|
||||
error.location.to_s.should eq "source.cr:3:11"
|
||||
error.message.should eq(
|
||||
"Favour method name 'picture?' over 'has_picture?'")
|
||||
end
|
||||
|
|
|
@ -25,12 +25,12 @@ module Ameba::Rules
|
|||
end
|
||||
|
||||
it "reports rule, pos and message" do
|
||||
source = Source.new "a = 1\n\n "
|
||||
source = Source.new "a = 1\n\n ", "source.cr"
|
||||
subject.catch(source).should_not be_valid
|
||||
|
||||
error = source.errors.first
|
||||
error.rule.should_not be_nil
|
||||
error.pos.should eq 3
|
||||
error.location.to_s.should eq "source.cr:3:1"
|
||||
error.message.should eq "Blank lines detected at the end of the file"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -15,12 +15,12 @@ module Ameba::Rules
|
|||
end
|
||||
|
||||
it "reports rule, pos and message" do
|
||||
source = Source.new "a = 1\n b = 2 "
|
||||
source = Source.new "a = 1\n b = 2 ", "source.cr"
|
||||
subject.catch(source).should_not be_valid
|
||||
|
||||
error = source.errors.first
|
||||
error.rule.should_not be_nil
|
||||
error.pos.should eq 2
|
||||
error.location.to_s.should eq "source.cr:2:7"
|
||||
error.message.should eq "Trailing whitespace detected"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,9 +3,9 @@ require "../../spec_helper"
|
|||
module Ameba
|
||||
subject = Rules::TypeNames.new
|
||||
|
||||
private def it_reports_name(content, expected)
|
||||
private def it_reports_name(code, expected)
|
||||
it "reports type name #{expected}" do
|
||||
s = Source.new content
|
||||
s = Source.new code
|
||||
Rules::TypeNames.new.catch(s).should_not be_valid
|
||||
s.errors.first.message.should contain expected
|
||||
end
|
||||
|
@ -47,11 +47,11 @@ module Ameba
|
|||
s = Source.new %(
|
||||
class My_class
|
||||
end
|
||||
)
|
||||
), "source.cr"
|
||||
subject.catch(s).should_not be_valid
|
||||
error = s.errors.first
|
||||
error.rule.should_not be_nil
|
||||
error.pos.should eq 2
|
||||
error.location.to_s.should eq "source.cr:2:9"
|
||||
error.message.should eq(
|
||||
"Type name should be camelcased: MyClass, but it was My_class"
|
||||
)
|
||||
|
|
|
@ -31,13 +31,13 @@ module Ameba::Rules
|
|||
else
|
||||
:two
|
||||
end
|
||||
)
|
||||
), "source.cr"
|
||||
subject.catch(s).should_not be_valid
|
||||
|
||||
error = s.errors.first
|
||||
error.should_not be_nil
|
||||
error.rule.should_not be_nil
|
||||
error.pos.should eq 2
|
||||
error.location.to_s.should eq "source.cr:2:9"
|
||||
error.message.should eq "Favour if over unless with else"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,9 +3,9 @@ require "../../spec_helper"
|
|||
module Ameba
|
||||
subject = Rules::VariableNames.new
|
||||
|
||||
private def it_reports_var_name(content, expected)
|
||||
private def it_reports_var_name(code, expected)
|
||||
it "reports method name #{expected}" do
|
||||
s = Source.new content
|
||||
s = Source.new code
|
||||
Rules::VariableNames.new.catch(s).should_not be_valid
|
||||
s.errors.first.message.should contain expected
|
||||
end
|
||||
|
@ -48,11 +48,11 @@ module Ameba
|
|||
it "reports rule, pos and message" do
|
||||
s = Source.new %(
|
||||
badName = "Yeah"
|
||||
)
|
||||
), "source.cr"
|
||||
subject.catch(s).should_not be_valid
|
||||
error = s.errors.first
|
||||
error.rule.should_not be_nil
|
||||
error.pos.should eq 2
|
||||
error.location.to_s.should eq "source.cr:2:9"
|
||||
error.message.should eq(
|
||||
"Var name should be underscore-cased: bad_name, not badName"
|
||||
)
|
||||
|
|
|
@ -3,22 +3,24 @@ require "../spec_helper"
|
|||
module Ameba
|
||||
describe Source do
|
||||
describe ".new" do
|
||||
it "allows to create a source by content and path" do
|
||||
s = Source.new("content", "path")
|
||||
it "allows to create a source by code and path" do
|
||||
s = Source.new("code", "path")
|
||||
s.path.should eq "path"
|
||||
s.content.should eq "content"
|
||||
s.lines.should eq ["content"]
|
||||
s.code.should eq "code"
|
||||
s.lines.should eq ["code"]
|
||||
end
|
||||
end
|
||||
|
||||
describe "#error" do
|
||||
it "adds and error" do
|
||||
s = Source.new ""
|
||||
s.error(DummyRule.new, 23, "Error!")
|
||||
s.errors.size.should eq 1
|
||||
s.errors.first.rule.should_not be_nil
|
||||
s.errors.first.pos.should eq 23
|
||||
s.errors.first.message.should eq "Error!"
|
||||
s = Source.new "", "source.cr"
|
||||
s.error(DummyRule.new, s.location(23, 2), "Error!")
|
||||
s.should_not be_valid
|
||||
|
||||
error = s.errors.first
|
||||
error.rule.should_not be_nil
|
||||
error.location.to_s.should eq "source.cr:23:2"
|
||||
error.message.should eq "Error!"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -16,7 +16,7 @@ module Ameba
|
|||
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"
|
||||
str << " * #{e.rule.name}: #{e.message}\n"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue