diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index 826c675..9b132e3 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -1,15 +1,31 @@ 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; - this.n = n; + constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) { + if((point1 instanceof Point) ) + if((point2 instanceof Point)){ + this.point1 = point1; + this.point2 = point2; + if(typeof slope === 'number' || typeof slope === 'undefined') + this.slope = slope; + else + throw new Error('the type of slope is not number'); + if(typeof n === 'number' || typeof n === 'undefined') + this.n = n; + else + throw new Error('the type of n is not number'); + } + else + throw new Error('the type of point2 is not Point') + else + throw new Error('the type of point1 is not Point') } calculateSlope = () => { - this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) + if((this.point1.y - this.point2.y) !== 0) + this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) + else + throw new Error('division by zero') } calculateNOfLineFunction = () => { @@ -31,8 +47,12 @@ class Line { } getPointByY(y) { - let x = (y - this.n) / this.slope; - return new Point({ x, y }) + if(this.slope !== 0){ + let x = (y - this.n) / this.slope; + return new Point({ x, y }) + } + else + throw new Error('division by zero') } } diff --git a/modules/ecs6-class/point.js b/modules/ecs6-class/point.js index e81b4a4..91541e8 100644 --- a/modules/ecs6-class/point.js +++ b/modules/ecs6-class/point.js @@ -1,13 +1,22 @@ class Point { constructor({x=0, y=0}={}) { + if(typeof x !== "number") + throw new Error('type of x is not number') + if(typeof(y) !== "number") + throw new Error('type of y is not number') this.x = x; this.y = y; } moveVertical(value) { - this.y += value; + if(typeof(value) !== "number") + throw new Error('the type of the value is not correctly') + this.y += value; } moveHorizontal(value) { + if(typeof(value) !== "number") + throw new Error('the type of the value is not correctly') this.x += value; + } } diff --git a/modules/geometry-calculation.js b/modules/geometry-calculation.js index 6e11643..614e89f 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -1,13 +1,30 @@ const Line = require('./ecs6-class/line') +const Point = require('./ecs6-class/point') const calculateDistance = (point1, point2) => { + if(!(point1 instanceof Point)) + throw new Error('the type of point1 is not Point') + if(!(point2 instanceof Point)) + throw new Error('the type of point2 is not 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 instanceof Line)) + throw new Error('the type of line1 is not Line') + if(!(line2 instanceof Line)) + throw new Error('the type of line2 is not Line') + if(line1.slope === undefined) + line1.calculateSlope() + if(line2.slope === undefined) + line2.calculateSlope() + if(line1.n === undefined) + line1.calculateNOfLineFunction() + if(line2.n === undefined) + line2.calculateNOfLineFunction() if (line1.slope === line2.slope) { if (line1.n === line2.n) { return true @@ -24,15 +41,20 @@ const calculateJunctionPoint = (line1, line2) => { } const isPointOnLine = (line, point) => { + if(!(line instanceof Line)) + throw new Error('the type of line is not Line') + if(!(point instanceof Point)) + throw new Error('the type of point is not Point') const proxyLine = new Line({ point1: line.point1, point2: point }) proxyLine.calculateSlope() - if (line.slope === proxyLine.slope) { - proxyLine.calculateNOfLineFunction() - if (line.n === proxyLine.n) { - return true + if (line.slope === proxyLine.slope) { + proxyLine.calculateNOfLineFunction() + if (line.n === proxyLine.n) { + return true + } } - } return false + } module.exports = { diff --git a/package-lock.json b/package-lock.json index 61f4a09..faca934 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,9 @@ "license": "ISC", "dependencies": { "jest": "^29.7.0" + }, + "devDependencies": { + "nyc": "^17.0.0" } }, "node_modules/@ampproject/remapping": { @@ -894,6 +897,19 @@ "version": "21.0.3", "license": "MIT" }, + "node_modules/aggregate-error": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/aggregate-error/-/aggregate-error-3.1.0.tgz", + "integrity": "sha512-4I7Td01quW/RpocfNayFdFVk1qSuoh0E7JrbRJ16nH01HhKFQ88INq9Sd+nd72zqRySlr9BmDA8xlEJ6vJMrYA==", + "dev": true, + "dependencies": { + "clean-stack": "^2.0.0", + "indent-string": "^4.0.0" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/ansi-escapes": { "version": "4.3.2", "license": "MIT", @@ -938,6 +954,24 @@ "node": ">= 8" } }, + "node_modules/append-transform": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/append-transform/-/append-transform-2.0.0.tgz", + "integrity": "sha512-7yeyCEurROLQJFv5Xj4lEGTy0borxepjFv1g22oAdqFu//SrAlDl1O1Nxx15SH1RoliUml6p8dwJW9jvZughhg==", + "dev": true, + "dependencies": { + "default-require-extensions": "^3.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/archy": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/archy/-/archy-1.0.0.tgz", + "integrity": "sha512-Xg+9RwCg/0p32teKdGMPTPnVXKD0w3DfHnFTficozsAgsvq2XenPJq/MYpzzQ/v8zrOyJn6Ds39VA4JIDwFfqw==", + "dev": true + }, "node_modules/argparse": { "version": "1.0.10", "license": "MIT", @@ -1103,6 +1137,48 @@ "version": "1.1.2", "license": "MIT" }, + "node_modules/caching-transform": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/caching-transform/-/caching-transform-4.0.0.tgz", + "integrity": "sha512-kpqOvwXnjjN44D89K5ccQC+RUrsy7jB/XLlRrx0D7/2HNcTPqzsb6XgYoErwko6QsV184CA2YgS1fxDiiDZMWA==", + "dev": true, + "dependencies": { + "hasha": "^5.0.0", + "make-dir": "^3.0.0", + "package-hash": "^4.0.0", + "write-file-atomic": "^3.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/caching-transform/node_modules/make-dir": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-3.1.0.tgz", + "integrity": "sha512-g3FeP20LNwhALb/6Cz6Dd4F2ngze0jz7tbzrD2wAV+o9FeNHe4rL+yK2md0J/fiSf1sa1ADhXqi5+oVwOM/eGw==", + "dev": true, + "dependencies": { + "semver": "^6.0.0" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/caching-transform/node_modules/write-file-atomic": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/write-file-atomic/-/write-file-atomic-3.0.3.tgz", + "integrity": "sha512-AvHcyZ5JnSfq3ioSyjrBkH9yW4m7Ayk8/9My/DD9onKeu/94fwrMocemO2QAJFAlnnDN+ZDS+ZjAR5ua1/PV/Q==", + "dev": true, + "dependencies": { + "imurmurhash": "^0.1.4", + "is-typedarray": "^1.0.0", + "signal-exit": "^3.0.2", + "typedarray-to-buffer": "^3.1.5" + } + }, "node_modules/callsites": { "version": "3.1.0", "license": "MIT", @@ -1173,6 +1249,15 @@ "version": "1.2.3", "license": "MIT" }, + "node_modules/clean-stack": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/clean-stack/-/clean-stack-2.2.0.tgz", + "integrity": "sha512-4diC9HaTE+KRAMWhDhrGOECgWZxoevMc5TlkObMqNSsVU62PYzXZ/SMTjzyGAFF1YusgxGcSWTEXBhp0CPwQ1A==", + "dev": true, + "engines": { + "node": ">=6" + } + }, "node_modules/cliui": { "version": "8.0.1", "license": "ISC", @@ -1211,6 +1296,12 @@ "version": "1.1.4", "license": "MIT" }, + "node_modules/commondir": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/commondir/-/commondir-1.0.1.tgz", + "integrity": "sha512-W9pAhw0ja1Edb5GVdIF1mjZw/ASI0AlShXM83UUGe2DVr5TdAPEA1OA8m/g8zWp9x6On7gqufY+FatDbC3MDQg==", + "dev": true + }, "node_modules/concat-map": { "version": "0.0.1", "license": "MIT" @@ -1265,6 +1356,15 @@ } } }, + "node_modules/decamelize": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-1.2.0.tgz", + "integrity": "sha512-z2S+W9X73hAUUki+N+9Za2lBlun89zigOyGrsax+KUQ6wKW4ZoWpEYBkGhQjwAjjDCkWxhY0VKEhk8wzY7F5cA==", + "dev": true, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/dedent": { "version": "1.5.1", "license": "MIT", @@ -1284,6 +1384,21 @@ "node": ">=0.10.0" } }, + "node_modules/default-require-extensions": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/default-require-extensions/-/default-require-extensions-3.0.1.tgz", + "integrity": "sha512-eXTJmRbm2TIt9MgWTsOH1wEuhew6XGZcMeGKCtLedIg/NCsg1iBePXkceTdK4Fii7pzmN9tGsZhKzZ4h7O/fxw==", + "dev": true, + "dependencies": { + "strip-bom": "^4.0.0" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/detect-newline": { "version": "3.1.0", "license": "MIT", @@ -1323,6 +1438,12 @@ "is-arrayish": "^0.2.1" } }, + "node_modules/es6-error": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/es6-error/-/es6-error-4.1.1.tgz", + "integrity": "sha512-Um/+FxMr9CISWh0bi5Zv0iOD+4cFh5qLeks1qhAopKVAJw3drgKbKySikp7wGhDL0HPeaja0P5ULZrxLkniUVg==", + "dev": true + }, "node_modules/escalade": { "version": "3.1.2", "license": "MIT", @@ -1410,6 +1531,38 @@ "node": ">=8" } }, + "node_modules/find-cache-dir": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/find-cache-dir/-/find-cache-dir-3.3.2.tgz", + "integrity": "sha512-wXZV5emFEjrridIgED11OoUKLxiYjAcqot/NJdAkOhlJ+vGzwhOAfcG5OX1jP+S0PcjEn8bdMJv+g2jwQ3Onig==", + "dev": true, + "dependencies": { + "commondir": "^1.0.1", + "make-dir": "^3.0.2", + "pkg-dir": "^4.1.0" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/avajs/find-cache-dir?sponsor=1" + } + }, + "node_modules/find-cache-dir/node_modules/make-dir": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-3.1.0.tgz", + "integrity": "sha512-g3FeP20LNwhALb/6Cz6Dd4F2ngze0jz7tbzrD2wAV+o9FeNHe4rL+yK2md0J/fiSf1sa1ADhXqi5+oVwOM/eGw==", + "dev": true, + "dependencies": { + "semver": "^6.0.0" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/find-up": { "version": "4.1.0", "license": "MIT", @@ -1421,6 +1574,39 @@ "node": ">=8" } }, + "node_modules/foreground-child": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/foreground-child/-/foreground-child-2.0.0.tgz", + "integrity": "sha512-dCIq9FpEcyQyXKCkyzmlPTFNgrCzPudOe+mhvJU5zAtlBnGVy2yKxtfsxK2tQBThwq225jcvBjpw1Gr40uzZCA==", + "dev": true, + "dependencies": { + "cross-spawn": "^7.0.0", + "signal-exit": "^3.0.2" + }, + "engines": { + "node": ">=8.0.0" + } + }, + "node_modules/fromentries": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/fromentries/-/fromentries-1.3.2.tgz", + "integrity": "sha512-cHEpEQHUg0f8XdtZCc2ZAhrHzKzT0MrFUTcvx+hfxYu7rGMDc5SKoXFh+n4YigxsHXRzc6OrCshdR1bWH6HHyg==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, "node_modules/fs.realpath": { "version": "1.0.0", "license": "ISC" @@ -1499,6 +1685,31 @@ "node": ">=8" } }, + "node_modules/hasha": { + "version": "5.2.2", + "resolved": "https://registry.npmjs.org/hasha/-/hasha-5.2.2.tgz", + "integrity": "sha512-Hrp5vIK/xr5SkeN2onO32H0MgNZ0f17HRNH39WfL0SYUNOTZ5Lz1TJ8Pajo/87dYGEFlLMm7mIc/k/s6Bvz9HQ==", + "dev": true, + "dependencies": { + "is-stream": "^2.0.0", + "type-fest": "^0.8.0" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/hasha/node_modules/type-fest": { + "version": "0.8.1", + "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.8.1.tgz", + "integrity": "sha512-4dbzIzqvjtgiM5rw1k5rEHtBANKmdudhGyBEajN01fEyhaAIhsoKNy6y7+IN93IfpFtwY9iqi7kD+xwKhQsNJA==", + "dev": true, + "engines": { + "node": ">=8" + } + }, "node_modules/hasown": { "version": "2.0.2", "license": "MIT", @@ -1544,6 +1755,15 @@ "node": ">=0.8.19" } }, + "node_modules/indent-string": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/indent-string/-/indent-string-4.0.0.tgz", + "integrity": "sha512-EdDDZu4A2OyIK7Lr/2zG+w5jmbuk1DVBnEwREQvBzspBJkCEbRa8GxU1lghYcaGJCnRWibjDXlq779X1/y5xwg==", + "dev": true, + "engines": { + "node": ">=8" + } + }, "node_modules/inflight": { "version": "1.0.6", "license": "ISC", @@ -1601,6 +1821,21 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/is-typedarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/is-typedarray/-/is-typedarray-1.0.0.tgz", + "integrity": "sha512-cyA56iCMHAh5CdzjJIa4aohJyeO1YbwLi3Jc35MmRU6poroFjIGZzUzupGiRPOjgHg9TLu43xbpwXk523fMxKA==", + "dev": true + }, + "node_modules/is-windows": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/is-windows/-/is-windows-1.0.2.tgz", + "integrity": "sha512-eXK1UInq2bPmjyX6e3VHIzMLobc4J94i4AWn+Hpq3OU5KkrRC96OAcR3PRJ/pGu6m8TRnBHP9dkXQVsT/COVIA==", + "dev": true, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/isexe": { "version": "2.0.0", "license": "ISC" @@ -1612,6 +1847,18 @@ "node": ">=8" } }, + "node_modules/istanbul-lib-hook": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/istanbul-lib-hook/-/istanbul-lib-hook-3.0.0.tgz", + "integrity": "sha512-Pt/uge1Q9s+5VAZ+pCo16TYMWPBIl+oaNIjgLQxcX0itS6ueeaA+pEfThZpH8WxhFgCiEb8sAJY6MdUKgiIWaQ==", + "dev": true, + "dependencies": { + "append-transform": "^2.0.0" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/istanbul-lib-instrument": { "version": "6.0.2", "license": "BSD-3-Clause", @@ -1653,6 +1900,23 @@ "version": "4.0.0", "license": "ISC" }, + "node_modules/istanbul-lib-processinfo": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/istanbul-lib-processinfo/-/istanbul-lib-processinfo-2.0.3.tgz", + "integrity": "sha512-NkwHbo3E00oybX6NGJi6ar0B29vxyvNwoC7eJ4G4Yq28UfY758Hgn/heV8VRFhevPED4LXfFz0DQ8z/0kw9zMg==", + "dev": true, + "dependencies": { + "archy": "^1.0.0", + "cross-spawn": "^7.0.3", + "istanbul-lib-coverage": "^3.2.0", + "p-map": "^3.0.0", + "rimraf": "^3.0.0", + "uuid": "^8.3.2" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/istanbul-lib-report": { "version": "3.0.1", "license": "BSD-3-Clause", @@ -2282,6 +2546,12 @@ "node": ">=8" } }, + "node_modules/lodash.flattendeep": { + "version": "4.4.0", + "resolved": "https://registry.npmjs.org/lodash.flattendeep/-/lodash.flattendeep-4.4.0.tgz", + "integrity": "sha512-uHaJFihxmJcEX3kT4I23ABqKKalJ/zDrDg0lsFtc1h+3uw49SIJ5beyhx5ExVRti3AvKoOJngIj7xz3oylPdWQ==", + "dev": true + }, "node_modules/lru-cache": { "version": "5.1.1", "license": "ISC", @@ -2380,6 +2650,18 @@ "version": "0.4.0", "license": "MIT" }, + "node_modules/node-preload": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/node-preload/-/node-preload-0.2.1.tgz", + "integrity": "sha512-RM5oyBy45cLEoHqCeh+MNuFAxO0vTFBLskvQbOKnEE7YTTSN4tbN8QWDIPQ6L+WvKsB/qLEGpYe2ZZ9d4W9OIQ==", + "dev": true, + "dependencies": { + "process-on-spawn": "^1.0.0" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/node-releases": { "version": "2.0.14", "license": "MIT" @@ -2401,6 +2683,134 @@ "node": ">=8" } }, + "node_modules/nyc": { + "version": "17.0.0", + "resolved": "https://registry.npmjs.org/nyc/-/nyc-17.0.0.tgz", + "integrity": "sha512-ISp44nqNCaPugLLGGfknzQwSwt10SSS5IMoPR7GLoMAyS18Iw5js8U7ga2VF9lYuMZ42gOHr3UddZw4WZltxKg==", + "dev": true, + "dependencies": { + "@istanbuljs/load-nyc-config": "^1.0.0", + "@istanbuljs/schema": "^0.1.2", + "caching-transform": "^4.0.0", + "convert-source-map": "^1.7.0", + "decamelize": "^1.2.0", + "find-cache-dir": "^3.2.0", + "find-up": "^4.1.0", + "foreground-child": "^2.0.0", + "get-package-type": "^0.1.0", + "glob": "^7.1.6", + "istanbul-lib-coverage": "^3.0.0", + "istanbul-lib-hook": "^3.0.0", + "istanbul-lib-instrument": "^6.0.2", + "istanbul-lib-processinfo": "^2.0.2", + "istanbul-lib-report": "^3.0.0", + "istanbul-lib-source-maps": "^4.0.0", + "istanbul-reports": "^3.0.2", + "make-dir": "^3.0.0", + "node-preload": "^0.2.1", + "p-map": "^3.0.0", + "process-on-spawn": "^1.0.0", + "resolve-from": "^5.0.0", + "rimraf": "^3.0.0", + "signal-exit": "^3.0.2", + "spawn-wrap": "^2.0.0", + "test-exclude": "^6.0.0", + "yargs": "^15.0.2" + }, + "bin": { + "nyc": "bin/nyc.js" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/nyc/node_modules/cliui": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/cliui/-/cliui-6.0.0.tgz", + "integrity": "sha512-t6wbgtoCXvAzst7QgXxJYqPt0usEfbgQdftEPbLL/cvv6HPE5VgvqCuAIDR0NgU52ds6rFwqrgakNLrHEjCbrQ==", + "dev": true, + "dependencies": { + "string-width": "^4.2.0", + "strip-ansi": "^6.0.0", + "wrap-ansi": "^6.2.0" + } + }, + "node_modules/nyc/node_modules/convert-source-map": { + "version": "1.9.0", + "resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-1.9.0.tgz", + "integrity": "sha512-ASFBup0Mz1uyiIjANan1jzLQami9z1PoYSZCiiYW2FczPbenXc45FZdBZLzOT+r6+iciuEModtmCti+hjaAk0A==", + "dev": true + }, + "node_modules/nyc/node_modules/make-dir": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-3.1.0.tgz", + "integrity": "sha512-g3FeP20LNwhALb/6Cz6Dd4F2ngze0jz7tbzrD2wAV+o9FeNHe4rL+yK2md0J/fiSf1sa1ADhXqi5+oVwOM/eGw==", + "dev": true, + "dependencies": { + "semver": "^6.0.0" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/nyc/node_modules/wrap-ansi": { + "version": "6.2.0", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-6.2.0.tgz", + "integrity": "sha512-r6lPcBGxZXlIcymEu7InxDMhdW0KDxpLgoFLcguasxCaJ/SOIZwINatK9KY/tf+ZrlywOKU0UDj3ATXUBfxJXA==", + "dev": true, + "dependencies": { + "ansi-styles": "^4.0.0", + "string-width": "^4.1.0", + "strip-ansi": "^6.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/nyc/node_modules/y18n": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.3.tgz", + "integrity": "sha512-JKhqTOwSrqNA1NY5lSztJ1GrBiUodLMmIZuLiDaMRJ+itFd+ABVE8XBjOvIWL+rSqNDC74LCSFmlb/U4UZ4hJQ==", + "dev": true + }, + "node_modules/nyc/node_modules/yargs": { + "version": "15.4.1", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-15.4.1.tgz", + "integrity": "sha512-aePbxDmcYW++PaqBsJ+HYUFwCdv4LVvdnhBy78E57PIor8/OVvhMrADFFEDh8DHDFRv/O9i3lPhsENjO7QX0+A==", + "dev": true, + "dependencies": { + "cliui": "^6.0.0", + "decamelize": "^1.2.0", + "find-up": "^4.1.0", + "get-caller-file": "^2.0.1", + "require-directory": "^2.1.1", + "require-main-filename": "^2.0.0", + "set-blocking": "^2.0.0", + "string-width": "^4.2.0", + "which-module": "^2.0.0", + "y18n": "^4.0.0", + "yargs-parser": "^18.1.2" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/nyc/node_modules/yargs-parser": { + "version": "18.1.3", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-18.1.3.tgz", + "integrity": "sha512-o50j0JeToy/4K6OZcaQmW6lyXXKhq7csREXcDwk2omFPJEwUNOVtJKvmDr9EI1fAJZUyZcRF7kxGBWmRXudrCQ==", + "dev": true, + "dependencies": { + "camelcase": "^5.0.0", + "decamelize": "^1.2.0" + }, + "engines": { + "node": ">=6" + } + }, "node_modules/once": { "version": "1.4.0", "license": "ISC", @@ -2457,6 +2867,18 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/p-map": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/p-map/-/p-map-3.0.0.tgz", + "integrity": "sha512-d3qXVTF/s+W+CdJ5A29wywV2n8CQQYahlgz2bFiA+4eVNJbHJodPZ+/gXwPGh0bOqA+j8S+6+ckmvLGPk1QpxQ==", + "dev": true, + "dependencies": { + "aggregate-error": "^3.0.0" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/p-try": { "version": "2.2.0", "license": "MIT", @@ -2464,6 +2886,21 @@ "node": ">=6" } }, + "node_modules/package-hash": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/package-hash/-/package-hash-4.0.0.tgz", + "integrity": "sha512-whdkPIooSu/bASggZ96BWVvZTRMOFxnyUG5PnTSGKoJE2gd5mbVNmR2Nj20QFzxYYgAXpoqC+AiXzl+UMRh7zQ==", + "dev": true, + "dependencies": { + "graceful-fs": "^4.1.15", + "hasha": "^5.0.0", + "lodash.flattendeep": "^4.4.0", + "release-zalgo": "^1.0.0" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/parse-json": { "version": "5.2.0", "license": "MIT", @@ -2558,6 +2995,18 @@ "url": "https://github.com/chalk/ansi-styles?sponsor=1" } }, + "node_modules/process-on-spawn": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/process-on-spawn/-/process-on-spawn-1.0.0.tgz", + "integrity": "sha512-1WsPDsUSMmZH5LeMLegqkPDrsGgsWwk1Exipy2hvB0o/F0ASzbpIctSCcZIK1ykJvtTJULEH+20WOFjMvGnCTg==", + "dev": true, + "dependencies": { + "fromentries": "^1.2.0" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/prompts": { "version": "2.4.2", "license": "MIT", @@ -2587,6 +3036,18 @@ "version": "18.2.0", "license": "MIT" }, + "node_modules/release-zalgo": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/release-zalgo/-/release-zalgo-1.0.0.tgz", + "integrity": "sha512-gUAyHVHPPC5wdqX/LG4LWtRYtgjxyX78oanFNTMMyFEfOqdC54s3eE82imuWKbOeqYht2CrNf64Qb8vgmmtZGA==", + "dev": true, + "dependencies": { + "es6-error": "^4.0.1" + }, + "engines": { + "node": ">=4" + } + }, "node_modules/require-directory": { "version": "2.1.1", "license": "MIT", @@ -2594,6 +3055,12 @@ "node": ">=0.10.0" } }, + "node_modules/require-main-filename": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/require-main-filename/-/require-main-filename-2.0.0.tgz", + "integrity": "sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg==", + "dev": true + }, "node_modules/resolve": { "version": "1.22.8", "license": "MIT", @@ -2633,6 +3100,22 @@ "node": ">=10" } }, + "node_modules/rimraf": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz", + "integrity": "sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==", + "deprecated": "Rimraf versions prior to v4 are no longer supported", + "dev": true, + "dependencies": { + "glob": "^7.1.3" + }, + "bin": { + "rimraf": "bin.js" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, "node_modules/semver": { "version": "6.3.1", "license": "ISC", @@ -2640,6 +3123,12 @@ "semver": "bin/semver.js" } }, + "node_modules/set-blocking": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz", + "integrity": "sha512-KiKBS8AnWGEyLzofFfmvKwpdPzqiy16LvQfK3yv/fVH7Bj13/wl3JSR1J+rfgRE9q7xUJK4qvgS8raSOeLUehw==", + "dev": true + }, "node_modules/shebang-command": { "version": "2.0.0", "license": "MIT", @@ -2687,6 +3176,38 @@ "source-map": "^0.6.0" } }, + "node_modules/spawn-wrap": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/spawn-wrap/-/spawn-wrap-2.0.0.tgz", + "integrity": "sha512-EeajNjfN9zMnULLwhZZQU3GWBoFNkbngTUPfaawT4RkMiviTxcX0qfhVbGey39mfctfDHkWtuecgQ8NJcyQWHg==", + "dev": true, + "dependencies": { + "foreground-child": "^2.0.0", + "is-windows": "^1.0.2", + "make-dir": "^3.0.0", + "rimraf": "^3.0.0", + "signal-exit": "^3.0.2", + "which": "^2.0.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/spawn-wrap/node_modules/make-dir": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-3.1.0.tgz", + "integrity": "sha512-g3FeP20LNwhALb/6Cz6Dd4F2ngze0jz7tbzrD2wAV+o9FeNHe4rL+yK2md0J/fiSf1sa1ADhXqi5+oVwOM/eGw==", + "dev": true, + "dependencies": { + "semver": "^6.0.0" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/sprintf-js": { "version": "1.0.3", "license": "BSD-3-Clause" @@ -2828,6 +3349,15 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/typedarray-to-buffer": { + "version": "3.1.5", + "resolved": "https://registry.npmjs.org/typedarray-to-buffer/-/typedarray-to-buffer-3.1.5.tgz", + "integrity": "sha512-zdu8XMNEDepKKR+XYOXAVPtWui0ly0NtohUscw+UmaHiAWT8hrV1rr//H6V+0DvJ3OQ19S979M0laLfX8rm82Q==", + "dev": true, + "dependencies": { + "is-typedarray": "^1.0.0" + } + }, "node_modules/undici-types": { "version": "5.26.5", "license": "MIT" @@ -2860,6 +3390,15 @@ "browserslist": ">= 4.21.0" } }, + "node_modules/uuid": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", + "dev": true, + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/v8-to-istanbul": { "version": "9.2.0", "license": "ISC", @@ -2892,6 +3431,12 @@ "node": ">= 8" } }, + "node_modules/which-module": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/which-module/-/which-module-2.0.1.tgz", + "integrity": "sha512-iBdZ57RDvnOR9AGBhML2vFZf7h8vmBjhoaZqODJBFWHVtKkDmKuHai3cx5PgVMrX5YDNp27AofYbAwctSS+vhQ==", + "dev": true + }, "node_modules/wrap-ansi": { "version": "7.0.0", "license": "MIT", diff --git a/package.json b/package.json index 56bf17b..9a8b6e4 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": "npm run test -- --coverage" }, "dependencies": { "jest": "^29.7.0" @@ -18,6 +19,8 @@ }, "homepage": "https://github.com/gemtechd/build-tests#readme", "author": "gemtechd", - "license": "ISC" - + "license": "ISC", + "devDependencies": { + "nyc": "^17.0.0" + } } diff --git a/tests/modules/ecs6-class/line.test.js b/tests/modules/ecs6-class/line.test.js new file mode 100644 index 0000000..8487ebf --- /dev/null +++ b/tests/modules/ecs6-class/line.test.js @@ -0,0 +1,93 @@ +const Line = require('../../../modules/ecs6-class/line') +const Point = require('../../../modules/ecs6-class/point') + +let mockCalculatSlope = jest.fn() + +// jest.mock('../../modules/ecs6-class/line',()=>{ +// return{ +// calculateSlope:mockCalculatSlope +// } +// }) + +// mockCalculatSlope.mockImplementation() + +describe('CONSTRUCTOR',() => { + it('should build an object with default values',() => { + const line = new Line({slope:5,n:4}) + expect(line.slope).toBe(5) + expect(line.n).toBe(4) + expect(line.point1.x).toBe(0) + }) + it('An error should be thrown when the point1 is not of type Point',() => { + expect(()=> new Line({point1:[],point2:new Point({x:2,y:2})})).toThrowError('the type of point1 is not Point') + }) + + it('An error should be thrown when the point2 is not of type Point',() => { + expect(()=> new Line({point1:new Point({x:2,y:2}),point2:[]})).toThrowError('the type of point2 is not Point') + }) + + it('An error should be thrown when the slope is not of type number',() => { + expect(()=> new Line({point1:new Point({x:2,y:2,}),point2:new Point({x:2,y:2}),slope:'a'})).toThrowError('the type of slope is not number') + }) + it('An error should be thrown when the n is not of type number',() => { + expect(()=> new Line({point1:new Point({x:2,y:2}),point2:new Point({x:2,y:2}),n:'a'})).toThrowError('the type of n is not number') + }) +}) + + +describe('CALCULATE_SLOPE',()=>{ + it('should return the correct answer',() => { + const line = new Line({point1:new Point({x:6,y:4}),point2:new Point({x:2,y:2})}) + line.calculateSlope() + expect(line.slope).toBe(0.5) + }) + it('An error should be thrown when the counter equals 0', () => { + const line = new Line({point1:new Point({x:6,y:4}),point2:new Point({x:2,y:4})}) + expect(()=>line.calculateSlope()).toThrowError('division by zero') + }) +}) + +describe('CALCULATE_N_OF_LINE_FUNCTION',() => { + it('should return the correct answer',() => { + const line = new Line({point1:new Point({x:6,y:4}),point2:new Point({x:2,y:2})}) + line.calculateSlope() + line.calculateNOfLineFunction() + expect(line.n).toBe(1) + }) +}) + +describe('GET_POINT_ON_X_ASIS', () =>{ + it('should return the correct answer',() => { + const line = new Line({point1:new Point({x:6,y:4}),point2:new Point({x:2,y:2}),slope:2,n:-8}) + const result = line.getPointOnXAsis() + expect(result).toEqual({x:4,y:0}) + }) +}) + +describe('GET_POINT_ON_Y_ASIS', () =>{ + it('should return the correct answer',() => { + const line = new Line({point1:new Point({x:6,y:4}),point2:new Point({x:2,y:2}),slope:2,n:-8}) + const result = line.getPointOnYAsis() + expect(result).toEqual({x:0,y:-8}) + }) +}) + +describe('GET_POINT_BY_X', () =>{ + it('should return the correct answer',() => { + const line = new Line({point1:new Point({x:6,y:4}),point2:new Point({x:2,y:2}),slope:2,n:-8}) + const result = line.getPointByX(2) + expect(result).toEqual({x:2,y:-4}) + }) +}) + +describe('GET_POINT_BY_Y', () =>{ + it('should return the correct answer',() => { + const line = new Line({point1:new Point({x:6,y:4}),point2:new Point({x:2,y:2}),slope:2,n:-8}) + const result = line.getPointByY(2) + expect(result).toEqual({x:5,y:2}) + }) + it('An error should be thrown when the slope equals 0', () => { + const line = new Line({point1:new Point({x:2,y:6}),point2:new Point({x:1,y:6}),n:6,slope:0}) + expect(()=>line.getPointByY(11)).toThrowError('division by zero') + }) +}) \ No newline at end of file diff --git a/tests/modules/ecs6-class/point.test.js b/tests/modules/ecs6-class/point.test.js new file mode 100644 index 0000000..732b7f0 --- /dev/null +++ b/tests/modules/ecs6-class/point.test.js @@ -0,0 +1,58 @@ +const Point = require('../../../modules/ecs6-class/point') + +describe('CONSTRUCTOR',() => { + it('should build an object with default values',()=>{ + const point = new Point() + expect(point).toEqual({x:0,y:0}) + }) + it('should build an object',()=>{ + const point = new Point({x:8,y:9}) + expect(point).toEqual({x:8,y:9}) + }) + it('should build an object',()=>{ + const point = new Point({x:-8,y:-9}) + expect(point).toEqual({x:-8,y:-9}) + }) + it('should build an object',()=>{ + const point = new Point({x:5.5}) + expect(point).toEqual({x:5.5,y:0}) + }) + + it('An error should be thrown when the type of x is not number' , () => { + expect(() => new Point({x:'l',y:9})).toThrowError('type of x is not number') + }) + it('An error should be thrown when the type of x is not number' , () => { + expect(() => new Point({x:[9,0],y:9})).toThrowError('type of x is not number') + }) + it('An error should be thrown when the type of x is not number' , () => { + expect(() => new Point({x:4,y:[]})).toThrowError('type of y is not number') + }) + it('An error should be thrown when the type of y is not number' , () => { + expect(() => new Point({x:5,y:'a'})).toThrowError('type of y is not number') + }) +}) + +describe('MOVE_VERTICAL', ()=> { + it('should return the correct answer',()=> { + const point = new Point({x:5,y:6}) + point.moveVertical(5) + expect(point.y).toBe(11) + }) + it('An error should be thrown when the type of value is not number',()=> { + const point = new Point({x:5,y:6}) + expect(() => point.moveVertical('a')).toThrowError('the type of the value is not correctly') +}) +}) + +describe('MOVE_HORIZONTAL', ()=> { + it('should return the correct answer',()=> { + const point = new Point({x:5,y:6}) + point.moveHorizontal(5) + expect(point.x).toBe(10) + }) + + it('An error should be thrown when the type of value is not number',()=> { + const point = new Point({x:5,y:6}) + expect(()=>point.moveHorizontal('a')).toThrowError('the type of the value is not correctly') + }) + }) \ No newline at end of file diff --git a/tests/modules/geometry-calculation.test.js b/tests/modules/geometry-calculation.test.js new file mode 100644 index 0000000..96ca348 --- /dev/null +++ b/tests/modules/geometry-calculation.test.js @@ -0,0 +1,98 @@ + +const Line = require('../../modules/ecs6-class/line') +const Point = require('../../modules/ecs6-class/point') +const { calculateDistance,calculateJunctionPoint,isPointOnLine} = require('../../modules/geometry-calculation') + + +describe('CALCULATE_DISTANCE',() => { + it('should return the correct answer', () => { + const line = new Line({point1:new Point({x:6,y:4}),point2:new Point({x:2,y:1})}) + const result = calculateDistance(line.point1,line.point2) + expect(result).toBe(5) + }) + it('An error should be thrown when point1 is not of type Point', () => { + expect(()=>calculateDistance('k',new Point({x:6,y:4}))).toThrowError('the type of point1 is not Point') + }) + it('An error should be thrown when point2 is not of type Point', () => { + expect(()=>calculateDistance(new Point({x:6,y:4}),'k')).toThrowError('the type of point2 is not Point') + }) +}) + +describe('CALCULATE_JUNCTION_POINT', () => { + it('should return true when the 2 lines are equal', () =>{ + const line1 = new Line({point1:new Point({x:6,y:4}),point2:new Point({x:2,y:1}),n:-0.5,slope:0.75}) + const line2 = new Line({point1:new Point({x:6,y:4}),point2:new Point({x:2,y:1}),n:-0.5,slope:0.75}) + const result = calculateJunctionPoint(line1,line2) + expect(result).toBeTruthy() + }) + it('should return false when the 2 lines are parallels', () =>{ + const line1 = new Line({point1:new Point({x:6,y:4}),point2:new Point({x:2,y:1}),n:-0.5,slope:0.75}) + const line2 = new Line({point1:new Point({x:5,y:3}),point2:new Point({x:1,y:0}),n:-0.75,slope:0.75}) + const result = calculateJunctionPoint(line1,line2) + expect(result).toBeFalsy() + }) + it('should return false when the 2 lines are parallels', () =>{ + const line1 = new Line({point1:new Point({x:6,y:4}),point2:new Point({x:2,y:1})}) + const line2 = new Line({point1:new Point({x:5,y:3}),point2:new Point({x:1,y:0})}) + const result = calculateJunctionPoint(line1,line2) + expect(result).toBeFalsy() + }) + it('should return point when the 2 lines are not equal', () =>{ + const line1 = new Line({point1:new Point({x:8,y:3}),point2:new Point({x:6,y:1}),slope:1}) + const line2 = new Line({point1:new Point({x:5,y:3}),point2:new Point({x:1,y:0}),slope:0.75}) + line1.calculateNOfLineFunction() + line2.calculateNOfLineFunction() + const result = calculateJunctionPoint(line1,line2) + expect(result).toEqual({ x: 17, y: 12 }) + }) + it('An error should be thrown when line2 is not of type Line', () =>{ + const line1 = new Line({point1:new Point({x:8,y:3}),point2:new Point({x:6,y:1}),slope:1}) + line1.calculateNOfLineFunction() + expect(()=>calculateJunctionPoint(line1,[])).toThrowError('the type of line2 is not Line') + }) + it('An error should be thrown when line1 is not of type Line', () =>{ + const line2 = new Line({point1:new Point({x:8,y:3}),point2:new Point({x:6,y:1}),slope:1}) + line2.calculateNOfLineFunction() + expect(()=>calculateJunctionPoint('g',line2)).toThrowError('the type of line1 is not Line') + }) +}) + + +describe('IS_POINT_ON_LINE',() => { + it('should return true when the point on this line',() => { + const line = new Line({point1:new Point({x:8,y:4}),point2:new Point({x:2,y:1}),slope:0.5}) + line.calculateNOfLineFunction() + const point = new Point({x:6,y:3}) + const result = isPointOnLine(line,point) + expect(result).toBeTruthy() + }) + + it('should return true when the point on this line',() => { + const line = new Line({point1:new Point({x:10,y:10}),point2:new Point({x:8,y:2}),slope:4}) + line.calculateNOfLineFunction() + const point = new Point({x:6,y:-6}) + const result = isPointOnLine(line,point) + expect(result).toBeTruthy() + }) + it('should return false when the point is not on this line',() => { + const line = new Line({point1:new Point({x:8,y:3}),point2:new Point({x:6,y:1}),slope:1}) + line.calculateNOfLineFunction() + const point = new Point({x:5,y:6}) + const result = isPointOnLine(line,point) + expect(result).toBeFalsy() + }) + it('An error should be thrown when the point is not of type Point', () =>{ + const line = new Line({point1:new Point({x:8,y:3}),point2:new Point({x:6,y:1}),slope:1}) + line.calculateNOfLineFunction() + expect(()=>isPointOnLine(line,'k')).toThrowError('the type of point is not Point') + }) + it('An error should be thrown when line is not of type Line', () => { + const point = new Point({x:6,y:4}) + expect(()=>isPointOnLine('k',point)).toThrowError('the type of line is not Line') + }) + it('An error should be thrown when the slope is undefined', () => { + const line = new Line({point1:new Point({x:8,y:4}),point2:new Point({x:6,y:1})}) + const point = new Point({x:6,y:4}) + expect(() => isPointOnLine(line,point)).toThrowError('division by zero') + }) +}) \ No newline at end of file