Docs & tests

This commit is contained in:
Vitalii Elenhaupt 2017-10-30 22:00:01 +02:00
parent 5e10113055
commit 9bba850a9b
No known key found for this signature in database
GPG key ID: 7558EF3A4056C706
12 changed files with 142 additions and 24 deletions

View file

@ -42,10 +42,10 @@ Inspecting 7 files.
7 inspected, 2 failures. 7 inspected, 2 failures.
src/ameba/formatter.cr:47 src/ameba/formatter.cr:47
Ameba::Rule::LineLength: Line too long [122] LineLength: Line too long [122]
src/ameba.cr:18 src/ameba.cr:18
Ameba::Rule::LineLength: Line too long [81] LineLength: Line too long [81]
``` ```
## Contributing ## Contributing

24
spec/ameba/rule_spec.cr Normal file
View 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

View 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
View 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

View file

@ -1,9 +1,4 @@
require "./spec_helper" require "./spec_helper"
describe Ameba do describe Ameba do
# TODO: Write tests
it "works" do
false.should eq(true)
end
end end

View file

@ -1,2 +1,7 @@
require "spec" require "spec"
require "../src/ameba" require "../src/ameba"
struct DummyRule < Ameba::Rule
def test(source)
end
end

View file

@ -1,12 +1,12 @@
require "./ameba/*" require "./ameba/*"
require "./ameba/rule/*" require "./ameba/rules/*"
module Ameba module Ameba
extend self extend self
RULES = [ abstract struct BaseRule
Rule::LineLength, abstract def test(source : Source)
] end
def run(formatter = DotFormatter.new) def run(formatter = DotFormatter.new)
run Dir["**/*.cr"], formatter run Dir["**/*.cr"], formatter

View file

@ -50,7 +50,7 @@ module Ameba
failure.errors.each do |error| failure.errors.each do |error|
mes << "#{failure.path}:#{error.pos}" mes << "#{failure.path}:#{error.pos}"
mes << "\n" mes << "\n"
mes << "#{error.rule}: #{error.message}\n\n" mes << "#{error.rule.name}: #{error.message}\n\n"
end end
end end
end end

17
src/ameba/rule.cr Normal file
View 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

View file

@ -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

View 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

View file

@ -1,7 +1,14 @@
module Ameba module Ameba
# An entity that represents a Crystal source file.
# Has path, lines of code and errors reported by rules.
class Source 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, record Error,
rule : String, rule : Rule,
pos : Int32, pos : Int32,
message : String message : String
@ -13,8 +20,16 @@ module Ameba
@lines = File.read_lines(@path) @lines = File.read_lines(@path)
end end
def error(rule, line_number : Int32, message : String) def initialize(@path : String, content : String)
errors << Error.new rule.class.name, line_number, message @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 end
end end