/src/base/tests/unit/assets/base-core-tests.js

https://github.com/ArnaudD/yui3 · JavaScript · 1233 lines · 966 code · 255 blank · 12 comment · 19 complexity · a15c56e51cfa95a7717bd4b4b2272cd0 MD5 · raw file

  1. YUI.add('base-core-tests', function(Y) {
  2. function areObjectsReallyEqual(o1, o2) {
  3. Y.ObjectAssert.areEqual(o1, o2);
  4. Y.ObjectAssert.areEqual(o2, o1);
  5. }
  6. function Test(cfg, lazy, silentInit) {
  7. this._lazyAddAttrs = lazy;
  8. this._silentInit = silentInit;
  9. Test.superclass.constructor.apply(this, arguments);
  10. }
  11. Test.NAME = 'test';
  12. Test.ATTRS = {
  13. attr1: {
  14. value: "Foo",
  15. setter: function(n) {
  16. return n;
  17. }
  18. },
  19. attr2: {
  20. value: "Bar",
  21. setter: function(n) {
  22. return n;
  23. }
  24. },
  25. attr3: {
  26. value: true
  27. },
  28. attr4: {
  29. value: 3
  30. },
  31. attr5: {
  32. value: 3
  33. },
  34. attr6: {
  35. value: false,
  36. setter: function(lock) {
  37. return lock;
  38. }
  39. },
  40. attr7: {
  41. value: false
  42. },
  43. attr8: {
  44. value: true
  45. },
  46. attr9: {
  47. value: true
  48. },
  49. attr10: {
  50. value: false
  51. },
  52. attr11: {
  53. value: true
  54. },
  55. attr12: {
  56. value: false
  57. },
  58. attr13: {
  59. value: false
  60. },
  61. attr14: {
  62. value: false,
  63. setter: function(val) {
  64. return val;
  65. }
  66. },
  67. attr15: {
  68. value: null,
  69. setter: function(val) {
  70. return false;
  71. }
  72. },
  73. attr16: {
  74. value: ['default'],
  75. getter: function() {
  76. return false;
  77. },
  78. setter: function(g) {
  79. return g;
  80. }
  81. },
  82. attr17: {
  83. value: null,
  84. setter: function(g) {
  85. return g;
  86. }
  87. },
  88. attr18: {
  89. writeOnce: true,
  90. value: null
  91. },
  92. attr19: {
  93. writeOnce: true,
  94. value: null
  95. },
  96. attr20: {
  97. writeOnce: true,
  98. value: null
  99. }
  100. };
  101. Y.extend(Test, Y.BaseCore, {
  102. foo: function() {}
  103. });
  104. function AttrHost(config) {
  105. AttrHost.superclass.constructor.apply(this, arguments);
  106. }
  107. AttrHost.NAME = "attrHost";
  108. AttrHost.ATTRS = {
  109. A: {
  110. value:"AVal",
  111. validator: Y.Lang.isString,
  112. broadcast:1
  113. },
  114. B: {
  115. validator: function(value) {
  116. return (value === undefined || Y.Lang.isString(value) || Y.Lang.isNumber(value));
  117. },
  118. broadcast:2
  119. },
  120. C: {
  121. writeOnce: true
  122. },
  123. D: {
  124. value:"DVal",
  125. readOnly: true
  126. },
  127. E: {
  128. value:"EVal",
  129. writeOnce: true
  130. },
  131. DE: {
  132. valueFn: function() {
  133. return this.get("D") + this.get("E");
  134. }
  135. },
  136. complex: {
  137. value: {
  138. X : {
  139. A: 1
  140. },
  141. Y : {
  142. A: 2
  143. },
  144. Z : {
  145. A: 3
  146. }
  147. }
  148. },
  149. initOnly : {
  150. writeOnce:"initOnly"
  151. }
  152. };
  153. Y.extend(AttrHost, Y.BaseCore);
  154. // -----
  155. function ExtendedAttrHost(config) {
  156. AttrHost.superclass.constructor.apply(this, arguments);
  157. }
  158. ExtendedAttrHost.NAME = "extendedAttrHost";
  159. ExtendedAttrHost.ATTRS = {
  160. A: {
  161. value:"ExtAVal"
  162. },
  163. B: {
  164. value:"ExtBVal",
  165. validator: function(value) {
  166. return ((value == undefined) || Y.Lang.isString(value));
  167. }
  168. },
  169. D: {
  170. value:"ExtDVal",
  171. setter: function(val) {
  172. return (Y.Lang.isString(val)) ? val.toUpperCase() : val;
  173. }
  174. },
  175. E: {
  176. value:"ExtEVal",
  177. getter: function(val) {
  178. return (Y.Lang.isString(val)) ? val.toLowerCase() : val;
  179. }
  180. },
  181. F: {
  182. value:"ExtFVal",
  183. setter: function(val) {
  184. return (Y.Lang.isString(val)) ? val : Y.AttributeCore.INVALID_VALUE;
  185. }
  186. },
  187. "complex.X.A" : {
  188. value: 1111
  189. },
  190. "complex.Y.A" : {
  191. value: 2222,
  192. setter: function(val) { // Should be ignored. Can't set setters for complex sub vals
  193. return val + 10000;
  194. }
  195. },
  196. G : {
  197. valueFn:function(val) {
  198. // Referring to H before it's set up
  199. return this.get("H") + 10;
  200. }
  201. },
  202. H: {
  203. value:5,
  204. getter: function(val) {
  205. return val*5;
  206. }
  207. },
  208. I : {
  209. value:{
  210. a: 5
  211. },
  212. getter: "_getI",
  213. setter: "_setI",
  214. validator: "_validateI"
  215. },
  216. PassThrough : {
  217. value: "passthrough",
  218. getter: function(val) {
  219. return this._passthrough;
  220. },
  221. setter: function(val) {
  222. this._passthrough = val;
  223. }
  224. },
  225. Z : {
  226. value: "z",
  227. getter: function(val) {
  228. return val.toUpperCase();
  229. }
  230. }
  231. };
  232. Y.extend(ExtendedAttrHost, AttrHost, {
  233. _validateI : function(val, name) {
  234. if (name.indexOf(".") == -1) {
  235. Y.Assert.areEqual("I", name);
  236. } else {
  237. Y.Assert.areEqual("I.a", name);
  238. }
  239. return true;
  240. },
  241. _getI : function(val, name) {
  242. if (name.indexOf(".") == -1) {
  243. Y.Assert.areEqual("I", name);
  244. } else {
  245. Y.Assert.areEqual("I.a", name);
  246. }
  247. return val;
  248. },
  249. _setI : function(val, name) {
  250. if (name.indexOf(".") == -1) {
  251. Y.Assert.areEqual("I", name);
  252. } else {
  253. Y.Assert.areEqual("I.a", name);
  254. }
  255. }
  256. });
  257. function CoreTestsHost(config) {
  258. CoreTestsHost.superclass.constructor.apply(this, arguments);
  259. }
  260. CoreTestsHost.NAME = "coreTestsHost";
  261. CoreTestsHost.ATTRS = {
  262. cloneDefaultObject : {
  263. value : {
  264. a:1,
  265. b:2,
  266. c:3
  267. }
  268. },
  269. cloneDefaultArray : {
  270. value : ["foo", "bar", "foobar"]
  271. },
  272. cloneDefaultString : {
  273. value : "foo"
  274. },
  275. cloneDefaultOverride : {
  276. value : {
  277. a:1, b:2, c:3
  278. },
  279. cloneDefaultValue : false
  280. },
  281. cloneDefaultShallow : {
  282. value : {
  283. a: {foo: "bar"}
  284. },
  285. cloneDefaultValue : "shallow"
  286. },
  287. cloneDefaultDeep : {
  288. value : {
  289. a: {foo: "bar"}
  290. },
  291. cloneDefaultValue : "deep"
  292. },
  293. cloneDefaultComplex : {
  294. value : new Y.BaseCore()
  295. }
  296. };
  297. Y.extend(CoreTestsHost, Y.BaseCore);
  298. var coreTemplate = {
  299. name: "Core Tests",
  300. testInit : function() {
  301. var h = new CoreTestsHost();
  302. Y.Assert.isTrue(h.get("initialized"));
  303. },
  304. testDestroy : function() {
  305. var h = new CoreTestsHost();
  306. Y.Assert.isFalse(h.get("destroyed"));
  307. h.destroy();
  308. Y.Assert.isTrue(h.get("destroyed"));
  309. },
  310. testToString : function() {
  311. var h = new CoreTestsHost(),
  312. re = /^coreTestsHost\[.*?\]$/,
  313. str = h.toString();
  314. Y.Assert.isTrue(re.test(str));
  315. },
  316. testCloneDefaultValueObject : function() {
  317. var h = new CoreTestsHost(),
  318. val = h.get("cloneDefaultObject");
  319. Y.Assert.isTrue(CoreTestsHost.ATTRS.cloneDefaultObject.value !== val);
  320. Y.ObjectAssert.areEqual({
  321. a:1,
  322. b:2,
  323. c:3
  324. }, val);
  325. },
  326. testCloneDefaultValueArray : function() {
  327. var h = new CoreTestsHost(),
  328. val = h.get("cloneDefaultArray");
  329. Y.Assert.isTrue(CoreTestsHost.ATTRS.cloneDefaultArray.value !== val);
  330. Y.ArrayAssert.itemsAreEqual(["foo", "bar", "foobar"], val);
  331. },
  332. testCloneDefaultValueString : function() {
  333. var h = new CoreTestsHost(),
  334. val = h.get("cloneDefaultString");
  335. Y.Assert.isTrue(CoreTestsHost.ATTRS.cloneDefaultString.value === val);
  336. },
  337. testCloneDefaultComplex : function() {
  338. var h = new CoreTestsHost(),
  339. val = h.get("cloneDefaultComplex");
  340. // Don't try to clone by default. We may hurt our backs
  341. Y.Assert.isTrue(CoreTestsHost.ATTRS.cloneDefaultComplex.value === val);
  342. },
  343. testCloneDefaultShallow : function() {
  344. var h = new CoreTestsHost(),
  345. val = h.get("cloneDefaultShallow");
  346. Y.Assert.isTrue(CoreTestsHost.ATTRS.cloneDefaultShallow.value !== val);
  347. Y.Assert.isTrue(CoreTestsHost.ATTRS.cloneDefaultShallow.value.a === val.a);
  348. Y.ObjectAssert.areEqual({
  349. foo:"bar"
  350. }, val.a);
  351. },
  352. testCloneDefaultDeep : function() {
  353. var h = new CoreTestsHost(),
  354. val = h.get("cloneDefaultDeep");
  355. Y.Assert.isTrue(CoreTestsHost.ATTRS.cloneDefaultDeep.value !== val);
  356. Y.Assert.isTrue(CoreTestsHost.ATTRS.cloneDefaultDeep.value.a !== val.a);
  357. Y.ObjectAssert.areEqual({
  358. foo:"bar"
  359. }, val.a);
  360. },
  361. testCloneDefaultOverride : function() {
  362. var h = new CoreTestsHost(),
  363. val = h.get("cloneDefaultOverride");
  364. Y.Assert.isTrue(CoreTestsHost.ATTRS.cloneDefaultOverride.value === val);
  365. Y.ObjectAssert.areEqual({
  366. a:1,
  367. b:2,
  368. c:3
  369. }, val);
  370. },
  371. testValueValueFnOverride : function() {
  372. var One = Y.extend(function() {
  373. One.superclass.constructor.apply(this, arguments);
  374. }, Y.Base, null, {
  375. ATTRS : {
  376. testMe : {
  377. value : "1a",
  378. valueFn: function() {
  379. return 1;
  380. }
  381. }
  382. }
  383. });
  384. var Two = Y.extend(function() {
  385. Two.superclass.constructor.apply(this, arguments);
  386. }, One, null, {
  387. ATTRS : {
  388. testMe : {
  389. value : 2
  390. }
  391. }
  392. });
  393. var Three = Y.extend(function() {
  394. Three.superclass.constructor.apply(this, arguments);
  395. }, Two, null, {
  396. ATTRS : {
  397. testMe : {
  398. value : "3a",
  399. valueFn: function() {
  400. return 3;
  401. }
  402. }
  403. }
  404. });
  405. var ThreeNoVal = Y.extend(function() {
  406. ThreeNoVal.superclass.constructor.apply(this, arguments);
  407. }, Three);
  408. var Four = Y.extend(function() {
  409. Four.superclass.constructor.apply(this, arguments);
  410. }, Three, null, {
  411. ATTRS : {
  412. testMe : {
  413. value : 4
  414. }
  415. }
  416. });
  417. var FourNoVal = Y.extend(function() {
  418. FourNoVal.superclass.constructor.apply(this, arguments);
  419. }, Four);
  420. var one = new One();
  421. var two = new Two();
  422. var three = new Three();
  423. var threeNoVal = new ThreeNoVal();
  424. var four = new Four();
  425. var fourNoVal = new FourNoVal();
  426. Y.Assert.areEqual(1, one.get("testMe"));
  427. Y.Assert.areEqual(2, two.get("testMe"));
  428. Y.Assert.areEqual(3, three.get("testMe"));
  429. Y.Assert.areEqual(3, threeNoVal.get("testMe"));
  430. Y.Assert.areEqual(4, four.get("testMe"));
  431. Y.Assert.areEqual(4, fourNoVal.get("testMe"));
  432. one.destroy();
  433. two.destroy();
  434. three.destroy();
  435. threeNoVal.destroy();
  436. four.destroy();
  437. fourNoVal.destroy();
  438. },
  439. testInitializerDestructorInvocation : function() {
  440. var expected = ["beforeConstructorTwo", "beforeConstructorOne", "initializerOne", "initializerTwo", "afterConstructorOne", "afterConstructorTwo", "destructorTwo", "destructorOne"],
  441. actual = [],
  442. initCfg = {
  443. foo: 1
  444. };
  445. function One(cfg) {
  446. actual.push("beforeConstructorOne");
  447. One.superclass.constructor.apply(this, arguments);
  448. actual.push("afterConstructorOne");
  449. }
  450. Y.extend(One, Y.BaseCore, {
  451. initializer : function(cfg) {
  452. Y.Assert.areSame(initCfg, cfg);
  453. actual.push("initializerOne");
  454. },
  455. destructor : function() {
  456. actual.push("destructorOne");
  457. }
  458. }, {
  459. NAME : "one",
  460. ATTRS : {
  461. "a" : {
  462. value: 1
  463. }
  464. }
  465. });
  466. function Two(cfg) {
  467. actual.push("beforeConstructorTwo");
  468. Two.superclass.constructor.apply(this, arguments);
  469. actual.push("afterConstructorTwo");
  470. }
  471. Y.extend(Two, One, {
  472. initializer : function(cfg) {
  473. Y.Assert.areSame(initCfg, cfg);
  474. actual.push("initializerTwo");
  475. },
  476. destructor : function() {
  477. actual.push("destructorTwo");
  478. }
  479. }, {
  480. NAME : "two",
  481. ATTRS : {
  482. "b" : {
  483. value: 2
  484. }
  485. }
  486. });
  487. var o = new Two(initCfg);
  488. o.destroy();
  489. Y.ArrayAssert.itemsAreEqual(expected, actual);
  490. }
  491. };
  492. var basicTemplate = {
  493. name: "Base Class Tests",
  494. createHost : function(cfg) {
  495. return new AttrHost(cfg);
  496. },
  497. setUp : function() {},
  498. tearDown : function() {},
  499. testDefault : function() {
  500. var h = this.createHost();
  501. Y.Assert.areEqual("AVal", h.get("A"));
  502. Y.Assert.areEqual(undefined, h.get("B"));
  503. Y.Assert.areEqual(undefined, h.get("C"));
  504. Y.Assert.areEqual("DVal", h.get("D")); // Readonly
  505. Y.Assert.areEqual("EVal", h.get("E")); // Write once, but not twice
  506. Y.Assert.areEqual("DValEVal", h.get("DE"));
  507. },
  508. testConstructor : function() {
  509. var h = this.createHost({A:"MyAVal", B:"MyBVal", C:"MyCVal", D:"MyDVal", E:"MyEVal", DE:"MyDEVal"});
  510. h.set("C", "MyNewCVal");
  511. h.set("D", "MyNewDVal");
  512. h.set("E", "MyNewEVal");
  513. Y.Assert.areEqual("MyAVal", h.get("A"));
  514. Y.Assert.areEqual("MyBVal", h.get("B"));
  515. Y.Assert.areEqual("MyCVal", h.get("C")); // Write Once, set in constructor
  516. Y.Assert.areEqual("DVal", h.get("D")); // Read Only
  517. Y.Assert.areEqual("MyEVal", h.get("E")); // Write Once, set in constructor
  518. Y.Assert.areEqual("MyDEVal", h.get("DE"));
  519. },
  520. testSet : function() {
  521. var h = this.createHost();
  522. h.set("A", "MyNewAVal");
  523. h.set("B", "MyNewBVal");
  524. h.set("C", "MyNewCVal");
  525. h.set("D", "MyNewDVal");
  526. h.set("E", "MyNewEVal");
  527. h.set("DE", "MyNewDEVal");
  528. Y.Assert.areEqual("MyNewAVal", h.get("A"));
  529. Y.Assert.areEqual("MyNewBVal", h.get("B"));
  530. Y.Assert.areEqual("MyNewCVal", h.get("C")); // Write once, set on first set.
  531. Y.Assert.areEqual("DVal", h.get("D")); // Read Only
  532. Y.Assert.areEqual("EVal", h.get("E")); // Write Once
  533. Y.Assert.areEqual("MyNewDEVal", h.get("DE"));
  534. },
  535. testWriteOncePostInit : function() {
  536. var h = this.createHost();
  537. h.set("E", "MyNewEVal");
  538. h.set("C", "MyNewCVal");
  539. Y.Assert.areEqual("MyNewCVal", h.get("C")); // Write Once, default value
  540. Y.Assert.areEqual("EVal", h.get("E")); // Write Once, default value
  541. },
  542. testWriteOnce : function() {
  543. var h = this.createHost({E:"MyEVal"});
  544. h.set("E", "MyNewEVal");
  545. Y.Assert.areEqual("MyEVal", h.get("E")); // Write Once, on init
  546. },
  547. testWriteOnceInitOnly : function() {
  548. var h = this.createHost({ initOnly: "initOnlyVal"});
  549. h.set("initOnly", "NewInitOnlyVal");
  550. Y.Assert.areEqual("initOnlyVal", h.get("initOnly"));
  551. },
  552. testWriteOnceInitOnlyNotProvided : function() {
  553. var h1 = this.createHost();
  554. h1.set("initOnly", "InitOnlyVal");
  555. Y.Assert.areEqual(undefined, h1.get("initOnly"));
  556. },
  557. testAdHocGetSet : function() {
  558. var h = this.createHost();
  559. Y.Assert.areEqual(undefined, h.get("AdHoc"));
  560. h.set("AdHoc", "TestAdHoc");
  561. Y.Assert.areEqual("TestAdHoc", h.get("AdHoc"));
  562. h.addAttr("AdHoc", {
  563. setter: function(val) {
  564. return val.toUpperCase();
  565. }
  566. });
  567. h.set("AdHoc", "TestAdHocConfigured");
  568. Y.Assert.areEqual("TESTADHOCCONFIGURED", h.get("AdHoc"));
  569. },
  570. testAdHocConstructorEnabled : function() {
  571. AttrHost.prototype._allowAdHocAttrs = true;
  572. var h = this.createHost({
  573. A: "MyAVal",
  574. foo: "foo",
  575. bar: "bar",
  576. plugins : ["not"]
  577. });
  578. AttrHost.prototype._allowAdHocAttrs = false;
  579. // Only add AdHoc Attrs
  580. Y.Assert.areEqual("foo", h.get("foo"));
  581. Y.Assert.areEqual("bar", h.get("bar"));
  582. // Configured Attrs
  583. Y.Assert.areEqual("DVal", h.get("D"));
  584. Y.Assert.areEqual("MyAVal", h.get("A"));
  585. // Not _NON_ATTRS_CFG
  586. Y.Assert.isUndefined(h.get("plugins"));
  587. },
  588. testAdHocConstructorDisabled : function() {
  589. var h = this.createHost({
  590. A: "MyAVal",
  591. foo: "foo",
  592. bar: "bar",
  593. plugins : ["not"]
  594. });
  595. // Only add AdHoc Attrs
  596. Y.Assert.areEqual(undefined, h.get("foo"));
  597. Y.Assert.areEqual(undefined, h.get("bar"));
  598. // Configured attributes
  599. Y.Assert.areEqual("DVal", h.get("D"));
  600. Y.Assert.areEqual("MyAVal", h.get("A"));
  601. // Not _NON_ATTRS_CFG
  602. Y.Assert.isUndefined(h.get("plugins"));
  603. },
  604. testMassSetGet : function() {
  605. var h = this.createHost();
  606. h.setAttrs({
  607. "A" : "MyNewAVal",
  608. "B": "MyNewBVal",
  609. "C": "MyNewCVal",
  610. "D": "MyNewDVal",
  611. "E": "MyNewEVal",
  612. "DE": "MyNewDEVal",
  613. complex: "MyNewComplexVal"
  614. });
  615. var expectedVals = {
  616. A: "MyNewAVal",
  617. B: "MyNewBVal",
  618. C: "MyNewCVal",
  619. D: "DVal",
  620. E: "EVal",
  621. DE: "MyNewDEVal",
  622. complex: "MyNewComplexVal",
  623. initialized: true,
  624. destroyed: false,
  625. initOnly: undefined
  626. };
  627. Y.Assert.areEqual(expectedVals.A, h.get("A"));
  628. Y.Assert.areEqual(expectedVals.B, h.get("B"));
  629. Y.Assert.areEqual(expectedVals.C, h.get("C")); // Write once, set on first set.
  630. Y.Assert.areEqual(expectedVals.D, h.get("D")); // Read Only
  631. Y.Assert.areEqual(expectedVals.E, h.get("E")); // Write Once
  632. Y.Assert.areEqual(expectedVals.DE, h.get("DE"));
  633. areObjectsReallyEqual(expectedVals, h.getAttrs());
  634. },
  635. testModifiedAttrs : function() {
  636. var h = this.createHost();
  637. h.setAttrs({
  638. A: "MyNewAVal",
  639. C: "MyNewCVal",
  640. D: "MyNewDVal"
  641. });
  642. var expectedVals = {
  643. A: "MyNewAVal",
  644. initialized:true
  645. };
  646. areObjectsReallyEqual(expectedVals, h.getAttrs(true));
  647. },
  648. testValidation : function() {
  649. var h = this.createHost();
  650. h.set("A", "MyAVal");
  651. Y.Assert.areEqual("MyAVal", h.get("A"));
  652. h.set("A", 100);
  653. Y.Assert.areEqual("MyAVal", h.get("A")); // Validation should prevent the attribute from being set
  654. h.set("B", "two");
  655. Y.Assert.areEqual("two", h.get("B"));
  656. h.set("B", 2);
  657. Y.Assert.areEqual(2, h.get("B"));
  658. h.set("B", false);
  659. Y.Assert.areEqual(2, h.get("B")); // Validation should prevent the attribute from being set
  660. },
  661. testPrivateSet : function() {
  662. var h = this.createHost();
  663. h.set("D", "MyNewDVal");
  664. h.set("E", "MyNewEVal");
  665. Y.Assert.areEqual("DVal", h.get("D"));
  666. Y.Assert.areEqual("EVal", h.get("E"));
  667. h._set("D", "TryDAgain");
  668. h._set("E", "TryEAgain");
  669. Y.Assert.areEqual("TryDAgain", h.get("D"));
  670. Y.Assert.areEqual("TryEAgain", h.get("E"));
  671. },
  672. testComplexDefault : function() {
  673. var h = this.createHost();
  674. var o = {
  675. X : {
  676. A: 1
  677. },
  678. Y : {
  679. A: 2
  680. },
  681. Z : {
  682. A: 3
  683. }
  684. };
  685. Y.Assert.areEqual(1, h.get("complex.X.A"));
  686. Y.Assert.areEqual(2, h.get("complex.Y.A"));
  687. Y.Assert.areEqual(3, h.get("complex.Z.A"));
  688. areObjectsReallyEqual({A:1}, h.get("complex.X"));
  689. areObjectsReallyEqual({A:2}, h.get("complex.Y"));
  690. areObjectsReallyEqual({A:3}, h.get("complex.Z"));
  691. var val = h.get("complex");
  692. Y.each(val, function(v, k) {
  693. areObjectsReallyEqual(v, o[k]);
  694. });
  695. },
  696. testComplexSet : function() {
  697. var h = this.createHost();
  698. h.set("complex.X.A", 111);
  699. Y.Assert.areEqual(111, h.get("complex.X.A"));
  700. h.set("complex.X.B", 112);
  701. Y.Assert.areEqual(112, h.get("complex.X.B"));
  702. areObjectsReallyEqual({A:111, B:112}, h.get("complex.X"));
  703. h.set("complex.W.B", 113);
  704. Y.Assert.areEqual(undefined, h.get("complex.W"));
  705. Y.Assert.areEqual(undefined, h.get("complex.W.B"));
  706. h.set("complex.Y", {B:222});
  707. Y.Assert.areEqual(222, h.get("complex.Y.B"));
  708. Y.Assert.areEqual(undefined, h.get("complex.Y.A"));
  709. },
  710. testInitialValidation: function() {
  711. var h = this.createHost({A:5});
  712. Y.Assert.areEqual("AVal", h.get("A")); // Numerical value validation failure should revert to default value
  713. },
  714. testProtect : function() {
  715. var h = this.createHost();
  716. var q = Y.Attribute.protectAttrs(AttrHost.ATTRS);
  717. Y.Assert.areNotSame(AttrHost.ATTRS, q);
  718. Y.Assert.areEqual(Y.dump(AttrHost.ATTRS), Y.dump(q));
  719. q.A.newprop = "new prop value";
  720. q.A.value = "modified value";
  721. Y.Assert.areNotEqual(Y.dump(AttrHost.ATTRS), Y.dump(q));
  722. }
  723. };
  724. var extendedTemplate = {
  725. name: "Extended Class Tests",
  726. createHost : function(cfg) {
  727. return new ExtendedAttrHost(cfg);
  728. },
  729. setUp : function() {},
  730. tearDown : function() {},
  731. testDefault : function() {
  732. var h = this.createHost();
  733. Y.Assert.areEqual("ExtAVal", h.get("A"));
  734. Y.Assert.areEqual("ExtBVal", h.get("B"));
  735. Y.Assert.areEqual(undefined, h.get("C"));
  736. Y.Assert.areEqual("EXTDVAL", h.get("D"));
  737. Y.Assert.areEqual("exteval", h.get("E"));
  738. Y.Assert.areEqual("ExtFVal", h.get("F"));
  739. Y.Assert.areEqual("EXTDVALexteval", h.get("DE"));
  740. },
  741. testConstructor : function() {
  742. var h = this.createHost({A:"MyAVal", B:"MyBVal", C:"MyCVal", D:"MyDVal", E:"MyEVal", F:"MyFVal"});
  743. h.set("C", "MyNewCVal");
  744. h.set("D", "MyNewDVal");
  745. h.set("E", "MyNewEVal");
  746. Y.Assert.areEqual("MyAVal", h.get("A"));
  747. Y.Assert.areEqual("MyBVal", h.get("B"));
  748. Y.Assert.areEqual("MyCVal", h.get("C"));
  749. Y.Assert.areEqual("EXTDVAL", h.get("D"));
  750. Y.Assert.areEqual("myeval", h.get("E"));
  751. Y.Assert.areEqual("MyFVal", h.get("F"));
  752. Y.Assert.areEqual("EXTDVALmyeval", h.get("DE"));
  753. },
  754. testSet : function() {
  755. var h = this.createHost();
  756. h.set("A", "MyNewAVal");
  757. h.set("B", "MyNewBVal");
  758. h.set("C", "MyNewCVal");
  759. h.set("D", "MyNewDVal");
  760. h.set("E", "MyNewEVal");
  761. h.set("F", "MyNewFVal");
  762. h.set("DE", "MyNewDEVal");
  763. Y.Assert.areEqual("MyNewAVal", h.get("A"));
  764. Y.Assert.areEqual("MyNewBVal", h.get("B"));
  765. Y.Assert.areEqual("MyNewCVal", h.get("C"));
  766. Y.Assert.areEqual("EXTDVAL", h.get("D"));
  767. Y.Assert.areEqual("exteval", h.get("E"));
  768. Y.Assert.areEqual("MyNewFVal", h.get("F"));
  769. Y.Assert.areEqual("MyNewDEVal", h.get("DE"));
  770. },
  771. testAdHocGetSet : function() {
  772. var h = this.createHost();
  773. Y.Assert.areEqual(undefined, h.get("AdHoc"));
  774. h.set("AdHoc", "TestAdHoc");
  775. Y.Assert.areEqual("TestAdHoc", h.get("AdHoc"));
  776. },
  777. testMassSetGet : function() {
  778. var h = this.createHost();
  779. h.setAttrs({
  780. "A" : "MyNewAVal",
  781. "B": "MyNewBVal",
  782. "C": "MyNewCVal",
  783. "D": "MyNewDVal",
  784. "E": "MyNewEVal",
  785. "F": "MyNewFVal",
  786. "DE": "MyNewDEVal",
  787. complex: "MyNewComplexVal",
  788. "I" : "MyNewIVal",
  789. "PassThrough" : "MyPassThrough",
  790. "Z" : "MyZ"
  791. });
  792. var expectedVals = {
  793. A: "MyNewAVal",
  794. B: "MyNewBVal",
  795. C: "MyNewCVal",
  796. D: "EXTDVAL",
  797. E: "exteval",
  798. F: "MyNewFVal",
  799. DE: "MyNewDEVal",
  800. complex: "MyNewComplexVal",
  801. G: 35,
  802. H: 25,
  803. I: "MyNewIVal",
  804. PassThrough: "MyPassThrough",
  805. Z: "MYZ",
  806. initialized:true,
  807. destroyed:false,
  808. initOnly: undefined
  809. };
  810. Y.Assert.areEqual(expectedVals.A, h.get("A"));
  811. Y.Assert.areEqual(expectedVals.B, h.get("B"));
  812. Y.Assert.areEqual(expectedVals.C, h.get("C")); // Write once, set on first set.
  813. Y.Assert.areEqual(expectedVals.D, h.get("D")); // Read Only
  814. Y.Assert.areEqual(expectedVals.E, h.get("E")); // Write Once
  815. Y.Assert.areEqual(expectedVals.F, h.get("F"));
  816. Y.Assert.areEqual(expectedVals.DE, h.get("DE"));
  817. areObjectsReallyEqual(expectedVals, h.getAttrs());
  818. },
  819. testModifiedAttrs : function() {
  820. var h = this.createHost();
  821. h.setAttrs({
  822. A: "MyNewAVal",
  823. C: "MyNewCVal",
  824. D: "MyNewDVal",
  825. F: "MyNewFVal"
  826. });
  827. var expectedVals = {
  828. A: "MyNewAVal",
  829. F: "MyNewFVal",
  830. initialized:true
  831. };
  832. areObjectsReallyEqual(expectedVals, h.getAttrs(true));
  833. },
  834. testValidation : function() {
  835. var h = this.createHost();
  836. h.set("A", "MyAVal");
  837. Y.Assert.areEqual("MyAVal", h.get("A"));
  838. h.set("A", 100);
  839. Y.Assert.areEqual("MyAVal", h.get("A")); // Validation should prevent the attribute from being set
  840. h.set("B", "two");
  841. Y.Assert.areEqual("two", h.get("B"));
  842. h.set("B", 2);
  843. Y.Assert.areEqual("two", h.get("B")); // Validation should prevent the attribute from being set
  844. h.set("B", true);
  845. Y.Assert.areEqual("two", h.get("B")); // Validation should prevent the attribute from being set
  846. h.set("F", "MyNewFVal");
  847. Y.Assert.areEqual("MyNewFVal", h.get("F"));
  848. h.set("F", 3);
  849. Y.Assert.areEqual("MyNewFVal", h.get("F")); // Validation should prevent the attribute from being set
  850. },
  851. testPrivateSet : function() {
  852. var h = this.createHost();
  853. h.set("D", "MyNewDVal");
  854. h.set("E", "MyNewEVal");
  855. Y.Assert.areEqual("EXTDVAL", h.get("D"));
  856. Y.Assert.areEqual("exteval", h.get("E"));
  857. h._set("D", "TryDAgain");
  858. h._set("E", "TryEAgain");
  859. Y.Assert.areEqual("TRYDAGAIN", h.get("D"));
  860. Y.Assert.areEqual("tryeagain", h.get("E"));
  861. },
  862. testComplexDefault : function() {
  863. var h = this.createHost();
  864. var o = {
  865. X : {
  866. A: 1111
  867. },
  868. Y : {
  869. A: 2222
  870. },
  871. Z : {
  872. A: 3
  873. }
  874. };
  875. Y.Assert.areEqual(1111, h.get("complex.X.A"));
  876. Y.Assert.areEqual(2222, h.get("complex.Y.A"));
  877. Y.Assert.areEqual(3, h.get("complex.Z.A"));
  878. areObjectsReallyEqual({A:1111}, h.get("complex.X"));
  879. areObjectsReallyEqual({A:2222}, h.get("complex.Y"));
  880. areObjectsReallyEqual({A:3}, h.get("complex.Z"));
  881. var val = h.get("complex");
  882. Y.each(val, function(v, k) {
  883. areObjectsReallyEqual(v, o[k]);
  884. });
  885. },
  886. testComplexSet : function() {
  887. var h = this.createHost();
  888. h.set("complex.X.A", 111);
  889. Y.Assert.areEqual(111, h.get("complex.X.A"));
  890. h.set("complex.X.B", 112);
  891. Y.Assert.areEqual(112, h.get("complex.X.B"));
  892. areObjectsReallyEqual({A:111, B:112}, h.get("complex.X"));
  893. h.set("complex.W.B", 113);
  894. Y.Assert.areEqual(undefined, h.get("complex.W"));
  895. Y.Assert.areEqual(undefined, h.get("complex.W.B"));
  896. h.set("complex.Y", {B:222});
  897. Y.Assert.areEqual(222, h.get("complex.Y.B"));
  898. Y.Assert.areEqual(undefined, h.get("complex.Y.A"));
  899. },
  900. testOnDemandInit : function() {
  901. var h = this.createHost();
  902. Y.Assert.areEqual(35, h.get("G"));
  903. Y.Assert.areEqual(25, h.get("H"));
  904. },
  905. testGetterSetterValidatorNameArg : function() {
  906. var h = this.createHost();
  907. h.set("I.a", 6);
  908. h.set("I", {a:7, b:8});
  909. }
  910. };
  911. var perfTemplate = {
  912. name: "Performance Tests",
  913. testTimeConstruction: function() {
  914. var start, end, n = 20, t, i;
  915. start = new Date().getTime();
  916. for (i = 0; i < n; i++) {
  917. t = new Test(null, false);
  918. t.getAttrs();
  919. t = null;
  920. }
  921. end = new Date().getTime();
  922. Y.log("Construction Time Populated (upfront): " + ((end-start)/n), "perf");
  923. start = new Date().getTime();
  924. for (i = 0; i < n; i++) {
  925. t = new Test();
  926. t.getAttrs();
  927. t = null;
  928. }
  929. end = new Date().getTime();
  930. Y.log("Construction Time Populated (lazy): " + ((end-start)/n), "perf");
  931. start = new Date().getTime();
  932. for (i = 0; i < n; i++) {
  933. t = new Test(null, false);
  934. t = null;
  935. }
  936. end = new Date().getTime();
  937. Y.log("Construction Time (upfront): " + ((end-start)/n), "perf");
  938. start = new Date().getTime();
  939. for (i = 0; i < n; i++) {
  940. t = new Test();
  941. t = null;
  942. }
  943. end = new Date().getTime();
  944. var time = (end-start)/n;
  945. var expectedTime = (Y.UA.ie && Y.UA.ie <= 6) ? 15 : 10;
  946. Y.log("Construction Time (lazy): " + time, "perf");
  947. start = new Date().getTime();
  948. for (i = 0; i < n; i++) {
  949. t = new Test(null, true, true);
  950. t = null;
  951. }
  952. end = new Date().getTime();
  953. Y.log("Construction Time (lazy and silent init): " + ((end-start)/n), "perf");
  954. Y.Assert.isTrue((time < expectedTime));
  955. },
  956. testStateForPerfSwitches : function() {
  957. // Lazy
  958. t = new Test();
  959. var x = t.getAttrs();
  960. // Non Lazy (Upfront)
  961. t = new Test(null, false);
  962. var y = t.getAttrs();
  963. // Lazy and Silent
  964. t = new Test(null, true, true);
  965. var z = t.getAttrs();
  966. Y.Assert.areEqual(Y.dump(x), Y.dump(y), "Lazy vs. Upfront: attr state is not equal");
  967. Y.Assert.areEqual(Y.dump(y), Y.dump(z), "Upfront vs. Lazy and Silent: attr state is not equal");
  968. }
  969. }
  970. var suite = new Y.Test.Suite("Base Core");
  971. suite.add(new Y.Test.Case(coreTemplate));
  972. suite.add(new Y.Test.Case(basicTemplate));
  973. suite.add(new Y.Test.Case(extendedTemplate));
  974. suite.add(new Y.Test.Case(basicTemplate));
  975. suite.add(new Y.Test.Case(extendedTemplate));
  976. Y.Test.Runner.add(suite);
  977. Y.Test.Runner.setName("Base Core Tests");
  978. });