Commit | Line | Data |
---|---|---|
6a1aa64f DV |
1 | if (typeof(dojo) != 'undefined') { dojo.require('MochiKit.Format'); } |
2 | if (typeof(JSAN) != 'undefined') { JSAN.use('MochiKit.Format'); } | |
3 | if (typeof(tests) == 'undefined') { tests = {}; } | |
4 | ||
5 | tests.test_Format = function (t) { | |
6 | t.is( truncToFixed(0.1234, 3), "0.123", "truncToFixed truncate" ); | |
7 | t.is( truncToFixed(0.12, 3), "0.120", "truncToFixed trailing zeros" ); | |
8 | t.is( truncToFixed(0.15, 1), "0.1", "truncToFixed no round" ); | |
9 | t.is( truncToFixed(0.15, 0), "0", "truncToFixed zero (edge case)" ); | |
10 | ||
11 | t.is( roundToFixed(0.1234, 3), "0.123", "roundToFixed truncate" ); | |
12 | t.is( roundToFixed(0.12, 3), "0.120", "roundToFixed trailing zeros" ); | |
13 | t.is( roundToFixed(0.15, 1), "0.2", "roundToFixed round" ); | |
14 | t.is( roundToFixed(0.15, 0), "0", "roundToFixed zero (edge case)" ); | |
15 | ||
16 | t.is( twoDigitFloat(-0.1234), "-0.12", "twoDigitFloat -0.1234 correct"); | |
17 | t.is( twoDigitFloat(-0.1), "-0.1", "twoDigitFloat -0.1 correct"); | |
18 | t.is( twoDigitFloat(-0), "0", "twoDigitFloat -0 correct"); | |
19 | t.is( twoDigitFloat(0), "0", "twoDigitFloat 0 correct"); | |
20 | t.is( twoDigitFloat(1), "1", "twoDigitFloat 1 correct"); | |
21 | t.is( twoDigitFloat(1.0), "1", "twoDigitFloat 1.0 correct"); | |
22 | t.is( twoDigitFloat(1.2), "1.2", "twoDigitFloat 1.2 correct"); | |
23 | t.is( twoDigitFloat(1.234), "1.23", "twoDigitFloat 1.234 correct"); | |
24 | t.is( percentFormat(123), "12300%", "percentFormat 123 correct"); | |
25 | t.is( percentFormat(1.23), "123%", "percentFormat 123 correct"); | |
26 | t.is( twoDigitAverage(1, 0), "0", "twoDigitAverage dbz correct"); | |
27 | t.is( twoDigitAverage(1, 1), "1", "twoDigitAverage 1 correct"); | |
28 | t.is( twoDigitAverage(1, 10), "0.1", "twoDigitAverage .1 correct"); | |
29 | function reprIs(a, b) { | |
30 | arguments[0] = repr(a); | |
31 | arguments[1] = repr(b); | |
32 | t.is.apply(this, arguments); | |
33 | } | |
34 | reprIs( lstrip("\r\t\n foo \n\t\r"), "foo \n\t\r", "lstrip whitespace chars" ); | |
35 | reprIs( rstrip("\r\t\n foo \n\t\r"), "\r\t\n foo", "rstrip whitespace chars" ); | |
36 | reprIs( strip("\r\t\n foo \n\t\r"), "foo", "strip whitespace chars" ); | |
37 | reprIs( lstrip("\r\n\t \r", "\r"), "\n\t \r", "lstrip custom chars" ); | |
38 | reprIs( rstrip("\r\n\t \r", "\r"), "\r\n\t ", "rstrip custom chars" ); | |
39 | reprIs( strip("\r\n\t \r", "\r"), "\n\t ", "strip custom chars" ); | |
40 | ||
41 | var nf = numberFormatter("$###,###.00 footer"); | |
42 | t.is( nf(1000.1), "$1,000.10 footer", "trailing zeros" ); | |
43 | t.is( nf(1000000.1), "$1,000,000.10 footer", "two seps" ); | |
44 | t.is( nf(100), "$100.00 footer", "shorter than sep" ); | |
45 | t.is( nf(100.555), "$100.56 footer", "rounding" ); | |
46 | t.is( nf(-100.555), "$-100.56 footer", "default neg" ); | |
47 | nf = numberFormatter("-$###,###.00"); | |
48 | t.is( nf(-100.555), "-$100.56", "custom neg" ); | |
49 | nf = numberFormatter("0000.0000"); | |
50 | t.is( nf(0), "0000.0000", "leading and trailing" ); | |
51 | t.is( nf(1.1), "0001.1000", "leading and trailing" ); | |
52 | t.is( nf(12345.12345), "12345.1235", "no need for leading/trailing" ); | |
53 | nf = numberFormatter("0000.0000"); | |
54 | t.is( nf("taco"), "", "default placeholder" ); | |
55 | nf = numberFormatter("###,###.00", "foo", "de_DE"); | |
56 | t.is( nf("taco"), "foo", "custom placeholder" ); | |
57 | t.is( nf(12345.12345), "12.345,12", "de_DE locale" ); | |
58 | nf = numberFormatter("#%"); | |
59 | t.is( nf(1), "100%", "trivial percent" ); | |
60 | t.is( nf(0.55), "55%", "percent" ); | |
61 | ||
62 | var customLocale = { | |
63 | separator: " apples and ", | |
64 | decimal: " bagels at ", | |
65 | percent: "am for breakfast"}; | |
66 | var customFormatter = numberFormatter("###,###.0%", "No breakfast", customLocale); | |
67 | t.is( customFormatter(23.458), "2 apples and 345 bagels at 8am for breakfast", "custom locale" ); | |
68 | ||
69 | nf = numberFormatter("###,###"); | |
70 | t.is( nf(123), "123", "large number format" ); | |
71 | t.is( nf(1234), "1,234", "large number format" ); | |
72 | t.is( nf(12345), "12,345", "large number format" ); | |
73 | t.is( nf(123456), "123,456", "large number format" ); | |
74 | t.is( nf(1234567), "1,234,567", "large number format" ); | |
75 | t.is( nf(12345678), "12,345,678", "large number format" ); | |
76 | t.is( nf(123456789), "123,456,789", "large number format" ); | |
77 | t.is( nf(1234567890), "1,234,567,890", "large number format" ); | |
78 | t.is( nf(12345678901), "12,345,678,901", "large number format" ); | |
79 | t.is( nf(123456789012), "123,456,789,012", "large number format" ); | |
80 | }; |