Skip to content

Commit

Permalink
100% test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Nov 6, 2023
1 parent 618894c commit 3547ea7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
7 changes: 5 additions & 2 deletions lib/latinum/bank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,11 @@ def parse(string, default_name: nil)
end
end

def load(value)
parse(value) if value
def load(input)
if input.is_a?(String)
input = input.strip
return parse(input) unless input.empty?
end
end

def dump(resource)
Expand Down
10 changes: 2 additions & 8 deletions lib/latinum/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,9 @@ def self.parse(string, default_name: nil)
# @parameter string [String | Nil] e.g. "5 NZD" or nil.
# @returns [Resource | Nil] The Resource that represents the parsed string.
def self.load(input)
case input
when String
# Remove any whitespaces
if input.is_a?(String)
input = input.strip
parse(input) unless input.empty?
when Resource
input
else
nil
return parse(input) unless input.empty?
end
end

Expand Down
31 changes: 31 additions & 0 deletions test/latinum/bank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,37 @@
end
end

with '.load and .dump' do
it "should load and dump resources" do
resource = Latinum::Resource.load("10 NZD")
string_representation = Latinum::Resource.dump(resource)

loaded_resource = bank.load(string_representation)

expect(loaded_resource).to be == loaded_resource
end

it "should load and dump nil correctly" do
expect(bank.load(nil)).to be == nil
expect(bank.dump(nil)).to be == nil
end

it "should handle empty strings correctly" do
expect(bank.load("")).to be == nil
end

it "should handle whitespace strings correctly" do
expect(bank.load(" ")).to be == nil
end

it "should load and dump resources correctly" do
resource = Latinum::Resource.new(10, 'NZD')

expect(bank.load("10.0 NZD")).to be == resource
expect(bank.dump(resource)).to be == "10.0 NZD"
end
end

it "should format the amounts correctly" do
resource = Latinum::Resource.new("10", "NZD")

Expand Down
7 changes: 0 additions & 7 deletions test/latinum/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@
expect(Latinum::Resource.load("10.0 NZD")).to be == resource
expect(Latinum::Resource.dump(resource)).to be == "10.0 NZD"
end

it "can pass through resources" do
resource = Latinum::Resource.new(10, 'NZD')

expect(Latinum::Resource.load(resource)).to be == resource
expect(Latinum::Resource.dump(resource)).to be == "10.0 NZD"
end
end

it "should inspect nicely" do
Expand Down

0 comments on commit 3547ea7

Please sign in to comment.