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