PageRenderTime 26ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/js/dojo/tests/_base/declare.js

https://github.com/Deaygo/Diomedes-playbook
JavaScript | 408 lines | 379 code | 19 blank | 10 comment | 4 complexity | 7f0ce09616bcf73b6a1a919ffb998b7c MD5 | raw file
  1. dojo.provide("tests._base.declare");
  2. tests.register("tests._base.declare",
  3. [
  4. function smokeTest(t){
  5. dojo.declare("tests._base.declare.tmp");
  6. var tmp = new tests._base.declare.tmp();
  7. dojo.declare("testsFoo");
  8. var tmp = new testsFoo();
  9. },
  10. function smokeTest2(t){
  11. dojo.declare("tests._base.declare.foo", null, {
  12. foo: "thonk"
  13. });
  14. var tmp = new tests._base.declare.foo();
  15. t.is("thonk", tmp.foo);
  16. dojo.declare("testsFoo2", null, {
  17. foo: "thonk"
  18. });
  19. var tmp2 = new testsFoo2();
  20. t.is("thonk", tmp2.foo);
  21. },
  22. function smokeTestWithCtor(t){
  23. dojo.declare("tests._base.declare.fooBar", null, {
  24. constructor: function(){
  25. this.foo = "blah";
  26. },
  27. foo: "thonk"
  28. });
  29. var tmp = new tests._base.declare.fooBar();
  30. t.is("blah", tmp.foo);
  31. },
  32. function smokeTestCompactArgs(t){
  33. dojo.declare("tests._base.declare.fooBar2", null, {
  34. foo: "thonk"
  35. });
  36. var tmp = new tests._base.declare.fooBar2();
  37. t.is("thonk", tmp.foo);
  38. },
  39. function subclass(t){
  40. dojo.declare("tests._base.declare.tmp3", null, {
  41. foo: "thonk"
  42. });
  43. dojo.declare("tests._base.declare.tmp4", tests._base.declare.tmp3);
  44. var tmp = new tests._base.declare.tmp4();
  45. t.is("thonk", tmp.foo);
  46. },
  47. function subclassWithCtor(t){
  48. dojo.declare("tests._base.declare.tmp5", null, {
  49. constructor: function(){
  50. this.foo = "blah";
  51. },
  52. foo: "thonk"
  53. });
  54. dojo.declare("tests._base.declare.tmp6", tests._base.declare.tmp5);
  55. var tmp = new tests._base.declare.tmp6();
  56. t.is("blah", tmp.foo);
  57. },
  58. function mixinSubclass(t){
  59. dojo.declare("tests._base.declare.tmp7", null, {
  60. foo: "thonk"
  61. });
  62. dojo.declare("tests._base.declare.tmp8", null, {
  63. constructor: function(){
  64. this.foo = "blah";
  65. }
  66. });
  67. var tmp = new tests._base.declare.tmp8();
  68. t.is("blah", tmp.foo);
  69. dojo.declare("tests._base.declare.tmp9",
  70. [
  71. tests._base.declare.tmp7, // prototypal
  72. tests._base.declare.tmp8 // mixin
  73. ]);
  74. var tmp2 = new tests._base.declare.tmp9();
  75. t.is("blah", tmp2.foo);
  76. },
  77. function superclassRef(t){
  78. dojo.declare("tests._base.declare.tmp10", null, {
  79. foo: "thonk"
  80. });
  81. dojo.declare("tests._base.declare.tmp11", tests._base.declare.tmp10, {
  82. constructor: function(){
  83. this.foo = "blah";
  84. }
  85. });
  86. var tmp = new tests._base.declare.tmp11();
  87. t.is("blah", tmp.foo);
  88. t.is("thonk", tests._base.declare.tmp11.superclass.foo);
  89. },
  90. function inheritedCall(t){
  91. var foo = "xyzzy";
  92. dojo.declare("tests._base.declare.tmp12", null, {
  93. foo: "thonk",
  94. bar: function(arg1, arg2){
  95. if(arg1){
  96. this.foo = arg1;
  97. }
  98. if(arg2){
  99. foo = arg2;
  100. }
  101. }
  102. });
  103. dojo.declare("tests._base.declare.tmp13", tests._base.declare.tmp12, {
  104. constructor: function(){
  105. this.foo = "blah";
  106. }
  107. });
  108. var tmp = new tests._base.declare.tmp13();
  109. t.is("blah", tmp.foo);
  110. t.is("xyzzy", foo);
  111. tmp.bar("zot");
  112. t.is("zot", tmp.foo);
  113. t.is("xyzzy", foo);
  114. tmp.bar("trousers", "squiggle");
  115. t.is("trousers", tmp.foo);
  116. t.is("squiggle", foo);
  117. },
  118. function inheritedExplicitCall(t){
  119. var foo = "xyzzy";
  120. dojo.declare("tests._base.declare.tmp14", null, {
  121. foo: "thonk",
  122. bar: function(arg1, arg2){
  123. if(arg1){
  124. this.foo = arg1;
  125. }
  126. if(arg2){
  127. foo = arg2;
  128. }
  129. }
  130. });
  131. dojo.declare("tests._base.declare.tmp15", tests._base.declare.tmp14, {
  132. constructor: function(){
  133. this.foo = "blah";
  134. },
  135. bar: function(arg1, arg2){
  136. this.inherited("bar", arguments, [arg2, arg1]);
  137. },
  138. baz: function(arg1, arg2){
  139. tests._base.declare.tmp15.superclass.bar.apply(this, arguments);
  140. }
  141. });
  142. var tmp = new tests._base.declare.tmp15();
  143. t.is("blah", tmp.foo);
  144. t.is("xyzzy", foo);
  145. tmp.baz("zot");
  146. t.is("zot", tmp.foo);
  147. t.is("xyzzy", foo);
  148. tmp.bar("trousers", "squiggle");
  149. t.is("squiggle", tmp.foo);
  150. t.is("trousers", foo);
  151. },
  152. function inheritedMixinCalls(t){
  153. dojo.declare("tests._base.declare.tmp16", null, {
  154. foo: "",
  155. bar: function(){
  156. this.foo += "tmp16";
  157. }
  158. });
  159. dojo.declare("tests._base.declare.mixin16", null, {
  160. bar: function(){
  161. this.inherited(arguments);
  162. this.foo += ".mixin16";
  163. }
  164. });
  165. dojo.declare("tests._base.declare.mixin17", tests._base.declare.mixin16, {
  166. bar: function(){
  167. this.inherited(arguments);
  168. this.foo += ".mixin17";
  169. }
  170. });
  171. dojo.declare("tests._base.declare.tmp17", [tests._base.declare.tmp16, tests._base.declare.mixin17], {
  172. bar: function(){
  173. this.inherited(arguments);
  174. this.foo += ".tmp17";
  175. }
  176. });
  177. var tmp = new tests._base.declare.tmp17();
  178. tmp.bar();
  179. t.is("tmp16.mixin16.mixin17.tmp17", tmp.foo);
  180. },
  181. function mixinPreamble(t){
  182. var passed = false;
  183. dojo.declare("tests._base.declare.tmp16");
  184. new tests._base.declare.tmp16({ preamble: function(){ passed = true; } });
  185. t.t(passed);
  186. },
  187. function basicMixin(t){
  188. // testing if a plain Class-like object can be inherited
  189. // by dojo.declare
  190. var d = new doh.Deferred;
  191. var Thing = function(args){
  192. dojo.mixin(this, args);
  193. }
  194. Thing.prototype.method = function(){
  195. t.t(true);
  196. d.callback(true);
  197. }
  198. dojo.declare("Thinger", Thing, {
  199. method: function(){
  200. this.inherited(arguments);
  201. }
  202. });
  203. var it = new Thinger();
  204. it.method();
  205. return d;
  206. },
  207. function mutatedMethods(t){
  208. // testing if methods can be mutated (within a reason)
  209. dojo.declare("tests._base.declare.tmp18", null, {
  210. constructor: function(){ this.clear(); },
  211. clear: function(){ this.flag = 0; },
  212. foo: function(){ ++this.flag; },
  213. bar: function(){ ++this.flag; },
  214. baz: function(){ ++this.flag; }
  215. });
  216. dojo.declare("tests._base.declare.tmp19", tests._base.declare.tmp18, {
  217. foo: function(){ ++this.flag; this.inherited(arguments); },
  218. bar: function(){ ++this.flag; this.inherited(arguments); },
  219. baz: function(){ ++this.flag; this.inherited(arguments); }
  220. });
  221. var x = new tests._base.declare.tmp19();
  222. // smoke tests
  223. t.is(0, x.flag);
  224. x.foo();
  225. t.is(2, x.flag);
  226. x.clear();
  227. t.is(0, x.flag);
  228. var a = 0;
  229. // dojo.connect() on a prototype method
  230. dojo.connect(tests._base.declare.tmp19.prototype, "foo", function(){ a = 1; });
  231. x.foo();
  232. t.is(2, x.flag);
  233. t.is(1, a);
  234. x.clear();
  235. a = 0;
  236. // extra chaining
  237. var old = tests._base.declare.tmp19.prototype.bar;
  238. tests._base.declare.tmp19.prototype.bar = function(){
  239. a = 1;
  240. ++this.flag;
  241. old.call(this);
  242. }
  243. x.bar();
  244. t.is(3, x.flag);
  245. t.is(1, a);
  246. x.clear();
  247. a = 0;
  248. // replacement
  249. tests._base.declare.tmp19.prototype.baz = function(){
  250. a = 1;
  251. ++this.flag;
  252. this.inherited("baz", arguments);
  253. }
  254. x.baz();
  255. t.is(2, x.flag);
  256. t.is(1, a);
  257. },
  258. function modifiedInstance(t){
  259. var stack;
  260. dojo.declare("tests._base.declare.tmp20", null, {
  261. foo: function(){ stack.push(20); }
  262. });
  263. dojo.declare("tests._base.declare.tmp21", null, {
  264. foo: function(){
  265. this.inherited(arguments);
  266. stack.push(21);
  267. }
  268. });
  269. dojo.declare("tests._base.declare.tmp22", tests._base.declare.tmp20, {
  270. foo: function(){
  271. this.inherited(arguments);
  272. stack.push(22);
  273. }
  274. });
  275. dojo.declare("tests._base.declare.tmp23",
  276. [tests._base.declare.tmp20, tests._base.declare.tmp21], {
  277. foo: function(){
  278. this.inherited(arguments);
  279. stack.push(22);
  280. }
  281. });
  282. var a = new tests._base.declare.tmp22();
  283. var b = new tests._base.declare.tmp23();
  284. var c = {
  285. foo: function(){
  286. this.inherited("foo", arguments);
  287. stack.push("INSIDE C");
  288. }
  289. };
  290. stack = [];
  291. a.foo();
  292. t.is([20, 22], stack);
  293. stack = [];
  294. b.foo();
  295. t.is([20, 21, 22], stack);
  296. dojo.mixin(a, c);
  297. dojo.mixin(b, c);
  298. stack = [];
  299. a.foo();
  300. t.is([20, 22, "INSIDE C"], stack);
  301. stack = [];
  302. b.foo();
  303. t.is([20, 21, 22, "INSIDE C"], stack);
  304. },
  305. function duplicatedBase(t){
  306. var stack;
  307. var A = dojo.declare(null, {
  308. constructor: function(){
  309. stack.push(1);
  310. }
  311. });
  312. var B = dojo.declare([A, A, A], {
  313. constructor: function(){
  314. stack.push(2);
  315. }
  316. });
  317. stack = [];
  318. new A;
  319. t.is([1], stack);
  320. stack = [];
  321. new B;
  322. t.is([1, 2], stack);
  323. },
  324. function indirectlyDuplicatedBase(t){
  325. var stack;
  326. var A = dojo.declare(null, {
  327. constructor: function(){
  328. stack.push(1);
  329. }
  330. });
  331. var B = dojo.declare(A, {
  332. constructor: function(){
  333. stack.push(2);
  334. }
  335. });
  336. var C = dojo.declare([A, B], {
  337. constructor: function(){
  338. stack.push(3);
  339. }
  340. });
  341. var D = dojo.declare([B, A], {
  342. constructor: function(){
  343. stack.push(4);
  344. }
  345. });
  346. stack = [];
  347. new C;
  348. t.is([1, 2, 3], stack);
  349. stack = [];
  350. new D;
  351. t.is([1, 2, 4], stack);
  352. },
  353. function wrongMultipleInheritance(t){
  354. var stack;
  355. var A = dojo.declare([], {
  356. constructor: function(){
  357. stack.push(1);
  358. }
  359. });
  360. var B = dojo.declare([A], {
  361. constructor: function(){
  362. stack.push(2);
  363. }
  364. });
  365. stack = [];
  366. new A;
  367. t.is([1], stack);
  368. stack = [];
  369. new B;
  370. t.is([1, 2], stack);
  371. },
  372. function impossibleBases(t){
  373. var A = dojo.declare(null);
  374. var B = dojo.declare(null);
  375. var C = dojo.declare([A, B]);
  376. var D = dojo.declare([B, A]);
  377. var flag = false;
  378. try{
  379. var E = dojo.declare([C, D]);
  380. }catch(e){
  381. flag = true;
  382. }
  383. t.t(flag);
  384. }
  385. // FIXME: there are still some permutations to test like:
  386. // - ctor arguments
  387. // - multi-level inheritance + L/R conflict checks
  388. ]
  389. );