From 049ec26b28f7b71c33dba9fe038cc48a789c2c08 Mon Sep 17 00:00:00 2001 From: ruty Date: Thu, 25 Jul 2024 15:49:55 +0300 Subject: [PATCH 1/4] add tests on point line and geomatry-calculate --- modules/ecs6-class/line.js | 27 +++- modules/ecs6-class/point.js | 21 ++- modules/geometry-calculation.js | 10 +- package-lock.json | 18 ++- package.json | 3 +- test/line.test | 0 test/modules/ecs6-class/line.test.js | 159 ++++++++++++++++++++++ test/modules/ecs6-class/point.test.js | 54 ++++++++ test/modules/geomatry-calculation.test.js | 79 +++++++++++ 9 files changed, 358 insertions(+), 13 deletions(-) create mode 100644 test/line.test create mode 100644 test/modules/ecs6-class/line.test.js create mode 100644 test/modules/ecs6-class/point.test.js create mode 100644 test/modules/geomatry-calculation.test.js diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index 826c675..cc830da 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 }) { + console.log(point1, point2, "p1p2p2p1p"); + 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,26 @@ 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") + } 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..df2930c 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..7411ed6 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -2,12 +2,15 @@ const Line = require('./ecs6-class/line') const calculateDistance = (point1, point2) => { 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.slope === line2.slope) { if (line1.n === line2.n) { return true @@ -24,10 +27,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 +40,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..355814a 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":"jest --coverage" }, "dependencies": { "jest": "^29.7.0" 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..3d5f91d --- /dev/null +++ b/test/modules/ecs6-class/line.test.js @@ -0,0 +1,159 @@ +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 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(l1.n).toEqual(2) + expect(l1.slope).toEqual(-3) + expect(l2.point1.x).toEqual(5) + expect(l2.point1.y).toEqual(5) + expect(l2.point2.x).toEqual(6) + expect(l2.point2.y).toEqual(6) + 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: 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, }) + expect(l1.slope).toEqual(-3) + + l1.calculateSlope() + l2.calculateSlope() + expect(l1.slope).toEqual(1) + expect(l2.slope).toEqual(1) + + + + + }) + }) + + 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 }), }) + + line1.calculateSlope() + line1.calculateNOfLineFunction() + 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 }), }) + line1.calculateSlope() + line1.calculateNOfLineFunction() + 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 }) }) + l2.calculateSlope() + l2.calculateNOfLineFunction() + + + 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 }) }) + l2.calculateSlope() + l2.calculateNOfLineFunction() + + + 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" }) + console.log(typeof (line4.point1)); + + + + console.log("loolooooooluuuuuuuuuuuu", line); + 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 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..6c37a40 --- /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(()=>{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") + // expect(()=>{p.moveVertical()}).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..25fb128 --- /dev/null +++ b/test/modules/geomatry-calculation.test.js @@ -0,0 +1,79 @@ + +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) + + }) + +}) \ No newline at end of file From 84fac570ac82435a2bf5bbeb3db1ded72c8be6d8 Mon Sep 17 00:00:00 2001 From: ruty Date: Thu, 25 Jul 2024 16:32:40 +0300 Subject: [PATCH 2/4] add tests on point line and geomatry-calculate --- modules/ecs6-class/line.js | 3 +++ test/modules/ecs6-class/line.test.js | 10 ++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index cc830da..cb8afd6 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -4,6 +4,9 @@ class Line { constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) { console.log(point1, point2, "p1p2p2p1p"); + // if (typeof(point1)!=Point||typeof(point2)!=Point||typeof(n)!="number"||typeof(slope)!="number") { + // throw new Error("the constractor of line must get 2 points of type number") + // } this.point1 = point1; this.point2 = point2; this.slope = slope; diff --git a/test/modules/ecs6-class/line.test.js b/test/modules/ecs6-class/line.test.js index 3d5f91d..7c81b69 100644 --- a/test/modules/ecs6-class/line.test.js +++ b/test/modules/ecs6-class/line.test.js @@ -129,10 +129,12 @@ describe("LINE_FUNCTION", 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" }) - console.log(typeof (line4.point1)); - - + let line4=new Line({ point1: "huhj", point2: "jij", n: "9" ,slope:"ijdi"}) + console.log(typeof(line4.point1)); + // it("the constractor of line must get 2 points of type number",function(){ + // expect(()=>{new Line({ point1: "huhj", point2: "jij", n: "9" ,slope:"ijdi"})}).toThrow("the constractor of line must get 2 points of type number") + // }) + console.log("loolooooooluuuuuuuuuuuu", line); it("the slope of line must be defined", function () { From 0bb3872f1942987e0f84ca0555eb2c9aa9f853e0 Mon Sep 17 00:00:00 2001 From: ruty Date: Thu, 25 Jul 2024 17:59:10 +0300 Subject: [PATCH 3/4] add tests on point line and geomatry-calculate --- modules/ecs6-class/line.js | 6 +++--- test/modules/ecs6-class/line.test.js | 9 ++++----- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index cb8afd6..ec9150e 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -2,10 +2,10 @@ const Point = require("./point"); class Line { constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) { - console.log(point1, point2, "p1p2p2p1p"); + console.log(typeof(point1), typeof(point2), "typeooooooooooop1p2p2p1p"); - // if (typeof(point1)!=Point||typeof(point2)!=Point||typeof(n)!="number"||typeof(slope)!="number") { - // throw new Error("the constractor of line must get 2 points of type number") + // if (typeof (x) != "number" || typeof (y) != "number") { + // throw new Error("aaaa") // } this.point1 = point1; this.point2 = point2; diff --git a/test/modules/ecs6-class/line.test.js b/test/modules/ecs6-class/line.test.js index 7c81b69..208d95c 100644 --- a/test/modules/ecs6-class/line.test.js +++ b/test/modules/ecs6-class/line.test.js @@ -77,7 +77,6 @@ describe("LINE_FUNCTION", function () { let line1 = new Line({ point1: new Point({ x: 1, y: 6 }), point2: new Point({ x: 2, y: 3 }), }) - line1.calculateSlope() line1.calculateNOfLineFunction() expect(line1.getPointOnYAsis()).toStrictEqual(new Point({ x: 0, y: 9 })) @@ -129,12 +128,12 @@ describe("LINE_FUNCTION", 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"}) - console.log(typeof(line4.point1)); + let line4 = new Line({ point1: "huhj", point2: "jij", n: "9", slope: "ijdi" }) + console.log(typeof (line4.point1)); // it("the constractor of line must get 2 points of type number",function(){ - // expect(()=>{new Line({ point1: "huhj", point2: "jij", n: "9" ,slope:"ijdi"})}).toThrow("the constractor of line must get 2 points of type number") + // expect(()=>{new Line({ point1: new Point({ x: 3, y: 7 }), point2: new Point({ x: 9, y: 7 }), n: 9 ,slope:"ui"})}).toThrow("aaaa") // }) - + console.log("loolooooooluuuuuuuuuuuu", line); it("the slope of line must be defined", function () { From 633ae3110ef77c46ba2e1b9e822cd7dbc7bb7541 Mon Sep 17 00:00:00 2001 From: ruty Date: Thu, 1 Aug 2024 12:33:40 +0300 Subject: [PATCH 4/4] add more tests --- modules/ecs6-class/line.js | 23 ++++-- modules/ecs6-class/point.js | 8 +- modules/geometry-calculation.js | 17 +++- package.json | 3 + test/modules/ecs6-class/line.test.js | 97 ++++++++++------------- test/modules/ecs6-class/point.test.js | 26 +++--- test/modules/geomatry-calculation.test.js | 35 +++++++- 7 files changed, 123 insertions(+), 86 deletions(-) diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index ec9150e..263204c 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -2,11 +2,8 @@ const Point = require("./point"); class Line { constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) { - console.log(typeof(point1), typeof(point2), "typeooooooooooop1p2p2p1p"); - // if (typeof (x) != "number" || typeof (y) != "number") { - // throw new Error("aaaa") - // } + this.point1 = point1; this.point2 = point2; this.slope = slope; @@ -22,7 +19,7 @@ class Line { if (this.slope === undefined) { throw new Error("the slope must be defined") } - if (typeof (this.slope) != "number") { + if (typeof (this.slope) !== "number") { throw new Error("the slope must be number") } this.n = this.point1.y - this.slope * this.point1.x @@ -42,9 +39,21 @@ class Line { if (x === undefined) { throw new Error("the function getPointByX must get x") } - if (typeof (x) != "number") { + 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: x, y: y }) } @@ -53,7 +62,7 @@ class Line { if (y === undefined) { throw new Error("the function getPointByY must get y") } - if (typeof (y) != "number") { + if (typeof (y) !== "number") { throw new Error("the function getPointByY must get y of type number") } diff --git a/modules/ecs6-class/point.js b/modules/ecs6-class/point.js index df2930c..a4f4679 100644 --- a/modules/ecs6-class/point.js +++ b/modules/ecs6-class/point.js @@ -1,6 +1,6 @@ class Point { - constructor({ x = 0, y = 0 } = {}) { - if (typeof (x) != "number" || typeof (y) != "number") { + constructor({x=0,y=0}={}){ + if (typeof (x) !== "number" || typeof (y) !== "number") { throw new Error("the argument must be type number") } this.x = x; @@ -12,7 +12,7 @@ class Point { } - if (typeof (value) != "number") { + if (typeof (value) !== "number") { throw new Error("the argument must be type number") } @@ -22,7 +22,7 @@ class Point { if (!value) { throw new Error("the function must get argument") } - if (typeof (value) != "number") { + if (typeof (value) !== "number") { throw new Error("the argument must be type number") } this.x += value; diff --git a/modules/geometry-calculation.js b/modules/geometry-calculation.js index 7411ed6..76607dc 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -1,6 +1,13 @@ -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 - point1.y) ** 2; const distance = Math.sqrt(distanceX + distanceY); @@ -8,9 +15,13 @@ const calculateDistance = (point1, point2) => { } 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 diff --git a/package.json b/package.json index 355814a..b1bf136 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,9 @@ "test:coverage":"jest --coverage" }, "dependencies": { + + }, + "devDependencies": { "jest": "^29.7.0" }, "repository": { diff --git a/test/modules/ecs6-class/line.test.js b/test/modules/ecs6-class/line.test.js index 208d95c..776aa89 100644 --- a/test/modules/ecs6-class/line.test.js +++ b/test/modules/ecs6-class/line.test.js @@ -1,9 +1,10 @@ +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 line after build the 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 }) @@ -16,36 +17,39 @@ describe("LINE_FUNCTION", function () { expect(l1.point1.y).toEqual(2) expect(l1.point2.x).toEqual(3) expect(l1.point2.y).toEqual(4) - expect(l1.n).toEqual(2) - expect(l1.slope).toEqual(-3) 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: 5, y: 5 }) - let point2 = new Point({ x: 6, y: 6 }) + 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, }) + 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(1) - - - - + expect(l2.slope).toEqual(10) }) }) @@ -57,7 +61,6 @@ describe("LINE_FUNCTION", function () { 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() @@ -65,82 +68,54 @@ describe("LINE_FUNCTION", function () { 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 }), }) - line1.calculateSlope() - line1.calculateNOfLineFunction() + 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 }), }) - line1.calculateSlope() - line1.calculateNOfLineFunction() + 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 }) }) - l2.calculateSlope() - l2.calculateNOfLineFunction() - - + 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 }) }) - l2.calculateSlope() - l2.calculateNOfLineFunction() - + 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" }) - console.log(typeof (line4.point1)); - // it("the constractor of line must get 2 points of type number",function(){ - // expect(()=>{new Line({ point1: new Point({ x: 3, y: 7 }), point2: new Point({ x: 9, y: 7 }), n: 9 ,slope:"ui"})}).toThrow("aaaa") - // }) - - - console.log("loolooooooluuuuuuuuuuuu", line); - it("the slope of line must be defined", function () { + 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 () { + 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 () { @@ -149,6 +124,18 @@ describe("LINE_FUNCTION", function () { 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") }) diff --git a/test/modules/ecs6-class/point.test.js b/test/modules/ecs6-class/point.test.js index 6c37a40..b093252 100644 --- a/test/modules/ecs6-class/point.test.js +++ b/test/modules/ecs6-class/point.test.js @@ -13,7 +13,7 @@ describe("POINT_FUNCTION", function () { }) }) - + describe("MOVE_VERTICAL", function () { it("the function move point to up or down", function () { let p1 = new Point({ x: 1, y: 2 }) @@ -31,23 +31,23 @@ describe("POINT_FUNCTION", function () { expect(p1.y).toEqual(2) }) }) - describe("ERROR",function(){ + describe("ERROR", function () { - it("the function must get argument",function(){ - let p=new Point({x:3,y:4}) - expect(()=>{p.moveVertical()}).toThrow("the function must get argument") - expect(()=>{p.moveHorizontal()}).toThrow("the function must get argument") + 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") - // expect(()=>{p.moveVertical()}).toThrow("the argument must be type number") + 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 index 25fb128..37eb11a 100644 --- a/test/modules/geomatry-calculation.test.js +++ b/test/modules/geomatry-calculation.test.js @@ -20,15 +20,13 @@ describe("GEOMATRY_CALCULATE_FUNCTION", 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) }) @@ -70,10 +68,39 @@ describe("GEOMATRY_CALCULATE_FUNCTION", 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