shard-ameba/spec/spec_helper.cr

201 lines
4.3 KiB
Crystal
Raw Normal View History

2017-10-26 16:46:58 +00:00
require "spec"
require "../src/ameba"
2017-10-30 20:00:01 +00:00
2017-11-01 20:05:41 +00:00
module Ameba
2018-12-08 20:52:32 +00:00
# Dummy Rule which does nothing.
2021-01-18 15:45:35 +00:00
class DummyRule < Rule::Base
2017-11-22 06:44:29 +00:00
properties do
description : String = "Dummy rule that does nothing."
end
2017-11-01 20:05:41 +00:00
def test(source)
end
2017-10-30 20:00:01 +00:00
end
2017-11-30 21:50:07 +00:00
2018-09-07 12:07:03 +00:00
class Source
def initialize(code : String, @path = "", normalize = true)
@code = normalize ? normalize_source(code) : code
end
private def normalize_source(code, separator = "\n")
lines = code.split(separator)
# remove unneeded first blank lines if any
2018-09-07 12:07:03 +00:00
lines.shift if lines[0].blank? && lines.size > 1
# find the minimum indentation
min_indent = lines.min_of do |line|
line.blank? ? code.size : line.size - line.lstrip.size
end
# remove the width of minimum indentation in each line
lines
.map! { |line| line.blank? ? line : line[min_indent..-1] }
.join(separator)
end
end
2021-01-18 15:45:35 +00:00
class NamedRule < Rule::Base
properties do
2020-06-15 11:19:23 +00:00
description "A rule with a custom name."
end
def self.name
"BreakingRule"
end
end
2021-01-18 15:45:35 +00:00
class ErrorRule < Rule::Base
properties do
2020-06-15 11:19:23 +00:00
description "Always adds an error at 1:1"
end
2017-11-30 21:50:07 +00:00
def test(source)
2018-06-10 21:15:12 +00:00
issue_for({1, 1}, "This rule always adds an error")
2017-11-30 21:50:07 +00:00
end
end
2017-11-01 20:05:41 +00:00
2021-01-18 15:45:35 +00:00
class ScopeRule < Rule::Base
2020-06-15 11:19:23 +00:00
@[YAML::Field(ignore: true)]
getter scopes = [] of AST::Scope
2020-06-15 11:19:23 +00:00
properties do
description "Internal rule to test scopes"
end
def test(source, node : Crystal::ASTNode, scope : AST::Scope)
@scopes << scope
end
end
2021-01-18 15:45:35 +00:00
class FlowExpressionRule < Rule::Base
2020-06-15 11:19:23 +00:00
@[YAML::Field(ignore: true)]
getter expressions = [] of AST::FlowExpression
2020-06-15 11:19:23 +00:00
properties do
description "Internal rule to test flow expressions"
end
def test(source, node, flow_expression : AST::FlowExpression)
@expressions << flow_expression
end
end
2021-01-18 15:45:35 +00:00
class RedundantControlExpressionRule < Rule::Base
2020-06-15 11:19:23 +00:00
@[YAML::Field(ignore: true)]
2020-03-24 16:01:23 +00:00
getter nodes = [] of Crystal::ASTNode
2020-06-15 11:19:23 +00:00
properties do
description "Internal rule to test redundant control expressions"
2020-03-24 16:01:23 +00:00
end
def test(source, node, visitor : AST::RedundantControlExpressionVisitor)
nodes << node
end
end
# A rule that always raises an error
2021-01-18 15:45:35 +00:00
class RaiseRule < Rule::Base
property should_raise = false
2020-06-15 11:19:23 +00:00
properties do
description "Internal rule that always raises"
end
def test(source)
should_raise && raise "something went wrong"
end
end
class DummyFormatter < Formatter::BaseFormatter
property started_sources : Array(Source)?
property finished_sources : Array(Source)?
property started_source : Source?
property finished_source : Source?
def started(sources)
@started_sources = sources
end
def source_finished(source : Source)
@started_source = source
end
def source_started(source : Source)
@finished_source = source
end
def finished(sources)
@finished_sources = sources
end
end
2017-11-01 20:05:41 +00:00
struct BeValidExpectation
def match(source)
source.valid?
end
def failure_message(source)
String.build do |str|
2018-06-10 21:15:12 +00:00
str << "Source expected to be valid, but there are issues: \n\n"
source.issues.reject(&.disabled?).each do |e|
str << " * #{e.rule.name}: #{e.message}\n"
2017-11-01 20:05:41 +00:00
end
end
end
def negative_failure_message(source)
"Source expected to be invalid, but it is valid."
end
end
class TestNodeVisitor < Crystal::Visitor
NODES = [
Crystal::NilLiteral,
Crystal::Var,
Crystal::Assign,
Crystal::OpAssign,
Crystal::MultiAssign,
Crystal::Block,
Crystal::Macro,
Crystal::Def,
Crystal::If,
Crystal::While,
Crystal::MacroLiteral,
Crystal::Expressions,
2020-03-24 16:01:23 +00:00
Crystal::ControlExpression,
]
def initialize(node)
node.accept self
end
def visit(node : Crystal::ASTNode)
true
end
{% for node in NODES %}
{{getter_name = node.stringify.split("::").last.underscore + "_nodes"}}
getter {{getter_name.id}} = [] of {{node}}
def visit(node : {{node}})
{{getter_name.id}} << node
true
end
{% end %}
end
2017-11-01 20:05:41 +00:00
end
def be_valid
Ameba::BeValidExpectation.new
2017-11-01 10:49:03 +00:00
end
def as_node(source)
Crystal::Parser.new(source).parse
end
def as_nodes(source)
Ameba::TestNodeVisitor.new(as_node source)
end