From 4b64b4e3067e5cc4f67c381d99127948d1e87f89 Mon Sep 17 00:00:00 2001 From: berkovich Date: Mon, 22 Jul 2024 10:01:45 +0300 Subject: [PATCH] create tests --- modules/ecs6-class/line.js | 20 +++++++++++++++++++- modules/ecs6-class/point.js | 11 ++++++++++- modules/geometry-calculation.js | 19 ++++++++++++++++++- package.json | 3 ++- 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index 826c675..144fdf8 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -2,6 +2,12 @@ const Point = require("./point"); class Line { constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) { + if(typeof(point1)!="object" && point1 !=undefined || typeof(point2)!="object" && point2 !=undefined){ + throw new Error('argument must be type point') + } + if (typeof(n)!="number"&& n !=undefined|| typeof(slope)!="number" && slope !=undefined){ + throw new Error('argument must be type number') + } this.point1 = point1; this.point2 = point2; this.slope = slope; @@ -9,10 +15,16 @@ class Line { } calculateSlope = () => { + if( this.point1.x==this.point1.y && this.point1.y==0 || this.point2.x==this.point2.y && this.point2.y==0){ + throw new Error("missing data") + } this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) } calculateNOfLineFunction = () => { + if(this.slope==undefined || this.point1==undefined){ + throw new Error("missing data") + } this.n = this.point1.y - this.slope * this.point1.x } @@ -26,14 +38,20 @@ class Line { getPointByX(x) { + if(typeof(x)!="number"){ + throw new Error('argument must be type number') + } let y = this.slope * x + this.n return new Point({ x, y }) } getPointByY(y) { + if(typeof(y)!="number"){ + throw new Error('argument must be type number') + } let x = (y - this.n) / this.slope; return new Point({ x, y }) } } -module.exports = Line \ No newline at end of file +module.exports = Line diff --git a/modules/ecs6-class/point.js b/modules/ecs6-class/point.js index e81b4a4..d8fe260 100644 --- a/modules/ecs6-class/point.js +++ b/modules/ecs6-class/point.js @@ -1,14 +1,23 @@ class Point { constructor({x=0, y=0}={}) { + if(typeof(x)!="number" || typeof(y)!="number"){ + throw new Error('argument must be type number') + } this.x = x; this.y = y; } moveVertical(value) { + if(typeof(value)!="number"){ + throw new Error('argument must be type number') + } this.y += value; } moveHorizontal(value) { + if(typeof(value)!="number"){ + throw new Error('argument must be type number') + } this.x += value; } } -module.exports = Point \ No newline at end of file +module.exports = Point diff --git a/modules/geometry-calculation.js b/modules/geometry-calculation.js index 6e11643..68a9fde 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -1,13 +1,24 @@ const Line = require('./ecs6-class/line') +const Point = require('./ecs6-class/point') const calculateDistance = (point1, point2) => { + if(!point1 || !point2){ + throw new Error('missing data') + } + if(typeof(point1)!= 'object' |typeof(point2)!='object'){ + throw new Error('argument must by 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("missing data") + } if (line1.slope === line2.slope) { if (line1.n === line2.n) { return true @@ -24,6 +35,9 @@ const calculateJunctionPoint = (line1, line2) => { } const isPointOnLine = (line, point) => { + if(line==undefined || point==undefined){ + throw new Error("missing data") + } const proxyLine = new Line({ point1: line.point1, point2: point }) proxyLine.calculateSlope() if (line.slope === proxyLine.slope) { @@ -40,3 +54,6 @@ module.exports = { calculateJunctionPoint, isPointOnLine } + + + diff --git a/package.json b/package.json index 56bf17b..ddf2df5 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"