From 6777b71da95f26c9008fc99fcfa8fefd52e8f5af Mon Sep 17 00:00:00 2001 From: chayale Date: Mon, 29 Jul 2024 09:21:47 +0300 Subject: [PATCH 1/5] finish test --- test/modules/ecs6-class/line.test.js | 63 +++++++++++++++++++++++++++ test/modules/ecs6-class/point.test.js | 53 ++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 test/modules/ecs6-class/line.test.js create mode 100644 test/modules/ecs6-class/point.test.js diff --git a/test/modules/ecs6-class/line.test.js b/test/modules/ecs6-class/line.test.js new file mode 100644 index 0000000..be49526 --- /dev/null +++ b/test/modules/ecs6-class/line.test.js @@ -0,0 +1,63 @@ +const Line = require("../../../modules/ecs6-class/line") +const Point = require("../../../modules/ecs6-class/point") + +const point1 = new Point({ x: 9, y: 6 }) +const point2 = new Point({ x: 7, y: 2 }) +const line = new Line({ point1, point2 }) + +describe("CALCULATE_SLOPE", function () { + + test("should return calculateSlope for two points", () => { + + line.calculateSlope() + expect(line.slope).toBe(2) + }) + + +}) + +describe("CALCULATE_N_OF_LINE_FUNCTION", () => { + test("", () => { + line.calculateNOfLineFunction() + expect(line.n).toBe(-12) + + }) +}) +describe("GET_POINT_ON_X_ASIS", () => { + test("", () => { + line.getPointOnXAsis() + expect(line.point1.x).toBe(9) + }) +}) +describe("GET_POINT_ON_Y_ASIS", () => { + test("", () => { + line.getPointOnYAsis() + expect(line.point1.y).toBe(6) + }) +}) +describe("GET_POINT_BY_X", () => { + test("", () => { + line.getPointByX() + expect(line.point1.x).toBe(9) + }) +}) + +describe("GET_POINT_BY_Y", () => { + test("", () => { + line.getPointByY() + expect(line.point1.y).toBe(6) + }) +}) +describe("ERRORS", function () { + test("", () => { + + expect(() => line555.calculateNOfLineFunction().toThrow('must give isn\t argument')) + // expect(()=>line.calculateSlope(point1,"abc").toThrow('type value isn\t number')) + // expect(()=>line.calculateSlope(point1,'true').toThrow('type value isn\t number')) + }) + +}) + + + + diff --git a/test/modules/ecs6-class/point.test.js b/test/modules/ecs6-class/point.test.js new file mode 100644 index 0000000..a26f5e8 --- /dev/null +++ b/test/modules/ecs6-class/point.test.js @@ -0,0 +1,53 @@ +const Point = require("../../../modules/ecs6-class/point") + +describe("MOVE_VERTICAL", function () { + it("should return point y", () => { + const point = new Point({ x: 5, y: 7 }) + point.moveVertical(2) + expect(point.y).toBe(9) + }) + it("should return point y", () => { + const point = new Point({ x: 5, y: 7 }) + point.moveVertical(-3) + expect(point.y).toBe(4) + }) + + + +}) +describe("MOVE_HORZIZONTAL", function () { + it("should return point x", () => { + const point = new Point({ x: 4, y: 6 }) + point.moveHorizontal(7) + expect(point.x).toBe(11) + }) + it("should return point x", () => { + const point = new Point({ x: 4, y: 6 }) + point.moveHorizontal(-3) + expect(point.x).toBe(1) + }) +}) +describe("ERRORS", function () { + const point = new Point({ x: 9, y: 4 }) + it('!value', () => { + expect(() => point.moveVertical()).toThrow('must give isn\t argument') + + }) + it('typeof(value)!="number"', () => { + expect(() => point.moveVertical("abc")).toThrow('type value isn\t number') + expect(() => point.moveVertical([9, 5])).toThrow('type value isn\t number') + expect(() => point.moveVertical('true')).toThrow('type value isn\t number') + }) + it('', () => { + expect(() => point.moveHorizontal()).toThrow('must give isn\t argument') + }) + + it('', () => { + + expect(() => point.moveHorizontal("abc")).toThrow('type value isn\t number') + expect(() => point.moveHorizontal([4, 6])).toThrow('type value isn\t number') + expect(() => point.moveHorizontal('false')).toThrow('type value isn\t number') + + }) + +}) \ No newline at end of file From 50aa3a5f92056b5d0dd51c60882560b58465090c Mon Sep 17 00:00:00 2001 From: chayale Date: Mon, 29 Jul 2024 09:23:10 +0300 Subject: [PATCH 2/5] finish test --- modules/ecs6-class/line.js | 18 +++++++++++++++++- modules/ecs6-class/point.js | 19 ++++++++++++++++++- modules/geometry-calculation.js | 8 +++++++- package.json | 3 ++- 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index 826c675..647b27a 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -9,10 +9,27 @@ class Line { } calculateSlope = () => { + + // if (!this.point1 || !this.point2) { + // throw("aaaa") + // } + // if(typeof()) this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) + + } calculateNOfLineFunction = () => { + // if (!this.slope) { + // throw new Error("the slope must be defined") + // } + // console.log(this.slope); + // if (this.point1 === undefined) { + // throw ("must give isn\t argument") + // } + // if (typeof (this.slope) != "number") { + // throw new Error("the slope must to number") + // } this.n = this.point1.y - this.slope * this.point1.x } @@ -24,7 +41,6 @@ class Line { return this.getPointByX(0) } - getPointByX(x) { let y = this.slope * x + this.n return new Point({ x, y }) diff --git a/modules/ecs6-class/point.js b/modules/ecs6-class/point.js index e81b4a4..b330da8 100644 --- a/modules/ecs6-class/point.js +++ b/modules/ecs6-class/point.js @@ -1,13 +1,30 @@ class Point { - constructor({x=0, y=0}={}) { + constructor({ x = 0, y = 0 } = {}) { this.x = x; this.y = y; } + moveVertical(value) { + if (!value) { + throw ("must give isn\t argument") + } + if (typeof (value) != "number") { + throw ("type value isn\t number") + } + this.y += value; + } + moveHorizontal(value) { + if (!value) { + throw ("must give isn\t argument") + } + if (typeof (value) != "number") { + throw ("type value isn\t number") + } this.x += value; + } } diff --git a/modules/geometry-calculation.js b/modules/geometry-calculation.js index 6e11643..c56555b 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -2,12 +2,14 @@ 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 || !line2){ + //throw("argument not give")} if (line1.slope === line2.slope) { if (line1.n === line2.n) { return true @@ -24,6 +26,9 @@ const calculateJunctionPoint = (line1, line2) => { } const isPointOnLine = (line, point) => { + if(!line || !point){ + throw ("not give argment") + } const proxyLine = new Line({ point1: line.point1, point2: point }) proxyLine.calculateSlope() if (line.slope === proxyLine.slope) { @@ -35,6 +40,7 @@ const isPointOnLine = (line, point) => { return false } + module.exports = { calculateDistance, calculateJunctionPoint, 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" From 6de17bef7b4f63106f2e4b0614732ce1e7272de1 Mon Sep 17 00:00:00 2001 From: chayale Date: Mon, 29 Jul 2024 09:32:02 +0300 Subject: [PATCH 3/5] finish test --- modules/geometry-calculation.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/geometry-calculation.js b/modules/geometry-calculation.js index c56555b..05551be 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -26,9 +26,9 @@ const calculateJunctionPoint = (line1, line2) => { } const isPointOnLine = (line, point) => { - if(!line || !point){ - throw ("not give argment") - } + // if(!line || !point){ + // throw ("not give argment") + // } const proxyLine = new Line({ point1: line.point1, point2: point }) proxyLine.calculateSlope() if (line.slope === proxyLine.slope) { From 459abeaeeb7121cddb8669e9bd8e5d37a6f6e590 Mon Sep 17 00:00:00 2001 From: chayale Date: Mon, 29 Jul 2024 09:42:08 +0300 Subject: [PATCH 4/5] add test --- test/modules/geometry-calaulation.test.js | 68 +++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 test/modules/geometry-calaulation.test.js diff --git a/test/modules/geometry-calaulation.test.js b/test/modules/geometry-calaulation.test.js new file mode 100644 index 0000000..1210e2d --- /dev/null +++ b/test/modules/geometry-calaulation.test.js @@ -0,0 +1,68 @@ +const Point = require("../../modules/ecs6-class/point") +const Line = require("../../modules/ecs6-class/line") +const { calculateDistance, calculateJunctionPoint, isPointOnLine } = require("../../modules/geometry-calculation") +let n = 7 +let n1 = 3 +let slope = -6 +let slope1 = 2 + +const point1 = new Point({ x: 5, y: 8 }) +const point2 = new Point({ x: 1, y: 9 }) +const point3 = new Point({ x: 6, y: 2 }) +const line1 = new Line({ point1, point2, slope, n }) +const line2 = new Line({ point1, point3, slope, n }) +const line3 = new Line({ point2, point3, slope1, n }) +const line4 = new Line({ point2, point3, slope, n1 }) + + +describe("CALCULATE_DISTANCE", () => { + test("", () => { + expect(calculateDistance(point1, point2)).toEqual(4.123105625617661) + }) + +}) + + +describe("CALCULATE_JUNCTION_POINT", () => { + test("", () => { + expect(calculateJunctionPoint(line1, line2)).toBe(true) + + }) + test("", () => { + + expect(calculateJunctionPoint(line1, line4)).toBe(false) + }) + test("", () => { + expect(calculateJunctionPoint(line1, line3)).toEqual({ x: NaN, y: NaN }) + }) +}) + +describe("IS_POINT_ON_LINE", () => { + + test("", () => { + expect(isPointOnLine(line1, point1)).toBe(false) + }) + test("", () => { + + const point5 = new Point({ x: 2, y: 2 }) + const point6 = new Point({ x: 1, y: 1 }) + + const line4 = new Line({ x: point5, y: point6, slope: 1, n: 0 }) + expect(isPointOnLine(line4, point6)).toBe(true) + }) + test("", () => { + + const point5 = new Point({ x: 2, y: 2 }) + const point6 = new Point({ x: 1, y: 1 }) + + const line4 = new Line({ x: point5, y: point6, slope: 1, n: 7 }) + expect(isPointOnLine(line4, point6)).toBe(false) + }) + +}) + +// describe("ERROR", () => { +// // it('', () => { +// // expect(() => line1.calculateJunctionPoint()).toThrow() +// // }) +// }) From 3569b34525f9d5a52ed68c7875a201140d474df4 Mon Sep 17 00:00:00 2001 From: chayale Date: Mon, 29 Jul 2024 14:26:29 +0300 Subject: [PATCH 5/5] finish --- modules/ecs6-class/line.js | 35 +++++---- modules/geometry-calculation.js | 29 ++++++-- test/modules/ecs6-class/line.test.js | 28 ++++++- test/modules/geometry-calaulation.test.js | 68 ----------------- test/modules/geometry-calculation.test.js | 89 +++++++++++++++++++++++ 5 files changed, 156 insertions(+), 93 deletions(-) delete mode 100644 test/modules/geometry-calaulation.test.js create mode 100644 test/modules/geometry-calculation.test.js diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index 647b27a..85fd0ae 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -2,6 +2,10 @@ const Point = require("./point"); class Line { constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) { + if (n != undefined && typeof (n) != 'number' || slope != undefined && typeof (slope) != 'number') + throw new Error('type of n and slope need be number') + if (!(point1 instanceof Point) || !(point2 instanceof Point)) + throw new Error('points need be object of Point class') this.point1 = point1; this.point2 = point2; this.slope = slope; @@ -9,27 +13,18 @@ class Line { } calculateSlope = () => { - - // if (!this.point1 || !this.point2) { - // throw("aaaa") - // } - // if(typeof()) - this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) + if ((this.point1.x - this.point2.x) === 0) { + throw ("It is impossible to divide by 0") + } + + this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) } calculateNOfLineFunction = () => { - // if (!this.slope) { - // throw new Error("the slope must be defined") - // } - // console.log(this.slope); - // if (this.point1 === undefined) { - // throw ("must give isn\t argument") - // } - // if (typeof (this.slope) != "number") { - // throw new Error("the slope must to number") - // } + if (!this.slope) + this.calculateSlope() this.n = this.point1.y - this.slope * this.point1.x } @@ -42,11 +37,19 @@ class Line { } getPointByX(x) { + if (this.slope == undefined) + this.calculateSlope(); + if(this.n == undefined) + this.calculateNOfLineFunction(); let y = this.slope * x + this.n return new Point({ x, y }) } getPointByY(y) { + if (this.slope == undefined) + this.calculateSlope(); + if(this.n == undefined) + this.calculateNOfLineFunction(); let x = (y - this.n) / this.slope; return new Point({ x, y }) } diff --git a/modules/geometry-calculation.js b/modules/geometry-calculation.js index 05551be..b1ad68b 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -1,6 +1,9 @@ -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('two points were not received') let distanceX = (point2.x - point1.x) ** 2; let distanceY = (point2.y - point1.y) ** 2; const distance = Math.sqrt(distanceX + distanceY); @@ -8,8 +11,16 @@ const calculateDistance = (point1, point2) => { } const calculateJunctionPoint = (line1, line2) => { - // if(!line1 || !line2){ - //throw("argument not give")} + if (!line1 || !line2) + throw ("It should take two arguments") + if (!line1.slope) + line1.calculateSlope() + if (!line1.n) + line1.calculateNOfLineFunction() + if (!line2.slope) + line2.calculateSlope() + if (!line2.n) + line2.calculateNOfLineFunction() if (line1.slope === line2.slope) { if (line1.n === line2.n) { return true @@ -26,9 +37,15 @@ const calculateJunctionPoint = (line1, line2) => { } const isPointOnLine = (line, point) => { - // if(!line || !point){ - // throw ("not give argment") - // } + + if (!line || !(line instanceof Line)) { + throw ("line were not received") + } + if (!point || !point instanceof Point) { + throw ("point were not received") + } + if (line.slope === undefined) + line.calculateSlope() const proxyLine = new Line({ point1: line.point1, point2: point }) proxyLine.calculateSlope() if (line.slope === proxyLine.slope) { diff --git a/test/modules/ecs6-class/line.test.js b/test/modules/ecs6-class/line.test.js index be49526..e8ebeae 100644 --- a/test/modules/ecs6-class/line.test.js +++ b/test/modules/ecs6-class/line.test.js @@ -9,6 +9,10 @@ describe("CALCULATE_SLOPE", function () { test("should return calculateSlope for two points", () => { + const point1 = new Point({ x: 9, y: 6 }) + const point2 = new Point({ x: 7, y: 2 }) + const line = new Line({ point1, point2 }) + line.calculateSlope() expect(line.slope).toBe(2) }) @@ -37,6 +41,11 @@ describe("GET_POINT_ON_Y_ASIS", () => { }) describe("GET_POINT_BY_X", () => { test("", () => { + const point1 = new Point({ x: 9, y: 6 }) + const point2 = new Point({ x: 7, y: 2 }) + const line = new Line({ point1, point2 }) + + line.getPointByX() expect(line.point1.x).toBe(9) }) @@ -44,6 +53,10 @@ describe("GET_POINT_BY_X", () => { describe("GET_POINT_BY_Y", () => { test("", () => { + const point1 = new Point({ x: 9, y: 6 }) + const point2 = new Point({ x: 7, y: 2 }) + const line = new Line({ point1, point2 }) + line.getPointByY() expect(line.point1.y).toBe(6) }) @@ -51,11 +64,20 @@ describe("GET_POINT_BY_Y", () => { describe("ERRORS", function () { test("", () => { - expect(() => line555.calculateNOfLineFunction().toThrow('must give isn\t argument')) - // expect(()=>line.calculateSlope(point1,"abc").toThrow('type value isn\t number')) - // expect(()=>line.calculateSlope(point1,'true').toThrow('type value isn\t number')) + expect(() => new Line({ point1: new Point({ x: 4, y: 3 }), point2: new Point({ x: 7, y: 5 }), n: '@', slope: 'chayale' })).toThrow("type of n and slope need be number") + }) + it('', () => { + expect(() => new Line({ point1: 'abc', point2: { x: 5, y: 9 } })).toThrow("points need be object of Point class") + + }) + it('should throw error when no points have been sent to the calculateSlope function', function () { + ll1 = new Line({}) + expect(() => ll1.calculateSlope()).toThrow('It is impossible to divide by 0') }) + + + }) diff --git a/test/modules/geometry-calaulation.test.js b/test/modules/geometry-calaulation.test.js deleted file mode 100644 index 1210e2d..0000000 --- a/test/modules/geometry-calaulation.test.js +++ /dev/null @@ -1,68 +0,0 @@ -const Point = require("../../modules/ecs6-class/point") -const Line = require("../../modules/ecs6-class/line") -const { calculateDistance, calculateJunctionPoint, isPointOnLine } = require("../../modules/geometry-calculation") -let n = 7 -let n1 = 3 -let slope = -6 -let slope1 = 2 - -const point1 = new Point({ x: 5, y: 8 }) -const point2 = new Point({ x: 1, y: 9 }) -const point3 = new Point({ x: 6, y: 2 }) -const line1 = new Line({ point1, point2, slope, n }) -const line2 = new Line({ point1, point3, slope, n }) -const line3 = new Line({ point2, point3, slope1, n }) -const line4 = new Line({ point2, point3, slope, n1 }) - - -describe("CALCULATE_DISTANCE", () => { - test("", () => { - expect(calculateDistance(point1, point2)).toEqual(4.123105625617661) - }) - -}) - - -describe("CALCULATE_JUNCTION_POINT", () => { - test("", () => { - expect(calculateJunctionPoint(line1, line2)).toBe(true) - - }) - test("", () => { - - expect(calculateJunctionPoint(line1, line4)).toBe(false) - }) - test("", () => { - expect(calculateJunctionPoint(line1, line3)).toEqual({ x: NaN, y: NaN }) - }) -}) - -describe("IS_POINT_ON_LINE", () => { - - test("", () => { - expect(isPointOnLine(line1, point1)).toBe(false) - }) - test("", () => { - - const point5 = new Point({ x: 2, y: 2 }) - const point6 = new Point({ x: 1, y: 1 }) - - const line4 = new Line({ x: point5, y: point6, slope: 1, n: 0 }) - expect(isPointOnLine(line4, point6)).toBe(true) - }) - test("", () => { - - const point5 = new Point({ x: 2, y: 2 }) - const point6 = new Point({ x: 1, y: 1 }) - - const line4 = new Line({ x: point5, y: point6, slope: 1, n: 7 }) - expect(isPointOnLine(line4, point6)).toBe(false) - }) - -}) - -// describe("ERROR", () => { -// // it('', () => { -// // expect(() => line1.calculateJunctionPoint()).toThrow() -// // }) -// }) diff --git a/test/modules/geometry-calculation.test.js b/test/modules/geometry-calculation.test.js new file mode 100644 index 0000000..50dcd9c --- /dev/null +++ b/test/modules/geometry-calculation.test.js @@ -0,0 +1,89 @@ +const Point = require("../../modules/ecs6-class/point") +const Line = require("../../modules/ecs6-class/line") +const { calculateDistance, calculateJunctionPoint, isPointOnLine } = require("../../modules/geometry-calculation") +let n = 7 +let n1 = 3 +let slope = -6 +let slope1 = 2 + +const point1 = new Point({ x: 5, y: 8 }) +const point2 = new Point({ x: 1, y: 9 }) +const point3 = new Point({ x: 6, y: 2 }) +const line1 = new Line({ point1, point2, slope, n }) +const line2 = new Line({ point1, point3, slope, n }) +const line3 = new Line({ point2, point3, slope1, n }) +const line4 = new Line({ point2, point3, slope, n1 }) + + +describe("CALCULATE_DISTANCE", () => { + test("", () => { + expect(calculateDistance(point1, point2)).toEqual(4.123105625617661) + }) + +}) + + +describe("CALCULATE_JUNCTION_POINT", () => { + test("should return ", () => { + expect(calculateJunctionPoint(line1, line2)).toBe(true) + + }) + test("should ", () => { + + expect(calculateJunctionPoint(line1, line4)).toBe(false) + }) + test("", () => { + let l1 = new Line({ point1: new Point({ x: 1, y: 4 }), point2: new Point({ x: 2, y: 7 }) }); + let l2 = new Line({ point1: new Point({ x: 6, y: 12 }), point2: new Point({ x: 3, y: 8 }) }); + expect(calculateJunctionPoint(l1, l2)).toEqual({ x: 1.7999999999999998, y: 6.3999999999999995 }) + }) +}) + + +describe("IS_POINT_ON_LINE", () => { + + const point5 = new Point({ x: 2, y: 2 }) + const point6 = new Point({ x: 1, y: 1 }) + + const line4 = new Line({ x: point5, y: point6, slope: 1, n: 0 }) + + test("",()=>{ + expect(isPointOnLine(line4, point5)).toBe(true) + }) + it("", () => { + let l = new Line({ point1: new Point({ x: 1, y: 4 }), point2: new Point({ x: 2, y: 2 }) }); + let p = new Point({ x: 3, y: 7 }); + expect(isPointOnLine(l, p)).toBe(false) + }) + it("", () => { + + expect(isPointOnLine(line4, point6)).toBe(true) + }) + it("", () => { + + const point5 = new Point({ x: 2, y: 2 }) + const point6 = new Point({ x: 1, y: 1 }) + + const line4 = new Line({ x: point5, y: point6, slope: 1, n: 7 }) + expect(isPointOnLine(line4, point6)).toBe(false) + }) + +}) + +describe("ERROR", () => { + it('', () => { + expect(() => calculateDistance(point1)).toThrow("two points were not received") + }) + it('', () => { + expect(() => calculateJunctionPoint(line2)).toThrow("It should take two arguments") + }) + it('', () => { + let l = new Point() + expect(() => isPointOnLine(l)).toThrow("line were not received") + }) + it('', () => { + let p = new Line({}) + expect(() => isPointOnLine(p)).toThrow("point were not received") + }) + +})