3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
7 Ext.test.session.addTest('DataTypes', {
8 name: 'Test data type conversion',
10 this.c = function(type, scope, value){
11 return type.convert.call(scope || {}, value);
19 test_auto: function(){
20 var type = Ext.data.Types.AUTO,
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');
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');
35 test_string: function(){
36 var type = Ext.data.Types.STRING,
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');
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');
53 var type = Ext.data.Types.INT;
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');
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');
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');
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)');
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');
80 var orig = Ext.data.Types.stripRe;
81 Ext.data.Types.stripRe = /[!]/g;
83 Y.Assert.areEqual(987654, this.c(type, null, '987!654.34'), 'Test with custom stripRe');
85 Ext.data.Types.stripRe = orig;
88 test_float: function(){
89 var type = Ext.data.Types.FLOAT;
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');
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');
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');
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)');
111 Y.Assert.areEqual(198.54, this.c(type, null, '$198.54'), 'Test with $ in the string');
114 var orig = Ext.data.Types.stripRe;
115 Ext.data.Types.stripRe = /[\$,]/g;
117 Y.Assert.areEqual(121343.17, this.c(type, null, '$121,343.17'), 'Test with custom stripRe');
119 Ext.data.Types.stripRe = orig;
122 test_bool: function(){
123 var type = Ext.data.Types.BOOL;
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');
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)');
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');
142 test_date: function(){
143 var type = Ext.data.Types.DATE,
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');
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');
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.
159 d = new Date(n * 1000);
160 Y.Assert.areEqual(d.getTime(), this.c(type, {dateFormat: 'timestamp'}, n).getTime(), 'Test with timestamp format');
163 var n = 11111111110000;
165 Y.Assert.areEqual(d.getTime(), this.c(type, {dateFormat: 'time'}, n).getTime(), 'Test with time format');
168 var format = 'Y-m-d',
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)');
174 //no format, default parse
175 val = 'Wed, 18 Oct 2002 13:00:00';
177 Y.Assert.areEqual(d, this.c(type, null, val).getTime(), 'Test with no format, valid date');
180 Y.Assert.isNull(this.c(type, null, val), 'Test with no format, invalid date');