defineclass_test.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2012 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.defineClassTest');
  15. goog.setTestOnly('goog.defineClassTest');
  16. goog.require('goog.testing.jsunit');
  17. function testSuper() {
  18. /** @constructor */
  19. function SomeSuper(){};
  20. var SomeClass = goog.defineClass(SomeSuper, {
  21. /** @constructor */
  22. constructor: function() {}
  23. });
  24. assertTrue(new SomeClass() instanceof SomeClass);
  25. assertTrue(new SomeClass() instanceof SomeSuper);
  26. }
  27. function testPrototypeProp() {
  28. var SomeClass = goog.defineClass(null, {
  29. /** @constructor */
  30. constructor: function() {},
  31. trueMethod: function() { return true; }
  32. });
  33. assertEquals(new SomeClass().falseProp, false);
  34. assertEquals(new SomeClass().trueMethod(), true);
  35. }
  36. function testInstanceProp() {
  37. var SomeClass = goog.defineClass(null, {
  38. /** @constructor */
  39. constructor: function() { this.falseProp = false; }
  40. });
  41. assertEquals(new SomeClass().falseProp, false);
  42. }
  43. function testPrototypeProp() {
  44. var SomeClass = goog.defineClass(null, {
  45. /** @constructor */
  46. constructor: function() {},
  47. trueMethod: function() { return true; }
  48. });
  49. assertEquals(new SomeClass().trueMethod(), true);
  50. assertEquals(SomeClass.prototype.trueMethod(), true);
  51. }
  52. function testStaticProp() {
  53. var SomeClass = goog.defineClass(null, {
  54. /** @constructor */
  55. constructor: function() {},
  56. statics: {someProp: 100}
  57. });
  58. assertEquals(new SomeClass().statics, undefined);
  59. assertEquals(new SomeClass().someProp, undefined);
  60. assertEquals(SomeClass.someProp, 100);
  61. }
  62. function testStaticPropFn() {
  63. var SomeClass = goog.defineClass(null, {
  64. /** @constructor */
  65. constructor: function() {},
  66. statics: function(cls) { cls.someProp = 100; }
  67. });
  68. assertEquals(new SomeClass().statics, undefined);
  69. assertEquals(new SomeClass().someProp, undefined);
  70. assertEquals(SomeClass.someProp, 100);
  71. }
  72. function testUid() {
  73. var SomeClass = goog.defineClass(null, {constructor: function() {}});
  74. var obj1 = new SomeClass();
  75. var obj2 = new SomeClass();
  76. assertEquals(goog.getUid(obj1), goog.getUid(obj1));
  77. assertEquals(goog.getUid(obj2), goog.getUid(obj2));
  78. assertNotEquals(goog.getUid(obj1), goog.getUid(obj2));
  79. }