Raise TypeError in Livecheck DSL for invalid arg

We previously added `raise TypeError` logic to the `Livecheck` DSL's
`#strategy` method. This updates the existing methods to follow suit
and modifies the tests accordingly.
This commit is contained in:
Sam Ford 2020-08-07 17:25:08 -04:00
parent e0ffc45954
commit b99e8626e2
No known key found for this signature in database
GPG Key ID: 95209E46C7FFDEFE
2 changed files with 36 additions and 7 deletions

View File

@ -22,9 +22,14 @@ class Livecheck
# Sets the regex instance variable to the argument given, returns the
# regex instance variable when no argument is given.
def regex(pattern = nil)
return @regex if pattern.nil?
@regex = pattern
case pattern
when nil
@regex
when Regexp
@regex = pattern
else
raise TypeError, "Livecheck#regex expects a Regexp"
end
end
# Sets the skip instance variable to true, indicating that livecheck
@ -32,8 +37,13 @@ class Livecheck
# its value is assigned to the skip_msg instance variable, else nil is
# assigned.
def skip(skip_msg = nil)
if skip_msg.is_a?(String)
@skip_msg = skip_msg
elsif skip_msg.present?
raise TypeError, "Livecheck#skip expects a String"
end
@skip = true
@skip_msg = skip_msg.presence
end
# Should livecheck be skipped for the formula?
@ -60,15 +70,17 @@ class Livecheck
# Sets the url instance variable to the argument given, returns the url
# instance variable when no argument is given.
def url(val = nil)
return @url if val.nil?
@url = case val
when nil
return @url
when :head, :stable, :devel
@formula.send(val).url
when :homepage
@formula.homepage
else
when String
val
else
raise TypeError, "Livecheck#url expects a String or valid Symbol"
end
end

View File

@ -20,6 +20,12 @@ describe Livecheck do
livecheckable.regex(/foo/)
expect(livecheckable.regex).to eq(/foo/)
end
it "raises a TypeError if the argument isn't a Regexp" do
expect {
livecheckable.regex("foo")
}.to raise_error(TypeError, "Livecheck#regex expects a Regexp")
end
end
describe "#skip" do
@ -34,6 +40,12 @@ describe Livecheck do
expect(livecheckable.instance_variable_get(:@skip)).to be true
expect(livecheckable.instance_variable_get(:@skip_msg)).to eq("foo")
end
it "raises a TypeError if the argument isn't a String" do
expect {
livecheckable.skip(/foo/)
}.to raise_error(TypeError, "Livecheck#skip expects a String")
end
end
describe "#skip?" do
@ -69,6 +81,11 @@ describe Livecheck do
it "returns the URL if set" do
livecheckable.url("foo")
expect(livecheckable.url).to eq("foo")
it "raises a TypeError if the argument isn't a String or Symbol" do
expect {
livecheckable.url(/foo/)
}.to raise_error(TypeError, "Livecheck#url expects a String or valid Symbol")
end
end