2018-06-16 11:50:59 +00:00
|
|
|
require "../../../spec_helper"
|
2018-03-08 16:55:35 +00:00
|
|
|
|
2018-06-16 11:50:59 +00:00
|
|
|
module Ameba::Rule::Lint
|
2018-03-08 16:55:35 +00:00
|
|
|
describe RandZero do
|
|
|
|
subject = RandZero.new
|
|
|
|
|
|
|
|
it "passes if it is not rand(1) or rand(0)" do
|
2022-04-04 19:14:45 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2018-03-08 16:55:35 +00:00
|
|
|
rand(1.0)
|
|
|
|
rand(0.11)
|
|
|
|
rand(2)
|
2022-04-04 19:14:45 +00:00
|
|
|
CRYSTAL
|
2018-03-08 16:55:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if it is rand(0)" do
|
2022-04-04 19:14:45 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
|
|
|
rand(0)
|
|
|
|
# ^^^^^ error: rand(0) always returns 0
|
|
|
|
CRYSTAL
|
2018-03-08 16:55:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if it is rand(1)" do
|
2022-04-04 19:14:45 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
|
|
|
rand(1)
|
|
|
|
# ^^^^^ error: rand(1) always returns 0
|
|
|
|
CRYSTAL
|
2018-03-08 16:55:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports rule, location and a message" do
|
|
|
|
s = Source.new "rand(1)", "source.cr"
|
|
|
|
subject.catch(s).should_not be_valid
|
2018-06-10 21:15:12 +00:00
|
|
|
issue = s.issues.first
|
2018-03-08 16:55:35 +00:00
|
|
|
|
2018-06-10 21:15:12 +00:00
|
|
|
issue.rule.should_not be_nil
|
|
|
|
issue.location.to_s.should eq "source.cr:1:1"
|
2018-11-24 17:38:13 +00:00
|
|
|
issue.end_location.to_s.should eq "source.cr:1:7"
|
2018-06-10 21:15:12 +00:00
|
|
|
issue.message.should eq "rand(1) always returns 0"
|
2018-03-08 16:55:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|