diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index 76a7359..864e442 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -2,17 +2,35 @@ const Point = require("./point"); class Line { constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) { + if ((!(point1 instanceof Point))) { + throw new Error('the constructor should get point1 arguments of "Point"') + } + if ((!(point2 instanceof Point))) { + throw new Error('the constructor should get point2 arguments of "Point"') + } + if ((typeof (slope) !== 'number' && typeof (slope) !== "undefined")) { + throw new Error('the slope in constractor should get undefined or number') + } + if ((typeof (n) !== "undefined" && typeof (n) !== "number")) { + throw new Error('the n in constractor should get undefined or number') + } this.point1 = point1; this.point2 = point2; this.slope = slope; this.n = n; } - calculateSlope() { + calculateSlope() {//שיפוע + if ((this.point1.x - this.point2.x) === 0) { + throw new Error('the argument equal to 0'); + } this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) } - calculateNOfLineFunction() { + calculateNOfLineFunction() {//מרחק + if(this.slope===undefined){ + throw new Error('The slope has not yet been defined') + } this.n = this.point1.y - this.slope * this.point1.x } @@ -26,11 +44,23 @@ class Line { getPointByX(x) { + if (x === undefined) { + throw new Error('the function should get a number') + } + if (typeof (x) != 'number') { + throw new Error('the function should get a number') + } let y = this.slope * x + this.n return new Point({ x, y }) } getPointByY(y) { + if (y === undefined) { + throw new Error('the function should get a number') + } + if (typeof (y) != 'number') { + throw new Error('the function should get a number') + } let x = (y - this.n) / this.slope; return new Point({ x, y }) } diff --git a/modules/ecs6-class/point.js b/modules/ecs6-class/point.js index e81b4a4..1610aff 100644 --- a/modules/ecs6-class/point.js +++ b/modules/ecs6-class/point.js @@ -1,12 +1,27 @@ class Point { constructor({x=0, y=0}={}) { + if(typeof(x)!='number'||typeof(y)!='number'){ + throw new Error('the constructor should get a number') + } this.x = x; this.y = y; } moveVertical(value) { + if(value===undefined){ + throw new Error('the function should get a number') + } + if(typeof(value)!='number'){ + throw new Error('the function should get a number') + } this.y += value; } moveHorizontal(value) { + if(value===undefined){ + throw new Error('the function should get a number') + } + if(typeof(value)!='number'){ + throw new Error('the function should get a number') + } this.x += value; } } diff --git a/modules/geometry-calculation.js b/modules/geometry-calculation.js index 6e11643..cfa7088 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -1,13 +1,38 @@ const Line = require('./ecs6-class/line') +const Point = require('./ecs6-class/point') -const calculateDistance = (point1, point2) => { +const calculateDistance = (point1, point2) => {//חישוב מרחק + if ((!(point1 instanceof Point)) || point1 === undefined) { + throw new Error('point1 must be of the Point class') + } + if (!(point2 instanceof Point) || point2 === undefined) { + throw new Error('point2 must be of the Point class') + } let distanceX = (point2.x - point1.x) ** 2; - let distanceY = (point2.y - point2.y) ** 2; + let distanceY = (point2.y - point1.y) ** 2; const distance = Math.sqrt(distanceX + distanceY); return distance; } -const calculateJunctionPoint = (line1, line2) => { +const calculateJunctionPoint = (line1, line2) => {// נקודת אמצע + if (line1 === undefined || !(line1 instanceof Line)) { + throw new Error('line1 must be of the Line class') + } + if (line2 === undefined || !(line2 instanceof Line)) { + throw new Error('line2 must be of the Line class') + } + if(line1.slope===undefined){ + throw new Error('The slope in line1 has not yet been defined') + } + if(line2.slope===undefined){ + throw new Error('The slope in line2 has not yet been defined') + } + if(line1.n===undefined){ + throw new Error('The n in line1 has not yet been defined') + } + if(line2.n===undefined){ + throw new Error('The n in line2 has not yet been defined') + } if (line1.slope === line2.slope) { if (line1.n === line2.n) { return true @@ -24,8 +49,17 @@ const calculateJunctionPoint = (line1, line2) => { } const isPointOnLine = (line, point) => { + if (!(line instanceof Line)) { + throw new Error('the function should get arg of "Line"') + } + if (typeof (point.x) !== 'number' || typeof (point.x) !== 'number' || !(point instanceof Point)) { + throw new Error('the function should get arg of "Point"') + } const proxyLine = new Line({ point1: line.point1, point2: point }) proxyLine.calculateSlope() + if(line.slope===undefined){ + throw new Error('The slope in line has not yet been defined') + } if (line.slope === proxyLine.slope) { proxyLine.calculateNOfLineFunction() if (line.n === proxyLine.n) { diff --git a/package.json b/package.json index 56bf17b..6becccd 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,9 @@ "description": "practice unit tests in javascript", "main": "index.js", "scripts": { - "test": "jest" + "test": "jest", + "coverage": "npm run test -- --coverage" + }, "dependencies": { "jest": "^29.7.0" diff --git a/tests/ecs6-class/line.test.js b/tests/ecs6-class/line.test.js new file mode 100644 index 0000000..48c23ef --- /dev/null +++ b/tests/ecs6-class/line.test.js @@ -0,0 +1,145 @@ +const Line = require('../../modules/ecs6-class/line'); +const point = require('../../modules/ecs6-class/point'); + +describe('CALCULATE_SIOPE', () => { + it('', () => { + const line1 = new Line({ point1: new point({ x: 1, y: 1 }), point2: new point({ x: 2, y: 2 }), n: 2, slope: 2 }) + line1.calculateSlope() + expect(line1.slope).toBe(1) + }); +}); + +describe('ERRORS', () => { + it('errors for calculateSlope function', () => { + const line2 = new Line({}) + expect(() => line2.calculateSlope()).toThrow('the argument equal to 0') + }); +}); + +describe('CALCULATE_N_OF_LINE_FUNCTION', () => { + it('', () => { + const line1 = new Line({ point1: new point({ x: 1, y: 1 }), point2: new point({ x: 2, y: 2 }), slope: 2 }) + line1.calculateNOfLineFunction() + expect(line1.n).toBe(-1) + }); +}); +describe('ERRORS', () => { + it('errors for calculateNOfLineFunction function', () => { + const line2 = new Line({}) + expect(() => line2.calculateNOfLineFunction()).toThrow('The slope has not yet been defined') + }); +}); + +describe('GET_POINT_ON_X_ASIS', () => { + it('', () => { + const line1 = new Line({ n: 2, slope: 2 }) + expect(line1.getPointOnXAsis()).toEqual({ x: -1, y: 0 }) + }); + it('mock on getPointOnXAsis', () => { + let mPoint = new point({ x: 2, y: 1 }); + const line1 = new Line({ point1: new point({ x: 1, y: 1 }), point2: new point({ x: 2, y: 2 }), n: 2, slope: 2 }) + jest.spyOn(line1, 'getPointByX').mockImplementation((y) => { + const x = (y - line1.n) / line1.slope; + mPoint = new point({ x, y }); + return mPoint; + }); + + const result2 = line1.getPointOnYAsis(); + expect(result2).toEqual(mPoint); + }); +}); + + +describe('GET_POINT_ON_Y_ASIS', () => { + test('', () => { + const line1 = new Line({ n: 2, slope: 2 }) + expect(line1.getPointOnYAsis()).toEqual({ x: 0, y: 2 }) + }); + test('mock on getPointOnYAsis', () => { + let mPoint = new point({ x: 2, y: 1 }); + const line1 = new Line({ point1: new point({ x: 1, y: 1 }), point2: new point({ x: 2, y: 2 }), n: 2, slope: 2 }) + jest.spyOn(line1, 'getPointByX').mockImplementation((x) => { + const y = line1.slope * x + line1.n; + mPoint = new point({ x, y }); + return mPoint; + }); + + const result2 = line1.getPointOnYAsis(); + expect(result2).toEqual(mPoint); + }); + +}); + + +describe('GET_POINT_BY_Y', () => { + test('', () => { + const lin = new Line({ n: 0, slope: 1 }) + expect(lin.getPointByY(6)).toEqual({ x: 6, y: 6 }) + }); +}); + +describe('ERRORS', () => { + test('check errors of getPointByY function', () => { + const TLine = new Line({}) + expect(() => TLine.getPointByY('c')).toThrow('the function should get a number') + expect(() => TLine.getPointByY([1, 2])).toThrow('the function should get a number') + expect(() => TLine.getPointByY(true)).toThrow('the function should get a number') + expect(() => TLine.getPointByY()).toThrow('the function should get a number') + expect(() => TLine.getPointByY(f => f)).toThrow('the function should get a number') + }); +}); + + +describe('GET_POINT_BY_X', () => { + test('', () => { + const lin1 = new Line({ n: 2, slope: 3 }) + expect(lin1.getPointByX(5)).toEqual({ x: 5, y: 17 }) + }); +}); + +describe('ERRORS', () => { + test('check errors of getPointByX function', () => { + const TLine = new Line({}) + expect(() => TLine.getPointByX('c')).toThrow('the function should get a number') + expect(() => TLine.getPointByX([1, 2])).toThrow('the function should get a number') + expect(() => TLine.getPointByX(true)).toThrow('the function should get a number') + expect(() => TLine.getPointByX()).toThrow('the function should get a number') + expect(() => TLine.getPointByX((f) => f)).toThrow('the function should get a number') + }); +}); + + +describe('ERRORS', () => { + test('check errors of constactor point1 and point2', () => { + expect(() => new Line({ point1: 'c' })).toThrow('the constructor should get point1 arguments of "Point"') + expect(() => new Line({ point1: [1, 2] })).toThrow('the constructor should get point1 arguments of "Point"') + expect(() => new Line({ point1: true })).toThrow('the constructor should get point1 arguments of "Point"') + expect(() => new Line({ point1: (f) => f })).toThrow('the constructor should get point1 arguments of "Point"') + expect(() => new Line({ point1: () => f })).toThrow('the constructor should get point1 arguments of "Point"') + }); + test('check errors of constactor point2', () => { + expect(() => new Line({ point2: 'c' })).toThrow('the constructor should get point2 arguments of "Point"') + expect(() => new Line({ point2: [1, 2] })).toThrow('the constructor should get point2 arguments of "Point"') + expect(() => new Line({ point2: true })).toThrow('the constructor should get point2 arguments of "Point"') + expect(() => new Line({ point2: (f) => f })).toThrow('the constructor should get point2 arguments of "Point"') + expect(() => new Line({ point2: () => f })).toThrow('the constructor should get point2 arguments of "Point"') + }) +}); + +describe('ERRORS', () => { + it('check errors of constactor n and slope', () => { + expect(() => new Line({ slope: 'c' })).toThrow('the slope in constractor should get undefined or number') + expect(() => new Line({ slope: [1, 2] })).toThrow('the slope in constractor should get undefined or number') + expect(() => new Line({ slope: true })).toThrow('the slope in constractor should get undefined or number') + expect(() => new Line({ slope: (f) => f })).toThrow('the slope in constractor should get undefined or number') + expect(() => new Line({ slope: () => f })).toThrow('the slope in constractor should get undefined or number'); + + }); + it('check errors of constactor slope', () => { + expect(() => new Line({ n: 'c' })).toThrow('the n in constractor should get undefined or number') + expect(() => new Line({ n: [1, 2] })).toThrow('the n in constractor should get undefined or number') + expect(() => new Line({ n: true })).toThrow('the n in constractor should get undefined or number') + expect(() => new Line({ n: (f) => f })).toThrow('the n in constractor should get undefined or number') + expect(() => new Line({ n: () => f })).toThrow('the n in constractor should get undefined or number'); + }); +}); \ No newline at end of file diff --git a/tests/ecs6-class/point.test.js b/tests/ecs6-class/point.test.js new file mode 100644 index 0000000..7b86cb2 --- /dev/null +++ b/tests/ecs6-class/point.test.js @@ -0,0 +1,47 @@ +const point = require('../../modules/ecs6-class/point') +let point1 = new point({}) + +describe('MOVE_VERTICAL', () => { + it('add a number of y', () => { + point1.moveVertical(5); + expect(point1).toEqual({ x: 0, y: 5 }); + }); +}); + +describe('MOVE_HORIZONTAL', () => { + it('add a number of x', () => { + point1.moveHorizontal(5); + expect(point1).toEqual({ x: 5, y: 5 }); + }); +}); + +describe('ERRORS', () => { + it('check errors of moveVertical function', () => { + expect(() => point1.moveVertical('c')).toThrow('the function should get a number') + expect(() => point1.moveVertical([1, 2])).toThrow('the function should get a number') + expect(() => point1.moveVertical(true)).toThrow('the function should get a number') + expect(() => point1.moveVertical()).toThrow('the function should get a number') + expect(() => point1.moveVertical((f) => f)).toThrow('the function should get a number') + }); +}); + +describe('ERRORS', () => { + it('check errors of moveHorizontal function', () => { + expect(() => point1.moveHorizontal('c')).toThrow('the function should get a number') + expect(() => point1.moveHorizontal([1, 2])).toThrow('the function should get a number') + expect(() => point1.moveHorizontal(true)).toThrow('the function should get a number') + expect(() => point1.moveHorizontal()).toThrow('the function should get a number') + expect(() => point1.moveHorizontal((f) => f)).toThrow('the function should get a number') + }); +}); + +describe('ERRORS', () => { + it('check errors of constactor', () => { + expect(() => new point({ x: 'c' })).toThrow('the constructor should get a number') + expect(() => new point({ y: [1, 2] })).toThrow('the constructor should get a number') + expect(() => new point({ x: true })).toThrow('the constructor should get a number') + expect(() => new point({ y: (f) => f })).toThrow('the constructor should get a number') + expect(() => new point({ x: () => f })).toThrow('the constructor should get a number') + + }); +}); \ No newline at end of file diff --git a/tests/geometry-calculation.test.js b/tests/geometry-calculation.test.js new file mode 100644 index 0000000..c49f1d1 --- /dev/null +++ b/tests/geometry-calculation.test.js @@ -0,0 +1,144 @@ +const geometry = require('../modules/geometry-calculation') +const Point = require('../modules/ecs6-class/point') +const Line = require('../modules/ecs6-class/line') + +const line1 = new Line({ point1:new Point({ x: 1, y: 1 }), point2:new Point({ x: 2, y: 2 }), n: 2, slope: 2 }); + + + +describe('CALCULATE_DISTANCE', () => { + it('should return distance ofthe points', () => { + expect(geometry.calculateDistance(new Point({ x: 1, y: 1 }), new Point({ x: 2, y: 2 }))).toBe(1.4142135623730951) + }); +}); + +describe('ERRORS', () => { + it('check errors of point1 in calculateDistance', () => { + expect(() => geometry.calculateDistance('c', { point2: new Point({}) })).toThrow('point1 must be of the Point class') + expect(() => geometry.calculateDistance([1, 2], new Point({}))).toThrow('point1 must be of the Point class') + expect(() => geometry.calculateDistance({ point1: true }, { point2: new Point({}) })).toThrow('point1 must be of the Point class') + expect(() => geometry.calculateDistance({ point1: (f) => f }, { point2: new Point({}) })).toThrow('point1 must be of the Point class') + expect(() => geometry.calculateDistance({ point1: () => f }, { point2: new Point({}) })).toThrow('point1 must be of the Point class') + }); + it('check errors of point2 in calculateDistance', () => { + expect(() => geometry.calculateDistance(new Point({}), { point2: 'c' })).toThrow('point2 must be of the Point class') + expect(() => geometry.calculateDistance(new Point({}), [1, 2])).toThrow('point2 must be of the Point class') + expect(() => geometry.calculateDistance(new Point({}), true)).toThrow('point2 must be of the Point class') + expect(() => geometry.calculateDistance(new Point({}), (f) => f)).toThrow('point2 must be of the Point class') + expect(() => geometry.calculateDistance(new Point({}), () => f)).toThrow('point2 must be of the Point class') + }); +}); + +describe('CALCULATE_JUNCTION_POINT', () => { + it('check if line1.slope end n equal to line 2', () => { + const line2 = new Line({ point1:new Point({ x: 1, y: 1 }), point2:new Point({ x: 2, y: 2 }), n: 2, slope: 2 }) + const line1 = new Line({ point1:new Point({ x: 1, y: 1 }), point2:new Point({ x: 2, y: 2 }), n: 2, slope: 2 }); + expect(geometry.calculateJunctionPoint(line1, line2)).toBe(true) + }); + it('', () => { + const line2 = new Line({ point1:new Point({ x: 1, y: 1 }), point2:new Point({ x: 2, y: 2 }), n: 3, slope: 2 }) + const line1 = new Line({ point1:new Point({ x: 1, y: 1 }), point2:new Point({ x: 2, y: 2 }), n: 2, slope: 2 }); + expect(geometry.calculateJunctionPoint(line1, line2)).toBe(false) + }); + it('', () => { + const line2 = new Line({ point1:new Point({ x: 1, y: 1 }), point2:new Point({ x: 2, y: 2 }), n: 2, slope: 6 }) + const line1 = new Line({ point1:new Point({ x: 1, y: 1 }), point2:new Point({ x: 2, y: 2 }), n: 2, slope: 2 }); + expect(geometry.calculateJunctionPoint(line1, line2)).toEqual({ x: 0, y: 2 }) + }); + it('mock on calculateJunctionPoint', () => { + const line2 = new Line({ n: 2, slope: 2 }); + const line1 = new Line({ point1:new Point({ x: 1, y: 1 }), point2:new Point({ x: 2, y: 2 }), n: 2, slope: 2 }); + jest.spyOn(line1, 'getPointByX').mockImplementation((x) => { + const y = line1.slope * x + line1.n; + return new Point({ x, y }); + }); + + jest.spyOn(line2, 'getPointByX').mockImplementation((x) => { + const y = line2.slope * x + line2.n; + return new Point({ x, y }); + }); + const result = geometry.calculateJunctionPoint(line1, line2); + expect(result).toBe(true) + + }) +}); + +describe('ERRORS', () => { + it('check errors of line1 in calculateJunctionPoint', () => { + expect(() => geometry.calculateJunctionPoint({ line1: 'c' }, { line2: new Line({}) })).toThrow('line1 must be of the Line class') + expect(() => geometry.calculateJunctionPoint({ line1: [1, 2] }, { line2: new Line({}) })).toThrow('line1 must be of the Line class') + expect(() => geometry.calculateJunctionPoint({ line1: true }, { line2: new Line({}) })).toThrow('line1 must be of the Line class') + expect(() => geometry.calculateJunctionPoint({ line1: (f) => f }, { line2: new Line({}) })).toThrow('line1 must be of the Line class') + expect(() => geometry.calculateJunctionPoint({ line1: () => f }, { line2: new Line({}) })).toThrow('line1 must be of the Line class'); + }); + it('check errors of line1 in calculateJunctionPoint', () => { + expect(() => geometry.calculateJunctionPoint(new Line({}), 'c')).toThrow('line2 must be of the Line class') + expect(() => geometry.calculateJunctionPoint(new Line({}), [1, 2])).toThrow('line2 must be of the Line class') + expect(() => geometry.calculateJunctionPoint(new Line({}), true)).toThrow('line2 must be of the Line class') + expect(() => geometry.calculateJunctionPoint(new Line({}), (f) => f)).toThrow('line2 must be of the Line class') + expect(() => geometry.calculateJunctionPoint(new Line({}), () => f)).toThrow('line2 must be of the Line class'); + }); + it('errors for calculateSlope function', () => { + expect(() => geometry.calculateJunctionPoint(new Line({ slope: 2 }), new Line({}))).toThrow('The slope in line2 has not yet been defined'); + }); + it('errors for calculateSlope function', () => { + expect(() => geometry.calculateJunctionPoint(new Line({}), new Line({ slope: 2 }))).toThrow('The slope in line1 has not yet been defined'); + }); + it('errors for calculateSlope function', () => { + expect(() => geometry.calculateJunctionPoint(new Line({slope: 2, n: 2 }), new Line({slope: 2}))).toThrow('The n in line2 has not yet been defined'); + }); + it('errors for calculateSlope function', () => { + expect(() => geometry.calculateJunctionPoint(new Line({slope: 2}), new Line({ slope: 2,n: 2 }))).toThrow('The n in line1 has not yet been defined'); + }); +}); + +describe('IS_POINT_ON LINE', () => { + it('should return true if line1.slope end n equal to line 2', () => { + const point = new Point({ x: 3, y: 3 }) + const line1 = new Line({ point1:new Point({ x: 1, y: 1 }), point2:new Point({ x: 2, y: 2 }), n: 2, slope: 2 }); + const line2 = new Line({ point1: line1.point1, point }) + line2.calculateSlope() + line2.calculateNOfLineFunction() + expect(geometry.isPointOnLine(line2, point)).toBe(true) + }); + + it('should return false if line1.slope not equal to line2', () => { + const point = new Point({ x: 5, y: 3 }) + const line1 = new Line({ point1:new Point({ x: 1, y: 1 }), point2:new Point({ x: 2, y: 2 }), n: 2, slope: 2 }); + const line2 = new Line({ point1: line1.point1, point }) + line2.calculateSlope() + line2.calculateNOfLineFunction() + expect(geometry.isPointOnLine(line2, point)).toBe(false) + }); + + it('should return false if line1.n not equal to line2', () => { + const point = new Point({ x: 6, y: 6 }) + const line2 = new Line({ point1:new Point({ x: 1, y: 1 }), point2:new Point({ x: 2, y: 2 }), n: 5 }) + line2.calculateSlope() + expect(geometry.isPointOnLine(line2, point)).toBe(false) + }); + it('mock on isPointOnLine', () => { + + }); +}); + +describe('ERRORS', () => { + it('check errors of point in isPointOnLine', () => { + expect(() => geometry.isPointOnLine(new Line({}), 'c')).toThrow('the function should get arg of "Point"'); + expect(() => geometry.isPointOnLine(new Line({}), [1, 2])).toThrow('the function should get arg of "Point"'); + expect(() => geometry.isPointOnLine(new Line({}), (f) => f)).toThrow('the function should get arg of "Point"'); + expect(() => geometry.isPointOnLine(new Line({}), true)).toThrow('the function should get arg of "Point"'); + expect(() => geometry.isPointOnLine(new Line({}), () => f)).toThrow('the function should get arg of "Point"'); + }); + it('check errors of line in isPointOnLine', () => { + expect(() => geometry.isPointOnLine({ point: new Point({ x: 1, y: 1 }) }, { line: 'c' })).toThrow('the function should get arg of "Line"') + expect(() => geometry.isPointOnLine({ point: new Point({ x: 1, y: 1 }) }, { line: [1, 2] })).toThrow('the function should get arg of "Line"') + expect(() => geometry.isPointOnLine({ point: new Point({ x: 1, y: 1 }) }, { line: true })).toThrow('the function should get arg of "Line"') + expect(() => geometry.isPointOnLine({ point: new Point({ x: 1, y: 1 }) }, { line: (f) => f })).toThrow('the function should get arg of "Line"') + expect(() => geometry.isPointOnLine({ point: new Point({ x: 1, y: 1 }) }, { line: () => f })).toThrow('the function should get arg of "Line"'); + }); + it('errors for calculateSlope function', () => { + expect(() => geometry.isPointOnLine(new Line({}),new Point({ x: 1, y: 1 }))).toThrow('The slope in line has not yet been defined'); + }); +}); +