From e2da13f5df0d274b7f1f8bb5f0cd8c8899dc580b Mon Sep 17 00:00:00 2001 From: estyRosenblat Date: Mon, 22 Jul 2024 18:26:36 +0300 Subject: [PATCH 1/3] finish function and t est --- modules/ecs6-class/line.js | 54 +++++++++-- modules/ecs6-class/point.js | 27 +++++- modules/geometry-calculation.js | 46 ++++++++-- package-lock.json | 45 +++++++++ package.json | 9 +- tests/modules/ecs6-class/line.test.js | 101 +++++++++++++++++++++ tests/modules/ecs6-class/point.test.js | 59 ++++++++++++ tests/modules/geometry-calculation.test.js | 58 ++++++++++++ 8 files changed, 379 insertions(+), 20 deletions(-) create mode 100644 tests/modules/ecs6-class/line.test.js create mode 100644 tests/modules/ecs6-class/point.test.js create mode 100644 tests/modules/geometry-calculation.test.js diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index b3bf0f7..4eac3f1 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -1,7 +1,24 @@ -const Point = require("./point"); - +const { Point } = require("./point"); class Line { constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) { + if (typeof (point1) !== "object" && typeof (point2) !== "object" && typeof (n) !== "number" && typeof (slope) != "number") { + throw Error('all parametrs isnt number') + } + + if (!(point1 instanceof Point)) { + throw new Error('point1 must be objects'); + } + if (!(point2 instanceof Point)) { + throw new Error('point2 must be objects'); + } + if (typeof n !== 'number' && n !== undefined) { + throw new Error('n must be a number'); + } + + if (typeof slope !== 'number' && slope !== undefined) { + throw new Error('slope must be a number'); + } + this.point1 = point1; this.point2 = point2; this.slope = slope; @@ -9,6 +26,9 @@ class Line { } calculateSlope = () => { + if (this.point1.x === this.point2.x) { + throw new Error("cut with zero"); + } this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) } @@ -24,16 +44,36 @@ class Line { return this.getPointByX(0) } - + getPointByX(x) { - let y = this.slope * x + this.n - return new Point({ x, y }) + if (x === undefined) { + throw new Error('x is undefined'); + } + if (typeof x !== 'number') { + throw new Error('x isnt number'); + } + + let y = this.slope * x + this.n; + return new Point({ x, y }); } - getPointByY(y) { + if (y === undefined) { + throw new Error('y is undefined'); + } + if (typeof y !== 'number') { + throw new Error('y isnt number'); + } + let x = (y - this.slope) / this.n; return new Point({ x, y }) } } -module.exports = Line \ No newline at end of file +module.exports = { + Line, getPointByY: Line.prototype.getPointByY, + getPointByX: Line.prototype.getPointByX, + getPointOnYAsis: Line.prototype.getPointOnYAsis, + getPointOnXAsis: Line.prototype.getPointOnXAsis, + calculateNOfLineFunction: Line.prototype.calculateNOfLineFunction, + calculateSlope: Line.prototype.calculateSlope, +} \ No newline at end of file diff --git a/modules/ecs6-class/point.js b/modules/ecs6-class/point.js index e81b4a4..2393662 100644 --- a/modules/ecs6-class/point.js +++ b/modules/ecs6-class/point.js @@ -1,14 +1,35 @@ class Point { - constructor({x=0, y=0}={}) { + constructor({ x = 0, y = 0 } = {}) { + if (typeof (x) !== "number" && typeof (y) != "number") { + throw Error('x and y isnt number') + } + if (typeof (x) != "number") { + throw Error('x isnt number') + } + if (typeof (y) != "number") { + throw Error('y isnt number') + } this.x = x; this.y = y; } + moveVertical(value) { + if (!value) { + throw Error('value is undefined') + } + if (typeof (value) !== "number") { + throw Error('value isnt number') + } this.y += value; } moveHorizontal(value) { + if (!value) { + throw Error('value is undefined') + } + if (typeof (value) !== "number") { + throw Error('value isnt number') + } this.x += value; } } - -module.exports = Point \ No newline at end of file +module.exports = { Point, moveVertical: Point.prototype.moveVertical, moveHorizontal: Point.prototype.moveHorizontal } \ No newline at end of file diff --git a/modules/geometry-calculation.js b/modules/geometry-calculation.js index 66856dc..851653a 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -1,13 +1,33 @@ -const Line = require('./ecs6-class/line') +const { Line } = require('./ecs6-class/line') +const { Point } = require("../modules/ecs6-class/point"); +//מחשבת מרחק const calculateDistance = (point1, point2) => { + if (!(point1 instanceof Point)&&!(point2 instanceof Point)){ + throw new Error('point1 and point2 must be objects'); + } + if (!(point1 instanceof Point)) { + throw new Error('point1 must be objects'); + } + if (!(point2 instanceof Point)) { + throw new Error('point2 must be objects'); + } 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) => { + if (!(line1 instanceof Line)&&!(line2 instanceof Line)){ + throw new Error('line1 and line2 must be Line'); + } + if (!(line1 instanceof Line)) { + throw new Error('line1 must be Line'); + } + if (!(line2 instanceof Line)) { + throw new Error('line2 must be Line'); + } if (line1.slope === line2.slope) { if (line1.n === line2.n) { return true @@ -22,15 +42,27 @@ const calculateJunctionPoint = (line1, line2) => { return junctionPoint } } - +//בודקת קיום נקודה על קו מסוים const isPointOnLine = (line, point) => { - const proxyLine = new Line(line.point1, point) + if (!(line instanceof Line)&&!(point instanceof Point)){ + throw new Error('line must be Line and point must be Point '); + } + if (!(point instanceof Point)) { + throw new Error('point must be Point'); + } + if (!(line instanceof Line)) { + throw new Error('line must be Line'); + } + const proxyLine = new Line({point1:line.point1,point2: point}) proxyLine.calculateSlope() - if (line.slope === proxyLine.slope) { + if (line.slope === proxyLine.slope) { proxyLine.calculateNOfLineFunction() - if (line.n === proxyLine.n2) { + if (line.n === proxyLine.n) { return true } + else{ + return false + } } return false } diff --git a/package-lock.json b/package-lock.json index 61f4a09..9134c97 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,9 @@ "license": "ISC", "dependencies": { "jest": "^29.7.0" + }, + "devDependencies": { + "jest-junit": "^16.0.0" } }, "node_modules/@ampproject/remapping": { @@ -1910,6 +1913,21 @@ "fsevents": "^2.3.2" } }, + "node_modules/jest-junit": { + "version": "16.0.0", + "resolved": "https://registry.npmjs.org/jest-junit/-/jest-junit-16.0.0.tgz", + "integrity": "sha512-A94mmw6NfJab4Fg/BlvVOUXzXgF0XIH6EmTgJ5NDPp4xoKq0Kr7sErb+4Xs9nZvu58pJojz5RFGpqnZYJTrRfQ==", + "dev": true, + "dependencies": { + "mkdirp": "^1.0.4", + "strip-ansi": "^6.0.1", + "uuid": "^8.3.2", + "xml": "^1.0.1" + }, + "engines": { + "node": ">=10.12.0" + } + }, "node_modules/jest-leak-detector": { "version": "29.7.0", "license": "MIT", @@ -2368,6 +2386,18 @@ "node": "*" } }, + "node_modules/mkdirp": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz", + "integrity": "sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==", + "dev": true, + "bin": { + "mkdirp": "bin/cmd.js" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/ms": { "version": "2.1.2", "license": "MIT" @@ -2860,6 +2890,15 @@ "browserslist": ">= 4.21.0" } }, + "node_modules/uuid": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", + "dev": true, + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/v8-to-istanbul": { "version": "9.2.0", "license": "ISC", @@ -2922,6 +2961,12 @@ "node": "^12.13.0 || ^14.15.0 || >=16.0.0" } }, + "node_modules/xml": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/xml/-/xml-1.0.1.tgz", + "integrity": "sha512-huCv9IH9Tcf95zuYCsQraZtWnJvBtLVE0QHMOs8bWyZAFZNDcYjsPq1nEx8jKA9y+Beo9v+7OBPRisQTjinQMw==", + "dev": true + }, "node_modules/y18n": { "version": "5.0.8", "license": "ISC", diff --git a/package.json b/package.json index 56bf17b..6e6df69 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "practice unit tests in javascript", "main": "index.js", "scripts": { - "test": "jest" + "test": "jest", + "test:coverage": "npm run test -- --coverage" }, "dependencies": { "jest": "^29.7.0" @@ -18,6 +19,8 @@ }, "homepage": "https://github.com/gemtechd/build-tests#readme", "author": "gemtechd", - "license": "ISC" - + "license": "ISC", + "devDependencies": { + "jest-junit": "^16.0.0" + } } diff --git a/tests/modules/ecs6-class/line.test.js b/tests/modules/ecs6-class/line.test.js new file mode 100644 index 0000000..44aee22 --- /dev/null +++ b/tests/modules/ecs6-class/line.test.js @@ -0,0 +1,101 @@ +const { Line, getPointByY, calculateNOfLineFunction, getPointOnXAsis, getPointOnYAsis, getPointByX } = require('../../../modules/ecs6-class/line'); +const point = require('../../../modules/ecs6-class/point'); +const { Point } = require('../../../modules/ecs6-class/point'); +const mockConstructor = jest.fn(constructor); +let line; +describe('constructor', () => { + it('should throw error when all isnt number ', () => { + expect(() => new Line({ point1: 'p', point2: 'new Point(1, 2)', n: '5', slope: '4' })).toThrow('all parametrs isnt number') + }); + + it('should throw error when point1 isnt object or point2 isnt object or n isnt number or slope isnt number ', () => { + expect(() => new Line({ point1: 'p', point2: new Point(1, 2), n: 5, slope: 4 })).toThrow('point1 must be objects') + expect(() => new Line({ point1: new Point(1, 2), point2: '4', n: 5, slope: 4 })).toThrow('point2 must be objects') + expect(() => new Line({ point1: new Point(1, 2), point2: new Point(1, 2), n: "invalid", slope: 4 })).toThrow('n must be a number'); + expect(() => new Line({ point1: new Point(1, 2), point2: new Point(1, 2), n: 4, slope: "invalid" })).toThrow('slope must be a number'); + }); + it('all isnt undefined', () => { + line = new Line({ point1: new Point(1, 2) }); + expect(line.point1).toEqual(new Point(1, 2)); + expect(line.point2).toEqual({ x: 0, y: 0 }); + expect(line.n).toBe(undefined); + expect(line.slope).toBe(undefined); + line = new Line({ point2: new Point(1, 2) }); + expect(line.point1).toEqual({ x: 0, y: 0 }); + expect(line.point2).toEqual(new Point(4, 8)); + expect(line.n).toBe(undefined); + expect(line.slope).toBe(undefined); + line = new Line({ n: 4 }); + expect(line.point1).toEqual({ x: 0, y: 0 }); + expect(line.point2).toEqual({ x: 0, y: 0 }); + expect(line.n).toBe(4); + expect(line.slope).toBe(undefined); + line = new Line({ slope: 7 }); + expect(line.point1).toEqual({ x: 0, y: 0 }); + expect(line.point2).toEqual({ x: 0, y: 0 }); + expect(line.n).toBe(undefined); + expect(line.slope).toBe(7); + line = new Line({ point1: new Point(1, 2), point2: new Point(4, 5), n: 4, slope: 7 }); + expect(line.point1).toEqual(new Point(1, 2)); + expect(line.point2).toEqual(new Point(4, 5)) + expect(line.n).toBe(4); + expect(line.slope).toBe(7); + }); +}); + +describe('calculateSlope', () => { + it('should throw error when it try to cut with zero', () => { + line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 1, y: 2 })), point2: mockConstructor(new Point({ x: 1, y: 10 })), n: 5, slope: 4 })) + expect(() => { line.calculateSlope() }).toThrow('cut with zero'); + }) + it('should be 3 when all is good', () => { + line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 10, y: 12 })), point2: mockConstructor(new Point({ x: 7, y: 3 })), n: 3, slope: 7 })); + line.calculateSlope() + expect(line.slope).toBe(3); + }) +}); +describe('calculateNOfLineFunction', () => { + it('should be 3 when all is good', () => { + line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 2, y: 7 })), point2: mockConstructor(new Point({ x: 5, y: 3 })), n: 2, slope: 2 })); + line.calculateNOfLineFunction() + expect(line.n).toBe(3); + }) +}); + +describe('getPointOnXAsis', () => { + it('should return the point on the X axis with the correct y-coordinate', () => { + line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 2, y: 7 })), point2: mockConstructor(new Point({ x: 5, y: 3 })), n: 2, slope: 2 })); + const result = line.getPointOnXAsis(); + expect(result.x).toBe(-1) + expect(result.y).toBe(0); + }); +}); +describe('getPointOnYAsis', () => { + it('should return the point on the y axis with the correct x-coordinate', () => { + line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 2, y: 7 })), point2: mockConstructor(new Point({ x: 5, y: 3 })), n: 2, slope: 2 })); + const result = line.getPointOnYAsis(); + expect(result.x).toBe(0) + expect(result.y).toBe(2); + }); +}); + +describe('getPointByX', () => { + it('should throw error when x is undefined', () => { + line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: undefined, y: 2 })), point2: mockConstructor(new Point({ x: undefined, y: 5 })), n: 5, slope: 4 })); + expect(() => line.getPointByX()).toThrow("x is undefined"); + }); + it('should throw error when x isnt a number', () => { + line = mockConstructor(new Line({ n: 5, slope: 4 })); + expect(() => line.getPointByX("2")).toThrow('x isnt number'); + }); +}); +describe('getPointByY', () => { + it('should throw error when y is undefined', () => { + line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 2 })), point2: mockConstructor(new Point({ x: 2 })), n: 5, slope: 4 })); + expect(() => line.getPointByY()).toThrow("y is undefined"); + }); + it('should throw error when y isnt a number', () => { + line = mockConstructor(new Line({ n: 5, slope: 4 })); + expect(() => line.getPointByY("1")).toThrow('y isnt number'); + }); +}); \ No newline at end of file diff --git a/tests/modules/ecs6-class/point.test.js b/tests/modules/ecs6-class/point.test.js new file mode 100644 index 0000000..d692ed0 --- /dev/null +++ b/tests/modules/ecs6-class/point.test.js @@ -0,0 +1,59 @@ +const {Point,moveVertical}=require('../../../modules/ecs6-class/point') +const { calculateSlope } = require('../../../modules/ecs6-class/line') +const mockConstructor = jest.fn(constructor); + +describe('constructor', () => { + it('should initialize x or y to default values when no parameters are provided', () => { + const point = new Point({x:5}); + expect(point.x).toBe(5); + expect(point.y).toBe(0); + const point1 = new Point({y:5}); + expect(point1.x).toBe(0); + expect(point1.y).toBe(5); + const point2= new Point(); + expect(point2.x).toBe(0); + expect(point2.y).toBe(0); + }); + it('should be ok when x and y are numbers', () => { + const point = new Point({x: 3, y: 4}); + expect(point.x).toBe(3); + expect(point.y).toBe(4); + }); + it('should throw error when x or y isnt a number ',()=>{ + expect(()=>new Point({x:'2',y:4})).toThrow('x isnt number') + expect(()=>new Point({x:4,y:'2'})).toThrow('y isnt number') + expect(()=>new Point({x:'2',y:'4'})).toThrow('x and y isnt number') + }) +}); +describe('moveVertical', () => { + it('should throw error when value is undefined', () => { + const point2= mockConstructor(new Point()); + expect(()=>point2.moveVertical()).toThrow('value is undefined'); + }); + it('should throw error when value isnt a number ',()=>{ + const point=mockConstructor(new Point()) + expect(()=>point.moveVertical('dddd')).toThrow('value isnt number'); + }) + it('should be ok when value is number ',()=>{ + const point =mockConstructor(new Point({x:5,y:4})) + point.moveVertical(5); + expect(point.y).toBe(9); + }) +}); + +describe('moveHorizontal', () => { + + it('should throw error when value is undefined', () => { + const point2= mockConstructor(new Point()); + expect(()=>point2.moveHorizontal()).toThrow('value is undefined'); + }); + it('should throw error when value isnt a number ',()=>{ + const point=mockConstructor(new Point()) + expect(()=>point.moveHorizontal('dddd')).toThrow('value isnt number'); + }) + it('should be ok when value is number ',()=>{ + const point =mockConstructor(new Point({x:5,y:4})) + point.moveHorizontal(5); + expect(point.x).toBe(10); + }) +}); \ No newline at end of file diff --git a/tests/modules/geometry-calculation.test.js b/tests/modules/geometry-calculation.test.js new file mode 100644 index 0000000..c5db9fe --- /dev/null +++ b/tests/modules/geometry-calculation.test.js @@ -0,0 +1,58 @@ +const { Line } = require('../../modules/ecs6-class/line'); +const { Point } = require('../../modules/ecs6-class/point'); +const { calculateDistance, calculateJunctionPoint, isPointOnLine } = require('../../modules/geometry-calculation'); + +describe('calculateDistance', () => { + it('should be good when point1 and point2 are object', () => { + expect(calculateDistance(new Point({ x: 4, y: 3 }), new Point({ x: 8, y: 6 }))).toBe(5); + }); + it('should throw error when point1 or point 2 is not an object or ', () => { + expect(() => calculateDistance('vvv', 'ffff')).toThrow('point1 and point2 must be objects') + expect(() => calculateDistance(new Point({ x: 4, y: 2 }), 'ffff')).toThrow('point2 must be objects') + expect(() => calculateDistance('ffff', new Point({ x: 4, y: 2 }))).toThrow('point1 must be objects') + }); +}); + +describe('calculateJunctionPoint', () => { + it('should throw error when point1 is not an object or ', () => { + const Line8 = new Line({ point1: new Point({ x: 1, y: 2 }), point2: new Point({ x: 1, y: 10 }), n: 5, slope: 4 }) + expect(() => calculateJunctionPoint('vvv', 'ffff')).toThrow('line1 and line2 must be Line') + expect(() => calculateJunctionPoint(Line8, 'ffff')).toThrow('line2 must be Line') + expect(() => calculateJunctionPoint('ffff', Line8)).toThrow('line1 must be Line') + }); + it('should get boolean result when slope1=slope2 ', () => { + const Line8 = new Line({ point1: new Point({ x: 1, y: 2 }), point2: new Point({ x: 1, y: 10 }), n: 4, slope: 4 }) + const Line9 = new Line({ point1: new Point({ x: 2, y: 3 }), point2: new Point({ x: 4, y: 5 }), n: 5, slope: 4 }) + const Line10 = new Line({ point1: new Point({ x: 1, y: 2 }), point2: new Point({ x: 1, y: 10 }), n: 4, slope: 4 }) + expect(calculateJunctionPoint(Line8, Line9)).toBe(false) + expect(calculateJunctionPoint(Line8, Line10)).toBe(true) + }); + it('should return result when slope1!=slope2 ', () => { + const Line8 = new Line({ point1: new Point({ x: 1, y: 2 }), point2: new Point({ x: 1, y: 10 }), n: 9, slope: 8 }) + const Line9 = new Line({ point1: new Point({ x: 2, y: 3 }), point2: new Point({ x: 4, y: 5 }), n: 5, slope: 10 }) + expect(calculateJunctionPoint(Line8, Line9)).toStrictEqual(new Point({ x: 2, y: 25 })) + }); +}) + + +describe('isPointOnLine', () => { + it('should thrwo error when point1 or point2 is not an object ', () => { + const Line8 = new Line({ point1: new Point({ x: 1, y: 2 }), point2: new Point({ x: 1, y: 10 }), n: 5, slope: 4 }) + const point = new Point({ x: 3, y: 4 }); + expect(() => isPointOnLine(Line8, 'ffff')).toThrow('point must be Point') + expect(() => isPointOnLine('Line8', point)).toThrow('line must be Line') + expect(() => isPointOnLine('ffff', 'Line8')).toThrow('line must be Line and point must be Point ') + }); + it('should return boolean result when line.slope === proxyLine.slope ', () => { + const Line7 = new Line({ point1: new Point({ x: 1, y: 2 }), n: 4, slope: -2 })//false + const Line8 = new Line({ point1: new Point({ x: 1, y: 2 }), n: 4, slope: -1 })//false + const Line9 = new Line({ point1: new Point({ x: 4, y: 6 }), n: 14, slope: -2 })//true + const Line10 = new Line({ point1: new Point({ x: 4, y: 6 }), n: 13, slope: -2 })//true + const point = new Point({ x: 3, y: 4 }); + const point2 = new Point({ x: 5, y: 4 }); + expect(isPointOnLine(Line7, point)).toBe(false) + expect(isPointOnLine(Line8, point)).toBe(false) + expect(isPointOnLine(Line10, point2)).toBe(false) + expect(isPointOnLine(Line9, point2)).toBe(true) + }); +}); \ No newline at end of file From 383bcd2943debeac998614a0f1df86210a97c910 Mon Sep 17 00:00:00 2001 From: estyRosenblat Date: Wed, 24 Jul 2024 14:18:11 +0300 Subject: [PATCH 2/3] fix error --- modules/ecs6-class/line.js | 35 ++++++++-------- modules/ecs6-class/point.js | 3 +- modules/geometry-calculation.js | 39 ++++++++++++------ package.json | 2 +- tests/modules/ecs6-class/line.test.js | 46 +++++++++++++--------- tests/modules/ecs6-class/point.test.js | 17 ++++---- tests/modules/geometry-calculation.test.js | 14 ++++++- 7 files changed, 95 insertions(+), 61 deletions(-) diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index 4eac3f1..f46e6f6 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -1,15 +1,12 @@ -const { Point } = require("./point"); +const Point = require("./point"); class Line { constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) { - if (typeof (point1) !== "object" && typeof (point2) !== "object" && typeof (n) !== "number" && typeof (slope) != "number") { - throw Error('all parametrs isnt number') - } if (!(point1 instanceof Point)) { - throw new Error('point1 must be objects'); + throw new Error('point1 must be Point objects'); } if (!(point2 instanceof Point)) { - throw new Error('point2 must be objects'); + throw new Error('point2 must be Point objects'); } if (typeof n !== 'number' && n !== undefined) { throw new Error('n must be a number'); @@ -44,7 +41,7 @@ class Line { return this.getPointByX(0) } - + getPointByX(x) { if (x === undefined) { throw new Error('x is undefined'); @@ -52,10 +49,16 @@ class Line { if (typeof x !== 'number') { throw new Error('x isnt number'); } - + if (!this.slope) { + this.calculateSlope(); + } + if (!this.n) { + this.calculateNOfLineFunction(); + } let y = this.slope * x + this.n; return new Point({ x, y }); } + getPointByY(y) { if (y === undefined) { throw new Error('y is undefined'); @@ -63,17 +66,15 @@ class Line { if (typeof y !== 'number') { throw new Error('y isnt number'); } - + if (!this.slope) { + this.calculateSlope(); + } + if (!this.n) { + this.calculateNOfLineFunction(); + } let x = (y - this.slope) / this.n; return new Point({ x, y }) } } -module.exports = { - Line, getPointByY: Line.prototype.getPointByY, - getPointByX: Line.prototype.getPointByX, - getPointOnYAsis: Line.prototype.getPointOnYAsis, - getPointOnXAsis: Line.prototype.getPointOnXAsis, - calculateNOfLineFunction: Line.prototype.calculateNOfLineFunction, - calculateSlope: Line.prototype.calculateSlope, -} \ No newline at end of file +module.exports = Line; diff --git a/modules/ecs6-class/point.js b/modules/ecs6-class/point.js index 2393662..595ae9e 100644 --- a/modules/ecs6-class/point.js +++ b/modules/ecs6-class/point.js @@ -8,6 +8,7 @@ class Point { } if (typeof (y) != "number") { throw Error('y isnt number') + } this.x = x; this.y = y; @@ -32,4 +33,4 @@ class Point { this.x += value; } } -module.exports = { Point, moveVertical: Point.prototype.moveVertical, moveHorizontal: Point.prototype.moveHorizontal } \ No newline at end of file +module.exports = Point \ No newline at end of file diff --git a/modules/geometry-calculation.js b/modules/geometry-calculation.js index 851653a..45f9256 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -1,9 +1,8 @@ -const { Line } = require('./ecs6-class/line') -const { Point } = require("../modules/ecs6-class/point"); +const Line = require('./ecs6-class/line') +const Point = require("../modules/ecs6-class/point"); -//מחשבת מרחק const calculateDistance = (point1, point2) => { - if (!(point1 instanceof Point)&&!(point2 instanceof Point)){ + if (!(point1 instanceof Point) && !(point2 instanceof Point)) { throw new Error('point1 and point2 must be objects'); } if (!(point1 instanceof Point)) { @@ -17,9 +16,8 @@ const calculateDistance = (point1, point2) => { const distance = Math.sqrt(distanceX + distanceY); return distance; } -//נקודת חיתוך const calculateJunctionPoint = (line1, line2) => { - if (!(line1 instanceof Line)&&!(line2 instanceof Line)){ + if (!(line1 instanceof Line) && !(line2 instanceof Line)) { throw new Error('line1 and line2 must be Line'); } if (!(line1 instanceof Line)) { @@ -28,6 +26,18 @@ const calculateJunctionPoint = (line1, line2) => { if (!(line2 instanceof Line)) { throw new Error('line2 must be Line'); } + if (!line1.slope) { + line1.calculateSlope(); + } + if (!line1.n) { + line1.calculateNOfLineFunction(); + } + if (!line2.slope) { + line2.calculateSlope(); + } + if (!line2.n) { + line2.calculateNOfLineFunction(); + } if (line1.slope === line2.slope) { if (line1.n === line2.n) { return true @@ -42,9 +52,8 @@ const calculateJunctionPoint = (line1, line2) => { return junctionPoint } } -//בודקת קיום נקודה על קו מסוים const isPointOnLine = (line, point) => { - if (!(line instanceof Line)&&!(point instanceof Point)){ + if (!(line instanceof Line) && !(point instanceof Point)) { throw new Error('line must be Line and point must be Point '); } if (!(point instanceof Point)) { @@ -53,14 +62,20 @@ const isPointOnLine = (line, point) => { if (!(line instanceof Line)) { throw new Error('line must be Line'); } - const proxyLine = new Line({point1:line.point1,point2: point}) + const proxyLine = new Line({ point1: line.point1, point2: point }) proxyLine.calculateSlope() - if (line.slope === proxyLine.slope) { + if (!line.slope) { + line.calculateSlope(); + } + if (!line.n) { + line.calculateNOfLineFunction(); + } + if (line.slope === proxyLine.slope) { proxyLine.calculateNOfLineFunction() - if (line.n === proxyLine.n) { + if (line.n === proxyLine.n) { return true } - else{ + else { return false } } diff --git a/package.json b/package.json index 6e6df69..18a30e5 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "test": "jest", "test:coverage": "npm run test -- --coverage" }, - "dependencies": { + "devDependencies": { "jest": "^29.7.0" }, "repository": { diff --git a/tests/modules/ecs6-class/line.test.js b/tests/modules/ecs6-class/line.test.js index 44aee22..d4b454b 100644 --- a/tests/modules/ecs6-class/line.test.js +++ b/tests/modules/ecs6-class/line.test.js @@ -1,16 +1,10 @@ -const { Line, getPointByY, calculateNOfLineFunction, getPointOnXAsis, getPointOnYAsis, getPointByX } = require('../../../modules/ecs6-class/line'); -const point = require('../../../modules/ecs6-class/point'); -const { Point } = require('../../../modules/ecs6-class/point'); -const mockConstructor = jest.fn(constructor); +const Line = require('../../../modules/ecs6-class/line'); +const Point = require('../../../modules/ecs6-class/point'); let line; describe('constructor', () => { - it('should throw error when all isnt number ', () => { - expect(() => new Line({ point1: 'p', point2: 'new Point(1, 2)', n: '5', slope: '4' })).toThrow('all parametrs isnt number') - }); - it('should throw error when point1 isnt object or point2 isnt object or n isnt number or slope isnt number ', () => { - expect(() => new Line({ point1: 'p', point2: new Point(1, 2), n: 5, slope: 4 })).toThrow('point1 must be objects') - expect(() => new Line({ point1: new Point(1, 2), point2: '4', n: 5, slope: 4 })).toThrow('point2 must be objects') + expect(() => new Line({ point1: 'p', point2: new Point(1, 2), n: 5, slope: 4 })).toThrow('point1 must be Point objects') + expect(() => new Line({ point1: new Point(1, 2), point2: '4', n: 5, slope: 4 })).toThrow('point2 must be Point objects') expect(() => new Line({ point1: new Point(1, 2), point2: new Point(1, 2), n: "invalid", slope: 4 })).toThrow('n must be a number'); expect(() => new Line({ point1: new Point(1, 2), point2: new Point(1, 2), n: 4, slope: "invalid" })).toThrow('slope must be a number'); }); @@ -45,18 +39,18 @@ describe('constructor', () => { describe('calculateSlope', () => { it('should throw error when it try to cut with zero', () => { - line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 1, y: 2 })), point2: mockConstructor(new Point({ x: 1, y: 10 })), n: 5, slope: 4 })) + line = (new Line({ point1: (new Point({ x: 1, y: 2 })), point2: (new Point({ x: 1, y: 10 })), n: 5, slope: 4 })) expect(() => { line.calculateSlope() }).toThrow('cut with zero'); }) it('should be 3 when all is good', () => { - line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 10, y: 12 })), point2: mockConstructor(new Point({ x: 7, y: 3 })), n: 3, slope: 7 })); + line = (new Line({ point1: (new Point({ x: 10, y: 12 })), point2: (new Point({ x: 7, y: 3 })), n: 3, slope: 7 })); line.calculateSlope() expect(line.slope).toBe(3); }) }); describe('calculateNOfLineFunction', () => { it('should be 3 when all is good', () => { - line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 2, y: 7 })), point2: mockConstructor(new Point({ x: 5, y: 3 })), n: 2, slope: 2 })); + line = (new Line({ point1: (new Point({ x: 2, y: 7 })), point2: (new Point({ x: 5, y: 3 })), n: 2, slope: 2 })); line.calculateNOfLineFunction() expect(line.n).toBe(3); }) @@ -64,7 +58,7 @@ describe('calculateNOfLineFunction', () => { describe('getPointOnXAsis', () => { it('should return the point on the X axis with the correct y-coordinate', () => { - line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 2, y: 7 })), point2: mockConstructor(new Point({ x: 5, y: 3 })), n: 2, slope: 2 })); + line = (new Line({ point1: (new Point({ x: 2, y: 7 })), point2: (new Point({ x: 5, y: 3 })), n: 2, slope: 2 })); const result = line.getPointOnXAsis(); expect(result.x).toBe(-1) expect(result.y).toBe(0); @@ -72,7 +66,7 @@ describe('getPointOnXAsis', () => { }); describe('getPointOnYAsis', () => { it('should return the point on the y axis with the correct x-coordinate', () => { - line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 2, y: 7 })), point2: mockConstructor(new Point({ x: 5, y: 3 })), n: 2, slope: 2 })); + line = (new Line({ point1: (new Point({ x: 2, y: 7 })), point2: (new Point({ x: 5, y: 3 })), n: 2, slope: 2 })); const result = line.getPointOnYAsis(); expect(result.x).toBe(0) expect(result.y).toBe(2); @@ -81,21 +75,35 @@ describe('getPointOnYAsis', () => { describe('getPointByX', () => { it('should throw error when x is undefined', () => { - line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: undefined, y: 2 })), point2: mockConstructor(new Point({ x: undefined, y: 5 })), n: 5, slope: 4 })); + line = (new Line({ point1: (new Point({ x: undefined, y: 2 })), point2: (new Point({ x: undefined, y: 5 })), n: 5, slope: 4 })); expect(() => line.getPointByX()).toThrow("x is undefined"); }); it('should throw error when x isnt a number', () => { - line = mockConstructor(new Line({ n: 5, slope: 4 })); + line = (new Line({ n: 5, slope: 4 })); expect(() => line.getPointByX("2")).toThrow('x isnt number'); }); + it('should toBe getPointByX', () => { + line = (new Line({ point1: (new Point({ x: 4, y: 7 })), point2: (new Point({ x: 2, y: 3 })), n: 2, slope: undefined })); + let line1 = (new Line({ point1: (new Point({ x: 4, y: 10 })), point2: (new Point({ x: 2, y: 3 })), n: undefined, slope: 2 })); + expect(line.getPointByX(5)).toEqual({ x: 5, y: 12 }) + expect(line1.getPointByX(5)).toEqual({ x: 5, y: 12 }) + + }); }); describe('getPointByY', () => { it('should throw error when y is undefined', () => { - line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 2 })), point2: mockConstructor(new Point({ x: 2 })), n: 5, slope: 4 })); + line = (new Line({ point1: (new Point({ x: 2 })), point2: (new Point({ x: 2 })), n: 5, slope: 4 })); expect(() => line.getPointByY()).toThrow("y is undefined"); }); it('should throw error when y isnt a number', () => { - line = mockConstructor(new Line({ n: 5, slope: 4 })); + line = (new Line({ n: 5, slope: 4 })); expect(() => line.getPointByY("1")).toThrow('y isnt number'); }); + it('should toBe getPointByY', () => { + line = (new Line({ point1: (new Point({ x: 4, y: 7 })), point2: (new Point({ x: 2, y: 3 })), n: 2, slope: undefined })); + const line1 = (new Line({ point1: (new Point({ x: 4, y: 10 })), point2: (new Point({ x: 2, y: 3 })), n: undefined, slope: 2 })); + expect(line.getPointByY(6)).toEqual({ x: 2, y: 6 }) + expect(line1.getPointByY(6)).toEqual({ x: 2, y: 6 }) + + }); }); \ No newline at end of file diff --git a/tests/modules/ecs6-class/point.test.js b/tests/modules/ecs6-class/point.test.js index d692ed0..41d8ace 100644 --- a/tests/modules/ecs6-class/point.test.js +++ b/tests/modules/ecs6-class/point.test.js @@ -1,6 +1,5 @@ -const {Point,moveVertical}=require('../../../modules/ecs6-class/point') -const { calculateSlope } = require('../../../modules/ecs6-class/line') -const mockConstructor = jest.fn(constructor); +const Point=require('../../../modules/ecs6-class/point') +// const { calculateSlope } = require('../../../modules/ecs6-class/line') describe('constructor', () => { it('should initialize x or y to default values when no parameters are provided', () => { @@ -27,15 +26,15 @@ describe('constructor', () => { }); describe('moveVertical', () => { it('should throw error when value is undefined', () => { - const point2= mockConstructor(new Point()); + const point2= (new Point()); expect(()=>point2.moveVertical()).toThrow('value is undefined'); }); it('should throw error when value isnt a number ',()=>{ - const point=mockConstructor(new Point()) + const point=(new Point()) expect(()=>point.moveVertical('dddd')).toThrow('value isnt number'); }) it('should be ok when value is number ',()=>{ - const point =mockConstructor(new Point({x:5,y:4})) + const point =(new Point({x:5,y:4})) point.moveVertical(5); expect(point.y).toBe(9); }) @@ -44,15 +43,15 @@ describe('moveVertical', () => { describe('moveHorizontal', () => { it('should throw error when value is undefined', () => { - const point2= mockConstructor(new Point()); + const point2= (new Point()); expect(()=>point2.moveHorizontal()).toThrow('value is undefined'); }); it('should throw error when value isnt a number ',()=>{ - const point=mockConstructor(new Point()) + const point=(new Point()) expect(()=>point.moveHorizontal('dddd')).toThrow('value isnt number'); }) it('should be ok when value is number ',()=>{ - const point =mockConstructor(new Point({x:5,y:4})) + const point =(new Point({x:5,y:4})) point.moveHorizontal(5); expect(point.x).toBe(10); }) diff --git a/tests/modules/geometry-calculation.test.js b/tests/modules/geometry-calculation.test.js index c5db9fe..b9ce3d8 100644 --- a/tests/modules/geometry-calculation.test.js +++ b/tests/modules/geometry-calculation.test.js @@ -1,5 +1,5 @@ -const { Line } = require('../../modules/ecs6-class/line'); -const { Point } = require('../../modules/ecs6-class/point'); +const Line = require('../../modules/ecs6-class/line'); +const Point = require('../../modules/ecs6-class/point'); const { calculateDistance, calculateJunctionPoint, isPointOnLine } = require('../../modules/geometry-calculation'); describe('calculateDistance', () => { @@ -32,6 +32,11 @@ describe('calculateJunctionPoint', () => { const Line9 = new Line({ point1: new Point({ x: 2, y: 3 }), point2: new Point({ x: 4, y: 5 }), n: 5, slope: 10 }) expect(calculateJunctionPoint(Line8, Line9)).toStrictEqual(new Point({ x: 2, y: 25 })) }); + it('should toBe calculateJunctionPoint', () => { + const line1 = new Line({ point1: (new Point({ x: 3, y: 8 })), point2: (new Point({ x: 1, y: 4 })), n: undefined, slope: undefined }); + const line2 = new Line({ point1: (new Point({ x: 7, y: 8 })), point2: (new Point({ x: 6, y: 2 })), n: undefined, slope: undefined }); + expect(calculateJunctionPoint(line1, line2)).toEqual({ x: 9, y: 20 }) + }); }) @@ -55,4 +60,9 @@ describe('isPointOnLine', () => { expect(isPointOnLine(Line10, point2)).toBe(false) expect(isPointOnLine(Line9, point2)).toBe(true) }); + it('should toBe calculateJunctionPoint', () => { + const line1 = new Line({ point1: (new Point({ x: 6, y: 8 })), point2: (new Point({ x: 8, y: 16 })), n: undefined, slope: undefined }); + const point = new Point({ x: 3, y: 4 }); + expect(isPointOnLine(line1, point)).toBe(false) + }); }); \ No newline at end of file From fed332493fa81f4a86e51f7fa098070930718369 Mon Sep 17 00:00:00 2001 From: estyRosenblat Date: Sun, 28 Jul 2024 10:25:05 +0300 Subject: [PATCH 3/3] fix my code --- package.json | 6 ++---- tests/modules/ecs6-class/line.test.js | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 18a30e5..f124e6f 100644 --- a/package.json +++ b/package.json @@ -19,8 +19,6 @@ }, "homepage": "https://github.com/gemtechd/build-tests#readme", "author": "gemtechd", - "license": "ISC", - "devDependencies": { - "jest-junit": "^16.0.0" - } + "license": "ISC" + } diff --git a/tests/modules/ecs6-class/line.test.js b/tests/modules/ecs6-class/line.test.js index d4b454b..0a2fe29 100644 --- a/tests/modules/ecs6-class/line.test.js +++ b/tests/modules/ecs6-class/line.test.js @@ -2,13 +2,20 @@ const Line = require('../../../modules/ecs6-class/line'); const Point = require('../../../modules/ecs6-class/point'); let line; describe('constructor', () => { + it('should be ok when all paramter is ok', () => { + line = new Line({ point1: new Point(1, 2), point2: new Point(4, 5), n: 4, slope: 7 }); + expect(line.point1).toEqual(new Point(1, 2)); + expect(line.point2).toEqual(new Point(4, 5)) + expect(line.n).toBe(4); + expect(line.slope).toBe(7); + }); it('should throw error when point1 isnt object or point2 isnt object or n isnt number or slope isnt number ', () => { expect(() => new Line({ point1: 'p', point2: new Point(1, 2), n: 5, slope: 4 })).toThrow('point1 must be Point objects') expect(() => new Line({ point1: new Point(1, 2), point2: '4', n: 5, slope: 4 })).toThrow('point2 must be Point objects') expect(() => new Line({ point1: new Point(1, 2), point2: new Point(1, 2), n: "invalid", slope: 4 })).toThrow('n must be a number'); expect(() => new Line({ point1: new Point(1, 2), point2: new Point(1, 2), n: 4, slope: "invalid" })).toThrow('slope must be a number'); }); - it('all isnt undefined', () => { + it('Not everything exists', () => { line = new Line({ point1: new Point(1, 2) }); expect(line.point1).toEqual(new Point(1, 2)); expect(line.point2).toEqual({ x: 0, y: 0 }); @@ -29,11 +36,6 @@ describe('constructor', () => { expect(line.point2).toEqual({ x: 0, y: 0 }); expect(line.n).toBe(undefined); expect(line.slope).toBe(7); - line = new Line({ point1: new Point(1, 2), point2: new Point(4, 5), n: 4, slope: 7 }); - expect(line.point1).toEqual(new Point(1, 2)); - expect(line.point2).toEqual(new Point(4, 5)) - expect(line.n).toBe(4); - expect(line.slope).toBe(7); }); }); @@ -42,14 +44,14 @@ describe('calculateSlope', () => { line = (new Line({ point1: (new Point({ x: 1, y: 2 })), point2: (new Point({ x: 1, y: 10 })), n: 5, slope: 4 })) expect(() => { line.calculateSlope() }).toThrow('cut with zero'); }) - it('should be 3 when all is good', () => { + it('The slope should be calculated correctly for different points with a positive slope', () => { line = (new Line({ point1: (new Point({ x: 10, y: 12 })), point2: (new Point({ x: 7, y: 3 })), n: 3, slope: 7 })); line.calculateSlope() expect(line.slope).toBe(3); }) }); describe('calculateNOfLineFunction', () => { - it('should be 3 when all is good', () => { + it('Should be the value of the point where the line intersects the y-axis.', () => { line = (new Line({ point1: (new Point({ x: 2, y: 7 })), point2: (new Point({ x: 5, y: 3 })), n: 2, slope: 2 })); line.calculateNOfLineFunction() expect(line.n).toBe(3);