decorator_tests.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. QUnit.test("decorator", function (assert) {
  2. var A = clazz(Object, {
  3. hello: function () {
  4. return "A";
  5. }
  6. });
  7. var Decorator = clazz(Object, {
  8. hello: function () {
  9. return "B" + this.decorated() + "B";
  10. }
  11. });
  12. var a = new A();
  13. assert.strictEqual(a.hello(), "A");
  14. a.decorateWith(new Decorator());
  15. var value = a.hello();
  16. assert.strictEqual(value, "BAB");
  17. });
  18. QUnit.test("super", function (assert) {
  19. var A = clazz(Object, {
  20. hello: function () {
  21. return "A";
  22. }
  23. });
  24. var B = clazz(A, {
  25. hello: function () {
  26. return "B" + this.super() + "B";
  27. }
  28. });
  29. var C = clazz(B, {
  30. hello: function () {
  31. return "C" + this.super() + "C";
  32. }
  33. });
  34. var D = clazz(B, {
  35. hello: function () {
  36. return "D";
  37. }
  38. });
  39. assert.strictEqual(new C().hello(), "CBABC");
  40. assert.strictEqual(new D().hello(), "D");
  41. });
  42. QUnit.test("super - constructors", function (assert) {
  43. var A = clazz(Object, {
  44. construct: function () {
  45. this.message = "A";
  46. }
  47. });
  48. var B = clazz(A, {
  49. construct: function () {
  50. this.super();
  51. this.message = "B" + this.message + "B";
  52. }
  53. });
  54. var C = clazz(B, {
  55. construct: function () {
  56. this.super();
  57. this.message = "C" + this.message + "C";
  58. }
  59. });
  60. var D = clazz(B, {
  61. construct: function () {
  62. this.message = "D";
  63. }
  64. });
  65. assert.strictEqual(new C().message, "CBABC");
  66. assert.strictEqual(new D().message, "D");
  67. });
  68. QUnit.test("inheritance", function (assert) {
  69. var counter = 0;
  70. var A = clazz(Object, {
  71. hello: function () {
  72. return "A";
  73. }
  74. });
  75. var B = clazz(A, {
  76. });
  77. var a = new A(), b = new B();
  78. assert.ok(a.hello() === "A", "instance of A has hello()");
  79. assert.ok(b.hello() === "A", "instance of B has hello()");
  80. assert.ok(b instanceof B, "b is instance of B");
  81. assert.ok(b instanceof Object, "b is instance of Object");
  82. assert.ok(b instanceof A, "b is instance of A");
  83. });
  84. QUnit.test("initialization", function (assert) {
  85. var counter = 0;
  86. var A = clazz(Object, {
  87. construct: function () {
  88. counter++;
  89. }
  90. });
  91. var B = clazz(A, {
  92. });
  93. assert.strictEqual(counter, 0);
  94. var a = new A();
  95. assert.strictEqual(counter, 1);
  96. var b = new B();
  97. assert.strictEqual(counter, 2);
  98. });