PageRenderTime 54ms CodeModel.GetById 13ms app.highlight 28ms RepoModel.GetById 1ms app.codeStats 0ms

/ext-4.0.7/src/core/test/unit/spec/util/Format.js

https://bitbucket.org/srogerf/javascript
JavaScript | 174 lines | 137 code | 15 blank | 22 comment | 0 complexity | bf8c8f7ce0433b2d34967145a87c0f35 MD5 | raw file
  1/*
  2
  3This file is part of Ext JS 4
  4
  5Copyright (c) 2011 Sencha Inc
  6
  7Contact:  http://www.sencha.com/contact
  8
  9GNU General Public License Usage
 10This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
 11
 12If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
 13
 14*/
 15describe("Ext.util.Format", function() {
 16    var num = Ext.util.Format.number,
 17        usMoney = Ext.util.Format.usMoney,
 18        currency = Ext.util.Format.currency,
 19        savedFormatLocale = {
 20            thousandSeparator: Ext.util.Format.thousandSeparator,
 21            decimalSeparator: Ext.util.Format.decimalSeparator,
 22            currencySign: Ext.util.Format.currencySign
 23        };
 24
 25    describe("usMoney", function(){
 26        it("should format with 2 decimals, prefixed by a dollar sign", function() {
 27            expect(usMoney(1234.567)).toEqual("$1,234.57");
 28        });
 29        it("should format with 2 decimals, prefixed by a negative sign, and a dollar sign", function() {
 30            expect(usMoney(-1234.567)).toEqual("-$1,234.57");
 31        });
 32        it("should format with a comma as a thousand separator", function() {
 33            expect(usMoney(1234567.89)).toEqual("$1,234,567.89");
 34        });
 35    });
 36
 37    describe("currency in FR locale", function(){
 38        beforeEach(function() {
 39            Ext.apply(Ext.util.Format, {
 40                thousandSeparator: '.',
 41                decimalSeparator: ',',
 42                currencySign: '\u20ac',
 43                dateFormat: 'd/m/Y'
 44            });
 45        });
 46        afterEach(function() {
 47            Ext.apply(Ext.util.Format, savedFormatLocale);
 48        });
 49
 50        it("should format with 2 decimals, prefixed by a euro sign", function() {
 51            expect(currency(1234.567)).toEqual("\u20ac1.234,57");
 52        });
 53        it("should format with 2 decimals, prefixed by a negative sign, and a euro sign", function() {
 54            expect(currency(-1234.567)).toEqual("-\u20ac1.234,57");
 55        });
 56    });
 57
 58    describe("number in default (US) locale", function() {
 59        it("should format with no decimals", function() {
 60            expect(num(1, "0")).toEqual("1");
 61        });
 62        it("should format with two decimals", function() {
 63            expect(num(1, "0.00")).toEqual("1.00");
 64        });
 65        it("should format+round with two decimals, and no thousand separators", function() {
 66            expect(num(1234.567, "0.00")).toEqual("1234.57");
 67        });
 68        it("should format+round with two decimals, and ',' as the thousand separator", function() {
 69            expect(num(1234.567, ",0.00")).toEqual("1,234.57");
 70        });
 71        it("should format+round with no decimals, and ',' as the thousand separator", function() {
 72            expect(num(1234.567, ",0")).toEqual("1,235");
 73        });
 74    });
 75
 76    describe("number using FR locale", function() {
 77        var savedFormatLocale = {
 78            thousandSeparator: Ext.util.Format.thousandSeparator,
 79            decimalSeparator: Ext.util.Format.decimalSeparator,
 80            currencySign: Ext.util.Format.currencySign,
 81            dateFormat: Ext.util.Format.dateFormat
 82        };
 83
 84        beforeEach(function() {
 85            Ext.apply(Ext.util.Format, {
 86                thousandSeparator: '.',
 87                decimalSeparator: ',',
 88                currencySign: '\u20ac',
 89                dateFormat: 'd/m/Y'
 90            });
 91        });
 92        afterEach(function() {
 93            Ext.apply(Ext.util.Format, savedFormatLocale);
 94        });
 95
 96        it("should format with no decimals", function() {
 97            expect(num(1, "0")).toEqual("1");
 98        });
 99        it("should format with two decimals", function() {
100            expect(num(1, "0.00")).toEqual("1,00");
101        });
102        it("should format+round with two decimals, and no thousand separators", function() {
103            expect(num(1234.567, "0.00")).toEqual("1234,57");
104        });
105        it("should format+round with two decimals after a ',', and '.' as the thousand separator", function() {
106            expect(num(1234.567, ",0.00")).toEqual("1.234,57");
107        });
108        it("should format+round with no decimals, and '.' as the thousand separator", function() {
109            expect(num(1234.567, ",0")).toEqual("1.235");
110        });
111    });
112
113    // In Ext4, the "/i" suffix allows you to use locale-specific separators in the format string, as opposed
114    // to US/UK conventions. Output however ALWAYS follows the local settings in the Format singleton which may
115    // be overridden by locale files.
116    describe("number using FR locale with /i", function() {
117        var savedFormatLocale = {
118            thousandSeparator: Ext.util.Format.thousandSeparator,
119            decimalSeparator: Ext.util.Format.decimalSeparator,
120            currencySign: Ext.util.Format.currencySign,
121            dateFormat: Ext.util.Format.dateFormat
122        };
123
124        // set up the FR formatting locale
125        beforeEach(function() {
126            Ext.apply(Ext.util.Format, {
127                thousandSeparator: '.',
128                decimalSeparator: ',',
129                currencySign: '\u20ac',
130                dateFormat: 'd/m/Y'
131            });
132        });
133        afterEach(function() {
134            Ext.apply(Ext.util.Format, savedFormatLocale);
135        });
136
137        // Demonstrate "Incorrect" use with "/i". '.' means thousand separator and ',' means decimal in FR locale.
138        // Read carefully. In the formatting strings below, '.' is taken to mean thousand separator, and
139        // ',' is taken to mean decimal separator
140        it("should format with no decimals", function() {
141            expect(num(1, "0.00/i")).toEqual("1");
142        });
143        it("should format+round with no decimals, and '.' as thousand separator", function() {
144            expect(num(1234.567, "0.00/i")).toEqual("1.235");
145        });
146        it("should format+round with three decimals after a ',', and '.' as the thousand separator", function() {
147            expect(num(1234.567, ",0.00/i")).toEqual("1.234,567");
148        });
149        it("should format+round with one decimal, and no thousand separator", function() {
150            expect(num(1234.567, ",0/i")).toEqual("1234,6");
151        });
152
153        // Correct usage
154        it("should format with two decimals", function() {
155            expect(num(1, "0,00/i")).toEqual("1,00");
156        });
157        it("should format+round with two decimals, and no thousand separators", function() {
158            expect(num(1234.567, "0,00/i")).toEqual("1234,57");
159        });
160        it("should format+round with two decimals after a ',', and '.' as the thousand separator", function() {
161            expect(num(1234.567, ".0,00/i")).toEqual("1.234,57");
162        });
163        it("should format+round with no decimals, and '.' as the thousand separator", function() {
164            expect(num(1234.567, ".0/i")).toEqual("1.235");
165        });
166
167    });
168    
169    it("should check for a 0 value before appending negative", function(){
170        expect(num(-2.842170943040401e-14, "0,000.00")).toEqual('0.00'); 
171    });
172    
173});
174