Skip to content

Commit

Permalink
Merge pull request #2 from tcdowney/allow-for-zero-negative-values
Browse files Browse the repository at this point in the history
added support for general cases where the amount is 0 or negative
  • Loading branch information
XenoPhex authored Feb 27, 2018
2 parents cc2eb56 + 3072120 commit c5bfbd0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 45 deletions.
3 changes: 0 additions & 3 deletions lib/palm_civet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ def self.to_bytes(bytes)
end

value = Float(matches[1])
if value <= 0
raise InvalidByteQuantityError
end

case matches[2][0].capitalize
when "T"
Expand Down
84 changes: 42 additions & 42 deletions spec/palm_civet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,33 +60,33 @@
expect(PalmCivet.to_bytes("\t\n\r 5MB ")).to eq(5 * PalmCivet::MEGABYTE)
end

context "raises an error" do
it "when the unit is missing" do
expect {
PalmCivet.to_bytes("5")
}.to raise_error(PalmCivet::InvalidByteQuantityError)
context 'when the byte amount is 0' do
it 'returns 0 bytes' do
expect(PalmCivet.to_bytes("0TB")).to eq(0)
end
end

it "when the unit is unrecognized" do
expect {
PalmCivet.to_bytes("5MBB")
}.to raise_error(PalmCivet::InvalidByteQuantityError)

expect {
PalmCivet.to_bytes("5BB")
}.to raise_error(PalmCivet::InvalidByteQuantityError)
context 'when the byte amount is negative' do
it 'returns a negative amount of bytes' do
expect(PalmCivet.to_bytes('-200B')).to eq(-200)
end
end

it "for negative values" do
expect {
PalmCivet.to_bytes("-5MB")
}.to raise_error(PalmCivet::InvalidByteQuantityError)
context "raises an error" do
it "when the unit is missing" do
expect {
PalmCivet.to_bytes("5")
}.to raise_error(PalmCivet::InvalidByteQuantityError)
end

it "for zero values" do
expect {
PalmCivet.to_bytes("0TB")
}.to raise_error(PalmCivet::InvalidByteQuantityError)
it "when the unit is unrecognized" do
expect {
PalmCivet.to_bytes("5MBB")
}.to raise_error(PalmCivet::InvalidByteQuantityError)

expect {
PalmCivet.to_bytes("5BB")
}.to raise_error(PalmCivet::InvalidByteQuantityError)
end
end
end
Expand Down Expand Up @@ -123,33 +123,33 @@
expect(PalmCivet.to_megabytes("\t\n\r 5MB ")).to eq(5)
end

context "raises an error" do
it "when the unit is missing" do
expect {
PalmCivet.to_megabytes("5")
}.to raise_error(PalmCivet::InvalidByteQuantityError)
context 'when the byte amount is 0' do
it 'returns 0 megabytes' do
expect(PalmCivet.to_megabytes('0TB')).to eq(0)
end
end

it "when the unit is unrecognized" do
expect {
PalmCivet.to_megabytes("5MBB")
}.to raise_error(PalmCivet::InvalidByteQuantityError)

expect {
PalmCivet.to_megabytes("5BB")
}.to raise_error(PalmCivet::InvalidByteQuantityError)
context 'when the byte amount is negative' do
it 'returns a negative amount of megabytes' do
expect(PalmCivet.to_megabytes('-200MB')).to eq(-200)
end
end

it "for negative values" do
expect {
PalmCivet.to_megabytes("-5MB")
}.to raise_error(PalmCivet::InvalidByteQuantityError)
context "raises an error" do
it "when the unit is missing" do
expect {
PalmCivet.to_megabytes("5")
}.to raise_error(PalmCivet::InvalidByteQuantityError)
end

it "for zero values" do
expect {
PalmCivet.to_megabytes("0TB")
}.to raise_error(PalmCivet::InvalidByteQuantityError)
it "when the unit is unrecognized" do
expect {
PalmCivet.to_megabytes("5MBB")
}.to raise_error(PalmCivet::InvalidByteQuantityError)

expect {
PalmCivet.to_megabytes("5BB")
}.to raise_error(PalmCivet::InvalidByteQuantityError)
end
end
end
Expand Down

0 comments on commit c5bfbd0

Please sign in to comment.