diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index 826c675..d6ac047 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -2,6 +2,20 @@ 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 entered argument point1 is not valid!") + } + if (!(point2 instanceof Point)){ + throw new Error("The entered argument point2 is not valid!") + } + + + if(typeof(slope)!=="undefined" && typeof(slope)!=="number"){ + throw new Error("slop should get a number") + } + if(typeof(n)!=="undefined" && typeof(n)!=="number"){ + throw new Error("n should get a number") + } this.point1 = point1; this.point2 = point2; this.slope = slope; @@ -9,7 +23,11 @@ class Line { } calculateSlope = () => { + // if(this.point1.x - this.point2.x==0){ + // throw new Error("it is impossible to divide by 0") + // } this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) + } calculateNOfLineFunction = () => { @@ -18,6 +36,7 @@ class Line { getPointOnXAsis() { return this.getPointByY(0) + } getPointOnYAsis() { @@ -26,11 +45,23 @@ class Line { getPointByX(x) { + if(x===undefined){ + throw new Error("the function should get number") + } + if(typeof(x)!="number"){ + throw new Error("the function should get 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 number") + } + if(typeof(y)!="number"){ + throw new Error("the function should get 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..73d8aa6 100644 --- a/modules/ecs6-class/point.js +++ b/modules/ecs6-class/point.js @@ -1,12 +1,30 @@ class Point { constructor({x=0, y=0}={}) { + if(typeof(x)!=="number"){ + throw new Error("x should be of type number") + } + if(typeof(y)!=="number"){ + throw new Error("y should be of type number") + } this.x = x; this.y = y; } moveVertical(value) { + if(value===undefined){ + throw new Error("the function should get number") + } + if(typeof(value)!="number"){ + throw new Error("the function should get number") + } this.y += value; } moveHorizontal(value) { + if(value===undefined){ + throw new Error("the function should get number") + } + if(typeof(value)!="number"){ + throw new Error("the function should get number") + } this.x += value; } } diff --git a/modules/geometry-calculation.js b/modules/geometry-calculation.js index 6e11643..d396889 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -1,16 +1,65 @@ const Line = require('./ecs6-class/line') +const Point = require('./ecs6-class/point') const calculateDistance = (point1, point2) => { - let distanceX = (point2.x - point1.x) ** 2; - let distanceY = (point2.y - point2.y) ** 2; - const distance = Math.sqrt(distanceX + distanceY); - return distance; + if(point1===undefined||point2===undefined){ + throw new Error('the function must get an arguments') + + } + if((!(point1 instanceof Point)) || (!(point2 instanceof Point))){ + throw new Error("the arguments is not instance of 'Point'") + } + let distanceX = (point2.x - point1.x) ** 2 + let distanceY = (point2.y - point1.y) ** 2 + const distance = Math.sqrt(distanceX + distanceY) + return distance } const calculateJunctionPoint = (line1, line2) => { + + if(line1===undefined||line2===undefined){ + throw new Error('the function must get line1 and line2') + + } + if((!(line1 instanceof Line)) && (!(line2 instanceof Line))){ + throw new Error("line1 and line2 should be of type 'Line'") + } + if((!(line1 instanceof Line))){ + throw new Error("line1 should be of type 'Line'") + } + if((!(line2 instanceof Line))){ + throw new Error("line2 should be of type 'Line'") + } + + + + + if(line1.slope===undefined){ + line1.calculateSlope() + } + if(line2.slope===undefined){ + line2.calculateSlope() + } + if(line1.n===undefined){ + line1.calculateNOfLineFunction() + + } + if(line2.n===undefined){ + line2.calculateNOfLineFunction() + } + + + // if(line1.n===undefined){ + + // } + // if(line2.n===undefined){ + + // } + + if (line1.slope === line2.slope) { if (line1.n === line2.n) { - return true + return true } else { return false @@ -22,10 +71,38 @@ const calculateJunctionPoint = (line1, line2) => { return junctionPoint } } - const isPointOnLine = (line, point) => { + + if(line===undefined && point===undefined){ + throw new Error('the function must get an two arguments') + } + + if(line===undefined){ + throw new Error('the function must accept a line') + } + + if(point===undefined){ + throw new Error('the function must accept a point') + + } + + if(!(line instanceof Line)){ + throw new Error("the arguments is not instance of 'line'") + + } + if(!(point instanceof Point)){ + throw new Error("the arguments is not instance of 'point'") + + } + + if(line.slope === undefined){ + line.calculateSlope() + } + + const proxyLine = new Line({ point1: line.point1, point2: point }) proxyLine.calculateSlope() + if (line.slope === proxyLine.slope) { proxyLine.calculateNOfLineFunction() if (line.n === proxyLine.n) { diff --git a/package.json b/package.json index 56bf17b..d2adae6 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "test": "jest" }, "dependencies": { - "jest": "^29.7.0" + "jest": "^29.7.0", + "coverage":"npm run test -- --coverage" }, "repository": { "type": "git", diff --git a/tests/modules/esc-6-class/line.test.js b/tests/modules/esc-6-class/line.test.js new file mode 100644 index 0000000..ceb7d4c --- /dev/null +++ b/tests/modules/esc-6-class/line.test.js @@ -0,0 +1,134 @@ +const Line = require('../../../modules/ecs6-class/line') +const Point = require('../../../modules/ecs6-class/point') +const point1 = new Point({ x: 1, y: 2 }) +const point2 = new Point({ x: 3, y: 4 }) +const line = new Line({ point1, point2,n:2,slope:4 }) +const line1 = new Line({}) +let lineTest=new Line({}) + +describe('CONSTRUCTOR',()=>{ + it('should return n after is built the constructor',()=>{ + expect(line.n).toBe(2) + }) + it('should return slope after is built the constructor',()=>{ + expect(line.slope).toBe(4) + }) + it('should return point1 after is built the constructor',()=>{ + expect(line.point1).toEqual({x:1,y:2}) + }) + it('should return point2 after is built the constructor',()=>{ + expect(line.point2).toEqual({x:3,y:4}) + }) + + +}) + +describe ('ERRORS',()=>{ + it('Error checker for constructor',()=>{ + expect(()=>new Line({point1:new Line({})})).toThrow('The entered argument point1 is not valid!') + expect(()=>new Line({n:"abc"})).toThrow('n should get a number') + expect(()=>new Line({point2:true})).toThrow('The entered argument point2 is not valid!') + expect(()=>new Line({point2:new Point['hello']})).toThrow('Point.hello is not a constructor') + expect(()=>new Line({slope:"sss"})).toThrow('slop should get a number') + + }) +}) + +describe('CALCULATE_SLOP', () => { + it('should calculate the slope', () => { + line.calculateSlope() + expect(line.slope).toBe(1) + + }) +}) + +describe('Error',()=>{ + it("Error checker for function calculateSlope",()=>{ + const line={point1:new Point,point2:new Point,slope:undefined,n:5} + console.log(line); + expect(()=>line.calculateSlope()).toThrow('line.calculateSlope is not a function') + }) +}) + + +describe('CALCULATE_N_OF_LINE_FUNCTION', () => { + it('should calculate n of line', () => { + line.calculateNOfLineFunction() + expect(line.slope).toBe(1) + + }) + +}) +describe('GET_POINT_ON_Y_ASIS',()=>{ + it('should return getPointOnYAsis as per 0',()=>{ + expect(line.getPointOnYAsis()).toEqual({x:0,y:1}) + }) + + it('mocking getPointByX function in getPointOnYAxis method', () => { + const line = new Line({}); + line.getPointByX = jest.fn().mockReturnValue({ x: 0, y: 5 }); + + const result = line.getPointOnYAsis(); + + expect(result).toEqual({ x: 0, y: 5 }); + }) + +}) +describe('GET_POINT_ON_X_ASIS',()=>{ + it('should return getPointOnXAsis as per 0',()=>{ + expect(line.getPointOnXAsis()).toEqual({x:-1,y:0}) + }) + + + it('mocking getPointByY function in getPointOnXAxis method', () => { + const line = new Line({}); + getPointByY = jest.fn().mockReturnValue({ x: 5, y: 0 }); + + const result = line.getPointOnXAsis(); + + expect(result).toEqual({ x: NaN, y:0}); + }) + +}) + + +describe('GET_POINT_BY_X',()=>{ + it('should retutn y by x',()=>{ + expect(line.getPointByX(3)).toEqual({x:3,y:4}) + }) +}) + +describe('ERRORS',()=>{ + it('Error checker for function getPointByX',()=>{ + expect(()=>lineTest.getPointByX('a')).toThrow('the function should get number') + expect(()=>lineTest.getPointByX(['acc','add'])).toThrow('the function should get number') + expect(()=>lineTest.getPointByX(true)).toThrow('the function should get number') + expect(()=>lineTest.getPointByX()).toThrow('the function should get number') + expect(()=>lineTest.getPointByX((v)=>v)).toThrow('the function should get number') + + }) + + +}) + +describe ('GET_POINT_BY_Y',()=>{ + it('should return x by y',()=>{ + expect(line.getPointByY(5)).toEqual({x:4,y:5}) + }) + + +}) + +describe('ERRORS',()=>{ + it('Error checker for function getPointByY',()=>{ + expect(()=>lineTest.getPointByY('a')).toThrow('the function should get number') + expect(()=>lineTest.getPointByY(['acc','add'])).toThrow('the function should get number') + expect(()=>lineTest.getPointByY(true)).toThrow('the function should get number') + expect(()=>lineTest.getPointByY()).toThrow('the function should get number') + expect(()=>lineTest.getPointByY((v)=>v)).toThrow('the function should get number') + + }) + +}) + + diff --git a/tests/modules/esc-6-class/point.test.js b/tests/modules/esc-6-class/point.test.js new file mode 100644 index 0000000..31b9e32 --- /dev/null +++ b/tests/modules/esc-6-class/point.test.js @@ -0,0 +1,64 @@ +const Point=require ('../../../modules/ecs6-class/point') +let point=new Point({}) + +describe('CONSTRUCTOR',()=>{ + it('should return x after is built the constructor',()=>{ + expect(point.x).toBe(0) + }) + it('should return y after is built the constructor',()=>{ + expect(point.y).toBe(0) + }) + + + +}) + +describe('ERRORS',()=>{ + it('Error checker for constructor',()=>{ + expect(()=>new Point({x:'a'})).toThrow('x should be of type number') + expect(()=>new Point({x:['acc','add']})).toThrow('x should be of type number') + expect(()=>new Point({y:true})).toThrow('y should be of type number') + expect(()=>new Point({y:(v)=>v})).toThrow('y should be of type number') + expect(()=>new Point({x:()=>false})).toThrow('x should be of type number') + +}) +}) + +describe('MOVE_VERTICAL',()=>{ + it('should move y',()=>{ + const point=new Point({x:1,y:2}) + point.moveVertical(2) + expect(point).toEqual({x:1,y:4}) + }) +}) +describe('ERRORS',()=>{ + it('Error checker for function moveVertical',()=>{ + expect(()=>point.moveVertical('a')).toThrow('the function should get number') + expect(()=>point.moveVertical(['acc','add'])).toThrow('the function should get number') + expect(()=>point.moveVertical(true)).toThrow('the function should get number') + expect(()=>point.moveVertical()).toThrow('the function should get number') + expect(()=>point.moveVertical((v)=>v)).toThrow('the function should get number') + + }) +}) + +describe('MOVE_HORIZONTAL',()=>{ + it('should move x',()=>{ + const point=new Point({x:1,y:2}) + point.moveHorizontal(2) + expect(point).toEqual({x:3,y:2}) + }) +}) +describe('ERRORS',()=>{ + it('Error checker for function moveHorizontal',()=>{ + expect(()=>point.moveHorizontal('a')).toThrow('the function should get number') + expect(()=>point.moveHorizontal(['acc','add'])).toThrow('the function should get number') + expect(()=>point.moveHorizontal(true)).toThrow('the function should get number') + expect(()=>point.moveHorizontal()).toThrow('the function should get number') + expect(()=>point.moveHorizontal((v)=>v)).toThrow('the function should get number') + + }) +}) + + + diff --git a/tests/modules/geometry-calculation.test.js b/tests/modules/geometry-calculation.test.js new file mode 100644 index 0000000..1199eeb --- /dev/null +++ b/tests/modules/geometry-calculation.test.js @@ -0,0 +1,165 @@ +const Line = require('../../modules/ecs6-class/line') +const Point = require('../../modules/ecs6-class/point') +const { calculateDistance, calculateJunctionPoint, isPointOnLine } = require('../../modules/geometry-calculation') + +const point1 = new Point({ x: 4, y: 3 }) +const point2 = new Point({ x: 3, y: 2 }) +const point3 = new Point({ x: 2, y: 2 }) +const point4 = new Point({ x: 2, y: 1 }) +const point5 = new Point({ x: 2, y: 2 }) +const point6 = new Point({ x: 1, y: 1 }) + +const line1 = new Line({ point1, point2, slope: 2, n: 3 }) +const line2 = new Line({ point1: point3, point2: point4, slope: 2, n: 5 }) +const line3 = new Line({ point1: point3, point2: point4, slope: 4, n: 3 }) +const line4 = new Line({ point1: point5, point2: point6, slope: 1, n: 0 }) +const line5 = new Line({ point1: point5, point2: point4, slope: 1, n: 3 }) + + +describe('CALCULATE_DISTANCE', () => { + it('should distance ', () => { + expect(calculateDistance(point1, point2)).toEqual(1.4142135623730951) + }) +}) +describe('ERRORS', () => { + it('Error checker for function calculateDistance', () => { + expect(() => calculateDistance('aaa')).toThrow('the function must get an arguments') + expect(() => calculateDistance(['a', 'b'])).toThrow('the function must get an arguments') + expect(() => calculateDistance(new Line({}))).toThrow('the function must get an arguments') + expect(() => calculateDistance(true)).toThrow('the function must get an arguments') + expect(() => calculateDistance({})).toThrow('the function must get an arguments') + expect(() => calculateDistance(line1, line2)).toThrow("the arguments is not instance of 'Point'") + expect(() => calculateDistance(new Line({}), new Line({}))).toThrow("the arguments is not instance of 'Point'") + expect(() => calculateDistance([], [])).toThrow("the arguments is not instance of 'Point'") + expect(() => calculateDistance('a', 'b')).toThrow("the arguments is not instance of 'Point'") + expect(() => calculateDistance(1, 2)).toThrow("the arguments is not instance of 'Point'") + + }) +}) + +describe('CALCULATE_JUNCTION_POINT', () => { + it('should return true', () => { + expect(calculateJunctionPoint(line1, line1)).toEqual(true) + }) + it('should return false', () => { + expect(calculateJunctionPoint(line1, line2)).toEqual(false) + }) + it('should return junctionPoint', () => { + expect(calculateJunctionPoint(line2, line3)).toEqual({ x: 1, y: 7 }) + }) + + it('should slope line1',()=>{ + const line=new Line({point1,point2,slope:undefined,n:5}) + expect(calculateJunctionPoint(line, line1)).toEqual({ x: 2, y: 7 }) + + }) + it('should slope line2',()=>{ + const line=new Line({point1,point2,slope:undefined,n:5}) + expect(calculateJunctionPoint(line1, line)).toEqual({ x: 2, y: 7 }) + + }) + + it('should n line1',()=>{ + const line=new Line({point1,point2,slope:3,n:undefined}) + expect(calculateJunctionPoint(line, line1)).toEqual({ x: 12, y: 27 }) + + }) + it('should n line2',()=>{ + const line=new Line({point1,point2,slope:4,n:undefined}) + expect(calculateJunctionPoint(line1, line)).toEqual({ x: 8, y: 19 }) + + }) + + + it('mocking calculateJunctionPoint function', () => { + const line1 = new Line({}); + const line2 = new Line({}); + line1.slope = 2; + line2.slope = 3; + line1.n = 1; + line2.n = 2; + + line1.getPointByX = jest.fn().mockReturnValue({ x: 3, y: 5 }); + + const result = calculateJunctionPoint(line1, line2); + + expect(result).toEqual({ x: 3, y: 5 }); + }) + + +}) + +describe('ERRORS', () => { + it('Error checker for function calculateJunctionPoint', () => { + + expect(() => calculateJunctionPoint()).toThrow('the function must get line1 and line2') + expect(() => calculateJunctionPoint(new Point({}), new Line({}))).toThrow("line1 should be of type 'Line'") + expect(() => calculateJunctionPoint(new Line({}), new Point({}))).toThrow("line2 should be of type 'Line'") + expect(() => calculateJunctionPoint(['a', 'b'])).toThrow('the function must get line1 and line2') + expect(() => calculateJunctionPoint(new Line({}))).toThrow('the function must get line1 and line2') + expect(() => calculateJunctionPoint(true)).toThrow("the function must get line1 and line2") + expect(() => calculateJunctionPoint({})).toThrow('the function must get line1 and line2') + expect(() => calculateJunctionPoint(point1, point2)).toThrow("line1 and line2 should be of type 'Line'") + expect(() => calculateJunctionPoint(new Point({}), new Point({}))).toThrow("line1 and line2 should be of type 'Line'") + expect(() => calculateJunctionPoint([], [])).toThrow("line1 and line2 should be of type 'Line'") + expect(() => calculateJunctionPoint('a', 'b')).toThrow("line1 and line2 should be of type 'Line'") + expect(() => calculateJunctionPoint(1, 2)).toThrow("line1 and line2 should be of type 'Line'") + expect(() => calculateJunctionPoint(new Line({ point1: {}, point2: {}, n: 5 }), new Line({}))).toThrow("The entered argument point1 is not valid!") + expect(() => calculateJunctionPoint(new Line({}), new Line({ point1: {}, point2: {}, slope: 5 }))).toThrow("The entered argument point1 is not valid!") + + }) +}) + +describe('IS_POINT_ON_LINE', () => { + it('should return true if the slope and n equals', () => { + + expect(isPointOnLine(line4, point6)).toEqual(true) + }) + it('should return false if the slope equal and n not equal', () => { + expect(isPointOnLine(line5, point6)).toEqual(false) + }) + it('should return true if the slope and n not equals', () => { + expect(isPointOnLine(line1, point1)).toEqual(false) + + }) + + it('should return slope if was missing',()=>{ + const line=new Line({point1,point2,slope:undefined,n:5}) + + expect(isPointOnLine(line,point1)).toBe(false) + console.log(line.slope); + }) + + + + it('mocking isPointOnLine function', () => { + const line = new Line({}); + const point = new Point({}); + line.slope = 1; + line.n = 2; + + const proxyLine = new Line({}); + proxyLine.slope = 1; + proxyLine.n = 2; + proxyLine.calculateNOfLineFunction = jest.fn(); + + const result = isPointOnLine(line, point); + + expect(result).toBe(false); + }) + + +}) +describe('Errors', () => { + it('Error checker for function isPointOnLine', () => { + expect(() => (isPointOnLine())).toThrow("the function must get an two arguments") + expect(() => (isPointOnLine(new Line({}), 'aaa'))).toThrow("the arguments is not instance of 'point'") + expect(() => (isPointOnLine(new Point(), new Point()))).toThrow("the arguments is not instance of 'line'") + expect(() => (isPointOnLine({ line: new Line({}) }))).toThrow( "the function must accept a point") + expect(() => (isPointOnLine(undefined,new Point({}) ))).toThrow('the function must accept a line') + expect(() => (isPointOnLine( () => (false) ))).toThrow('the function must accept a point') + + + +}) +})