currency_test.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // Copyright 2009 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.i18n.currencyTest');
  15. goog.setTestOnly('goog.i18n.currencyTest');
  16. goog.require('goog.i18n.NumberFormat');
  17. goog.require('goog.i18n.currency');
  18. goog.require('goog.i18n.currency.CurrencyInfo');
  19. goog.require('goog.object');
  20. goog.require('goog.testing.PropertyReplacer');
  21. goog.require('goog.testing.jsunit');
  22. var stubs = new goog.testing.PropertyReplacer();
  23. function setUp() {
  24. stubs.replace(
  25. goog.i18n.currency, 'CurrencyInfo',
  26. goog.object.clone(goog.i18n.currency.CurrencyInfo));
  27. }
  28. function tearDown() {
  29. stubs.reset();
  30. }
  31. function testIsAvailable() {
  32. assertTrue(goog.i18n.currency.isAvailable('USD'));
  33. assertTrue(goog.i18n.currency.isAvailable('CLP'));
  34. assertFalse(goog.i18n.currency.isAvailable('LRD'));
  35. assertFalse(goog.i18n.currency.isAvailable('XYZ'));
  36. }
  37. function testAddTier2Support() {
  38. assertFalse('LRD' in goog.i18n.currency.CurrencyInfo);
  39. assertThrows(function() {
  40. goog.i18n.currency.getLocalCurrencyPattern('LRD');
  41. });
  42. goog.i18n.currency.addTier2Support();
  43. assertTrue('LRD' in goog.i18n.currency.CurrencyInfo);
  44. assertEquals(
  45. "'$'#,##0.00", goog.i18n.currency.getLocalCurrencyPattern('LRD'));
  46. }
  47. function testCurrencyPattern() {
  48. assertEquals(
  49. "'$'#,##0.00", goog.i18n.currency.getLocalCurrencyPattern('USD'));
  50. assertEquals(
  51. "'US$'#,##0.00", goog.i18n.currency.getPortableCurrencyPattern('USD'));
  52. assertEquals(
  53. "USD '$'#,##0.00", goog.i18n.currency.getGlobalCurrencyPattern('USD'));
  54. assertEquals("'¥'#,##0", goog.i18n.currency.getLocalCurrencyPattern('JPY'));
  55. assertEquals(
  56. "'JP¥'#,##0", goog.i18n.currency.getPortableCurrencyPattern('JPY'));
  57. assertEquals(
  58. "JPY '¥'#,##0", goog.i18n.currency.getGlobalCurrencyPattern('JPY'));
  59. assertEquals(
  60. "'€'#,##0.00", goog.i18n.currency.getLocalCurrencyPattern('EUR'));
  61. assertEquals(
  62. "'€'#,##0.00", goog.i18n.currency.getPortableCurrencyPattern('EUR'));
  63. assertEquals(
  64. "EUR '€'#,##0.00", goog.i18n.currency.getGlobalCurrencyPattern('EUR'));
  65. assertEquals(
  66. "'¥'#,##0.00", goog.i18n.currency.getLocalCurrencyPattern('CNY'));
  67. assertEquals(
  68. "'RMB¥'#,##0.00", goog.i18n.currency.getPortableCurrencyPattern('CNY'));
  69. assertEquals(
  70. "CNY '¥'#,##0.00", goog.i18n.currency.getGlobalCurrencyPattern('CNY'));
  71. assertEquals(
  72. "'Rial'#,##0", goog.i18n.currency.getLocalCurrencyPattern('YER'));
  73. assertEquals(
  74. "'Rial'#,##0", goog.i18n.currency.getPortableCurrencyPattern('YER'));
  75. assertEquals(
  76. "YER 'Rial'#,##0", goog.i18n.currency.getGlobalCurrencyPattern('YER'));
  77. assertEquals(
  78. "'CHF'#,##0.00", goog.i18n.currency.getLocalCurrencyPattern('CHF'));
  79. assertEquals(
  80. "'CHF'#,##0.00", goog.i18n.currency.getPortableCurrencyPattern('CHF'));
  81. assertEquals(
  82. "'CHF'#,##0.00", goog.i18n.currency.getGlobalCurrencyPattern('CHF'));
  83. }
  84. function testCurrencyFormatCHF() {
  85. var formatter;
  86. var str;
  87. formatter = new goog.i18n.NumberFormat(
  88. goog.i18n.currency.getLocalCurrencyPattern('CHF'));
  89. str = formatter.format(123456.7899);
  90. assertEquals('CHF123,456.79', str);
  91. formatter = new goog.i18n.NumberFormat(
  92. goog.i18n.currency.getPortableCurrencyPattern('CHF'));
  93. str = formatter.format(123456.7899);
  94. assertEquals('CHF123,456.79', str);
  95. formatter = new goog.i18n.NumberFormat(
  96. goog.i18n.currency.getGlobalCurrencyPattern('CHF'));
  97. str = formatter.format(123456.7899);
  98. assertEquals('CHF123,456.79', str);
  99. }
  100. function testCurrencyFormatYER() {
  101. var formatter;
  102. var str;
  103. formatter = new goog.i18n.NumberFormat(
  104. goog.i18n.currency.getLocalCurrencyPattern('YER'));
  105. str = formatter.format(123456.7899);
  106. assertEquals('Rial123,457', str);
  107. formatter = new goog.i18n.NumberFormat(
  108. goog.i18n.currency.getPortableCurrencyPattern('YER'));
  109. str = formatter.format(123456.7899);
  110. assertEquals('Rial123,457', str);
  111. formatter = new goog.i18n.NumberFormat(
  112. goog.i18n.currency.getGlobalCurrencyPattern('YER'));
  113. str = formatter.format(123456.7899);
  114. assertEquals('YER Rial123,457', str);
  115. }
  116. function testCurrencyFormatCNY() {
  117. var formatter;
  118. var str;
  119. formatter = new goog.i18n.NumberFormat(
  120. goog.i18n.currency.getLocalCurrencyPattern('CNY'));
  121. str = formatter.format(123456.7899);
  122. assertEquals('¥123,456.79', str);
  123. formatter = new goog.i18n.NumberFormat(
  124. goog.i18n.currency.getPortableCurrencyPattern('CNY'));
  125. str = formatter.format(123456.7899);
  126. assertEquals('RMB¥123,456.79', str);
  127. formatter = new goog.i18n.NumberFormat(
  128. goog.i18n.currency.getGlobalCurrencyPattern('CNY'));
  129. str = formatter.format(123456.7899);
  130. assertEquals('CNY ¥123,456.79', str);
  131. }
  132. function testCurrencyFormatCZK() {
  133. var formatter;
  134. var str;
  135. formatter = new goog.i18n.NumberFormat(
  136. goog.i18n.currency.getLocalCurrencyPattern('CZK'));
  137. str = formatter.format(123456.7899);
  138. assertEquals('123,456.79 Kč', str);
  139. formatter = new goog.i18n.NumberFormat(
  140. goog.i18n.currency.getPortableCurrencyPattern('CZK'));
  141. str = formatter.format(123456.7899);
  142. assertEquals('123,456.79 Kč', str);
  143. formatter = new goog.i18n.NumberFormat(
  144. goog.i18n.currency.getGlobalCurrencyPattern('CZK'));
  145. str = formatter.format(123456.7899);
  146. assertEquals('CZK 123,456.79 Kč', str);
  147. }
  148. function testCurrencyFormatEUR() {
  149. var formatter;
  150. var str;
  151. formatter = new goog.i18n.NumberFormat(
  152. goog.i18n.currency.getLocalCurrencyPattern('EUR'));
  153. str = formatter.format(123456.7899);
  154. assertEquals('€123,456.79', str);
  155. formatter = new goog.i18n.NumberFormat(
  156. goog.i18n.currency.getPortableCurrencyPattern('EUR'));
  157. str = formatter.format(123456.7899);
  158. assertEquals('€123,456.79', str);
  159. formatter = new goog.i18n.NumberFormat(
  160. goog.i18n.currency.getGlobalCurrencyPattern('EUR'));
  161. str = formatter.format(123456.7899);
  162. assertEquals('EUR €123,456.79', str);
  163. }
  164. function testCurrencyFormatJPY() {
  165. var formatter;
  166. var str;
  167. formatter = new goog.i18n.NumberFormat(
  168. goog.i18n.currency.getLocalCurrencyPattern('JPY'));
  169. str = formatter.format(123456.7899);
  170. assertEquals('¥123,457', str);
  171. formatter = new goog.i18n.NumberFormat(
  172. goog.i18n.currency.getPortableCurrencyPattern('JPY'));
  173. str = formatter.format(123456.7899);
  174. assertEquals('JP¥123,457', str);
  175. formatter = new goog.i18n.NumberFormat(
  176. goog.i18n.currency.getGlobalCurrencyPattern('JPY'));
  177. str = formatter.format(123456.7899);
  178. assertEquals('JPY ¥123,457', str);
  179. }
  180. function testCurrencyFormatPLN() {
  181. var formatter;
  182. var str;
  183. formatter = new goog.i18n.NumberFormat(
  184. goog.i18n.currency.getLocalCurrencyPattern('PLN'));
  185. str = formatter.format(123456.7899);
  186. assertEquals('123,456.79 zł', str);
  187. formatter = new goog.i18n.NumberFormat(
  188. goog.i18n.currency.getPortableCurrencyPattern('PLN'));
  189. str = formatter.format(123456.7899);
  190. assertEquals('123,456.79 zł', str);
  191. formatter = new goog.i18n.NumberFormat(
  192. goog.i18n.currency.getGlobalCurrencyPattern('PLN'));
  193. str = formatter.format(123456.7899);
  194. assertEquals('PLN 123,456.79 zł', str);
  195. }
  196. function testCurrencyFormatUSD() {
  197. var formatter;
  198. var str;
  199. formatter = new goog.i18n.NumberFormat(
  200. goog.i18n.currency.getLocalCurrencyPattern('USD'));
  201. str = formatter.format(123456.7899);
  202. assertEquals('$123,456.79', str);
  203. formatter = new goog.i18n.NumberFormat(
  204. goog.i18n.currency.getPortableCurrencyPattern('USD'));
  205. str = formatter.format(123456.7899);
  206. assertEquals('US$123,456.79', str);
  207. formatter = new goog.i18n.NumberFormat(
  208. goog.i18n.currency.getGlobalCurrencyPattern('USD'));
  209. str = formatter.format(123456.7899);
  210. assertEquals('USD $123,456.79', str);
  211. }
  212. function testIsPrefixSignPosition() {
  213. assertTrue(goog.i18n.currency.isPrefixSignPosition('USD'));
  214. assertTrue(goog.i18n.currency.isPrefixSignPosition('EUR'));
  215. }
  216. function testGetCurrencySign() {
  217. assertEquals('USD $', goog.i18n.currency.getGlobalCurrencySign('USD'));
  218. assertEquals('$', goog.i18n.currency.getLocalCurrencySign('USD'));
  219. assertEquals('US$', goog.i18n.currency.getPortableCurrencySign('USD'));
  220. assertEquals('YER Rial', goog.i18n.currency.getGlobalCurrencySign('YER'));
  221. assertEquals('Rial', goog.i18n.currency.getLocalCurrencySign('YER'));
  222. assertEquals('Rial', goog.i18n.currency.getPortableCurrencySign('YER'));
  223. }