Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.09 KB | None | 0 0
  1. // 22: class - creation
  2. // To do: make all tests pass, leave the assert lines unchanged!
  3. // Follow the hints of the failure messages!
  4.  
  5. describe('Class creation', () => {
  6. it('is as simple as `class XXX {}`', function() {
  7. let TestClass = class XXX {};
  8. const instance = new TestClass();
  9. assert.equal(typeof instance, 'object');
  10. });
  11. it('a class is block scoped', () => {
  12. // class Inside {}
  13. {class Inside {}}
  14. assert.equal(typeof Inside, 'undefined');
  15. });
  16. it('the `constructor` is a special method', function() {
  17. class User {
  18. constructor(id) {this.id = id}
  19. }
  20. const user = new User(42);
  21. assert.equal(user.id, 42);
  22. });
  23. it('defining a method by writing it inside the class body', function() {
  24. class User {
  25. writesTests() {return false}
  26. }
  27. const notATester = new User();
  28. assert.equal(notATester.writesTests(), false);
  29. });
  30. it('multiple methods need no commas (opposed to object notation)', function() {
  31. class User {
  32. wroteATest() { this.everWroteATest = true; }
  33. isLazy() { return ! this.everWroteATest }
  34. }
  35. const tester = new User();
  36. assert.equal(tester.isLazy(), true);
  37. tester.wroteATest();
  38. assert.equal(tester.isLazy(), false);
  39. });
  40. it('anonymous class', () => {
  41. const classType = typeof function(){};
  42. assert.equal(classType, 'function');
  43. });
  44. });
  45.  
  46. // 23: class - accessors
  47. // To do: make all tests pass, leave the assert lines unchanged!
  48. // Follow the hints of the failure messages!
  49.  
  50. describe('Class accessors (getter and setter)', () => {
  51. it('a getter is defined like a method prefixed with `get`', () => {
  52. class MyAccount {
  53. get balance() { return Infinity; }
  54. }
  55. assert.equal(new MyAccount().balance, Infinity);
  56. });
  57. it('a setter has the prefix `set`', () => {
  58. class MyAccount {
  59. get balance() { return this.amount; }
  60. set balance(amount) { this.amount = amount; }
  61. }
  62. const account = new MyAccount();
  63. account.balance = 23;
  64. assert.equal(account.balance, 23);
  65. });
  66.  
  67. describe('dynamic accessors', () => {
  68. it('a dynamic getter name is enclosed in `[]`', function() {
  69. const balance = 'yourMoney';
  70. class YourAccount {
  71. get [balance]() { return -Infinity; }
  72. }
  73. assert.equal(new YourAccount().yourMoney, -Infinity);
  74. });
  75. it('a dynamic setter name as well', function() {
  76. const propertyName = 'balance';
  77. class MyAccount {
  78. get [propertyName]() { return this.amount; }
  79. set [propertyName](amount) { this.amount = 23; }
  80. }
  81. const account = new MyAccount();
  82. account.balance = 42;
  83. assert.equal(account.balance, 23);
  84. });
  85. });
  86. });
  87.  
  88.  
  89. // 24: class - static keyword
  90. // To do: make all tests pass, leave the assert lines unchanged!
  91. // Follow the hints of the failure messages!
  92.  
  93. describe('Inside a class you can use the `static` keyword', () => {
  94. describe('for methods', () => {
  95. class UnitTest {}
  96. it('a static method just has the prefix `static`', () => {
  97. class TestFactory {
  98. static makeTest() { return new UnitTest(); }
  99. }
  100. assert.ok(TestFactory.makeTest() instanceof UnitTest);
  101. });
  102. it('the method name can be dynamic/computed at runtime', () => {
  103. const methodName = 'createTest';
  104. class TestFactory {
  105. static [methodName]() { return new UnitTest(); }
  106. }
  107. assert.ok(TestFactory.createTest() instanceof UnitTest);
  108. });
  109. });
  110. describe('for accessors', () => {
  111. it('a getter name can be static, just prefix it with `static`', () => {
  112. class UnitTest {
  113. static get testType() { return 'unit'; }
  114. }
  115. assert.equal(UnitTest.testType, 'unit');
  116. });
  117. it('even a static getter name can be dynamic/computed at runtime', () => {
  118. const type = 'test' + 'Type';
  119. class IntegrationTest {
  120. static get [type]() { return 'integration'; }
  121. }
  122. assert.ok('testType' in IntegrationTest);
  123. assert.equal(IntegrationTest.testType, 'integration');
  124. });
  125. });
  126. });
  127.  
  128.  
  129. // 25: class - extends
  130. // To do: make all tests pass, leave the assert lines unchanged!
  131. // Follow the hints of the failure messages!
  132.  
  133. describe('Classes can inherit from another using `extends`', () => {
  134. describe('the default super class is `Object`', () => {
  135. it('a `class A` is an instance of `Object`', () => {
  136. class A {}
  137. assert.equal(new A() instanceof Object, true);
  138. });
  139. it('when B extends A, B is also instance of `Object`', () => {
  140. class A {}
  141. class B extends A {}
  142. assert.equal(new B() instanceof A, true);
  143. assert.equal(new B() instanceof Object, true);
  144. });
  145. it('a class can extend `null`, and is not an instance of Object', () => {
  146. class NullClass extends null {}
  147. let nullInstance = new NullClass();
  148. assert.equal(nullInstance instanceof Object, false);
  149. });
  150. });
  151. describe('instance of', () => {
  152. it('when B inherits from A, `new B()` is also an instance of A', () => {
  153. class A {};
  154. class B extends A {}
  155. assert.equal(new B() instanceof A, true);
  156. });
  157. it('extend over multiple levels', () => {
  158. class A {}
  159. class B extends A {}
  160. class C extends B {}
  161. assert.equal(new C instanceof A, true);
  162. });
  163. });
  164. });
  165.  
  166.  
  167. // 26: class - more-extends
  168. // To do: make all tests pass, leave the assert lines unchanged!
  169. // Follow the hints of the failure messages!
  170.  
  171. describe('Classes can inherit from another', () => {
  172. it('extend an `old style` "class", a function, still works', () => {
  173. class A {};
  174. class B extends A {}
  175. assert.equal(new B() instanceof A, true);
  176. });
  177.  
  178. describe('prototypes are as you know them', () => {
  179. class A {}
  180. class B extends A {}
  181. it('A is the prototype of B', () => {
  182. const isIt = A.isPrototypeOf(B);
  183. assert.equal(isIt, true);
  184. });
  185. it('A`s prototype is also B`s prototype', () => {
  186. const proto = B.prototype;
  187. // Remember: don't touch the assert!!! :)
  188. assert.equal(A.prototype.isPrototypeOf(proto), true);
  189. });
  190. });
  191.  
  192. describe('`extends` using an expression', () => {
  193. it('e.g. the inline assignment of the parent class', () => {
  194. let A;
  195. class B extends (A = function(){}) {}
  196. assert.equal(new B() instanceof A, true);
  197. });
  198. it('or calling a function that returns the parent class', () => {
  199. const returnParent = (beNull) => beNull ? null : class {};
  200. class B extends returnParent(true) {}
  201. assert.equal(Object.getPrototypeOf(B.prototype), null);
  202. });
  203. });
  204. });
  205.  
  206.  
  207. // 28: class - super in constructor
  208. // To do: make all tests pass, leave the assert lines unchanged!
  209. // Follow the hints of the failure messages!
  210.  
  211. describe('Inside a class`s constructor `super()` can be used', () => {
  212. it('`extend` a class and use `super()` to call the parent constructor', () => {
  213. class A {constructor() { this.levels = 1; }}
  214. class B extends A {
  215. constructor() {
  216. super()
  217. this.levels++;
  218. }
  219. }
  220. assert.equal(new B().levels, 2);
  221. });
  222. it('`super()` may also take params', () => {
  223. class A {constructor(startValue=1, addTo=1) { this.counter = startValue + addTo; }}
  224. class B extends A {
  225. constructor(...args) {
  226. super(43);
  227. this.counter++;
  228. }
  229. }
  230. assert.equal(new B(42, 2).counter, 45);
  231. });
  232. it('it is important where you place your `super()` call!', () => {
  233. class A {inc() { this.countUp = 1; }}
  234. class B extends A {
  235. inc() {
  236. this.countUp = 2;
  237. super.inc();
  238. return this.countUp;
  239. }
  240. }
  241. assert.equal(new B().inc(), 1);
  242. });
  243. it('use `super.constructor` to find out if there is a parent constructor', () => {
  244. class ParentClassA {constructor() {"parent";}}
  245. class B extends ParentClassA {
  246. constructor() {
  247. super();
  248. this.isTop = '' + super.constructor;
  249. }
  250. }
  251. assert(new B().isTop.includes('ParentClassA'), new B().isTop);
  252. });
  253. });
  254.  
  255. // 27: class - super inside a method
  256. // To do: make all tests pass, leave the assert lines unchanged!
  257. // Follow the hints of the failure messages!
  258.  
  259. describe('Inside a class use `super` to access parent methods', () => {
  260. it('use of `super` without `extends` fails (already when transpiling)', () => {
  261. class A {hasSuper() { return false ; }}
  262. assert.equal(new A().hasSuper(), false);
  263. });
  264. it('`super` with `extends` calls the method of the given name of the parent class', () => {
  265. class A {hasSuper() { return true; }}
  266. class B extends A {hasSuper() { return true; }}
  267. assert.equal(new B().hasSuper(), true);
  268. });
  269. it('when overridden a method does NOT automatically call its super method', () => {
  270. class A {hasSuper() { return true; }}
  271. class B extends A {hasSuper() { return void 0; }}
  272. assert.equal(new B().hasSuper(), void 0);
  273. });
  274. it('`super` works across any number of levels of inheritance', () => {
  275. class A {iAmSuper() { return true; }}
  276. class B extends A {}
  277. class C extends B {iAmSuper() { return super.iAmSuper(); }}
  278. assert.equal(new C().iAmSuper(), true);
  279. });
  280. it('accessing an undefined member of the parent class returns `undefined`', () => {
  281. class A {}
  282. class B extends A {getMethod() { return super.getMethod; }}
  283. assert.equal(new B().getMethod(), void 0);
  284. });
  285. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement