traits.test.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. const { expect } = require('chai');
  2. const { AdvancedBase } = require("../src/AdvancedBase");
  3. class TestClass extends AdvancedBase {
  4. static IMPLEMENTS = {
  5. test_trait: {
  6. test_method: () => 'A'
  7. },
  8. override_trait: {
  9. preserved_method: () => 'B',
  10. override_method: () => 'C',
  11. },
  12. }
  13. }
  14. class TestSubClass extends TestClass {
  15. static IMPLEMENTS = {
  16. override_trait: {
  17. override_method: () => 'D',
  18. }
  19. }
  20. }
  21. describe('traits', () => {
  22. it('instance.as', () => {
  23. const o = new TestClass();
  24. expect(o.as).to.be.a('function');
  25. const ot = o.as('test_trait');
  26. expect(ot.test_method).to.be.a('function');
  27. expect(ot.test_method()).to.equal('A');
  28. });
  29. it('traits of parent', () => {
  30. const o = new TestSubClass();
  31. console.log(o._get_merged_static_object('IMPLEMENTS'))
  32. expect(o.as).to.be.a('function');
  33. const ot = o.as('test_trait');
  34. expect(ot.test_method).to.be.a('function');
  35. expect(ot.test_method()).to.equal('A');
  36. })
  37. it('trait method overrides', () => {
  38. const o = new TestSubClass();
  39. expect(o.as).to.be.a('function');
  40. const ot = o.as('override_trait');
  41. expect(ot.preserved_method).to.be.a('function');
  42. expect(ot.override_method).to.be.a('function');
  43. expect (ot.preserved_method()).to.equal('B');
  44. expect (ot.override_method()).to.equal('D');
  45. })
  46. });