mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Docs & tests
This commit is contained in:
parent
5e10113055
commit
9bba850a9b
12 changed files with 142 additions and 24 deletions
|
@ -42,10 +42,10 @@ Inspecting 7 files.
|
|||
7 inspected, 2 failures.
|
||||
|
||||
src/ameba/formatter.cr:47
|
||||
Ameba::Rule::LineLength: Line too long [122]
|
||||
LineLength: Line too long [122]
|
||||
|
||||
src/ameba.cr:18
|
||||
Ameba::Rule::LineLength: Line too long [81]
|
||||
LineLength: Line too long [81]
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
|
24
spec/ameba/rule_spec.cr
Normal file
24
spec/ameba/rule_spec.cr
Normal file
|
@ -0,0 +1,24 @@
|
|||
require "../../spec/spec_helper"
|
||||
|
||||
module Ameba
|
||||
describe RULES do
|
||||
it "contains available rules" do
|
||||
Ameba::RULES.empty?.should be_false
|
||||
end
|
||||
end
|
||||
|
||||
describe Rule do
|
||||
describe "#catch" do
|
||||
it "accepts and returns source" do
|
||||
s = Source.new "", ""
|
||||
DummyRule.new.catch(s).should eq s
|
||||
end
|
||||
end
|
||||
|
||||
describe "#name" do
|
||||
it "returns name of the rule" do
|
||||
DummyRule.new.name.should eq "DummyRule"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
28
spec/ameba/rules/line_length_spec.cr
Normal file
28
spec/ameba/rules/line_length_spec.cr
Normal file
|
@ -0,0 +1,28 @@
|
|||
require "../../spec_helper"
|
||||
|
||||
module Ameba::Rules
|
||||
subject = LineLength.new
|
||||
long_line = "*" * 80
|
||||
|
||||
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
|
||||
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
|
||||
end
|
||||
|
||||
it "reports rule, pos and message" do
|
||||
source = Source.new "", long_line
|
||||
subject.catch(source).valid?.should be_false
|
||||
|
||||
error = source.errors.first
|
||||
error.rule.should eq subject
|
||||
error.pos.should eq 1
|
||||
error.message.should eq "Line too long (80 symbols)"
|
||||
end
|
||||
end
|
||||
end
|
28
spec/ameba/source_spec.cr
Normal file
28
spec/ameba/source_spec.cr
Normal file
|
@ -0,0 +1,28 @@
|
|||
require "../spec_helper"
|
||||
|
||||
module Ameba
|
||||
describe Source do
|
||||
describe ".new" do
|
||||
it "allows to create a source by file path" do
|
||||
Source.new(__FILE__).path.should eq __FILE__
|
||||
end
|
||||
|
||||
it "allows to create a source by content and path" do
|
||||
s = Source.new(__FILE__, "content")
|
||||
s.path.should eq __FILE__
|
||||
s.lines.should eq %w(content)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#error" do
|
||||
it "adds and error" do
|
||||
s = Source.new(__FILE__)
|
||||
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!"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,9 +1,4 @@
|
|||
require "./spec_helper"
|
||||
|
||||
describe Ameba do
|
||||
# TODO: Write tests
|
||||
|
||||
it "works" do
|
||||
false.should eq(true)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,2 +1,7 @@
|
|||
require "spec"
|
||||
require "../src/ameba"
|
||||
|
||||
struct DummyRule < Ameba::Rule
|
||||
def test(source)
|
||||
end
|
||||
end
|
|
@ -1,12 +1,12 @@
|
|||
require "./ameba/*"
|
||||
require "./ameba/rule/*"
|
||||
require "./ameba/rules/*"
|
||||
|
||||
module Ameba
|
||||
extend self
|
||||
|
||||
RULES = [
|
||||
Rule::LineLength,
|
||||
]
|
||||
abstract struct BaseRule
|
||||
abstract def test(source : Source)
|
||||
end
|
||||
|
||||
def run(formatter = DotFormatter.new)
|
||||
run Dir["**/*.cr"], formatter
|
||||
|
|
|
@ -50,7 +50,7 @@ module Ameba
|
|||
failure.errors.each do |error|
|
||||
mes << "#{failure.path}:#{error.pos}"
|
||||
mes << "\n"
|
||||
mes << "#{error.rule}: #{error.message}\n\n"
|
||||
mes << "#{error.rule.name}: #{error.message}\n\n"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
17
src/ameba/rule.cr
Normal file
17
src/ameba/rule.cr
Normal file
|
@ -0,0 +1,17 @@
|
|||
module Ameba
|
||||
RULES = [
|
||||
Rules::LineLength,
|
||||
]
|
||||
|
||||
abstract struct Rule
|
||||
abstract def test(source : Source)
|
||||
|
||||
def catch(source : Source)
|
||||
source.tap { |s| test s }
|
||||
end
|
||||
|
||||
def name
|
||||
self.class.name.gsub("Ameba::Rules::", "")
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,9 +0,0 @@
|
|||
struct Ameba::Rule::LineLength
|
||||
def test(source)
|
||||
source.lines.each_with_index do |line, index|
|
||||
if line.size > 79
|
||||
source.error self, index + 1, "Line too long [#{line.size}]"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
15
src/ameba/rules/line_length.cr
Normal file
15
src/ameba/rules/line_length.cr
Normal file
|
@ -0,0 +1,15 @@
|
|||
module Ameba::Rules
|
||||
# A rule that checks the size of lines in sources.
|
||||
#
|
||||
# This rule will report an error if there is at least
|
||||
# one line longer than 79 symbols.
|
||||
struct LineLength < Rule
|
||||
def test(source)
|
||||
source.lines.each_with_index do |line, index|
|
||||
if line.size > 79
|
||||
source.error self, index + 1, "Line too long (#{line.size} symbols)"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,7 +1,14 @@
|
|||
module Ameba
|
||||
# An entity that represents a Crystal source file.
|
||||
# Has path, lines of code and errors reported by rules.
|
||||
class Source
|
||||
|
||||
# Represents an error caught by Ameba.
|
||||
#
|
||||
# Each error has the rule that created this error,
|
||||
# position of the error and a message.
|
||||
record Error,
|
||||
rule : String,
|
||||
rule : Rule,
|
||||
pos : Int32,
|
||||
message : String
|
||||
|
||||
|
@ -13,8 +20,16 @@ module Ameba
|
|||
@lines = File.read_lines(@path)
|
||||
end
|
||||
|
||||
def error(rule, line_number : Int32, message : String)
|
||||
errors << Error.new rule.class.name, line_number, message
|
||||
def initialize(@path : String, content : String)
|
||||
@lines = content.split("\n")
|
||||
end
|
||||
|
||||
def error(rule : Rule, line_number : Int32, message : String)
|
||||
errors << Error.new rule, line_number, message
|
||||
end
|
||||
|
||||
def valid?
|
||||
errors.empty?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue