PageRenderTime 89ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/srogerf/javascript
JavaScript | 521 lines | 471 code | 42 blank | 8 comment | 0 complexity | 82ce6590af82fbd54e3cf064aea37a1f MD5 | raw file
  1. describe("Ext.util.Format", function() {
  2. var savedFormatLocale = {
  3. thousandSeparator: Ext.util.Format.thousandSeparator,
  4. decimalSeparator: Ext.util.Format.decimalSeparator,
  5. currencySign: Ext.util.Format.currencySign
  6. };
  7. describe("undef", function() {
  8. it("should return the value itself if defined", function() {
  9. expect(Ext.util.Format.undef("this is a defined value")).toBe("this is a defined value");
  10. expect(Ext.util.Format.undef(12345)).toBe(12345);
  11. expect(Ext.util.Format.undef(12345.67)).toBe(12345.67);
  12. expect(Ext.util.Format.undef(true)).toBe(true);
  13. });
  14. it("should return an empty string if the value is undefined", function() {
  15. expect(Ext.util.Format.undef(undefined)).toBe("");
  16. });
  17. });
  18. describe("defaultValue", function() {
  19. it("should return the value itself if defined", function () {
  20. expect(Ext.util.Format.defaultValue("value", "default value")).toBe("value");
  21. });
  22. it("should return the default value if the value is undefined", function() {
  23. expect(Ext.util.Format.defaultValue(undefined, "default value")).toBe("default value");
  24. });
  25. it("should return the default value if the value is empty", function() {
  26. expect(Ext.util.Format.defaultValue("", "default value")).toBe("default value");
  27. });
  28. });
  29. describe("substr", function() {
  30. it("should truncate the string from the start position", function() {
  31. expect(Ext.util.Format.substr("abc", 0, 1)).toBe("a");
  32. expect(Ext.util.Format.substr("abc", 1, 1)).toBe("b");
  33. expect(Ext.util.Format.substr("abc", 2, 1)).toBe("c");
  34. });
  35. it("should truncate the string at the specified length", function() {
  36. expect(Ext.util.Format.substr("abc", 0, 0)).toBe("");
  37. expect(Ext.util.Format.substr("abc", 0, 1)).toBe("a");
  38. expect(Ext.util.Format.substr("abc", 0, 2)).toBe("ab");
  39. expect(Ext.util.Format.substr("abc", 0, 3)).toBe("abc");
  40. });
  41. it("should convert non-string values to its string representation and then truncate", function() {
  42. expect(Ext.util.Format.substr(1234, 1, 2)).toBe("23");
  43. expect(Ext.util.Format.substr(true, 1, 2)).toBe("ru");
  44. });
  45. it("should start at the end of the string if start value is negative", function() {
  46. expect(Ext.util.Format.substr("abc", -1, 1)).toBe("c");
  47. expect(Ext.util.Format.substr("abc", -2, 1)).toBe("b");
  48. expect(Ext.util.Format.substr("abc", -3, 1)).toBe("a");
  49. expect(Ext.util.Format.substr("abc", -4, 1)).toBe("a");
  50. expect(Ext.util.Format.substr("abc", -5, 1)).toBe("a");
  51. });
  52. it("should return empty string if start position is out of bounds", function() {
  53. expect(Ext.util.Format.substr("abc", 4, 1)).toBe("");
  54. expect(Ext.util.Format.substr("abc", 5, 1)).toBe("");
  55. expect(Ext.util.Format.substr("abc", 6, 1)).toBe("");
  56. });
  57. it("should return empty string if length is negative", function() {
  58. expect(Ext.util.Format.substr("abc", 0, -1)).toBe("");
  59. expect(Ext.util.Format.substr("abc", 0, -2)).toBe("");
  60. expect(Ext.util.Format.substr("abc", 0, -3)).toBe("");
  61. });
  62. it("should return the whole string if specified length is greater than string length", function() {
  63. expect(Ext.util.Format.substr("abc", 0, 4)).toBe("abc");
  64. expect(Ext.util.Format.substr("abc", 0, 5)).toBe("abc");
  65. expect(Ext.util.Format.substr("abc", 1, 3)).toBe("bc");
  66. expect(Ext.util.Format.substr("abc", 2, 2)).toBe("c");
  67. });
  68. });
  69. describe("lowercase", function() {
  70. it("should preserve lowercase strings", function() {
  71. expect(Ext.util.Format.lowercase("lowercase string")).toBe("lowercase string");
  72. });
  73. it("should convert uppercase strings to lowercase", function() {
  74. expect(Ext.util.Format.lowercase("UPPERCASE STRING")).toBe("uppercase string");
  75. });
  76. it("should convert mixed lowercase/uppercase strings to lowercase", function() {
  77. expect(Ext.util.Format.lowercase("MIXED string")).toBe("mixed string");
  78. expect(Ext.util.Format.lowercase("mixed STRING")).toBe("mixed string");
  79. expect(Ext.util.Format.lowercase("MiXeD sTrIng")).toBe("mixed string");
  80. });
  81. it("should be null/undefined safe", function() {
  82. expect(Ext.util.Format.lowercase(undefined)).toBe("undefined");
  83. expect(Ext.util.Format.lowercase(null)).toBe("null");
  84. });
  85. it("should cast non-string values before processing", function() {
  86. expect(Ext.util.Format.lowercase(123)).toBe("123");
  87. expect(Ext.util.Format.lowercase(true)).toBe("true");
  88. });
  89. });
  90. describe("uppercase", function() {
  91. it("should preserve uppercase strings", function() {
  92. expect(Ext.util.Format.uppercase("UPPERCASE STRING")).toBe("UPPERCASE STRING");
  93. });
  94. it("should convert lowercase strings to uppercase", function() {
  95. expect(Ext.util.Format.uppercase("lowercase string")).toBe("LOWERCASE STRING");
  96. });
  97. it("should convert mixed lowercase/uppercase strings to uppercase", function() {
  98. expect(Ext.util.Format.uppercase("MIXED string")).toBe("MIXED STRING");
  99. expect(Ext.util.Format.uppercase("mixed STRING")).toBe("MIXED STRING");
  100. expect(Ext.util.Format.uppercase("MiXeD sTrIng")).toBe("MIXED STRING");
  101. });
  102. it("should be null/undefined safe", function() {
  103. expect(Ext.util.Format.uppercase(undefined)).toBe("UNDEFINED");
  104. expect(Ext.util.Format.uppercase(null)).toBe("NULL");
  105. });
  106. it("should cast non-string values before processing", function() {
  107. expect(Ext.util.Format.uppercase(123)).toBe("123");
  108. expect(Ext.util.Format.uppercase(true)).toBe("TRUE");
  109. });
  110. });
  111. describe("usMoney", function(){
  112. it("should format with 2 decimals, prefixed by a dollar sign", function() {
  113. expect(Ext.util.Format.usMoney(1234.567)).toEqual("$1,234.57");
  114. });
  115. it("should format with 2 decimals, prefixed by a negative sign, and a dollar sign", function() {
  116. expect(Ext.util.Format.usMoney(-1234.567)).toEqual("-$1,234.57");
  117. });
  118. it("should format with a comma as a thousand separator", function() {
  119. expect(Ext.util.Format.usMoney(1234567.89)).toEqual("$1,234,567.89");
  120. });
  121. });
  122. describe("currency in FR locale", function(){
  123. beforeEach(function() {
  124. Ext.apply(Ext.util.Format, {
  125. thousandSeparator: '.',
  126. decimalSeparator: ',',
  127. currencySign: '\u20ac',
  128. dateFormat: 'd/m/Y'
  129. });
  130. });
  131. afterEach(function() {
  132. Ext.apply(Ext.util.Format, savedFormatLocale);
  133. });
  134. it("should format with 2 decimals, prefixed by a euro sign", function() {
  135. expect(Ext.util.Format.currency(1234.567)).toEqual("\u20ac1.234,57");
  136. });
  137. it("should format with 2 decimals, prefixed by a negative sign, and a euro sign", function() {
  138. expect(Ext.util.Format.currency(-1234.567)).toEqual("-\u20ac1.234,57");
  139. });
  140. });
  141. describe("currency", function() {
  142. it("should allow 0 for a decimal value", function(){
  143. expect(Ext.util.Format.currency(100, '$', 0)).toBe('$100');
  144. });
  145. it("should position currency signal where specified", function() {
  146. expect(Ext.util.Format.currency(123.45, '$', 2)).toBe("$123.45");
  147. expect(Ext.util.Format.currency(123.45, '$', 2, false)).toBe("$123.45");
  148. expect(Ext.util.Format.currency(123.45, '$', 2, true)).toBe("123.45$");
  149. });
  150. });
  151. describe("number in default (US) locale", function() {
  152. it("should format with no decimals", function() {
  153. expect(Ext.util.Format.number(1, "0")).toEqual("1");
  154. });
  155. it("should format with two decimals", function() {
  156. expect(Ext.util.Format.number(1, "0.00")).toEqual("1.00");
  157. });
  158. it("should format+round with two decimals, and no thousand separators", function() {
  159. expect(Ext.util.Format.number(1234.567, "0.00")).toEqual("1234.57");
  160. });
  161. it("should format+round with two decimals, and ',' as the thousand separator", function() {
  162. expect(Ext.util.Format.number(1234.567, ",0.00")).toEqual("1,234.57");
  163. });
  164. it("should format+round with no decimals, and ',' as the thousand separator", function() {
  165. expect(Ext.util.Format.number(1234.567, ",0")).toEqual("1,235");
  166. });
  167. });
  168. describe("number using FR locale", function() {
  169. var savedFormatLocale = {
  170. thousandSeparator: Ext.util.Format.thousandSeparator,
  171. decimalSeparator: Ext.util.Format.decimalSeparator,
  172. currencySign: Ext.util.Format.currencySign,
  173. dateFormat: Ext.util.Format.dateFormat
  174. };
  175. beforeEach(function() {
  176. Ext.apply(Ext.util.Format, {
  177. thousandSeparator: '.',
  178. decimalSeparator: ',',
  179. currencySign: '\u20ac',
  180. dateFormat: 'd/m/Y'
  181. });
  182. });
  183. afterEach(function() {
  184. Ext.apply(Ext.util.Format, savedFormatLocale);
  185. });
  186. it("should format with no decimals", function() {
  187. expect(Ext.util.Format.number(1, "0")).toEqual("1");
  188. });
  189. it("should format with two decimals", function() {
  190. expect(Ext.util.Format.number(1, "0.00")).toEqual("1,00");
  191. });
  192. it("should format+round with two decimals, and no thousand separators", function() {
  193. expect(Ext.util.Format.number(1234.567, "0.00")).toEqual("1234,57");
  194. });
  195. it("should format+round with two decimals after a ',', and '.' as the thousand separator", function() {
  196. expect(Ext.util.Format.number(1234.567, ",0.00")).toEqual("1.234,57");
  197. });
  198. it("should format+round with no decimals, and '.' as the thousand separator", function() {
  199. expect(Ext.util.Format.number(1234.567, ",0")).toEqual("1.235");
  200. });
  201. });
  202. // In Ext4, the "/i" suffix allows you to use locale-specific separators in the format string, as opposed
  203. // to US/UK conventions. Output however ALWAYS follows the local settings in the Format singleton which may
  204. // be overridden by locale files.
  205. describe("number using FR locale with /i", function() {
  206. var savedFormatLocale = {
  207. thousandSeparator: Ext.util.Format.thousandSeparator,
  208. decimalSeparator: Ext.util.Format.decimalSeparator,
  209. currencySign: Ext.util.Format.currencySign,
  210. dateFormat: Ext.util.Format.dateFormat
  211. };
  212. // set up the FR formatting locale
  213. beforeEach(function() {
  214. Ext.apply(Ext.util.Format, {
  215. thousandSeparator: '.',
  216. decimalSeparator: ',',
  217. currencySign: '\u20ac',
  218. dateFormat: 'd/m/Y'
  219. });
  220. });
  221. afterEach(function() {
  222. Ext.apply(Ext.util.Format, savedFormatLocale);
  223. });
  224. // Demonstrate "Incorrect" use with "/i". '.' means thousand separator and ',' means decimal in FR locale.
  225. // Read carefully. In the formatting strings below, '.' is taken to mean thousand separator, and
  226. // ',' is taken to mean decimal separator
  227. it("should format with no decimals", function() {
  228. expect(Ext.util.Format.number(1, "0.00/i")).toEqual("1");
  229. });
  230. it("should format+round with no decimals, and '.' as thousand separator", function() {
  231. expect(Ext.util.Format.number(1234.567, "0.00/i")).toEqual("1.235");
  232. });
  233. it("should format+round with three decimals after a ',', and '.' as the thousand separator", function() {
  234. expect(Ext.util.Format.number(1234.567, ",0.00/i")).toEqual("1.234,567");
  235. });
  236. it("should format+round with one decimal, and no thousand separator", function() {
  237. expect(Ext.util.Format.number(1234.567, ",0/i")).toEqual("1234,6");
  238. });
  239. // Correct usage
  240. it("should format with two decimals", function() {
  241. expect(Ext.util.Format.number(1, "0,00/i")).toEqual("1,00");
  242. });
  243. it("should format+round with two decimals, and no thousand separators", function() {
  244. expect(Ext.util.Format.number(1234.567, "0,00/i")).toEqual("1234,57");
  245. });
  246. it("should format+round with two decimals after a ',', and '.' as the thousand separator", function() {
  247. expect(Ext.util.Format.number(1234.567, ".0,00/i")).toEqual("1.234,57");
  248. });
  249. it("should format+round with no decimals, and '.' as the thousand separator", function() {
  250. expect(Ext.util.Format.number(1234.567, ".0/i")).toEqual("1.235");
  251. });
  252. });
  253. it("should check for a 0 value before appending negative", function(){
  254. expect(Ext.util.Format.number(-2.842170943040401e-14, "0,000.00")).toEqual('0.00');
  255. });
  256. describe("number", function() {
  257. it("should return the number itself if formatString is not specified", function() {
  258. expect(Ext.util.Format.number(12345.67, undefined)).toBe(12345.67);
  259. expect(Ext.util.Format.number(12345.67, null)).toBe(12345.67);
  260. });
  261. it("should return empty string if value is not a number", function() {
  262. expect(Ext.util.Format.number("this is not a number", "0.00")).toBe("");
  263. });
  264. it("should raise error if more than one decimal point is specified in the format string", function() {
  265. expect(function() { Ext.util.Format.number("1234.67", "0.0.00") }).toRaiseExtError();
  266. });
  267. });
  268. describe("date", function() {
  269. it("should return empty string for undefined values", function() {
  270. expect(Ext.util.Format.date(undefined)).toBe('');
  271. });
  272. it("should return empty string for null values", function() {
  273. expect(Ext.util.Format.date(null)).toBe('');
  274. });
  275. it("should parse string dates", function() {
  276. expect(Ext.util.Format.date("10/15/81")).toBe("10/15/1981");
  277. });
  278. it("should format according to Ext.Date.defaultFormat if no format was specified", function() {
  279. var date = new Date(1981, 9, 15, 15, 46, 30);
  280. expect(Ext.util.Format.date(date)).toBe("10/15/1981");
  281. });
  282. it("should format according to specified format when specified", function() {
  283. var date = new Date(1981, 9, 15, 15, 46, 30);
  284. expect(Ext.util.Format.date(date, "d/m/Y H:i:s")).toBe("15/10/1981 15:46:30");
  285. });
  286. });
  287. describe("dateRenderer", function() {
  288. it("should return a function that formats dates with the specified format", function() {
  289. var date = new Date(1981, 9, 15, 15, 46, 30);
  290. expect(Ext.util.Format.dateRenderer("d/m/Y H:i:s").call(this, date)).toBe("15/10/1981 15:46:30");
  291. });
  292. });
  293. describe("stripTags", function() {
  294. it("should return undefined if value is undefined", function() {
  295. expect(Ext.util.Format.stripTags(undefined)).toBeUndefined();
  296. });
  297. it("should return null if value is null", function() {
  298. expect(Ext.util.Format.stripTags(null)).toBeNull();
  299. });
  300. it("should return the exact original value if it doesn't contains any tags", function() {
  301. expect(Ext.util.Format.stripTags("this string contains no tags")).toBe("this string contains no tags");
  302. });
  303. it("should strip tags when found", function() {
  304. expect(Ext.util.Format.stripTags("<p>this string <b>contains</b> tags</p>")).toBe("this string contains tags");
  305. });
  306. });
  307. describe("stripScripts", function() {
  308. it("should return undefined if value is undefined", function() {
  309. expect(Ext.util.Format.stripScripts(undefined)).toBeUndefined();
  310. });
  311. it("should return null if value is null", function() {
  312. expect(Ext.util.Format.stripScripts(null)).toBeNull();
  313. });
  314. it("should return the exact original value if it doesn't contains any scripts", function() {
  315. expect(Ext.util.Format.stripTags("this string contains no scripts")).toBe("this string contains no scripts");
  316. });
  317. it("should stript scripts when found", function() {
  318. expect(Ext.util.Format.stripScripts("<script>alert('foo');</script>this string <b>contains</b> scripts")).toBe("this string <b>contains</b> scripts");
  319. });
  320. });
  321. describe("fileSize", function() {
  322. it("should return the size in bytes if size < 1024", function() {
  323. expect(Ext.util.Format.fileSize(-9999999999)).toBe("-9999999999 bytes");
  324. expect(Ext.util.Format.fileSize(0)).toBe("0 bytes");
  325. expect(Ext.util.Format.fileSize(1023)).toBe("1023 bytes");
  326. });
  327. it("should return the size in kilobytes if 1024 <= size < 1MB", function() {
  328. expect(Ext.util.Format.fileSize(1024)).toBe("1 KB");
  329. expect(Ext.util.Format.fileSize(1024 * 1024 - 1)).toBe("1024 KB");
  330. });
  331. it("should return the size in megabytes if size >= 1MB", function() {
  332. expect(Ext.util.Format.fileSize(1024 * 1024)).toBe("1 MB");
  333. expect(Ext.util.Format.fileSize(1024 * 1024 * 100)).toBe("100 MB");
  334. expect(Ext.util.Format.fileSize(1024 * 1024 * 1024)).toBe("1024 MB");
  335. });
  336. });
  337. describe("math", function() {
  338. it("should return the first argument and the evaluation of the second argument", function() {
  339. expect(Ext.util.Format.math(12, '+ 12')).toBe(24);
  340. expect(Ext.util.Format.math(12, '* 12')).toBe(144);
  341. });
  342. });
  343. describe("round", function() {
  344. it("should preserve the original value if the precision is not specified", function() {
  345. expect(Ext.util.Format.round(1234.56)).toBe(1234.56);
  346. });
  347. it("should preserve the original value if precision is not a number", function() {
  348. expect(Ext.util.Format.round(1234.56, "invalid precision")).toBe(1234.56);
  349. });
  350. it("should round the value to the specified precision", function() {
  351. expect(Ext.util.Format.round(1234.50, 1)).toBe(1234.5);
  352. expect(Ext.util.Format.round(1234.54, 1)).toBe(1234.5);
  353. expect(Ext.util.Format.round(1234.55, 1)).toBe(1234.6);
  354. expect(Ext.util.Format.round(1234.59, 1)).toBe(1234.6);
  355. });
  356. });
  357. describe("numberRenderer", function() {
  358. it("should return a function that formats a number with the specified format", function() {
  359. expect(Ext.util.Format.numberRenderer("0.00")(123.321)).toBe("123.32");
  360. });
  361. });
  362. describe("plural", function() {
  363. it("should return the singular form if count == 1", function() {
  364. expect(Ext.util.Format.plural(1, "car")).toBe("1 car");
  365. expect(Ext.util.Format.plural(1, "child", "children")).toBe("1 child");
  366. });
  367. it("should return the plural as singular+s by default if count <> 1", function() {
  368. expect(Ext.util.Format.plural(0, "car")).toBe("0 cars");
  369. expect(Ext.util.Format.plural(2, "car")).toBe("2 cars");
  370. });
  371. it("should return the specified plural if count <> 1", function() {
  372. expect(Ext.util.Format.plural(0, "child", "children")).toBe("0 children");
  373. expect(Ext.util.Format.plural(2, "child", "children")).toBe("2 children");
  374. });
  375. });
  376. describe("nl2br", function() {
  377. it("should convert newline characters to <br/>", function() {
  378. expect(Ext.util.Format.nl2br("first line\nsecond line")).toBe("first line<br/>second line");
  379. });
  380. it("should be null/undefined safe", function() {
  381. expect(Ext.util.Format.nl2br(undefined)).toBe("");
  382. expect(Ext.util.Format.nl2br(null)).toBe("");
  383. });
  384. });
  385. describe("capitalize", function() {
  386. it("should be alias to Ext.String.capitalize", function() {
  387. expect(Ext.util.Format.capitalize).toBe(Ext.String.capitalize);
  388. });
  389. });
  390. describe("ellipsis", function() {
  391. it("should be alias to Ext.String.ellipsis", function() {
  392. expect(Ext.util.Format.ellipsis).toBe(Ext.String.ellipsis);
  393. });
  394. });
  395. describe("format", function() {
  396. it("should be alias to Ext.String.format", function() {
  397. expect(Ext.util.Format.format).toBe(Ext.String.format);
  398. });
  399. });
  400. describe("htmlDecode", function() {
  401. it("should be alias to Ext.String.htmlDecode", function() {
  402. expect(Ext.util.Format.htmlDecode).toBe(Ext.String.htmlDecode);
  403. });
  404. });
  405. describe("htmlEncode", function() {
  406. it("should be alias to Ext.String.htmlEncode", function() {
  407. expect(Ext.util.Format.htmlEncode).toBe(Ext.String.htmlEncode);
  408. });
  409. });
  410. describe("leftPad", function() {
  411. it("should be alias to Ext.String.leftPad", function() {
  412. expect(Ext.util.Format.leftPad).toBe(Ext.String.leftPad);
  413. });
  414. });
  415. describe("trim", function() {
  416. it("should be alias to Ext.String.trim", function() {
  417. expect(Ext.util.Format.trim).toBe(Ext.String.trim);
  418. });
  419. });
  420. describe("parseBox", function() {
  421. it("should return a box when 4 margins are specified", function() {
  422. expect(Ext.util.Format.parseBox("1 2 3 4")).toEqual({
  423. top: 1,
  424. right: 2,
  425. bottom: 3,
  426. left: 4
  427. });
  428. });
  429. it("should return a box when 3 margins are specified", function() {
  430. expect(Ext.util.Format.parseBox("1 2 3")).toEqual({
  431. top: 1,
  432. right: 2,
  433. bottom: 3,
  434. left: 2
  435. });
  436. });
  437. it("should return a box when 2 margins are specified", function() {
  438. expect(Ext.util.Format.parseBox("1 2")).toEqual({
  439. top: 1,
  440. right: 2,
  441. bottom: 1,
  442. left: 2
  443. });
  444. });
  445. it("should return a box when 1 margin is specified", function() {
  446. expect(Ext.util.Format.parseBox("1")).toEqual({
  447. top: 1,
  448. right: 1,
  449. bottom: 1,
  450. left: 1
  451. });
  452. });
  453. it("should return a box when 1 margin is specified as number", function() {
  454. expect(Ext.util.Format.parseBox(1)).toEqual({
  455. top: 1,
  456. right: 1,
  457. bottom: 1,
  458. left: 1
  459. });
  460. });
  461. it("should return a 0 margin box when no margin is specified", function() {
  462. var zeroMarginBox = {
  463. top: 0,
  464. right: 0,
  465. bottom: 0,
  466. left: 0
  467. };
  468. expect(Ext.util.Format.parseBox(undefined)).toEqual(zeroMarginBox);
  469. expect(Ext.util.Format.parseBox(null)).toEqual(zeroMarginBox);
  470. expect(Ext.util.Format.parseBox("")).toEqual(zeroMarginBox);
  471. expect(Ext.util.Format.parseBox(" ")).toEqual(zeroMarginBox);
  472. });
  473. });
  474. describe("escapeRegex", function() {
  475. it("should escape regular expressions", function() {
  476. expect(Ext.util.Format.escapeRegex("-.*+?^${}()|[]/\\abc0123")).toBe("\\-\\.\\*\\+\\?\\^\\$\\{\\}\\(\\)\\|\\[\\]\\/\\\\abc0123");
  477. });
  478. });
  479. });