forked from geddy/geddy
/
model_datatypes.js
211 lines (177 loc) · 5.83 KB
/
model_datatypes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Load the basic Geddy toolkit
require('../lib/geddy');
geddy.config = {
i18n: {
defaultLocale: 'en-us'
}
};
var model = require('../lib/model')
, assert = require('assert')
, tests;
geddy.model = model;
var ByTor = function () {
this.property('numberProp', 'number');
this.property('intProp', 'int');
this.property('objectProp', 'object');
this.property('arrayProp', 'array');
this.property('dateProp', 'date');
this.property('datetimeProp', 'datetime');
this.property('timeProp', 'time');
};
ByTor = geddy.model.register('ByTor', ByTor);
var tests = new (function () {
this.testAllOptional = function () {
var params = {};
var byTor = ByTor.create(params);
assert.ok(byTor.isValid());
};
this.testNumber = function () {
var byTor;
// Actual number, valid
byTor = ByTor.create({numberProp: 2112});
assert.ok(byTor.isValid());
// Numeric string, valid
byTor = ByTor.create({numberProp: '2112'});
assert.ok(byTor.isValid());
// Non-numeric string, error
byTor = ByTor.create({numberProp: 'Snow Dog'});
assert.notStrictEqual(byTor.errors.numberProp, undefined);
};
this.testInt = function () {
var byTor;
// Actual int, valid
byTor = ByTor.create({intProp: 2112});
assert.ok(byTor.isValid());
// Actual int, valid
byTor = ByTor.create({intProp: '2112'});
assert.ok(byTor.isValid());
// Float with zero decimal, valid
byTor = ByTor.create({intProp: 2112.0});
assert.ok(byTor.isValid());
// Float with greater-than-zero decimal, error
byTor = ByTor.create({intProp: 2112.2112});
assert.notStrictEqual(byTor.errors.intProp, undefined);
// Non-numeric string, error
byTor = ByTor.create({intProp: 'away from here'});
assert.notStrictEqual(byTor.errors.intProp, undefined);
};
this.testObject = function () {
var byTor;
// Actual Object, valid
byTor = ByTor.create({objectProp: {}});
assert.ok(byTor.isValid());
// Sure, technically Arrays are Objects, but this still isn't right
byTor = ByTor.create({objectProp: []});
assert.notStrictEqual(byTor.errors.objectProp, undefined);
// string, should fail
byTor = ByTor.create({objectProp: 'As gray traces of dawn ...'});
assert.notStrictEqual(byTor.errors.objectProp, undefined);
};
this.testArray = function () {
var byTor;
// Actual Array, valid
byTor = ByTor.create({arrayProp: []});
assert.ok(byTor.isValid());
// Sure, technically Arrays are Objects, but this still isn't right
byTor = ByTor.create({arrayProp: {}});
assert.notStrictEqual(byTor.errors.arrayProp, undefined);
// string, should fail
byTor = ByTor.create({arrayProp: 'As gray traces of dawn ...'});
assert.notStrictEqual(byTor.errors.arrayProp, undefined);
};
this.testDate = function () {
var byTor;
var dates = [
'12/27/1968'
, '12-27-1968'
, '12.27.1968'
, '1968/12/27'
, '1968-12-27'
, '1968.12.27'
, [1968, 12, 27]
, new Date(1968, 11, 27)
, new Date('12/27/1968')
, 'Fri, 27 Dec 1968'
];
var dt;
for (var i = 0, ii = dates.length; i < ii; i++) {
dt = dates[i];
byTor = ByTor.create({dateProp: dt});
assert.ok(byTor.isValid(), 'Testing valid');
assert.equal(byTor.dateProp.getFullYear(), 1968, 'Testing getFullYear');
assert.equal(byTor.dateProp.getMonth(), 11, 'Testing getMonth'); // Zero-based
assert.equal(byTor.dateProp.getDate(), 27, 'Testing getDate');
}
};
this.testDatetime = function () {
var byTor;
var dates, dt;
// Dates with no set time -- time should be set to 12 midnight
dates = [
'1968/12/27'
, '1968-12-27'
, '1968.12.27'
, [1968, 12, 27]
, new Date(1968, 11, 27)
, new Date('12/27/1968')
, 'Fri, 27 Dec 1968'
];
for (var i = 0, ii = dates.length; i < ii; i++) {
dt = dates[i];
byTor = ByTor.create({datetimeProp: dt});
assert.ok(byTor.isValid());
assert.equal(byTor.datetimeProp.getFullYear(), 1968);
assert.equal(byTor.datetimeProp.getMonth(), 11); // Zero-based
assert.equal(byTor.datetimeProp.getDate(), 27);
assert.equal(byTor.datetimeProp.getHours(), 0);
assert.equal(byTor.datetimeProp.getMinutes(), 0);
assert.equal(byTor.datetimeProp.getSeconds(), 0);
}
// Dates with times
dates = [
'1968-12-27 16:10:03'
, [1968, 12, 27, 16, 10, 3]
, new Date(1968, 11, 27, 16, 10, 3)
, 'Fri, 27 Dec 1968 16:10:03'
];
for (var i = 0, ii = dates.length; i < ii; i++) {
dt = dates[i];
byTor = ByTor.create({datetimeProp: dt});
assert.ok(byTor.isValid());
assert.equal(byTor.datetimeProp.getFullYear(), 1968);
assert.equal(byTor.datetimeProp.getMonth(), 11); // Zero-based
assert.equal(byTor.datetimeProp.getDate(), 27);
assert.equal(byTor.datetimeProp.getHours(), 16);
assert.equal(byTor.datetimeProp.getMinutes(), 10);
assert.equal(byTor.datetimeProp.getSeconds(), 3);
}
};
this.testTime = function () {
var byTor;
var dates, dt, vals;
// Obj key is the input string, value is the list of values
// for the parse Date object's h/m/s/ms
dates = {
'21:12' : [21, 12, 0, 0]
, '1:11': [1, 11, 0, 0]
, '1:11:03': [1, 11, 3, 0]
, '1:11:03.999': [1, 11, 3, 999]
};
for (var p in dates) {
dt = p;
vals = dates[p];
byTor = ByTor.create({timeProp: dt});
assert.ok(byTor.isValid());
assert.equal(byTor.timeProp.getHours(), vals[0]);
assert.equal(byTor.timeProp.getMinutes(), vals[1]);
assert.equal(byTor.timeProp.getSeconds(), vals[2]);
assert.equal(byTor.timeProp.getMilliseconds(), vals[3]);
}
};
})();
for (var p in tests) {
if (typeof tests[p] == 'function') {
console.log('Running ' + p);
tests[p]();
}
}