Skip to content

Commit

Permalink
added unit test for DiskSize
Browse files Browse the repository at this point in the history
  • Loading branch information
shundhammer committed Dec 22, 2015
1 parent 809fb96 commit 20a68bf
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/Makefile.am
Expand Up @@ -90,7 +90,9 @@ ynclude_DATA = \
ylibdir = @ylibdir@/storage
ylib_DATA = \
lib/storage/target_map_formatter.rb \
lib/storage/used_storage_features.rb
lib/storage/used_storage_features.rb \
lib/storage/storage_proposal.rb \
lib/storage/disk_size.rb


scrconf_DATA = \
Expand Down
2 changes: 1 addition & 1 deletion src/lib/storage/storage_proposal.rb
Expand Up @@ -23,7 +23,7 @@

require "yast"
require "storage"
require "disk_size"
require "storage/disk_size"
require "pp"

# This file can be invoked separately for minimal testing.
Expand Down
5 changes: 3 additions & 2 deletions test/Makefile.am
Expand Up @@ -3,12 +3,13 @@
#

TESTS = \
disk_size_test.rb \
storage_proposal_test.rb \
format_target_map_test.rb \
storage_snapper_configure_snapper_test.rb \
storage_get_disk_partition.rb \
storage_boot_on_raid1.rb \
partitions_test.rb \
storage_boot_on_raid1.rb \
partitions_test.rb \
include/partitioning_custom_part_check_generated_include_test.rb

TEST_EXTENSIONS = .rb
Expand Down
138 changes: 138 additions & 0 deletions test/disk_size_test.rb
@@ -0,0 +1,138 @@
#!/usr/bin/env rspec

require_relative "spec_helper"
require "storage/disk_size"
require "pp"


describe Yast::Storage::DiskSize do

describe "constructed empty" do
it "should have a size_k of 0" do
disk_size = Yast::Storage::DiskSize.new
expect( disk_size.size_k ).to be == 0
end
end

describe "created with 42 kiB" do
disk_size = Yast::Storage::DiskSize.kiB(42)
it "should return the correct human readable string" do
expect( disk_size.to_s ).to be == "42.00 kiB"
end
it "should have the correct numeric value internally" do
expect( disk_size.size_k ).to be == 42
end
end

describe "created with 43 MiB" do
disk_size = Yast::Storage::DiskSize.MiB(43)
it "should return the correct human readable string" do
expect( disk_size.to_s ).to be == "43.00 MiB"
end
it "should have the correct numeric value internally" do
expect( disk_size.size_k ).to be == 43 * 1024
end
end

describe "created with 44 GiB" do
disk_size = Yast::Storage::DiskSize.GiB(44)
it "should return the correct human readable string" do
expect( disk_size.to_s ).to be == "44.00 GiB"
end
it "should have the correct numeric value internally" do
expect( disk_size.size_k ).to be == 44 * 1024**2
end
end

describe "created with 45 TiB" do
disk_size = Yast::Storage::DiskSize.TiB(45)
it "should return the correct human readable string" do
expect( disk_size.to_s ).to be == "45.00 TiB"
end
it "should have the correct numeric value internally" do
expect( disk_size.size_k ).to be == 45 * 1024**3
end
end

describe "created with 46 PiB" do
disk_size = Yast::Storage::DiskSize.PiB(46)
it "should return the correct human readable string" do
expect( disk_size.to_s ).to be == "46.00 PiB"
end
it "should have the correct numeric value internally" do
expect( disk_size.size_k ).to be == 46 * 1024**4
end
end

describe "created with 47 EiB" do
disk_size = Yast::Storage::DiskSize.EiB(47)
it "should return the correct human readable string" do
expect( disk_size.to_s ).to be == "47.00 EiB"
end
it "should not overflow and have the correct numeric value internally" do
expect( disk_size.size_k ).to be == 47 * 1024**5
end
end

describe "created with a huge number" do
disk_size = Yast::Storage::DiskSize.TiB(48 * 1024**5)
it "should return the correct human readable string" do
expect( disk_size.to_s ).to be == "49152.00 YiB"
end
it "should not overflow" do
expect( disk_size.size_k ).to be > 0
end
it "should have the correct numeric value internally" do
expect( disk_size.size_k ).to be == 48 * 1024**8
end
end

describe "arithmetic operations" do
it "should accept addition of another DiskSize" do
disk_size = Yast::Storage::DiskSize.GiB(10) + Yast::Storage::DiskSize.GiB(20)
expect( disk_size.size_k ).to be == 30 * 1024**2
end
it "should accept addition of an int" do
disk_size = Yast::Storage::DiskSize.MiB(20) + 512
expect( disk_size.size_k ).to be == 20 * 1024 + 512
end
it "should accept multiplication with an int" do
disk_size = Yast::Storage::DiskSize.MiB(12) * 3
expect( disk_size.size_k ).to be == 12 * 1024 * 3
end
it "should accept division by an int" do
disk_size = Yast::Storage::DiskSize.MiB(12) / 3
expect( disk_size.size_k ).to be == 12 / 3 * 1024
end
it "should refuse multiplication with another DiskSize" do
expect{ Yast::Storage::DiskSize.MiB(12) * Yast::Storage::DiskSize.MiB(3) }.to raise_exception TypeError
end
end

describe "comparison operations" do
disk_size1 = Yast::Storage::DiskSize.GiB(24)
disk_size2 = Yast::Storage::DiskSize.GiB(32)
disk_size3 = Yast::Storage::DiskSize.GiB(32)
it "operator < should compare correctly" do
expect( disk_size1 < disk_size2 ).to be == true
expect( disk_size2 < disk_size3 ).to be == false
end
it "operator > should compare correctly" do
expect( disk_size1 > disk_size2 ).to be == false
expect( disk_size2 > disk_size3 ).to be == false
end
it "operator == should compare correctly" do
expect( disk_size2 == disk_size3 ).to be == true
end
it "operator != should compare correctly" do
expect( disk_size1 != disk_size2 ).to be == true
end
it "operator <= should compare correctly" do
expect( disk_size2 <= disk_size3 ).to be == true
end
it "operator >= should compare correctly" do
expect( disk_size2 >= disk_size3 ).to be == true
end
end

end

0 comments on commit 20a68bf

Please sign in to comment.