-
Notifications
You must be signed in to change notification settings - Fork 0
BIRDS-327: Add dollars_to_cents formatter #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ab4bda8
4002cd5
c51f435
76b0254
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| PATH | ||
| remote: . | ||
| specs: | ||
| parameter_substitution (3.2.0) | ||
| parameter_substitution (3.3.0) | ||
| activesupport (>= 6.0) | ||
| builder (~> 3.2) | ||
| invoca-utils (~> 0.3) | ||
|
|
@@ -22,7 +22,7 @@ | |
| tzinfo (~> 2.0) | ||
| appraisal (2.5.0) | ||
| bundler | ||
| rake | ||
|
Check failure on line 25 in Gemfile.lock
|
||
| thor (>= 0.14.0) | ||
| appraisal-matrix (0.2.0) | ||
| appraisal (~> 2.2) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class ParameterSubstitution::Formatters::DollarsToCents < ParameterSubstitution::Formatters::Base | ||
| def self.description | ||
| "Converts a dollar amount to cents as an integer" | ||
| end | ||
|
|
||
| def self.format(value) | ||
| (Float(value) * 100).round.to_i rescue nil | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class ParameterSubstitution | ||
| VERSION = "3.2.0" | ||
| VERSION = "3.3.0" | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative '../../../../lib/parameter_substitution/formatters/dollars_to_cents' | ||
|
|
||
| describe ParameterSubstitution::Formatters::DollarsToCents do | ||
| context "DollarsToCents formatter test" do | ||
| before do | ||
| @format_class = ParameterSubstitution::Formatters::DollarsToCents | ||
| end | ||
|
|
||
| it "have a key" do | ||
| expect(@format_class.key).to eq("dollars_to_cents") | ||
| end | ||
|
|
||
| it "provide a description" do | ||
| expect(@format_class.description).to eq("Converts a dollar amount to cents as an integer") | ||
| end | ||
|
|
||
| it "converts valid string dollar amounts to cents" do | ||
| expect(@format_class.format("10.50")).to eq(1050) | ||
| expect(@format_class.format("10")).to eq(1000) | ||
| expect(@format_class.format("0.01")).to eq(1) | ||
| expect(@format_class.format("123.456")).to eq(12346) | ||
| expect(@format_class.format("0.001")).to eq(0) | ||
| expect(@format_class.format("100")).to eq(10000) | ||
| expect(@format_class.format("99.99")).to eq(9999) | ||
| expect(@format_class.format("-10.50")).to eq(-1050) | ||
| end | ||
|
|
||
| it "converts valid numeric dollar amounts to cents" do | ||
| expect(@format_class.format(10.50)).to eq(1050) | ||
| expect(@format_class.format(10)).to eq(1000) | ||
| expect(@format_class.format(0.01)).to eq(1) | ||
| expect(@format_class.format(123.456)).to eq(12346) | ||
| expect(@format_class.format(0.001)).to eq(0) | ||
| expect(@format_class.format(100)).to eq(10000) | ||
| expect(@format_class.format(99.99)).to eq(9999) | ||
| expect(@format_class.format(-10.50)).to eq(-1050) | ||
| end | ||
|
Comment on lines
+30
to
+39
|
||
|
|
||
| it "returns nil for nil or invalid strings" do | ||
| expect(@format_class.format(nil)).to eq(nil) | ||
| expect(@format_class.format("")).to eq(nil) | ||
| expect(@format_class.format("$10.50")).to eq(nil) | ||
| expect(@format_class.format("not_a_number")).to eq(nil) | ||
| end | ||
| end | ||
| end | ||
Uh oh!
There was an error while loading. Please reload this page.