Upgrade to ExtJS 3.2.0 - Released 03/30/2010
[extjs.git] / test / unit / data / Types.js
1 /*!
2  * Ext JS Library 3.2.0
3  * Copyright(c) 2006-2010 Ext JS, Inc.
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 Ext.test.session.addTest('DataTypes', {
8     name: 'Test data type conversion',
9     setUp: function(){
10         this.c = function(type, scope, value){
11             return type.convert.call(scope || {}, value);    
12         };
13     },
14     
15     tearDown: function(){
16         delete this.c;
17     },
18     
19     test_auto: function(){
20         var type = Ext.data.Types.AUTO,
21             d = new Date();
22         
23         Y.Assert.areEqual('auto', type.type, 'Check the string representation is correct');
24         Y.Assert.areEqual(Ext.data.SortTypes.none, type.sortType, 'Check the default sort type');
25         
26         Y.Assert.isUndefined(this.c(type, null, undefined), 'Test convert with undefined');
27         Y.Assert.isNull(this.c(type, null, null), 'Test convert with undefined');
28         Y.Assert.areEqual(1, this.c(type, null, 1), 'Test with a number');
29         Y.Assert.areEqual(-37.4, this.c(type, null, -37.4), 'Test with a float');
30         Y.Assert.areEqual('foo', this.c(type, null, 'foo'), 'Test with a string');
31         Y.Assert.areEqual(d, this.c(type, null, d), 'Test with a date');
32         Y.Assert.isTrue(this.c(type, null, true), 'Test with a boolean value');
33     },
34     
35     test_string: function(){
36         var type = Ext.data.Types.STRING,
37             d = new Date();
38             
39         Y.Assert.areEqual('string', type.type, 'Check the string representation is correct');
40         Y.Assert.areEqual(Ext.data.SortTypes.asUCString, type.sortType, 'Check the default sort type');
41         
42         Y.Assert.areEqual('', this.c(type, null, undefined), 'Test with undefined');
43         Y.Assert.areEqual('', this.c(type, null, null), 'Test with null');
44         Y.Assert.areEqual('', this.c(type, null, ''), 'Test with empty string');
45         Y.Assert.areEqual('username', this.c(type, null, 'username'), 'Test with simple string');
46         Y.Assert.areEqual('72', this.c(type, null, 72), 'Test with integer');
47         Y.Assert.areEqual('3.4', this.c(type, null, 3.4), 'Test with float number');
48         Y.Assert.areEqual('false', this.c(type, null, 'false'), 'Test with boolean false');
49         Y.Assert.areEqual(d.toString(), this.c(type, null, d), 'Test with date');
50     },
51     
52     test_int: function(){
53         var type = Ext.data.Types.INT;
54         
55         Y.Assert.areEqual('int', type.type, 'Check the string representation is correct');
56         Y.Assert.areEqual(type, Ext.data.Types.INTEGER, 'Test INTEGER alias works correctly');
57         Y.Assert.areEqual(Ext.data.SortTypes.none, type.sortType, 'Check the default sort type');
58         
59         // test invalid
60         Y.Assert.areEqual(0, this.c(type, null, undefined), 'Test with undefined');
61         Y.Assert.areEqual(0, this.c(type, null, null), 'Test with null');
62         Y.Assert.areEqual(0, this.c(type, null, ''), 'Test with empty string');
63         
64         // test expected
65         Y.Assert.areEqual(14, this.c(type, null, '14'), 'Test with numeric string value');
66         Y.Assert.areEqual(100, this.c(type, null, 100), 'Test with integer');
67         Y.Assert.areEqual(-12, this.c(type, null, -12), 'Test with negative integer');
68         
69         // test with floats
70         Y.Assert.areEqual(71, this.c(type, null, 71.41), 'Test with float');
71         Y.Assert.areEqual(-14, this.c(type, null, -14.99), 'Test with negative float');
72         Y.Assert.areEqual(16, this.c(type, null, '16.03'), 'Test with float (string)');
73         
74         // test stripRe
75         Y.Assert.areEqual(12, this.c(type, null, '$12'), 'Test with $ in the string');
76         Y.Assert.areEqual(-112, this.c(type, null, '-112%'), 'Test with %');
77         Y.Assert.areEqual(123456, this.c(type, null, '123,456.00'), 'Test with , seperating numbers');
78         
79         //custom stripRe
80         var orig = Ext.data.Types.stripRe;
81         Ext.data.Types.stripRe = /[!]/g;
82         
83         Y.Assert.areEqual(987654, this.c(type, null, '987!654.34'), 'Test with custom stripRe');
84         
85         Ext.data.Types.stripRe = orig;
86     },
87     
88     test_float: function(){
89         var type = Ext.data.Types.FLOAT;
90         
91         Y.Assert.areEqual('float', type.type, 'Check the string representation is correct');
92         Y.Assert.areEqual(type, Ext.data.Types.NUMBER, 'Test NUMBER alias works correctly');
93         Y.Assert.areEqual(Ext.data.SortTypes.none, type.sortType, 'Check the default sort type');
94         
95         // test invalid
96         Y.Assert.areEqual(0, this.c(type, null, undefined), 'Test with undefined');
97         Y.Assert.areEqual(0, this.c(type, null, null), 'Test with null');
98         Y.Assert.areEqual(0, this.c(type, null, ''), 'Test with empty string');
99         
100         // test ints
101         Y.Assert.areEqual(14, this.c(type, null, '14'), 'Test with numeric string value');
102         Y.Assert.areEqual(100, this.c(type, null, 100), 'Test with integer');
103         Y.Assert.areEqual(-12, this.c(type, null, -12), 'Test with negative integer');
104         
105         // test with floats
106         Y.Assert.areEqual(71.41, this.c(type, null, 71.41), 'Test with float');
107         Y.Assert.areEqual(-14.99, this.c(type, null, -14.99), 'Test with negative float');
108         Y.Assert.areEqual(16.03, this.c(type, null, '16.03'), 'Test with float (string)');
109         
110         // test stripRe
111         Y.Assert.areEqual(198.54, this.c(type, null, '$198.54'), 'Test with $ in the string');
112         
113         //custom stripRe
114         var orig = Ext.data.Types.stripRe;
115         Ext.data.Types.stripRe = /[\$,]/g;
116         
117         Y.Assert.areEqual(121343.17, this.c(type, null, '$121,343.17'), 'Test with custom stripRe');
118         
119         Ext.data.Types.stripRe = orig;
120     },
121     
122     test_bool: function(){
123         var type = Ext.data.Types.BOOL;
124         
125         Y.Assert.areEqual('bool', type.type, 'Check the string representation is correct');
126         Y.Assert.areEqual(type, Ext.data.Types.BOOLEAN, 'Test BOOLEAN alias works correctly');
127         Y.Assert.areEqual(Ext.data.SortTypes.none, type.sortType, 'Check the default sort type');
128         
129         Y.Assert.isTrue(this.c(type, null, true), 'Test with true (boolean)');
130         Y.Assert.isTrue(this.c(type, null, 'true'), 'Test with true (string)');
131         Y.Assert.isTrue(this.c(type, null, 1), 'Test with 1 (number)' );
132         Y.Assert.isTrue(this.c(type, null, '1'), 'Test with 1 (string)');
133         
134         Y.Assert.isFalse(this.c(type, null, false), 'Test with bool');
135         Y.Assert.isFalse(this.c(type, null, 7), 'Test with number');
136         Y.Assert.isFalse(this.c(type, null, 'foo'), 'Test with string');
137         Y.Assert.isFalse(this.c(type, null, {}), 'Test with object');
138         Y.Assert.isFalse(this.c(type, null, []), 'Test with array');
139         Y.Assert.isFalse(this.c(type, null, new Date()), 'Test with date');
140     },
141     
142     test_date: function(){
143         var type = Ext.data.Types.DATE,
144             d = new Date();
145         
146         Y.Assert.areEqual('date', type.type, 'Check the string representation is correct');
147         Y.Assert.areEqual(Ext.data.SortTypes.asDate, type.sortType, 'Check the default sort type');
148         
149         Y.Assert.isNull(this.c(type, null, undefined), 'Test with undefined');
150         Y.Assert.isNull(this.c(type, null, null), 'Test with null');
151         Y.Assert.isNull(this.c(type, null, false), 'Test with false');
152         Y.Assert.areEqual(d, this.c(type, null, d), 'Test with date');
153         
154         // these aren't really great, but they aren't to test the validity of the dates, just that they get parsed
155         // in the right format and returned.
156         
157         // timestamp
158         var n = 1234567890;
159         d = new Date(n * 1000);
160         Y.Assert.areEqual(d.getTime(), this.c(type, {dateFormat: 'timestamp'}, n).getTime(), 'Test with timestamp format');
161         
162         // time
163         var n = 11111111110000;
164         d = new Date(n);
165         Y.Assert.areEqual(d.getTime(), this.c(type, {dateFormat: 'time'}, n).getTime(), 'Test with time format');
166         
167         //custom format
168         var format = 'Y-m-d',
169             val = '1986-03-03';
170             
171         d = Date.parseDate(val, format);
172         Y.Assert.areEqual(d.getTime(), this.c(type, {dateFormat: format}, val).getTime(), 'Test with custom format (Y-m-d)');
173         
174         //no format, default parse
175         val = 'Wed, 18 Oct 2002 13:00:00';
176         d = Date.parse(val);
177         Y.Assert.areEqual(d, this.c(type, null, val).getTime(), 'Test with no format, valid date');
178         
179         val = 'will fail';
180         Y.Assert.isNull(this.c(type, null, val), 'Test with no format, invalid date');
181     }
182 });