]> git.proxmox.com Git - extjs.git/blame - extjs/packages/core/src/lang/Assert.js
add extjs 6.0.1 sources
[extjs.git] / extjs / packages / core / src / lang / Assert.js
CommitLineData
6527f429
DM
1// @define Ext.lang.Assert\r
2// @define Ext.Assert\r
3// @require Ext.lang.Error\r
4//<debug>\r
5/**\r
6 * @class Ext.Assert\r
7 * This class provides help value testing methods useful for diagnostics. These are often\r
8 * used in `debugHooks`:\r
9 * \r
10 * Ext.define('Foo.bar.Class', {\r
11 *\r
12 * debugHooks: {\r
13 * method: function (a) {\r
14 * Ext.Assert.truthy(a, 'Expected "a" to be truthy');\r
15 * },\r
16 *\r
17 * foo: function (object) {\r
18 * Ext.Assert.isFunctionProp(object, 'doSomething');\r
19 * }\r
20 * }\r
21 * });\r
22 * \r
23 * **NOTE:** This class is entirely removed in production builds so all uses of it should\r
24 * be either in `debug` conditional comments or `debugHooks`.\r
25 * \r
26 * The following type detection methods from the `Ext` object are wrapped as assertions\r
27 * by this class:\r
28 * \r
29 * * `isEmpty`\r
30 * * `isArray`\r
31 * * `isDate`\r
32 * * `isObject`\r
33 * * `isSimpleObject`\r
34 * * `isPrimitive`\r
35 * * `isFunction`\r
36 * * `isNumber`\r
37 * * `isNumeric`\r
38 * * `isString`\r
39 * * `isBoolean`\r
40 * * `isElement`\r
41 * * `isTextNode`\r
42 * * `isDefined`\r
43 * * `isIterable`\r
44 * \r
45 * These appear both their exact name and with a "Prop" suffix for checking a property on\r
46 * an object. For example, these are almost identical:\r
47 * \r
48 * Ext.Assert.isFunction(object.foo);\r
49 *\r
50 * Ext.Assert.isFunctionProp(object, 'foo');\r
51 *\r
52 * The difference is the default error message generated is better in the second use case\r
53 * than the first.\r
54 * \r
55 * The above list are also expanded for "Not" flavors (and "Not...Prop"):\r
56 * \r
57 * * `isNotEmpty`\r
58 * * `isNotArray`\r
59 * * `isNotDate`\r
60 * * `isNotObject`\r
61 * * `isNotSimpleObject`\r
62 * * `isNotPrimitive`\r
63 * * `isNotFunction`\r
64 * * `isNotNumber`\r
65 * * `isNotNumeric`\r
66 * * `isNotString`\r
67 * * `isNotBoolean`\r
68 * * `isNotElement`\r
69 * * `isNotTextNode`\r
70 * * `isNotDefined`\r
71 * * `isNotIterable`\r
72 */\r
73Ext.Assert = {\r
74\r
75 /**\r
76 * Checks that the first argument is falsey and throws an `Error` if it is not.\r
77 */\r
78 falsey: function (b, msg) {\r
79 if (b) {\r
80 Ext.raise(msg || ('Expected a falsey value but was ' + b));\r
81 }\r
82 },\r
83\r
84 /**\r
85 * Checks that the first argument is falsey and throws an `Error` if it is not.\r
86 */\r
87 falseyProp: function (object, property) {\r
88 Ext.Assert.truthy(object);\r
89 var b = object[property];\r
90 if (b) {\r
91 if (object.$className) {\r
92 property = object.$className + '#' + property;\r
93 }\r
94 Ext.raise('Expected a falsey value for ' + property +\r
95 ' but was ' + b);\r
96 }\r
97 },\r
98\r
99 /**\r
100 * Checks that the first argument is truthy and throws an `Error` if it is not.\r
101 */\r
102 truthy: function (b, msg) {\r
103 if (!b) {\r
104 Ext.raise(msg || ('Expected a truthy value but was ' + typeof b));\r
105 }\r
106 },\r
107\r
108 /**\r
109 * Checks that the first argument is truthy and throws an `Error` if it is not.\r
110 */\r
111 truthyProp: function (object, property) {\r
112 Ext.Assert.truthy(object);\r
113 var b = object[property];\r
114 if (!b) {\r
115 if (object.$className) {\r
116 property = object.$className + '#' + property;\r
117 }\r
118 Ext.raise('Expected a truthy value for ' + property +\r
119 ' but was ' + typeof b);\r
120 }\r
121 }\r
122};\r
123\r
124(function () {\r
125 function makeAssert (name, kind) {\r
126 var testFn = Ext[name],\r
127 def;\r
128 return function (value, msg) {\r
129 if (!testFn(value)) {\r
130 Ext.raise(msg || def ||\r
131 (def = 'Expected value to be ' + kind));\r
132 }\r
133 };\r
134 }\r
135\r
136 function makeAssertProp (name, kind) {\r
137 var testFn = Ext[name],\r
138 def;\r
139 return function (object, prop) {\r
140 Ext.Assert.truthy(object);\r
141 if (!testFn(object[prop])) {\r
142 Ext.raise(def || (def = 'Expected ' +\r
143 (object.$className ? object.$className + '#' : '') +\r
144 prop + ' to be ' + kind));\r
145 }\r
146 };\r
147 }\r
148\r
149 function makeNotAssert (name, kind) {\r
150 var testFn = Ext[name],\r
151 def;\r
152 return function (value, msg) {\r
153 if (testFn(value)) {\r
154 Ext.raise(msg || def ||\r
155 (def = 'Expected value to NOT be ' + kind));\r
156 }\r
157 };\r
158 }\r
159\r
160 function makeNotAssertProp (name, kind) {\r
161 var testFn = Ext[name],\r
162 def;\r
163 return function (object, prop) {\r
164 Ext.Assert.truthy(object);\r
165 if (testFn(object[prop])) {\r
166 Ext.raise(def || (def = 'Expected ' +\r
167 (object.$className ? object.$className + '#' : '') +\r
168 prop + ' to NOT be ' + kind));\r
169 }\r
170 };\r
171 }\r
172\r
173 for (var name in Ext) {\r
174 if (name.substring(0,2) == "is" && Ext.isFunction(Ext[name])) {\r
175 var kind = name.substring(2);\r
176 Ext.Assert[name] = makeAssert(name, kind);\r
177 Ext.Assert[name + 'Prop'] = makeAssertProp(name, kind);\r
178 Ext.Assert['isNot' + kind] = makeNotAssert(name, kind);\r
179 Ext.Assert['isNot' + kind + 'Prop'] = makeNotAssertProp(name, kind);\r
180 }\r
181 }\r
182}());\r
183\r
184//</debug>\r