Skip to content

Commit

Permalink
- add test for protected members
Browse files Browse the repository at this point in the history
  • Loading branch information
wizard04wsu committed May 29, 2021
1 parent 7169f41 commit c7e3ecd
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 3 deletions.
46 changes: 43 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ console.log((new Rectangle(2, 3)).toString()); // [object Rectangle]
console.log(Rectangle(2, 3)); // area = 6
```

### Inherit from a parent class
### Inherit from a superclass

```javascript
const Rectangle = Class.extend(function Rectangle($super, width, height){
Expand All @@ -95,7 +95,7 @@ let s = new Square(2);
console.log(s.dimensions()); // 2 x 2
```

### Use static methods of a parent class
### Use static methods of the parent class

```javascript
const Rectangle = Class.extend(function Rectangle($super, width, height){
Expand All @@ -119,4 +119,44 @@ console.log(s.area()); // 4

### Use protected members

TODO
```javascript
const Rectangle = Class.extend(function Rectangle($super, width, height){
const prot = $super();

prot.width = width;
prot.height = height;

Object.defineProperty(this, "width", {
enumerable: true, configurable: true,
get(){ return prot.width; },
set(width){ return prot.width = width; }
});
Object.defineProperty(this, "height", {
enumerable: true, configurable: true,
get(){ return prot.height; },
set(height){ return prot.height = height; }
});

this.dimensions = ()=>prot.width+" x "+prot.height;
});

const Square = Rectangle.extend(function Square($super, width){
const prot = $super(width, width);

Object.defineProperty(this, "width", {
enumerable: true, configurable: true,
get(){ return prot.width; },
set(width){ return prot.width = prot.height = width; }
});
Object.defineProperty(this, "height", {
enumerable: true, configurable: true,
get(){ return prot.height; },
set(height){ return prot.height = prot.width = height; }
});
});

let s = new Square(2);
console.log(s.dimensions()); // 2 x 2
s.height = 3;
console.log(s.dimensions()); // 3 x 3
```
45 changes: 45 additions & 0 deletions test/Class testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ console.group("Readme Examples");
console.assert(r.toString() === "[object Rectangle]", r.toString());
console.assert(r.area() === 6, r.area());
console.groupEnd();

console.group("Use an applier function");
Rectangle = Class.extend(function Rectangle($super, width, height){
$super();
Expand All @@ -266,6 +267,7 @@ console.group("Readme Examples");
console.assert((new Rectangle(2, 3)).toString() === "[object Rectangle]", (new Rectangle(2, 3)).toString());
console.assert(Rectangle(2, 3) === "area = 6", Rectangle(2, 3));
console.groupEnd();

console.group("Inherit from a parent class");
Rectangle = Class.extend(function Rectangle($super, width, height){
$super();
Expand All @@ -281,6 +283,7 @@ console.group("Readme Examples");

console.assert(s.dimensions() === "2 x 2", s.dimensions());
console.groupEnd();

console.group("Use static methods of a parent class");
Rectangle = Class.extend(function Rectangle($super, width, height){
$super();
Expand All @@ -301,4 +304,46 @@ console.group("Readme Examples");
console.assert(Rectangle.area(2, 2) === 4, Rectangle.area(2, 2));
console.assert(s.area() === 4, s.area());
console.groupEnd();

console.group("Use protected members");
Rectangle = Class.extend(function Rectangle($super, width, height){
const prot = $super();

prot.width = width;
prot.height = height;

Object.defineProperty(this, "width", {
enumerable: true, configurable: true,
get(){ return prot.width; },
set(width){ return prot.width = width; }
});
Object.defineProperty(this, "height", {
enumerable: true, configurable: true,
get(){ return prot.height; },
set(height){ return prot.height = height; }
});

this.dimensions = ()=>prot.width+" x "+prot.height;
});

Square = Rectangle.extend(function Square($super, width){
const prot = $super(width, width);

Object.defineProperty(this, "width", {
enumerable: true, configurable: true,
get(){ return prot.width; },
set(width){ return prot.width = prot.height = width; }
});
Object.defineProperty(this, "height", {
enumerable: true, configurable: true,
get(){ return prot.height; },
set(height){ return prot.height = prot.width = height; }
});
});

s = new Square(2);
console.assert(s.dimensions() === "2 x 2", s.dimensions());
s.height = 3;
console.assert(s.dimensions() === "3 x 3", s.dimensions());
console.groupEnd();
console.groupEnd();

0 comments on commit c7e3ecd

Please sign in to comment.