diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index 826c675..263204c 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -2,6 +2,8 @@ const Point = require("./point"); class Line { constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) { + + this.point1 = point1; this.point2 = point2; this.slope = slope; @@ -13,10 +15,18 @@ class Line { } calculateNOfLineFunction = () => { + + if (this.slope === undefined) { + throw new Error("the slope must be defined") + } + if (typeof (this.slope) !== "number") { + throw new Error("the slope must be number") + } this.n = this.point1.y - this.slope * this.point1.x } getPointOnXAsis() { + return this.getPointByY(0) } @@ -26,13 +36,38 @@ class Line { getPointByX(x) { + if (x === undefined) { + throw new Error("the function getPointByX must get x") + } + if (typeof (x) !== "number") { + throw new Error("the function getPointByX must get x of type number") + } + if (this.slope === undefined) { + throw new Error("slope must be defined") + } + if (typeof (this.slope) !== "number") { + throw new Error("slope must of type number") + } + if (this.n === undefined) { + throw new Error("n must be defined") + } + if (typeof (this.n) !== "number") { + throw new Error("n must be number") + } let y = this.slope * x + this.n - return new Point({ x, y }) + return new Point({ x: x, y: y }) } getPointByY(y) { + if (y === undefined) { + throw new Error("the function getPointByY must get y") + } + if (typeof (y) !== "number") { + throw new Error("the function getPointByY must get y of type number") + } + let x = (y - this.n) / this.slope; - return new Point({ x, y }) + return new Point({ x: x, y: y }) } } diff --git a/modules/ecs6-class/point.js b/modules/ecs6-class/point.js index e81b4a4..a4f4679 100644 --- a/modules/ecs6-class/point.js +++ b/modules/ecs6-class/point.js @@ -1,14 +1,33 @@ class Point { - constructor({x=0, y=0}={}) { + constructor({x=0,y=0}={}){ + if (typeof (x) !== "number" || typeof (y) !== "number") { + throw new Error("the argument must be type number") + } this.x = x; this.y = y; } moveVertical(value) { + if (!value) { + throw new Error("the function must get argument") + } + + + if (typeof (value) !== "number") { + throw new Error("the argument must be type number") + } + this.y += value; } moveHorizontal(value) { + if (!value) { + throw new Error("the function must get argument") + } + if (typeof (value) !== "number") { + throw new Error("the argument must be type number") + } this.x += value; } } + module.exports = Point \ No newline at end of file diff --git a/modules/geometry-calculation.js b/modules/geometry-calculation.js index 6e11643..76607dc 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -1,13 +1,27 @@ -const Line = require('./ecs6-class/line') +const Line = require('./ecs6-class/line'); +const Point = require('./ecs6-class/point'); const calculateDistance = (point1, point2) => { + if (point1 == undefined || point2 == undefined) { + throw new Error("the function calculateDistance must get 2 points") + } + if (!(point1 instanceof Point) || !(point2 instanceof Point)) { + throw new Error("the function calculateDistance must get 2 points of type Point") + } 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 == undefined || line2 == undefined) + throw new Error("the function calculateJunctionPoint must get 2 lines") + + if (!(line1 instanceof Line) || !(line2 instanceof Line)) { + throw new Error("the function calculateJunctionPoint must get 2 lines of type Line") + } + if (line1.slope === line2.slope) { if (line1.n === line2.n) { return true @@ -24,10 +38,12 @@ const calculateJunctionPoint = (line1, line2) => { } const isPointOnLine = (line, point) => { - const proxyLine = new Line({ point1: line.point1, point2: point }) + + const proxyLine = new Line({ point1: line.point1, point2: point, n: undefined, slope: undefined }) proxyLine.calculateSlope() if (line.slope === proxyLine.slope) { proxyLine.calculateNOfLineFunction() + if (line.n === proxyLine.n) { return true } @@ -35,6 +51,7 @@ const isPointOnLine = (line, point) => { return false } + module.exports = { calculateDistance, calculateJunctionPoint, diff --git a/package-lock.json b/package-lock.json index 61f4a09..e6f6f63 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1053,10 +1053,11 @@ } }, "node_modules/braces": { - "version": "3.0.2", - "license": "MIT", + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.3.tgz", + "integrity": "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==", "dependencies": { - "fill-range": "^7.0.1" + "fill-range": "^7.1.1" }, "engines": { "node": ">=8" @@ -1401,8 +1402,9 @@ } }, "node_modules/fill-range": { - "version": "7.0.1", - "license": "MIT", + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", + "integrity": "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==", "dependencies": { "to-regex-range": "^5.0.1" }, @@ -1586,7 +1588,8 @@ }, "node_modules/is-number": { "version": "7.0.0", - "license": "MIT", + "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", + "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", "engines": { "node": ">=0.12.0" } @@ -2803,7 +2806,8 @@ }, "node_modules/to-regex-range": { "version": "5.0.1", - "license": "MIT", + "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", + "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", "dependencies": { "is-number": "^7.0.0" }, diff --git a/package.json b/package.json index 56bf17b..b1bf136 100644 --- a/package.json +++ b/package.json @@ -4,9 +4,13 @@ "description": "practice unit tests in javascript", "main": "index.js", "scripts": { - "test": "jest" + "test": "jest", + "test:coverage":"jest --coverage" }, "dependencies": { + + }, + "devDependencies": { "jest": "^29.7.0" }, "repository": { diff --git a/test/line.test b/test/line.test new file mode 100644 index 0000000..e69de29 diff --git a/test/modules/ecs6-class/line.test.js b/test/modules/ecs6-class/line.test.js new file mode 100644 index 0000000..776aa89 --- /dev/null +++ b/test/modules/ecs6-class/line.test.js @@ -0,0 +1,147 @@ +const { default: expect } = require("expect") +const Line = require("../../../modules/ecs6-class/line") +const Point = require("../../../modules/ecs6-class/point") + +describe("LINE_FUNCTION", function () { + describe("BUILD_LINE", function () { + it("should show the points of line after build the line", function () { + let p1 = new Point({ x: 1, y: 2 }) + let p2 = new Point({ x: 3, y: 4 }) + let point1 = new Point({ x: 5, y: 5 }) + let point2 = new Point({ x: 6, y: 6 }) + let l1 = new Line({ point1: p1, point2: p2, n: 2, slope: -3 }) + let l2 = new Line({ point1: point1, point2: point2, }) + let l3 = new Line({ point1: undefined, point2: undefined }) + expect(l3.n).toBe(undefined) + expect(l1.point1.x).toEqual(1) + expect(l1.point1.y).toEqual(2) + expect(l1.point2.x).toEqual(3) + expect(l1.point2.y).toEqual(4) + expect(l2.point1.x).toEqual(5) + expect(l2.point1.y).toEqual(5) + expect(l2.point2.x).toEqual(6) + expect(l2.point2.y).toEqual(6) + }) + + it("should show the n and slope of line after build the line", function () { + let p1 = new Point({ x: 1, y: 2 }) + let p2 = new Point({ x: 3, y: 4 }) + let point1 = new Point({ x: 5, y: 5 }) + let point2 = new Point({ x: 6, y: 6 }) + let l1 = new Line({ point1: p1, point2: p2, n: 2, slope: -3 }) + let l2 = new Line({ point1: point1, point2: point2, }) + let l3 = new Line({ point1: undefined, point2: undefined }) + expect(l1.n).toEqual(2) + expect(l1.slope).toEqual(-3) + expect(l2.n).toBe(undefined) + expect(l2.slope).toBe(undefined) + }) + }) + describe("CALCULATE_SLOPE", function () { + it("should calculate the slope between 2 points", function () { + let p1 = new Point({ x: 1, y: 2 }) + let p2 = new Point({ x: 3, y: 4 }) + let point1 = new Point({ x: 10, y: 100 }) + let point2 = new Point({ x: 5, y: 50 }) + let l1 = new Line({ point1: p1, point2: p2, n: 2, slope: -3 }) + let l2 = new Line({ point1: point1, point2: point2 }) + expect(l1.slope).toEqual(-3) + l1.calculateSlope() + l2.calculateSlope() + expect(l1.slope).toEqual(1) + expect(l2.slope).toEqual(10) + }) + }) + + describe("CALCULATE_N_OF_LINE_FUNCTION", function () { + it("should calculate the n of line function according to point1 and point2 and slope", function () { + let p1 = new Point({ x: 1, y: 2 }) + let p2 = new Point({ x: 3, y: 4 }) + let point1 = new Point({ x: 5, y: 5 }) + let point2 = new Point({ x: 6, y: 6 }) + let l1 = new Line({ point1: p1, point2: p2, n: 2, slope: -3 }) + let l2 = new Line({ point1: point1, point2: point2, }) + l1.calculateSlope() + l2.calculateSlope() + l1.calculateNOfLineFunction() + l2.calculateNOfLineFunction() + expect(l1.n).toEqual(1) + expect(l2.n).toEqual(0) + + }) + }) + + describe("GET_POINT_ON_Y_ASIS", function () { + it("should retutn a new point on y", function () { + let line1 = new Line({ point1: new Point({ x: 1, y: 6 }), point2: new Point({ x: 2, y: 3 }), n: 9, slope: -3 }) + expect(line1.getPointOnYAsis()).toStrictEqual(new Point({ x: 0, y: 9 })) + }) + }) + + describe("GET_POINT_ON_X_ASIS", function () { + it("should retutn a new point on x", function () { + let line1 = new Line({ point1: new Point({ x: 1, y: 6 }), point2: new Point({ x: 2, y: 3 }), n: 9, slope: -3 }) + expect(line1.getPointOnXAsis()).toStrictEqual(new Point({ x: 3, y: 0 })) + }) + }) + + describe("GET_POINT_BY_X", function () { + it("should calculate y of point on line according to x", function () { + let l2 = new Line({ point1: new Point({ x: 5, y: 6 }), point2: new Point({ x: 7, y: 8 }), n: 1, slope: 1 }) + expect(l2.getPointByX(1)).toEqual({ x: 1, y: 2 }) + + }) + }) + + + describe("GET_POINT_BY_Y", function () { + it("should calculate x of point on line according to y", function () { + let l2 = new Line({ point1: new Point({ x: 5, y: 6 }), point2: new Point({ x: 7, y: 8 }), n: 1, slope: 1 }) + + expect(l2.getPointByY(1)).toEqual({ x: 0, y: 1 }) + }) + }) + + describe("ERROR", function () { + let line = new Line({ point1: new Point({ x: 3, y: 7 }), point2: new Point({ x: 9, y: 7 }), n: 9 }) + let line2 = new Line({ point1: new Point({ x: 3, y: 7 }), point2: new Point({ x: 9, y: 7 }), n: 9, slope: "jskjk" }) + let line3 = new Line({ point1: new Point({ x: 3, y: 7 }), point2: new Point({ x: 9, y: 7 }), n: 9, slope: 6 }) + let line4 = new Line({ point1: "huhj", point2: "jij", n: "9", slope: "ijdi" }) + let line5 = new Line({ point1: new Point({ x: 3, y: 7 }), point2: new Point({ x: 9, y: 7 }), n: 9, slope: "jhj" }) + let line6 = new Line({ point1: new Point({ x: 3, y: 7 }), point2: new Point({ x: 9, y: 7 }), slope: 99 }) + let line7 = new Line({ point1: new Point({ x: 3, y: 7 }), point2: new Point({ x: 9, y: 7 }), n: "ghg", slope: 99 }) + + it("the slope of line must be defined", function () { + expect(() => { line.calculateNOfLineFunction() }).toThrow("the slope must be defined") + }) + + it("the slope of line must be number", function () { + expect(() => { line2.calculateNOfLineFunction() }).toThrow("the slope must be number") + }) + it("the function getPointByX must get x", function () { + expect(() => { line3.getPointByX() }).toThrow("the function getPointByX must get x") + }) + it("the function getPointByX must get x of type number", function () { + expect(() => { line3.getPointByX("iui") }).toThrow("the function getPointByX must get x of type number") + }) + it("the function getPointByX must get n of type number", function () { + expect(() => { line6.getPointByX(1) }).toThrow("n must be defined") + }) + it("the function getPointByX must get n of type number", function () { + expect(() => { line7.getPointByX(1) }).toThrow("n must be number") + }) + it(" in function getPointByX slope must be defined", function () { + expect(() => { line.getPointByX(4) }).toThrow("slope must be defined") + }) + it(" in function getPointByX slope must of type number", function () { + expect(() => { line5.getPointByX(4) }).toThrow("slope must of type number") + }) + it("the function getPointByY must get y", function () { + expect(() => { line3.getPointByY() }).toThrow("the function getPointByY must get y") + }) + it("the function getPointByY must get y of type number", function () { + expect(() => { line3.getPointByY("iui") }).toThrow("the function getPointByY must get y of type number") + }) + + }) +}) \ No newline at end of file diff --git a/test/modules/ecs6-class/point.test.js b/test/modules/ecs6-class/point.test.js new file mode 100644 index 0000000..b093252 --- /dev/null +++ b/test/modules/ecs6-class/point.test.js @@ -0,0 +1,54 @@ +const { default: expect } = require("expect") +const Point = require("../../../modules/ecs6-class/point") +describe("POINT_FUNCTION", function () { + describe("BUILD_POINT", function () { + it("should show the valus of x and y after build the point", function () { + let p1 = new Point({ x: 3, y: 4 }) + let p2 = new Point() + expect(p1.x).toEqual(3) + expect(p1.y).toEqual(4) + expect(p2.x).toEqual(0) + expect(p2.y).toEqual(0) + + + }) + }) + + describe("MOVE_VERTICAL", function () { + it("the function move point to up or down", function () { + let p1 = new Point({ x: 1, y: 2 }) + p1.moveVertical(4) + expect(p1.y).toEqual(6) + expect(p1.x).toEqual(1) + }) + + }) + describe("MOVE_HORIZONTAL", function () { + it("the function move point to right or left", function () { + let p1 = new Point({ x: 1, y: 2 }) + p1.moveHorizontal(4) + expect(p1.x).toEqual(5) + expect(p1.y).toEqual(2) + }) + }) + describe("ERROR", function () { + + it("the function must get argument", function () { + let p = new Point({ x: 3, y: 4 }) + expect(() => { new Point({ x: "x", y: "y" }) }).toThrow(new Error("the argument must be type number")) + expect(() => { p.moveVertical() }).toThrow("the function must get argument") + expect(() => { p.moveHorizontal() }).toThrow("the function must get argument") + + }) + it("the function must get arguments from type number", function () { + let p = new Point({ x: 3, y: 4 }) + expect(() => { new Point({ x: "x", y: "y" }) }).toThrow("the argument must be type number") + expect(() => { p.moveVertical("x") }).toThrow("the argument must be type number") + expect(() => { p.moveHorizontal("x") }).toThrow("the argument must be type number") + + }) + + }) + +}) + diff --git a/test/modules/geomatry-calculation.test.js b/test/modules/geomatry-calculation.test.js new file mode 100644 index 0000000..37eb11a --- /dev/null +++ b/test/modules/geomatry-calculation.test.js @@ -0,0 +1,106 @@ + +const Point = require("../../modules/ecs6-class/point") +const Line = require("../../modules/ecs6-class/line") +const Geomatry = require("../../modules/geometry-calculation") + +describe("GEOMATRY_CALCULATE_FUNCTION", function () { + describe("CALCULATE_DISTANSE", function () { + it("should return distance between 2 points", function () { + let point1 = new Point({ x: 4, y: 5 }) + let point2 = new Point({ x: 6, y: 7 }) + + + expect(Geomatry.calculateDistance(point1, point2)).toEqual(2.8284271247461903) + expect(Geomatry.calculateDistance(point1, point2)).toBe(2.8284271247461903) + + }) + }) + + describe("CALCULATE_JUNCTION_POINT", function () { + it("should return true when the 2 lines the same", function () { + let line1 = new Line(new Point({ x: 1, y: 1 }), new Point({ x: 2, y: 2 }), 2, -1) + let line2 = new Line(new Point({ x: 1, y: 1 }), new Point({ x: 2, y: 2 }), 2, -1) + expect(Geomatry.calculateJunctionPoint(line1, line2)).toBe(true) + + }) + it("should return false when the n isn't same", function () { + let line1 = new Line({ point1: new Point({ x: 1, y: 1 }), point2: new Point({ x: 2, y: 2 }), n: 2, slope: -1 }) + let line2 = new Line({ point1: new Point({ x: 1, y: 1 }), point2: new Point({ x: 2, y: 2 }), n: 5, slope: -1 }) + + expect(Geomatry.calculateJunctionPoint(line1, line2)).toBe(false) + + }) + it("should return junctionPoint when the slope isn't equale", function () { + let line1 = new Line({ point1: new Point({ x: 10, y: 0 }), point2: new Point({ x: 5, y: 0 }), n: 5, slope: 2 }) + let line2 = new Line({ point1: new Point({ x: 10, y: 0 }), point2: new Point({ x: 5, y: 0 }), n: 1, slope: 3 }) + expect(Geomatry.calculateJunctionPoint(line1, line2)).toEqual({ "x": 4, "y": 13 }) + + }) + }) + + describe("IS_POINT_ON_LINE", function () { + it("should return false if the lineSlope and proxyLineSlope isn't equales", function () { + + let point = new Point({ x: 5, y: 2 }) + let line = new Line({ point1: new Point({ x: 10, y: 4 }), point2: new Point({ x: 5, y: 0 }), n: 5, slope: 2 }) + + expect(Geomatry.isPointOnLine(line, point)).toBe(false) + + }) + + it("should return false if the lineSlope and proxyLineSlope isn't equales", function () { + + let point = new Point({ x: 5, y: 2 }) + let line = new Line({ point1: new Point({ x: 10, y: 4 }), point2: new Point({ x: 5, y: 2 }) }) + let p = new Point({ x: 10, y: 9 }) + let l = new Line({ point1: new Point({ x: 30, y: 10 }), point2: new Point({ x: 10, y: 9 }) }) + line.calculateSlope() + line.calculateNOfLineFunction() + l.calculateSlope() + l.calculateNOfLineFunction() + expect(Geomatry.isPointOnLine(line, point)).toBe(true) + expect(Geomatry.isPointOnLine(l, p)).toBe(true) + + }) + + }) + it("should return false if only line.n not equale to proxyLine.n", function () { + + let point = new Point({ x: 5, y: 2 }) + let line = new Line({ point1: new Point({ x: 10, y: 4 }), point2: new Point({ x: 5, y: 0 }), n: 5, slope: 0.4 }) + + + expect(Geomatry.isPointOnLine(line, point)).toBe(false) + + }) + + describe("ERROR", function () { + + it("the function calculateDistance must get 2 points", function () { + let p = new Point({ x: 3, y: 4 }) + expect(() => { Geomatry.calculateDistance(p) }).toThrow("the function calculateDistance must get 2 points") + }) + + it("the function calculateDistance must get 2 points of type Point", function () { + let p1 = "p1" + let p2 = "p2" + expect(() => { Geomatry.calculateDistance(p1, p2) }).toThrow("the function calculateDistance must get 2 points of type Point") + }) + + it("the function calculateJunctionPoint must get 2 lines", function () { + let line1 = new Line({ point1: new Point({ x: 1, y: 6 }), point2: new Point({ x: 2, y: 3 }), n: 4, slope: 5 }) + + expect(() => { Geomatry.calculateJunctionPoint(line1) }).toThrow("the function calculateJunctionPoint must get 2 lines") + + }) + + it("the function calculateJunctionPoint must get 2 lines of type Line", function () { + expect(() => { Geomatry.calculateJunctionPoint("line1,", "line2") }).toThrow("the function calculateJunctionPoint must get 2 lines of type Line") + + }) + + + + }) + +}) \ No newline at end of file