Skip to content

Commit fe3da69

Browse files
committed
Added Homework for Classical Inheritance
1 parent dd15d36 commit fe3da69

5 files changed

Lines changed: 175 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Functions and Function Expressions
2+
==================================
3+
4+
### Task 1.
5+
* Write a function that sums an array of numbers:
6+
* Numbers must be always of type `Number`
7+
* Returns `null` if the array is empty
8+
* Throws Error if the parameter is not passed (undefined)
9+
* Throws if any of the elements is not convertible to `Number`
10+
11+
### Task 2.
12+
* Write a function that finds all the prime numbers in a range
13+
* It should return the prime numbers in an array
14+
* It must throw an Error if any of the range params is not convertible to `Number`
15+
* It must throw an Error if any of the range params is missing
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "test-driven-hw-mocha",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "mocha -R spec tests"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"devDependencies": {
12+
"chai": "^3.0.0"
13+
}
14+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* Task Description */
2+
/*
3+
Create a function constructor for Person. Each Person must have:
4+
* properties `firstname`, `lastname` and `age`
5+
* firstname and lastname must always be strings between 3 and 20 characters, containing only Latin letters
6+
* age must always be a number in the range 0 150
7+
* the setter of age can receive a convertible-to-number value
8+
* if any of the above is not met, throw Error
9+
* property `fullname`
10+
* the getter returns a string in the format 'FIRST_NAME LAST_NAME'
11+
* the setter receives a string is the format 'FIRST_NAME LAST_NAME'
12+
* it must parse it and set `firstname` and `lastname`
13+
* method `introduce()` that returns a string in the format 'Hello! My name is FULL_NAME and I am AGE-years-old'
14+
* all methods and properties must be attached to the prototype of the Person
15+
* all methods and property setters must return this, if they are not supposed to return other value
16+
* enables method-chaining
17+
*/
18+
function solve() {
19+
var Person = (function () {
20+
function Person() {
21+
}
22+
23+
return Person;
24+
} ());
25+
return Person;
26+
}
27+
module.exports = solve;
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/* globals describe, it */
2+
var Person = require('../tasks/task-1')();
3+
var expect = require('chai').expect;
4+
5+
describe('Tests for Classical Inheritance in JavaScript', function () {
6+
describe('"Task 1"', function () {
7+
var CONSTS = {
8+
VALID: {
9+
FIRSTNAME: 'Firsttest',
10+
LASTNAME: 'Lasttest',
11+
FULLNAME: 'full test',
12+
AGE: 5
13+
},
14+
INVALID: {
15+
FIRSTNAME: {
16+
SHORT: 'Ff',
17+
LONG: 'F' + (new Array(21).join('f'))
18+
},
19+
LASTNAME: {
20+
SHORT: 'Ll',
21+
LONG: 'L' + (new Array(21).join('l'))
22+
},
23+
AGE: {
24+
SMALLER: -1,
25+
LARGER: 151
26+
}
27+
}
28+
};
29+
30+
it('expect Person function constructor to exist', function () {
31+
expect(Person).to.exist;
32+
});
33+
34+
it('expect Person to be a function', function () {
35+
expect(Person).to.be.a('function');
36+
});
37+
38+
it('expect new Person object with valid firstname, lastname and age to set them', function () {
39+
var p = new Person(CONSTS.VALID.FIRSTNAME, CONSTS.VALID.LASTNAME, CONSTS.VALID.AGE);
40+
expect(p.firstname).to.equal(CONSTS.VALID.FIRSTNAME);
41+
expect(p.lastname).to.equal(CONSTS.VALID.LASTNAME);
42+
expect(p.age).to.equal(CONSTS.VALID.AGE);
43+
});
44+
45+
it('expect new Person object with valid firstname, lastname to set them have fullname', function () {
46+
var p = new Person(CONSTS.VALID.FIRSTNAME, CONSTS.VALID.LASTNAME, CONSTS.VALID.AGE);
47+
expect(p.firstname).to.equal(CONSTS.VALID.FIRSTNAME);
48+
expect(p.lastname).to.equal(CONSTS.VALID.LASTNAME);
49+
expect(p.fullname).to.equal(CONSTS.VALID.FIRSTNAME + ' ' + CONSTS.VALID.LASTNAME);
50+
});
51+
52+
it('expect new Person with short fistname to throw error', function () {
53+
function fn() {
54+
new Person(CONSTS.INVALID.FIRSTNAME.SHORT,
55+
CONSTS.VALID.LASTNAME,
56+
CONSTS.VALID.AGE);
57+
}
58+
expect(fn).to.throw();
59+
});
60+
it('expect new Person with long fistname to throw error', function () {
61+
function fn() {
62+
new Person(CONSTS.INVALID.FIRSTNAME.LONG, CONSTS.VALID.LASTNAME, CONSTS.VALID.AGE);
63+
}
64+
expect(fn).to.throw();
65+
});
66+
67+
it('expect new Person with short lastname to throw error', function () {
68+
function fn() {
69+
new Person(CONSTS.VALID.FIRSTNAME, CONSTS.INVALID.LASTNAME.SHORT, CONSTS.VALID.AGE);
70+
}
71+
expect(fn).to.throw();
72+
});
73+
it('expect new Person with long lastname to throw error', function () {
74+
function fn() {
75+
new Person(CONSTS.VALID.FIRSTNAME, CONSTS.INVALID.LASTNAME.LONG, CONSTS.VALID.AGE);
76+
}
77+
expect(fn).to.throw();
78+
});
79+
80+
81+
it('expect new Person with age ' + CONSTS.INVALID.AGE.SMALLER + ' to throw error', function () {
82+
function fn() {
83+
new Person(CONSTS.VALID.FIRSTNAME, CONSTS.VALID.LASTNAME, CONSTS.INVALID.AGE.SMALLER);
84+
}
85+
expect(fn).to.throw();
86+
});
87+
it('expect new Person with age ' + CONSTS.INVALID.AGE.LARGER + ' to throw error', function () {
88+
function fn() {
89+
new Person(CONSTS.VALID.FIRSTNAME, CONSTS.VALID.LASTNAME, CONSTS.INVALID.AGE.LARGER);
90+
}
91+
expect(fn).to.throw();
92+
});
93+
94+
it('expect person, with valid firstname, lastname and age, to return correct introduce', function () {
95+
var expected = 'Hello! My name is ' + CONSTS.VALID.FIRSTNAME + ' ' + CONSTS.VALID.LASTNAME +
96+
' and I am ' + CONSTS.VALID.AGE + '-years-old';
97+
98+
var actual = (new Person(CONSTS.VALID.FIRSTNAME, CONSTS.VALID.LASTNAME, CONSTS.VALID.AGE))
99+
.introduce();
100+
expect(actual).to.equal(expected);
101+
});
102+
103+
it('expect introduce to be attached to prototype', function () {
104+
expect(Person.prototype).to.has.property('introduce');
105+
});
106+
107+
it('expect introduce to be attached to prototype', function () {
108+
expect(Person.prototype).to.has.property('introduce');
109+
});
110+
});
111+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* globals describe, it */
2+
3+
4+
var solve = require('../tasks/task-2');
5+
6+
var expect = require('chai').expect;
7+
describe('Tests for "Task 2"', function () {
8+
});

0 commit comments

Comments
 (0)