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 # Sets the regex instance variable to the argument given, returns the
# regex instance variable when no argument is given. # regex instance variable when no argument is given.
def regex(pattern = nil) def regex(pattern = nil)
return @regex if pattern.nil? case pattern
when nil
@regex = pattern @regex
when Regexp
@regex = pattern
else
raise TypeError, "Livecheck#regex expects a Regexp"
end
end end
# Sets the skip instance variable to true, indicating that livecheck # 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 # its value is assigned to the skip_msg instance variable, else nil is
# assigned. # assigned.
def skip(skip_msg = nil) 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 = true
@skip_msg = skip_msg.presence
end end
# Should livecheck be skipped for the formula? # 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 # Sets the url instance variable to the argument given, returns the url
# instance variable when no argument is given. # instance variable when no argument is given.
def url(val = nil) def url(val = nil)
return @url if val.nil?
@url = case val @url = case val
when nil
return @url
when :head, :stable, :devel when :head, :stable, :devel
@formula.send(val).url @formula.send(val).url
when :homepage when :homepage
@formula.homepage @formula.homepage
else when String
val val
else
raise TypeError, "Livecheck#url expects a String or valid Symbol"
end end
end end

View File

@ -20,6 +20,12 @@ describe Livecheck do
livecheckable.regex(/foo/) livecheckable.regex(/foo/)
expect(livecheckable.regex).to eq(/foo/) expect(livecheckable.regex).to eq(/foo/)
end 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 end
describe "#skip" do 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)).to be true
expect(livecheckable.instance_variable_get(:@skip_msg)).to eq("foo") expect(livecheckable.instance_variable_get(:@skip_msg)).to eq("foo")
end 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 end
describe "#skip?" do describe "#skip?" do
@ -69,6 +81,11 @@ describe Livecheck do
it "returns the URL if set" do it "returns the URL if set" do
livecheckable.url("foo") livecheckable.url("foo")
expect(livecheckable.url).to eq("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
end end