Skip to content
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

toFixed(): Add edge case tests (Jasmine & QUnit) #168

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions tests/jasmine/core/toFixedSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
describe('toFixed()', function(){

it('should work with all float rounding edge cases', function(){
expect( accounting.toFixed(1.005, 2) ).toBe( '1.01' );
expect( accounting.toFixed(1.005, 20) ).toBe( '1.00500000000000000000' );
});

it('should round negative values away from zero', function(){
expect( accounting.toFixed(-1.005, 2) ).toBe( '-1.01' );
});

it('should accept negative exponents', function(){
expect( accounting.toFixed(2.7755e-10, 2) ).toBe( '0.00' );
});

});
1 change: 1 addition & 0 deletions tests/jasmine/runner.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<script src="core/formatNumberSpec.js"></script>
<script src="core/unformatSpec.js"></script>
<script src="core/formatMoneySpec.js"></script>
<script src="core/toFixedSpec.js"></script>
</head>
<body>
<script>
Expand Down
16 changes: 10 additions & 6 deletions tests/qunit/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,27 @@ $(document).ready(function() {
test("accounting.toFixed()", function() {
equals(accounting.toFixed(54321, 5), "54321.00000", 'Performs basic float zero-padding');
equals(accounting.toFixed(0.615, 2), "0.62", 'Rounds 0.615 to "0.62" instead of "0.61"');
equals(accounting.toFixed(1.005, 2), "1.01", 'Round 1.005 to "1.01" instead of "1.00"');
equals(accounting.toFixed(1.005, 20), "1.00500000000000000000", 'Return correct value with high precision numbers');
equals(accounting.toFixed(-1.005, 2), "-1.01", 'Negative values should round away from zero');
equals(accounting.toFixed(2.7755e-10, 2), "0.00", 'Accept negative exponents');
});

test("accounting.formatNumber()", function() {
// Check custom precision and separators:
equals(accounting.formatNumber(4999.99, 2, ".", ","), "4.999,99", 'Custom precision and decimal/thousand separators are a-ok')

// check usage with options object parameter:
equal(accounting.formatNumber(5318008, {
precision : 3,
thousand : "__",
decimal : "--"
}), "5__318__008--000", 'Correctly handles custom precision and separators passed in via second param options object');


// check rounding:
equals(accounting.formatNumber(0.615, 2), "0.62", 'Rounds 0.615 up to "0.62" with precision of 2');

// manually and recursively formatted arrays should have same values:
var numbers = [8008135, [1234, 5678], 1000];
var formattedManually = [accounting.formatNumber(8008135), [accounting.formatNumber(1234), accounting.formatNumber(5678)], accounting.formatNumber(1000)];
Expand All @@ -48,7 +52,7 @@ $(document).ready(function() {
equals(accounting.formatMoney(-500000, "� ", 0), "� -500,000", 'negative values, custom params, works ok');
equals(accounting.formatMoney(5318008, { symbol: "GBP", format: "%v %s" }), "5,318,008.00 GBP", "`format` parameter is observed in string output");
equals(accounting.formatMoney(1000, { format: "test %v 123 %s test" }), "test 1,000.00 123 $ test", "`format` parameter is observed in string output, despite being rather strange");

// Format param is an object:
var format = {
pos: "%s %v",
Expand All @@ -58,7 +62,7 @@ $(document).ready(function() {
equals(accounting.formatMoney(0, { symbol: "GBP", format:format}), "GBP --", "`format` parameter provided given as an object with `zero` format, correctly observed in string output");
equals(accounting.formatMoney(-1000, { symbol: "GBP", format:format}), "GBP (1,000.00)", "`format` parameter provided given as an object with `neg` format, correctly observed in string output");
equals(accounting.formatMoney(1000, { symbol: "GBP", format:{neg:"--%v %s"}}), "GBP1,000.00", "`format` parameter provided, but only `neg` value provided - positive value should be formatted by default format (%s%v)");

accounting.settings.currency.format = "%s%v";
accounting.formatMoney(0, {format:""});
equals(typeof accounting.settings.currency.format, "object", "`settings.currency.format` default string value should be reformatted to an object, the first time it is used");
Expand Down Expand Up @@ -93,5 +97,5 @@ $(document).ready(function() {


});

});