From 2d6436d1ba08347dca57dc3b69015936353a43d4 Mon Sep 17 00:00:00 2001 From: brachy vacshtok Date: Sun, 13 Jul 2025 11:00:15 +0300 Subject: [PATCH 1/6] add test --- jest.config.js | 9 ++ modules/ecs6-class/line.ts | 24 ++-- modules/geometry-calculation.ts | 3 +- package-lock.json | 189 +++++++++++++++++++++++++++++++ package.json | 1 + test/gemetry-calculation.test.ts | 75 ++++++++++++ test/line.test.ts | 72 ++++++++++++ test/point.test.ts | 31 +++++ tsconfig.json | 12 ++ 9 files changed, 404 insertions(+), 12 deletions(-) create mode 100644 jest.config.js create mode 100644 test/gemetry-calculation.test.ts create mode 100644 test/line.test.ts create mode 100644 test/point.test.ts create mode 100644 tsconfig.json diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..659a07b --- /dev/null +++ b/jest.config.js @@ -0,0 +1,9 @@ +const { createDefaultPreset } = require("ts-jest"); + +const tsJestTransformCfg = createDefaultPreset().transform; + +/** @type {import("jest").Config} **/ +module.exports = { + preset: 'ts-jest', + testEnvironment: 'node', +}; \ No newline at end of file diff --git a/modules/ecs6-class/line.ts b/modules/ecs6-class/line.ts index e2d6086..912b9e6 100644 --- a/modules/ecs6-class/line.ts +++ b/modules/ecs6-class/line.ts @@ -19,9 +19,9 @@ export default class Line { } calculateNOfLineFunction() { - if (this.slope) - this.n = this.point1.y - this.slope * this.point1.x - } + if (this.slope !== undefined) + this.n = this.point1.y - this.slope * this.point1.x +} getPointOnXAsis() { return this.getPointByY(0) @@ -33,18 +33,20 @@ export default class Line { getPointByX(x: number) { - if (this.slope && this.n) { - let y = this.slope * x + this.n - return new Point({ x, y }) - } + if (this.slope !== undefined && this.n !== undefined) { + let y = this.slope * x + this.n + return new Point({ x, y }) } + return undefined; +} getPointByY(y: number) { - if (this.slope && this.n) { - let x = (y - this.n) / this.slope; - return new Point({ x, y }) - } + if (this.slope !== undefined && this.n !== undefined) { + let x = (y - this.n) / this.slope; + return new Point({ x, y }) } + return undefined; +} } diff --git a/modules/geometry-calculation.ts b/modules/geometry-calculation.ts index 22ab78b..4bbbea7 100644 --- a/modules/geometry-calculation.ts +++ b/modules/geometry-calculation.ts @@ -3,7 +3,7 @@ import Point from './ecs6-class/point'; export const calculateDistance = (point1: Point, point2: Point): number => { 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; } @@ -24,6 +24,7 @@ export const calculateJunctionPoint = (line1: Line, line2: Line): Boolean | Poin return junctionPoint } } + return undefined; } export const isPointOnLine = (line: Line, point: Point): Boolean => { diff --git a/package-lock.json b/package-lock.json index 5bc5ea8..3da30fe 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "devDependencies": { "@types/jest": "^30.0.0", "jest": "^30.0.4", + "ts-jest": "^29.4.0", "ts-node": "^10.9.2", "typescript": "^5.8.3" } @@ -1662,6 +1663,13 @@ "sprintf-js": "~1.0.2" } }, + "node_modules/async": { + "version": "3.2.6", + "resolved": "https://registry.npmjs.org/async/-/async-3.2.6.tgz", + "integrity": "sha512-htCUDlxyyCLMgaM3xXg0C0LW2xqfuQ6p05pCEIsXuyQ+a1koYKTuBMzRNwmybfLgvJDMd0r1LTn4+E0Ti6C2AA==", + "dev": true, + "license": "MIT" + }, "node_modules/babel-jest": { "version": "30.0.4", "resolved": "https://registry.npmjs.org/babel-jest/-/babel-jest-30.0.4.tgz", @@ -1823,6 +1831,19 @@ "node": "^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7" } }, + "node_modules/bs-logger": { + "version": "0.2.6", + "resolved": "https://registry.npmjs.org/bs-logger/-/bs-logger-0.2.6.tgz", + "integrity": "sha512-pd8DCoxmbgc7hyPKOvxtqNcjYoOsABPQdcCUjGp3d42VR2CX1ORhk2A87oqqu5R1kk+76nsxZupkmyd+MVtCog==", + "dev": true, + "license": "MIT", + "dependencies": { + "fast-json-stable-stringify": "2.x" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/bser": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/bser/-/bser-2.1.1.tgz", @@ -2153,6 +2174,22 @@ "dev": true, "license": "MIT" }, + "node_modules/ejs": { + "version": "3.1.10", + "resolved": "https://registry.npmjs.org/ejs/-/ejs-3.1.10.tgz", + "integrity": "sha512-UeJmFfOrAQS8OJWPZ4qtgHyWExa088/MtK5UEyoJGFH67cDEXkZSviOiKRCZ4Xij0zxI3JECgYs3oKx+AizQBA==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "jake": "^10.8.5" + }, + "bin": { + "ejs": "bin/cli.js" + }, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/electron-to-chromium": { "version": "1.5.181", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.181.tgz", @@ -2300,6 +2337,29 @@ "bser": "2.1.1" } }, + "node_modules/filelist": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/filelist/-/filelist-1.0.4.tgz", + "integrity": "sha512-w1cEuf3S+DrLCQL7ET6kz+gmlJdbq9J7yXCSjK/OZCPA+qEN1WyF4ZAf0YYJa4/shHJra2t/d/r8SV4Ji+x+8Q==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "minimatch": "^5.0.1" + } + }, + "node_modules/filelist/node_modules/minimatch": { + "version": "5.1.6", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.1.6.tgz", + "integrity": "sha512-lKwV/1brpG6mBUFHtb7NUmtABCb2WZZmm2wNiOA5hAb8VdCS4B3dtMWyvcoViccwAW/COERjXLt0zP1zXUN26g==", + "dev": true, + "license": "ISC", + "dependencies": { + "brace-expansion": "^2.0.1" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/fill-range": { "version": "7.1.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", @@ -2681,6 +2741,49 @@ "@pkgjs/parseargs": "^0.11.0" } }, + "node_modules/jake": { + "version": "10.9.2", + "resolved": "https://registry.npmjs.org/jake/-/jake-10.9.2.tgz", + "integrity": "sha512-2P4SQ0HrLQ+fw6llpLnOaGAvN2Zu6778SJMrCUwns4fOoG9ayrTiZk3VV8sCPkVZF8ab0zksVpS8FDY5pRCNBA==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "async": "^3.2.3", + "chalk": "^4.0.2", + "filelist": "^1.0.4", + "minimatch": "^3.1.2" + }, + "bin": { + "jake": "bin/cli.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/jake/node_modules/brace-expansion": { + "version": "1.1.12", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.12.tgz", + "integrity": "sha512-9T9UjW3r0UW5c1Q7GTwllptXwhvYmEzFhzMfZ9H7FQWt+uZePjZPjBP/W1ZEyZ1twGWom5/56TF4lPcqjnDHcg==", + "dev": true, + "license": "MIT", + "dependencies": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "node_modules/jake/node_modules/minimatch": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", + "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", + "dev": true, + "license": "ISC", + "dependencies": { + "brace-expansion": "^1.1.7" + }, + "engines": { + "node": "*" + } + }, "node_modules/jest": { "version": "30.0.4", "resolved": "https://registry.npmjs.org/jest/-/jest-30.0.4.tgz", @@ -3371,6 +3474,13 @@ "node": ">=8" } }, + "node_modules/lodash.memoize": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-4.1.2.tgz", + "integrity": "sha512-t7j+NzmgnQzTAYXcsHYLgimltOV1MXHtlOWf6GjL9Kj8GK5FInw5JotxvbOs+IvV1/Dzo04/fCGfLVs7aXb4Ag==", + "dev": true, + "license": "MIT" + }, "node_modules/lru-cache": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz", @@ -4221,6 +4331,85 @@ "node": ">=8.0" } }, + "node_modules/ts-jest": { + "version": "29.4.0", + "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-29.4.0.tgz", + "integrity": "sha512-d423TJMnJGu80/eSgfQ5w/R+0zFJvdtTxwtF9KzFFunOpSeD+79lHJQIiAhluJoyGRbvj9NZJsl9WjCUo0ND7Q==", + "dev": true, + "license": "MIT", + "dependencies": { + "bs-logger": "^0.2.6", + "ejs": "^3.1.10", + "fast-json-stable-stringify": "^2.1.0", + "json5": "^2.2.3", + "lodash.memoize": "^4.1.2", + "make-error": "^1.3.6", + "semver": "^7.7.2", + "type-fest": "^4.41.0", + "yargs-parser": "^21.1.1" + }, + "bin": { + "ts-jest": "cli.js" + }, + "engines": { + "node": "^14.15.0 || ^16.10.0 || ^18.0.0 || >=20.0.0" + }, + "peerDependencies": { + "@babel/core": ">=7.0.0-beta.0 <8", + "@jest/transform": "^29.0.0 || ^30.0.0", + "@jest/types": "^29.0.0 || ^30.0.0", + "babel-jest": "^29.0.0 || ^30.0.0", + "jest": "^29.0.0 || ^30.0.0", + "jest-util": "^29.0.0 || ^30.0.0", + "typescript": ">=4.3 <6" + }, + "peerDependenciesMeta": { + "@babel/core": { + "optional": true + }, + "@jest/transform": { + "optional": true + }, + "@jest/types": { + "optional": true + }, + "babel-jest": { + "optional": true + }, + "esbuild": { + "optional": true + }, + "jest-util": { + "optional": true + } + } + }, + "node_modules/ts-jest/node_modules/semver": { + "version": "7.7.2", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.7.2.tgz", + "integrity": "sha512-RF0Fw+rO5AMf9MAyaRXI4AV0Ulj5lMHqVxxdSgiVbixSCXoEmmX/jk0CuJw4+3SqroYO9VoUh+HcuJivvtJemA==", + "dev": true, + "license": "ISC", + "bin": { + "semver": "bin/semver.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/ts-jest/node_modules/type-fest": { + "version": "4.41.0", + "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-4.41.0.tgz", + "integrity": "sha512-TeTSQ6H5YHvpqVwBRcnLDCBnDOHWYu7IvGbHT6N8AOymcr9PJGjc1GTtiWZTYg0NCgYwvnYWEkVChQAr9bjfwA==", + "dev": true, + "license": "(MIT OR CC0-1.0)", + "engines": { + "node": ">=16" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/ts-node": { "version": "10.9.2", "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.9.2.tgz", diff --git a/package.json b/package.json index f9371ee..58402fa 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "devDependencies": { "@types/jest": "^30.0.0", "jest": "^30.0.4", + "ts-jest": "^29.4.0", "ts-node": "^10.9.2", "typescript": "^5.8.3" } diff --git a/test/gemetry-calculation.test.ts b/test/gemetry-calculation.test.ts new file mode 100644 index 0000000..d488dca --- /dev/null +++ b/test/gemetry-calculation.test.ts @@ -0,0 +1,75 @@ +import { calculateDistance, calculateJunctionPoint, isPointOnLine } from '../modules/geometry-calculation'; +import Line from '../modules/ecs6-class/line'; +import Point from '../modules/ecs6-class/point'; + +// modules/geometry-calculation.test.ts + +describe('calculateDistance', () => { + it('מחזירה 0 עבור אותה נקודה', () => { + const p = new Point({ x: 1, y: 1 }); + expect(calculateDistance(p, p)).toBe(0); + }); + + it('מחזירה מרחק נכון בין שתי נקודות', () => { + const p1 = new Point({ x: 0, y: 0 }); + const p2 = new Point({ x: 3, y: 4 }); + expect(calculateDistance(p1, p2)).toBe(5); + }); +}); + +describe('calculateJunctionPoint', () => { + it('מחזירה true עבור קווים חופפים', () => { + const l1 = new Line({ point1: new Point({ x: 0, y: 0 }), point2: new Point({ x:1, y: 1 }) }); + l1.calculateSlope(); + l1.calculateNOfLineFunction(); + const l2 = new Line({ point1: new Point({ x: 2, y: 2 }), point2: new Point({ x: 3, y: 3 }) }); + l2.calculateSlope(); + l2.calculateNOfLineFunction(); + expect(calculateJunctionPoint(l1, l2)).toBe(true); + }); + + it('מחזירה false עבור קווים מקבילים', () => { + const l1 = new Line({ point1: new Point({ x: 0, y: 0 }), point2: new Point({ x: 1, y: 1 }) }); + l1.calculateSlope(); + l1.calculateNOfLineFunction(); + const l2 = new Line({ point1: new Point({ x: 0, y: 1 }), point2: new Point({ x: 1, y: 2 }) }); + l2.calculateSlope(); + l2.calculateNOfLineFunction(); + expect(calculateJunctionPoint(l1, l2)).toBe(false); + }); + + it('מחזירה נקודת חיתוך עבור קווים נחתכים', () => { + const l1 = new Line({ point1: new Point({ x: 0, y:0}), point2: new Point({ x:1, y: 1 }) }); + l1.calculateSlope(); + l1.calculateNOfLineFunction(); + const l2 = new Line({ point1: new Point({ x: 0, y: 1 }), point2: new Point({ x: 1, y: 0 }) }); + l2.calculateSlope(); + l2.calculateNOfLineFunction(); + + console.log('l1:', l1.slope, l1.n); + console.log('l2:', l2.slope, l2.n); + + const result = calculateJunctionPoint(l1, l2) as Point; + expect(result).toBeInstanceOf(Point); + expect(result.x).toBeCloseTo(0.5); + expect(result.y).toBeCloseTo(0.5); + }); +}); + +describe('isPointOnLine', () => { + it('מחזירה true עבור נקודה על הקו', () => { + const l = new Line({ point1: new Point({ x: 0, y: 0 }), point2: new Point({ x: 2, y: 2 }) }); + l.calculateSlope(); + l.calculateNOfLineFunction(); + const p = new Point({ x: 1, y: 1 }); + expect(isPointOnLine(l, p)).toBe(true); + }); + + it('מחזירה false עבור נקודה שלא על הקו', () => { + const l = new Line({ point1: new Point({ x: 0, y: 0 }), point2: new Point({ x: 2, y: 2 }) }); + l.calculateSlope(); + l.calculateNOfLineFunction(); + const p = new Point({ x: 1, y: 2 }); + expect(isPointOnLine(l, p)).toBe(false); + }); +}); \ No newline at end of file diff --git a/test/line.test.ts b/test/line.test.ts new file mode 100644 index 0000000..1825cb5 --- /dev/null +++ b/test/line.test.ts @@ -0,0 +1,72 @@ +import Line from '../modules/ecs6-class/line'; +import Point from '../modules/ecs6-class/point'; + +describe('Line', () => { + it('should create a line with two points', () => { + const p1 = new Point({ x: 0, y: 0 }); + const p2 = new Point({ x: 2, y: 2 }); + const line = new Line({ point1: p1, point2: p2 }); + expect(line.point1).toEqual(p1); + expect(line.point2).toEqual(p2); + }); + + it('should calculate the correct slope', () => { + const p1 = new Point({ x: 1, y: 2 }); + const p2 = new Point({ x: 3, y: 6 }); + const line = new Line({ point1: p1, point2: p2 }); + line.calculateSlope(); + expect(line.slope).toBeCloseTo(2); + }); + + it('should calculate the correct n (y-intercept)', () => { + const p1 = new Point({ x: 1, y: 2 }); + const p2 = new Point({ x: 3, y: 6 }); + const line = new Line({ point1: p1, point2: p2 }); + line.calculateSlope(); + line.calculateNOfLineFunction(); + expect(line.n).toBeCloseTo(0); + }); + + it('should get correct point by X', () => { + const p1 = new Point({ x: 0, y: 1 }); + const p2 = new Point({ x: 1, y: 3 }); + const line = new Line({ point1: p1, point2: p2 }); + line.calculateSlope(); + line.calculateNOfLineFunction(); + const point = line.getPointByX(2); + expect(point?.x).toBeCloseTo(2); + expect(point?.y).toBeCloseTo(5); + }); + + it('should get correct point by Y', () => { + const p1 = new Point({ x: 0, y: 1 }); + const p2 = new Point({ x: 1, y: 3 }); + const line = new Line({ point1: p1, point2: p2 }); + line.calculateSlope(); + line.calculateNOfLineFunction(); + const point = line.getPointByY(7); + expect(point?.y).toBeCloseTo(7); + expect(point?.x).toBeCloseTo(3); + }); + + it('should get point on X axis (y=0)', () => { + const p1 = new Point({ x: 0, y: 1 }); + const p2 = new Point({ x: 1, y: 3 }); + const line = new Line({ point1: p1, point2: p2 }); + line.calculateSlope(); + line.calculateNOfLineFunction(); + const point = line.getPointOnXAsis(); + expect(point?.y).toBeCloseTo(0); + }); + + it('should get point on Y axis (x=0)', () => { + const p1 = new Point({ x: 0, y: 1 }); + const p2 = new Point({ x: 1, y: 3 }); + const line = new Line({ point1: p1, point2: p2 }); + line.calculateSlope(); + line.calculateNOfLineFunction(); + const point = line.getPointOnYAsis(); + expect(point?.x).toBeCloseTo(0); + expect(point?.y).toBeCloseTo(1); + }); +}); \ No newline at end of file diff --git a/test/point.test.ts b/test/point.test.ts new file mode 100644 index 0000000..98e72ce --- /dev/null +++ b/test/point.test.ts @@ -0,0 +1,31 @@ +import Point from '../modules/ecs6-class/point'; + +describe('Point', () => { + it('should create a point with default values', () => { + const p = new Point(); + expect(p.x).toBe(0); + expect(p.y).toBe(0); + }); + + it('should create a point with given values', () => { + const p = new Point({ x: 5, y: 7 }); + expect(p.x).toBe(5); + expect(p.y).toBe(7); + }); + + it('should move vertically', () => { + const p = new Point({ x: 1, y: 2 }); + p.moveVertical(3); + expect(p.y).toBe(5); + p.moveVertical(-2); + expect(p.y).toBe(3); + }); + + it('should move horizontally', () => { + const p = new Point({ x: 4, y: 2 }); + p.moveHorizontal(6); + expect(p.x).toBe(10); + p.moveHorizontal(-3); + expect(p.x).toBe(7); + }); +}); \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..951c35d --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "target": "ES2019", + "module": "CommonJS", + "strict": true, + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "skipLibCheck": true, + "outDir": "./dist" + }, + "include": ["modules", "test"] +} From b3a5ceb93c9da14ac2573992a8095a968a7772a6 Mon Sep 17 00:00:00 2001 From: shBlu Date: Sun, 13 Jul 2025 17:13:47 +0300 Subject: [PATCH 2/6] finish task 1 --- modules/ecs6-class/line.ts | 28 +++++++++++-------- package.json | 3 +- ...n.test.ts => geometry-calculation.test.ts} | 19 +++++-------- test/line.test.ts | 8 ------ test/point.test.ts | 12 ++++++-- 5 files changed, 36 insertions(+), 34 deletions(-) rename test/{gemetry-calculation.test.ts => geometry-calculation.test.ts} (79%) diff --git a/modules/ecs6-class/line.ts b/modules/ecs6-class/line.ts index 912b9e6..07a2e1f 100644 --- a/modules/ecs6-class/line.ts +++ b/modules/ecs6-class/line.ts @@ -15,8 +15,13 @@ export default class Line { calculateSlope() { - this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) + const dx = this.point2.x - this.point1.x; + if (dx === 0) { + this.slope = undefined; + } else { + this.slope = (this.point2.y - this.point1.y) / dx; } +} calculateNOfLineFunction() { if (this.slope !== undefined) @@ -33,19 +38,20 @@ export default class Line { getPointByX(x: number) { - if (this.slope !== undefined && this.n !== undefined) { - let y = this.slope * x + this.n - return new Point({ x, y }) - } - return undefined; + if (this.slope === undefined) this.calculateSlope(); + if (this.n === undefined) this.calculateNOfLineFunction(); + if (this.slope === undefined || this.n === undefined) return undefined; + const y = this.slope * x + this.n; + return new Point({ x, y }); } getPointByY(y: number) { - if (this.slope !== undefined && this.n !== undefined) { - let x = (y - this.n) / this.slope; - return new Point({ x, y }) - } - return undefined; + if (this.slope === undefined) this.calculateSlope(); + if (this.n === undefined) this.calculateNOfLineFunction(); + if (this.slope === 0) return undefined; // קו אופקי, אין x יחיד + if (this.slope === undefined || this.n === undefined) return undefined; + const x = (y - this.n) / this.slope; + return new Point({ x, y }); } diff --git a/package.json b/package.json index 58402fa..a373297 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "", "main": "index.js", "scripts": { - "test": "jest" + "test": "jest", + "coverage": "jest --coverage" }, "repository": { "type": "git", diff --git a/test/gemetry-calculation.test.ts b/test/geometry-calculation.test.ts similarity index 79% rename from test/gemetry-calculation.test.ts rename to test/geometry-calculation.test.ts index d488dca..8874471 100644 --- a/test/gemetry-calculation.test.ts +++ b/test/geometry-calculation.test.ts @@ -2,15 +2,13 @@ import { calculateDistance, calculateJunctionPoint, isPointOnLine } from '../mod import Line from '../modules/ecs6-class/line'; import Point from '../modules/ecs6-class/point'; -// modules/geometry-calculation.test.ts - describe('calculateDistance', () => { - it('מחזירה 0 עבור אותה נקודה', () => { + it('returns 0 for the same point', () => { const p = new Point({ x: 1, y: 1 }); expect(calculateDistance(p, p)).toBe(0); }); - it('מחזירה מרחק נכון בין שתי נקודות', () => { + it('returns correct distance between two points', () => { const p1 = new Point({ x: 0, y: 0 }); const p2 = new Point({ x: 3, y: 4 }); expect(calculateDistance(p1, p2)).toBe(5); @@ -18,7 +16,7 @@ describe('calculateDistance', () => { }); describe('calculateJunctionPoint', () => { - it('מחזירה true עבור קווים חופפים', () => { + it('returns true for overlapping lines', () => { const l1 = new Line({ point1: new Point({ x: 0, y: 0 }), point2: new Point({ x:1, y: 1 }) }); l1.calculateSlope(); l1.calculateNOfLineFunction(); @@ -28,7 +26,7 @@ describe('calculateJunctionPoint', () => { expect(calculateJunctionPoint(l1, l2)).toBe(true); }); - it('מחזירה false עבור קווים מקבילים', () => { + it('returns false for parallel lines', () => { const l1 = new Line({ point1: new Point({ x: 0, y: 0 }), point2: new Point({ x: 1, y: 1 }) }); l1.calculateSlope(); l1.calculateNOfLineFunction(); @@ -38,7 +36,7 @@ describe('calculateJunctionPoint', () => { expect(calculateJunctionPoint(l1, l2)).toBe(false); }); - it('מחזירה נקודת חיתוך עבור קווים נחתכים', () => { + it('returns intersection point for intersecting lines', () => { const l1 = new Line({ point1: new Point({ x: 0, y:0}), point2: new Point({ x:1, y: 1 }) }); l1.calculateSlope(); l1.calculateNOfLineFunction(); @@ -46,9 +44,6 @@ describe('calculateJunctionPoint', () => { l2.calculateSlope(); l2.calculateNOfLineFunction(); - console.log('l1:', l1.slope, l1.n); - console.log('l2:', l2.slope, l2.n); - const result = calculateJunctionPoint(l1, l2) as Point; expect(result).toBeInstanceOf(Point); expect(result.x).toBeCloseTo(0.5); @@ -57,7 +52,7 @@ describe('calculateJunctionPoint', () => { }); describe('isPointOnLine', () => { - it('מחזירה true עבור נקודה על הקו', () => { + it('returns true for a point on the line', () => { const l = new Line({ point1: new Point({ x: 0, y: 0 }), point2: new Point({ x: 2, y: 2 }) }); l.calculateSlope(); l.calculateNOfLineFunction(); @@ -65,7 +60,7 @@ describe('isPointOnLine', () => { expect(isPointOnLine(l, p)).toBe(true); }); - it('מחזירה false עבור נקודה שלא על הקו', () => { + it('returns false for a point not on the line', () => { const l = new Line({ point1: new Point({ x: 0, y: 0 }), point2: new Point({ x: 2, y: 2 }) }); l.calculateSlope(); l.calculateNOfLineFunction(); diff --git a/test/line.test.ts b/test/line.test.ts index 1825cb5..7e7fdd4 100644 --- a/test/line.test.ts +++ b/test/line.test.ts @@ -31,8 +31,6 @@ describe('Line', () => { const p1 = new Point({ x: 0, y: 1 }); const p2 = new Point({ x: 1, y: 3 }); const line = new Line({ point1: p1, point2: p2 }); - line.calculateSlope(); - line.calculateNOfLineFunction(); const point = line.getPointByX(2); expect(point?.x).toBeCloseTo(2); expect(point?.y).toBeCloseTo(5); @@ -42,8 +40,6 @@ describe('Line', () => { const p1 = new Point({ x: 0, y: 1 }); const p2 = new Point({ x: 1, y: 3 }); const line = new Line({ point1: p1, point2: p2 }); - line.calculateSlope(); - line.calculateNOfLineFunction(); const point = line.getPointByY(7); expect(point?.y).toBeCloseTo(7); expect(point?.x).toBeCloseTo(3); @@ -53,8 +49,6 @@ describe('Line', () => { const p1 = new Point({ x: 0, y: 1 }); const p2 = new Point({ x: 1, y: 3 }); const line = new Line({ point1: p1, point2: p2 }); - line.calculateSlope(); - line.calculateNOfLineFunction(); const point = line.getPointOnXAsis(); expect(point?.y).toBeCloseTo(0); }); @@ -63,8 +57,6 @@ describe('Line', () => { const p1 = new Point({ x: 0, y: 1 }); const p2 = new Point({ x: 1, y: 3 }); const line = new Line({ point1: p1, point2: p2 }); - line.calculateSlope(); - line.calculateNOfLineFunction(); const point = line.getPointOnYAsis(); expect(point?.x).toBeCloseTo(0); expect(point?.y).toBeCloseTo(1); diff --git a/test/point.test.ts b/test/point.test.ts index 98e72ce..564f230 100644 --- a/test/point.test.ts +++ b/test/point.test.ts @@ -13,18 +13,26 @@ describe('Point', () => { expect(p.y).toBe(7); }); - it('should move vertically', () => { + it('should move vertically up', () => { const p = new Point({ x: 1, y: 2 }); p.moveVertical(3); expect(p.y).toBe(5); + }); + + it('should move vertically down', () => { + const p = new Point({ x: 1, y: 5 }); p.moveVertical(-2); expect(p.y).toBe(3); }); - it('should move horizontally', () => { + it('should move horizontally right', () => { const p = new Point({ x: 4, y: 2 }); p.moveHorizontal(6); expect(p.x).toBe(10); + }); + + it('should move horizontally left', () => { + const p = new Point({ x: 10, y: 2 }); p.moveHorizontal(-3); expect(p.x).toBe(7); }); From fced74e0622390913cbe7ec01f60e430d5fb7027 Mon Sep 17 00:00:00 2001 From: brachy vacshtoc Date: Mon, 14 Jul 2025 13:09:06 +0300 Subject: [PATCH 3/6] Correcting mistakes --- modules/ecs6-class/line.ts | 1 + test/line.test.ts | 8 +++++++ test/point.test.ts | 46 ++++++++++++++++++++------------------ 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/modules/ecs6-class/line.ts b/modules/ecs6-class/line.ts index 07a2e1f..a89f1eb 100644 --- a/modules/ecs6-class/line.ts +++ b/modules/ecs6-class/line.ts @@ -21,6 +21,7 @@ export default class Line { } else { this.slope = (this.point2.y - this.point1.y) / dx; } + return this.slope; } calculateNOfLineFunction() { diff --git a/test/line.test.ts b/test/line.test.ts index 7e7fdd4..1825cb5 100644 --- a/test/line.test.ts +++ b/test/line.test.ts @@ -31,6 +31,8 @@ describe('Line', () => { const p1 = new Point({ x: 0, y: 1 }); const p2 = new Point({ x: 1, y: 3 }); const line = new Line({ point1: p1, point2: p2 }); + line.calculateSlope(); + line.calculateNOfLineFunction(); const point = line.getPointByX(2); expect(point?.x).toBeCloseTo(2); expect(point?.y).toBeCloseTo(5); @@ -40,6 +42,8 @@ describe('Line', () => { const p1 = new Point({ x: 0, y: 1 }); const p2 = new Point({ x: 1, y: 3 }); const line = new Line({ point1: p1, point2: p2 }); + line.calculateSlope(); + line.calculateNOfLineFunction(); const point = line.getPointByY(7); expect(point?.y).toBeCloseTo(7); expect(point?.x).toBeCloseTo(3); @@ -49,6 +53,8 @@ describe('Line', () => { const p1 = new Point({ x: 0, y: 1 }); const p2 = new Point({ x: 1, y: 3 }); const line = new Line({ point1: p1, point2: p2 }); + line.calculateSlope(); + line.calculateNOfLineFunction(); const point = line.getPointOnXAsis(); expect(point?.y).toBeCloseTo(0); }); @@ -57,6 +63,8 @@ describe('Line', () => { const p1 = new Point({ x: 0, y: 1 }); const p2 = new Point({ x: 1, y: 3 }); const line = new Line({ point1: p1, point2: p2 }); + line.calculateSlope(); + line.calculateNOfLineFunction(); const point = line.getPointOnYAsis(); expect(point?.x).toBeCloseTo(0); expect(point?.y).toBeCloseTo(1); diff --git a/test/point.test.ts b/test/point.test.ts index 564f230..497c541 100644 --- a/test/point.test.ts +++ b/test/point.test.ts @@ -2,38 +2,40 @@ import Point from '../modules/ecs6-class/point'; describe('Point', () => { it('should create a point with default values', () => { - const p = new Point(); - expect(p.x).toBe(0); - expect(p.y).toBe(0); + const point = new Point(); + expect(point.x).toBe(0); + expect(point.y).toBe(0); }); it('should create a point with given values', () => { - const p = new Point({ x: 5, y: 7 }); - expect(p.x).toBe(5); - expect(p.y).toBe(7); + const point = new Point({ x: 5, y: 10 }); + expect(point.x).toBe(5); + expect(point.y).toBe(10); }); +}); - it('should move vertically up', () => { - const p = new Point({ x: 1, y: 2 }); - p.moveVertical(3); - expect(p.y).toBe(5); +describe('Point Movement', () => { + it('should move the point vertically', () => { + const point = new Point({ x: 3, y: 4 }); + point.moveVertical(2); + expect(point.y).toBe(6); }); - it('should move vertically down', () => { - const p = new Point({ x: 1, y: 5 }); - p.moveVertical(-2); - expect(p.y).toBe(3); + it('should move the point vertically downwards', () => { + const point = new Point({ x: 3, y: 4 }); + point.moveVertical(-3); + expect(point.y).toBe(1); }); - it('should move horizontally right', () => { - const p = new Point({ x: 4, y: 2 }); - p.moveHorizontal(6); - expect(p.x).toBe(10); + it('should move the point horizontally to the right', () => { + const point = new Point({ x: 3, y: 4 }); + point.moveHorizontal(3); + expect(point.x).toBe(6); }); - it('should move horizontally left', () => { - const p = new Point({ x: 10, y: 2 }); - p.moveHorizontal(-3); - expect(p.x).toBe(7); + it('should move the point horizontally to the left', () => { + const point = new Point({ x: 3, y: 4 }); + point.moveHorizontal(-2); + expect(point.x).toBe(1); }); }); \ No newline at end of file From b7a33239f2466f1c8ca4fc1e3460264ad7fc505d Mon Sep 17 00:00:00 2001 From: brachy Date: Mon, 14 Jul 2025 14:40:05 +0300 Subject: [PATCH 4/6] fix 2 functions --- modules/ecs6-class/line.ts | 3 ++- test/geometry-calculation.test.ts | 8 -------- test/line.test.ts | 5 ----- 3 files changed, 2 insertions(+), 14 deletions(-) diff --git a/modules/ecs6-class/line.ts b/modules/ecs6-class/line.ts index a89f1eb..58dd3ce 100644 --- a/modules/ecs6-class/line.ts +++ b/modules/ecs6-class/line.ts @@ -25,6 +25,7 @@ export default class Line { } calculateNOfLineFunction() { + this.calculateSlope(); if (this.slope !== undefined) this.n = this.point1.y - this.slope * this.point1.x } @@ -49,7 +50,7 @@ export default class Line { getPointByY(y: number) { if (this.slope === undefined) this.calculateSlope(); if (this.n === undefined) this.calculateNOfLineFunction(); - if (this.slope === 0) return undefined; // קו אופקי, אין x יחיד + if (this.slope === 0) return undefined; if (this.slope === undefined || this.n === undefined) return undefined; const x = (y - this.n) / this.slope; return new Point({ x, y }); diff --git a/test/geometry-calculation.test.ts b/test/geometry-calculation.test.ts index 8874471..ee6815a 100644 --- a/test/geometry-calculation.test.ts +++ b/test/geometry-calculation.test.ts @@ -18,30 +18,24 @@ describe('calculateDistance', () => { describe('calculateJunctionPoint', () => { it('returns true for overlapping lines', () => { const l1 = new Line({ point1: new Point({ x: 0, y: 0 }), point2: new Point({ x:1, y: 1 }) }); - l1.calculateSlope(); l1.calculateNOfLineFunction(); const l2 = new Line({ point1: new Point({ x: 2, y: 2 }), point2: new Point({ x: 3, y: 3 }) }); - l2.calculateSlope(); l2.calculateNOfLineFunction(); expect(calculateJunctionPoint(l1, l2)).toBe(true); }); it('returns false for parallel lines', () => { const l1 = new Line({ point1: new Point({ x: 0, y: 0 }), point2: new Point({ x: 1, y: 1 }) }); - l1.calculateSlope(); l1.calculateNOfLineFunction(); const l2 = new Line({ point1: new Point({ x: 0, y: 1 }), point2: new Point({ x: 1, y: 2 }) }); - l2.calculateSlope(); l2.calculateNOfLineFunction(); expect(calculateJunctionPoint(l1, l2)).toBe(false); }); it('returns intersection point for intersecting lines', () => { const l1 = new Line({ point1: new Point({ x: 0, y:0}), point2: new Point({ x:1, y: 1 }) }); - l1.calculateSlope(); l1.calculateNOfLineFunction(); const l2 = new Line({ point1: new Point({ x: 0, y: 1 }), point2: new Point({ x: 1, y: 0 }) }); - l2.calculateSlope(); l2.calculateNOfLineFunction(); const result = calculateJunctionPoint(l1, l2) as Point; @@ -54,7 +48,6 @@ describe('calculateJunctionPoint', () => { describe('isPointOnLine', () => { it('returns true for a point on the line', () => { const l = new Line({ point1: new Point({ x: 0, y: 0 }), point2: new Point({ x: 2, y: 2 }) }); - l.calculateSlope(); l.calculateNOfLineFunction(); const p = new Point({ x: 1, y: 1 }); expect(isPointOnLine(l, p)).toBe(true); @@ -62,7 +55,6 @@ describe('isPointOnLine', () => { it('returns false for a point not on the line', () => { const l = new Line({ point1: new Point({ x: 0, y: 0 }), point2: new Point({ x: 2, y: 2 }) }); - l.calculateSlope(); l.calculateNOfLineFunction(); const p = new Point({ x: 1, y: 2 }); expect(isPointOnLine(l, p)).toBe(false); diff --git a/test/line.test.ts b/test/line.test.ts index 1825cb5..dc1d10e 100644 --- a/test/line.test.ts +++ b/test/line.test.ts @@ -22,7 +22,6 @@ describe('Line', () => { const p1 = new Point({ x: 1, y: 2 }); const p2 = new Point({ x: 3, y: 6 }); const line = new Line({ point1: p1, point2: p2 }); - line.calculateSlope(); line.calculateNOfLineFunction(); expect(line.n).toBeCloseTo(0); }); @@ -31,7 +30,6 @@ describe('Line', () => { const p1 = new Point({ x: 0, y: 1 }); const p2 = new Point({ x: 1, y: 3 }); const line = new Line({ point1: p1, point2: p2 }); - line.calculateSlope(); line.calculateNOfLineFunction(); const point = line.getPointByX(2); expect(point?.x).toBeCloseTo(2); @@ -42,7 +40,6 @@ describe('Line', () => { const p1 = new Point({ x: 0, y: 1 }); const p2 = new Point({ x: 1, y: 3 }); const line = new Line({ point1: p1, point2: p2 }); - line.calculateSlope(); line.calculateNOfLineFunction(); const point = line.getPointByY(7); expect(point?.y).toBeCloseTo(7); @@ -53,7 +50,6 @@ describe('Line', () => { const p1 = new Point({ x: 0, y: 1 }); const p2 = new Point({ x: 1, y: 3 }); const line = new Line({ point1: p1, point2: p2 }); - line.calculateSlope(); line.calculateNOfLineFunction(); const point = line.getPointOnXAsis(); expect(point?.y).toBeCloseTo(0); @@ -63,7 +59,6 @@ describe('Line', () => { const p1 = new Point({ x: 0, y: 1 }); const p2 = new Point({ x: 1, y: 3 }); const line = new Line({ point1: p1, point2: p2 }); - line.calculateSlope(); line.calculateNOfLineFunction(); const point = line.getPointOnYAsis(); expect(point?.x).toBeCloseTo(0); From e0f4230aca7afce3b346735350dc3ffdaf4f7a59 Mon Sep 17 00:00:00 2001 From: brachy Date: Mon, 14 Jul 2025 14:58:44 +0300 Subject: [PATCH 5/6] The latest patch --- modules/ecs6-class/line.ts | 2 ++ modules/geometry-calculation.ts | 3 +++ test/geometry-calculation.test.ts | 8 -------- test/line.test.ts | 4 ---- 4 files changed, 5 insertions(+), 12 deletions(-) diff --git a/modules/ecs6-class/line.ts b/modules/ecs6-class/line.ts index 58dd3ce..b52b0ce 100644 --- a/modules/ecs6-class/line.ts +++ b/modules/ecs6-class/line.ts @@ -31,10 +31,12 @@ export default class Line { } getPointOnXAsis() { + this.calculateNOfLineFunction(); return this.getPointByY(0) } getPointOnYAsis() { + this.calculateNOfLineFunction(); return this.getPointByX(0) } diff --git a/modules/geometry-calculation.ts b/modules/geometry-calculation.ts index 4bbbea7..315db6e 100644 --- a/modules/geometry-calculation.ts +++ b/modules/geometry-calculation.ts @@ -9,6 +9,8 @@ export const calculateDistance = (point1: Point, point2: Point): number => { } export const calculateJunctionPoint = (line1: Line, line2: Line): Boolean | Point | undefined => { + line1.calculateNOfLineFunction(); + line2.calculateNOfLineFunction(); if (line1.slope === line2.slope) { if (line1.n === line2.n) { return true @@ -28,6 +30,7 @@ export const calculateJunctionPoint = (line1: Line, line2: Line): Boolean | Poin } export const isPointOnLine = (line: Line, point: Point): Boolean => { + line.calculateNOfLineFunction(); const proxyLine = new Line({ point1: line.point1, point2: point }) proxyLine.calculateSlope() if (line.slope === proxyLine.slope) { diff --git a/test/geometry-calculation.test.ts b/test/geometry-calculation.test.ts index ee6815a..ad7f4f7 100644 --- a/test/geometry-calculation.test.ts +++ b/test/geometry-calculation.test.ts @@ -18,25 +18,19 @@ describe('calculateDistance', () => { describe('calculateJunctionPoint', () => { it('returns true for overlapping lines', () => { const l1 = new Line({ point1: new Point({ x: 0, y: 0 }), point2: new Point({ x:1, y: 1 }) }); - l1.calculateNOfLineFunction(); const l2 = new Line({ point1: new Point({ x: 2, y: 2 }), point2: new Point({ x: 3, y: 3 }) }); - l2.calculateNOfLineFunction(); expect(calculateJunctionPoint(l1, l2)).toBe(true); }); it('returns false for parallel lines', () => { const l1 = new Line({ point1: new Point({ x: 0, y: 0 }), point2: new Point({ x: 1, y: 1 }) }); - l1.calculateNOfLineFunction(); const l2 = new Line({ point1: new Point({ x: 0, y: 1 }), point2: new Point({ x: 1, y: 2 }) }); - l2.calculateNOfLineFunction(); expect(calculateJunctionPoint(l1, l2)).toBe(false); }); it('returns intersection point for intersecting lines', () => { const l1 = new Line({ point1: new Point({ x: 0, y:0}), point2: new Point({ x:1, y: 1 }) }); - l1.calculateNOfLineFunction(); const l2 = new Line({ point1: new Point({ x: 0, y: 1 }), point2: new Point({ x: 1, y: 0 }) }); - l2.calculateNOfLineFunction(); const result = calculateJunctionPoint(l1, l2) as Point; expect(result).toBeInstanceOf(Point); @@ -48,14 +42,12 @@ describe('calculateJunctionPoint', () => { describe('isPointOnLine', () => { it('returns true for a point on the line', () => { const l = new Line({ point1: new Point({ x: 0, y: 0 }), point2: new Point({ x: 2, y: 2 }) }); - l.calculateNOfLineFunction(); const p = new Point({ x: 1, y: 1 }); expect(isPointOnLine(l, p)).toBe(true); }); it('returns false for a point not on the line', () => { const l = new Line({ point1: new Point({ x: 0, y: 0 }), point2: new Point({ x: 2, y: 2 }) }); - l.calculateNOfLineFunction(); const p = new Point({ x: 1, y: 2 }); expect(isPointOnLine(l, p)).toBe(false); }); diff --git a/test/line.test.ts b/test/line.test.ts index dc1d10e..bb51f05 100644 --- a/test/line.test.ts +++ b/test/line.test.ts @@ -30,7 +30,6 @@ describe('Line', () => { const p1 = new Point({ x: 0, y: 1 }); const p2 = new Point({ x: 1, y: 3 }); const line = new Line({ point1: p1, point2: p2 }); - line.calculateNOfLineFunction(); const point = line.getPointByX(2); expect(point?.x).toBeCloseTo(2); expect(point?.y).toBeCloseTo(5); @@ -40,7 +39,6 @@ describe('Line', () => { const p1 = new Point({ x: 0, y: 1 }); const p2 = new Point({ x: 1, y: 3 }); const line = new Line({ point1: p1, point2: p2 }); - line.calculateNOfLineFunction(); const point = line.getPointByY(7); expect(point?.y).toBeCloseTo(7); expect(point?.x).toBeCloseTo(3); @@ -50,7 +48,6 @@ describe('Line', () => { const p1 = new Point({ x: 0, y: 1 }); const p2 = new Point({ x: 1, y: 3 }); const line = new Line({ point1: p1, point2: p2 }); - line.calculateNOfLineFunction(); const point = line.getPointOnXAsis(); expect(point?.y).toBeCloseTo(0); }); @@ -59,7 +56,6 @@ describe('Line', () => { const p1 = new Point({ x: 0, y: 1 }); const p2 = new Point({ x: 1, y: 3 }); const line = new Line({ point1: p1, point2: p2 }); - line.calculateNOfLineFunction(); const point = line.getPointOnYAsis(); expect(point?.x).toBeCloseTo(0); expect(point?.y).toBeCloseTo(1); From 339e099429ddff310f67a7624d5c2b8d01888851 Mon Sep 17 00:00:00 2001 From: brachy vacshtoc Date: Tue, 15 Jul 2025 13:36:00 +0300 Subject: [PATCH 6/6] push Change --- modules/ecs6-class/line.ts | 75 +++++++++++++++++++------------------- test/line.test.ts | 71 ++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 37 deletions(-) diff --git a/modules/ecs6-class/line.ts b/modules/ecs6-class/line.ts index b52b0ce..82a44a7 100644 --- a/modules/ecs6-class/line.ts +++ b/modules/ecs6-class/line.ts @@ -1,62 +1,63 @@ import Point from "./point"; export default class Line { - point1: Point - point2: Point - n: number | undefined - slope: number | undefined + point1: Point; + point2: Point; + n: number | undefined; + slope: number | undefined; + constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }: { point1?: Point, point2?: Point, n?: number, slope?: number } = {}) { this.point1 = point1; this.point2 = point2; this.slope = slope; this.n = n; } - - - + setPoints(p1: Point, p2: Point) { + this.point1 = p1; + this.point2 = p2; + } calculateSlope() { - const dx = this.point2.x - this.point1.x; - if (dx === 0) { - this.slope = undefined; - } else { - this.slope = (this.point2.y - this.point1.y) / dx; - } - return this.slope; -} + const dx = this.point2.x - this.point1.x; + if (dx === 0) { + this.slope = undefined; + } else { + this.slope = (this.point2.y - this.point1.y) / dx; + } + return this.slope; + } calculateNOfLineFunction() { this.calculateSlope(); - if (this.slope !== undefined) - this.n = this.point1.y - this.slope * this.point1.x -} + if (this.slope !== undefined) { + this.n = this.point1.y - this.slope * this.point1.x; + } else { + this.n = undefined; + } + } getPointOnXAsis() { this.calculateNOfLineFunction(); - return this.getPointByY(0) + return this.getPointByY(0); } getPointOnYAsis() { this.calculateNOfLineFunction(); - return this.getPointByX(0) + return this.getPointByX(0); } - getPointByX(x: number) { - if (this.slope === undefined) this.calculateSlope(); - if (this.n === undefined) this.calculateNOfLineFunction(); - if (this.slope === undefined || this.n === undefined) return undefined; - const y = this.slope * x + this.n; - return new Point({ x, y }); -} + if (this.slope === undefined) this.calculateSlope(); + if (this.n === undefined) this.calculateNOfLineFunction(); + if (this.slope === undefined || this.n === undefined) return undefined; + const y = this.slope * x + this.n; + return new Point({ x, y }); + } getPointByY(y: number) { - if (this.slope === undefined) this.calculateSlope(); - if (this.n === undefined) this.calculateNOfLineFunction(); - if (this.slope === 0) return undefined; - if (this.slope === undefined || this.n === undefined) return undefined; - const x = (y - this.n) / this.slope; - return new Point({ x, y }); -} - - -} + if (this.slope === undefined) this.calculateSlope(); + if (this.n === undefined) this.calculateNOfLineFunction(); + if (this.slope === undefined || this.n === undefined) return undefined; + const x = (y - this.n) / this.slope; + return new Point({ x, y }); + } +} \ No newline at end of file diff --git a/test/line.test.ts b/test/line.test.ts index bb51f05..453754b 100644 --- a/test/line.test.ts +++ b/test/line.test.ts @@ -1,7 +1,78 @@ import Line from '../modules/ecs6-class/line'; import Point from '../modules/ecs6-class/point'; +jest.mock('../modules/ecs6-class/line', () => { + return jest.fn().mockImplementation(() => { + const instance = { + point1: new Point({ x: 0, y: 0 }), + point2: new Point({ x: 2, y: 2 }), + slope: undefined, + n: undefined, + setPoints: jest.fn(function (p1: Point, p2: Point) { + this.point1 = p1; + this.point2 = p2; + }), + calculateSlope: jest.fn(function () { + const dx = this.point2.x - this.point1.x; + if (dx === 0) { + this.slope = undefined; + } else { + this.slope = (this.point2.y - this.point1.y) / dx; + } + return this.slope; + }), + calculateNOfLineFunction: jest.fn(function () { + if (this.slope === undefined) { + this.calculateSlope(); + } + if (this.slope !== undefined) { + this.n = this.point1.y - this.slope * this.point1.x; + } + }), + getPointByX: jest.fn(function (x) { + if (this.slope === undefined) this.calculateSlope(); + if (this.n === undefined) this.calculateNOfLineFunction(); + if (this.slope === undefined || this.n === undefined) return undefined; + const y = this.slope * x + this.n; + return new Point({ x, y }); + }), + getPointByY: jest.fn(function (y) { + if (this.slope === undefined) this.calculateSlope(); + if (this.n === undefined) this.calculateNOfLineFunction(); + if (this.slope === undefined || this.n === undefined) return undefined; + const x = (y - this.n) / this.slope; + return new Point({ x, y }); + }), + getPointOnXAsis: jest.fn(function () { + this.calculateNOfLineFunction(); + return this.getPointByY(0); + }), + getPointOnYAsis: jest.fn(function () { + this.calculateNOfLineFunction(); + return this.getPointByX(0); + }), + }; + return instance; + }); +}); + describe('Line', () => { + let mockLine: Line; + beforeEach(() => { + mockLine = new Line(); + mockLine.setPoints(new Point({ x: 0, y: 0 }), new Point({ x: 2, y: 2 })); + }); + + it('should mock calculateSlope', () => { + const slope = mockLine.calculateSlope(); + expect(slope).toBe(1); + }); + + it('should mock calculateNOfLineFunction', () => { + mockLine.calculateNOfLineFunction(); + expect(mockLine.n).toBe(0); + }); + it('should create a line with two points', () => { const p1 = new Point({ x: 0, y: 0 }); const p2 = new Point({ x: 2, y: 2 });