Skip to content

Commit

Permalink
Add specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija committed Oct 7, 2024
1 parent 908d9dd commit 1af3092
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
7 changes: 7 additions & 0 deletions spec/ameba/cli/cmd_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ module Ameba::Cli
end
end

%w[-u --up-to-version].each do |flag|
it "accepts #{flag} flag" do
c = Cli.parse_args [flag, "1.5.0"]
c.version.should eq "1.5.0"
end
end

it "accepts --only flag" do
c = Cli.parse_args ["--only", "RULE1,RULE2"]
c.only.should eq %w[RULE1 RULE2]
Expand Down
27 changes: 27 additions & 0 deletions spec/ameba/config_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ module Ameba
it "loads custom config" do
config = Config.load config_sample
config.should_not be_nil
config.version.should_not be_nil
config.globs.should_not be_nil
config.formatter.should_not be_nil
end
Expand All @@ -93,6 +94,7 @@ module Ameba
it "loads default config" do
config = Config.load
config.should_not be_nil
config.version.should be_nil
config.globs.should_not be_nil
config.formatter.should_not be_nil
end
Expand Down Expand Up @@ -169,6 +171,31 @@ module Ameba
end
end

describe "#version, version=" do
config = Config.load config_sample
version = SemanticVersion.parse("1.5.0")

it "contains default version" do
config.version.should_not be_nil
end

it "allows to set version" do
config.version = version
config.version.should eq version
end

it "allows to set version using a string" do
config.version = version.to_s
config.version.should eq version
end

it "raises an error if version is not valid" do
expect_raises(Exception) do
config.version = "foo"
end
end
end

describe "#update_rule" do
config = Config.load config_sample

Expand Down
15 changes: 15 additions & 0 deletions spec/ameba/runner_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Ameba
config.formatter = formatter
config.globs = files

config.update_rule VersionedRule.rule_name, enabled: false
config.update_rule ErrorRule.rule_name, enabled: false
config.update_rule PerfRule.rule_name, enabled: false
config.update_rule AtoAA.rule_name, enabled: false
Expand Down Expand Up @@ -48,6 +49,20 @@ module Ameba
formatter.finished_source.should_not be_nil
end

it "checks accordingly to the rule #since_version" do
rules = [VersionedRule.new] of Rule::Base
source = Source.new "", "source.cr"

v1_0_0 = SemanticVersion.parse("1.0.0")
Runner.new(rules, [source], formatter, default_severity, false, v1_0_0).run.success?.should be_true

v1_5_0 = SemanticVersion.parse("1.5.0")
Runner.new(rules, [source], formatter, default_severity, false, v1_5_0).run.success?.should be_false

v1_10_0 = SemanticVersion.parse("1.10.0")
Runner.new(rules, [source], formatter, default_severity, false, v1_10_0).run.success?.should be_false
end

it "skips rule check if source is excluded" do
path = "source.cr"
source = Source.new "", path
Expand Down
2 changes: 2 additions & 0 deletions spec/fixtures/config.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
version: "1.5.0"

Lint/ComparisonToBoolean:
Enabled: true
11 changes: 11 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ module Ameba
end
end

class VersionedRule < Rule::Base
properties do
since_version "1.5.0"
description "Rule with a custom version."
end

def test(source)
issue_for({1, 1}, "This rule always adds an error")
end
end

# Rule extended description
class ErrorRule < Rule::Base
properties do
Expand Down

0 comments on commit 1af3092

Please sign in to comment.