-
Notifications
You must be signed in to change notification settings - Fork 41
tests #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
tests #34
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,17 +2,35 @@ const Point = require("./point"); | |
|
|
||
| class Line { | ||
| constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) { | ||
| if ((!(point1 instanceof Point))) { | ||
| throw new Error('the constructor should get point1 arguments of "Point"') | ||
| } | ||
| if ((!(point2 instanceof Point))) { | ||
| throw new Error('the constructor should get point2 arguments of "Point"') | ||
| } | ||
| if ((typeof (slope) !== 'number' && typeof (slope) !== "undefined")) { | ||
| throw new Error('the slope in constractor should get undefined or number') | ||
| } | ||
| if ((typeof (n) !== "undefined" && typeof (n) !== "number")) { | ||
| throw new Error('the n in constractor should get undefined or number') | ||
| } | ||
| this.point1 = point1; | ||
| this.point2 = point2; | ||
| this.slope = slope; | ||
| this.n = n; | ||
| } | ||
|
|
||
| calculateSlope() { | ||
| calculateSlope() {//שיפוע | ||
| if ((this.point1.x - this.point2.x) === 0) { | ||
| throw new Error('the argument equal to 0'); | ||
| } | ||
| this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) | ||
| } | ||
|
|
||
| calculateNOfLineFunction() { | ||
| calculateNOfLineFunction() {//מרחק | ||
| if(this.slope===undefined){ | ||
| throw new Error('The slope has not yet been defined') | ||
| } | ||
| this.n = this.point1.y - this.slope * this.point1.x | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. was the slope already calculated? |
||
| } | ||
|
|
||
|
|
@@ -26,11 +44,23 @@ class Line { | |
|
|
||
|
|
||
| getPointByX(x) { | ||
| if (x === undefined) { | ||
| throw new Error('the function should get a number') | ||
| } | ||
| if (typeof (x) != 'number') { | ||
| throw new Error('the function should get a number') | ||
| } | ||
| let y = this.slope * x + this.n | ||
| return new Point({ x, y }) | ||
| } | ||
|
|
||
| getPointByY(y) { | ||
| if (y === undefined) { | ||
| throw new Error('the function should get a number') | ||
| } | ||
| if (typeof (y) != 'number') { | ||
| throw new Error('the function should get a number') | ||
| } | ||
| let x = (y - this.n) / this.slope; | ||
| return new Point({ x, y }) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,38 @@ | ||
| const Line = require('./ecs6-class/line') | ||
| const Point = require('./ecs6-class/point') | ||
|
|
||
| const calculateDistance = (point1, point2) => { | ||
| const calculateDistance = (point1, point2) => {//חישוב מרחק | ||
| if ((!(point1 instanceof Point)) || point1 === undefined) { | ||
| throw new Error('point1 must be of the Point class') | ||
| } | ||
| if (!(point2 instanceof Point) || point2 === undefined) { | ||
| throw new Error('point2 must be of the Point class') | ||
| } | ||
| 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) => { | ||
| const calculateJunctionPoint = (line1, line2) => {// נקודת אמצע | ||
| if (line1 === undefined || !(line1 instanceof Line)) { | ||
| throw new Error('line1 must be of the Line class') | ||
| } | ||
| if (line2 === undefined || !(line2 instanceof Line)) { | ||
| throw new Error('line2 must be of the Line class') | ||
| } | ||
| if(line1.slope===undefined){ | ||
| throw new Error('The slope in line1 has not yet been defined') | ||
| } | ||
| if(line2.slope===undefined){ | ||
| throw new Error('The slope in line2 has not yet been defined') | ||
| } | ||
| if(line1.n===undefined){ | ||
| throw new Error('The n in line1 has not yet been defined') | ||
| } | ||
| if(line2.n===undefined){ | ||
| throw new Error('The n in line2 has not yet been defined') | ||
| } | ||
| if (line1.slope === line2.slope) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are both slopes calculated? |
||
| if (line1.n === line2.n) { | ||
| return true | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are both n calculated already? |
||
|
|
@@ -24,8 +49,17 @@ const calculateJunctionPoint = (line1, line2) => { | |
| } | ||
|
|
||
| const isPointOnLine = (line, point) => { | ||
| if (!(line instanceof Line)) { | ||
| throw new Error('the function should get arg of "Line"') | ||
| } | ||
| if (typeof (point.x) !== 'number' || typeof (point.x) !== 'number' || !(point instanceof Point)) { | ||
| throw new Error('the function should get arg of "Point"') | ||
| } | ||
| const proxyLine = new Line({ point1: line.point1, point2: point }) | ||
| proxyLine.calculateSlope() | ||
| if(line.slope===undefined){ | ||
| throw new Error('The slope in line has not yet been defined') | ||
| } | ||
| if (line.slope === proxyLine.slope) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is the slope calculated? |
||
| proxyLine.calculateNOfLineFunction() | ||
| if (line.n === proxyLine.n) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,9 @@ | |
| "description": "practice unit tests in javascript", | ||
| "main": "index.js", | ||
| "scripts": { | ||
| "test": "jest" | ||
| "test": "jest", | ||
| "coverage": "npm run test -- --coverage" | ||
|
|
||
| }, | ||
| "dependencies": { | ||
| "jest": "^29.7.0" | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the jest module is not in the correct place |
||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| const Line = require('../../modules/ecs6-class/line'); | ||
| const point = require('../../modules/ecs6-class/point'); | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't declare on top of the module a list of parameter, each test should have its own. And if you need the same parameters for a group of tests, declare them inside the describe block in the |
||
| describe('CALCULATE_SIOPE', () => { | ||
| it('', () => { | ||
| const line1 = new Line({ point1: new point({ x: 1, y: 1 }), point2: new point({ x: 2, y: 2 }), n: 2, slope: 2 }) | ||
| line1.calculateSlope() | ||
| expect(line1.slope).toBe(1) | ||
| }); | ||
| }); | ||
|
|
||
| describe('ERRORS', () => { | ||
| it('errors for calculateSlope function', () => { | ||
| const line2 = new Line({}) | ||
| expect(() => line2.calculateSlope()).toThrow('the argument equal to 0') | ||
| }); | ||
| }); | ||
|
|
||
| describe('CALCULATE_N_OF_LINE_FUNCTION', () => { | ||
| it('', () => { | ||
| const line1 = new Line({ point1: new point({ x: 1, y: 1 }), point2: new point({ x: 2, y: 2 }), slope: 2 }) | ||
| line1.calculateNOfLineFunction() | ||
| expect(line1.n).toBe(-1) | ||
| }); | ||
| }); | ||
| describe('ERRORS', () => { | ||
| it('errors for calculateNOfLineFunction function', () => { | ||
| const line2 = new Line({}) | ||
| expect(() => line2.calculateNOfLineFunction()).toThrow('The slope has not yet been defined') | ||
| }); | ||
| }); | ||
|
|
||
| describe('GET_POINT_ON_X_ASIS', () => { | ||
| it('', () => { | ||
| const line1 = new Line({ n: 2, slope: 2 }) | ||
| expect(line1.getPointOnXAsis()).toEqual({ x: -1, y: 0 }) | ||
| }); | ||
| it('mock on getPointOnXAsis', () => { | ||
| let mPoint = new point({ x: 2, y: 1 }); | ||
| const line1 = new Line({ point1: new point({ x: 1, y: 1 }), point2: new point({ x: 2, y: 2 }), n: 2, slope: 2 }) | ||
| jest.spyOn(line1, 'getPointByX').mockImplementation((y) => { | ||
| const x = (y - line1.n) / line1.slope; | ||
| mPoint = new point({ x, y }); | ||
| return mPoint; | ||
| }); | ||
|
|
||
| const result2 = line1.getPointOnYAsis(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this test is a bit weird: you description is not what you really do |
||
| expect(result2).toEqual(mPoint); | ||
| }); | ||
| }); | ||
|
|
||
|
|
||
| describe('GET_POINT_ON_Y_ASIS', () => { | ||
| test('', () => { | ||
| const line1 = new Line({ n: 2, slope: 2 }) | ||
| expect(line1.getPointOnYAsis()).toEqual({ x: 0, y: 2 }) | ||
| }); | ||
| test('mock on getPointOnYAsis', () => { | ||
| let mPoint = new point({ x: 2, y: 1 }); | ||
| const line1 = new Line({ point1: new point({ x: 1, y: 1 }), point2: new point({ x: 2, y: 2 }), n: 2, slope: 2 }) | ||
| jest.spyOn(line1, 'getPointByX').mockImplementation((x) => { | ||
| const y = line1.slope * x + line1.n; | ||
| mPoint = new point({ x, y }); | ||
| return mPoint; | ||
| }); | ||
|
|
||
| const result2 = line1.getPointOnYAsis(); | ||
| expect(result2).toEqual(mPoint); | ||
| }); | ||
|
|
||
| }); | ||
|
|
||
|
|
||
| describe('GET_POINT_BY_Y', () => { | ||
| test('', () => { | ||
| const lin = new Line({ n: 0, slope: 1 }) | ||
| expect(lin.getPointByY(6)).toEqual({ x: 6, y: 6 }) | ||
| }); | ||
| }); | ||
|
|
||
| describe('ERRORS', () => { | ||
| test('check errors of getPointByY function', () => { | ||
| const TLine = new Line({}) | ||
| expect(() => TLine.getPointByY('c')).toThrow('the function should get a number') | ||
| expect(() => TLine.getPointByY([1, 2])).toThrow('the function should get a number') | ||
| expect(() => TLine.getPointByY(true)).toThrow('the function should get a number') | ||
| expect(() => TLine.getPointByY()).toThrow('the function should get a number') | ||
| expect(() => TLine.getPointByY(f => f)).toThrow('the function should get a number') | ||
| }); | ||
| }); | ||
|
|
||
|
|
||
| describe('GET_POINT_BY_X', () => { | ||
| test('', () => { | ||
| const lin1 = new Line({ n: 2, slope: 3 }) | ||
| expect(lin1.getPointByX(5)).toEqual({ x: 5, y: 17 }) | ||
| }); | ||
| }); | ||
|
|
||
| describe('ERRORS', () => { | ||
| test('check errors of getPointByX function', () => { | ||
| const TLine = new Line({}) | ||
| expect(() => TLine.getPointByX('c')).toThrow('the function should get a number') | ||
| expect(() => TLine.getPointByX([1, 2])).toThrow('the function should get a number') | ||
| expect(() => TLine.getPointByX(true)).toThrow('the function should get a number') | ||
| expect(() => TLine.getPointByX()).toThrow('the function should get a number') | ||
| expect(() => TLine.getPointByX((f) => f)).toThrow('the function should get a number') | ||
| }); | ||
| }); | ||
|
|
||
|
|
||
| describe('ERRORS', () => { | ||
| test('check errors of constactor point1 and point2', () => { | ||
| expect(() => new Line({ point1: 'c' })).toThrow('the constructor should get point1 arguments of "Point"') | ||
| expect(() => new Line({ point1: [1, 2] })).toThrow('the constructor should get point1 arguments of "Point"') | ||
| expect(() => new Line({ point1: true })).toThrow('the constructor should get point1 arguments of "Point"') | ||
| expect(() => new Line({ point1: (f) => f })).toThrow('the constructor should get point1 arguments of "Point"') | ||
| expect(() => new Line({ point1: () => f })).toThrow('the constructor should get point1 arguments of "Point"') | ||
| }); | ||
| test('check errors of constactor point2', () => { | ||
| expect(() => new Line({ point2: 'c' })).toThrow('the constructor should get point2 arguments of "Point"') | ||
| expect(() => new Line({ point2: [1, 2] })).toThrow('the constructor should get point2 arguments of "Point"') | ||
| expect(() => new Line({ point2: true })).toThrow('the constructor should get point2 arguments of "Point"') | ||
| expect(() => new Line({ point2: (f) => f })).toThrow('the constructor should get point2 arguments of "Point"') | ||
| expect(() => new Line({ point2: () => f })).toThrow('the constructor should get point2 arguments of "Point"') | ||
| }) | ||
| }); | ||
|
|
||
| describe('ERRORS', () => { | ||
| it('check errors of constactor n and slope', () => { | ||
| expect(() => new Line({ slope: 'c' })).toThrow('the slope in constractor should get undefined or number') | ||
| expect(() => new Line({ slope: [1, 2] })).toThrow('the slope in constractor should get undefined or number') | ||
| expect(() => new Line({ slope: true })).toThrow('the slope in constractor should get undefined or number') | ||
| expect(() => new Line({ slope: (f) => f })).toThrow('the slope in constractor should get undefined or number') | ||
| expect(() => new Line({ slope: () => f })).toThrow('the slope in constractor should get undefined or number'); | ||
|
|
||
| }); | ||
| it('check errors of constactor slope', () => { | ||
| expect(() => new Line({ n: 'c' })).toThrow('the n in constractor should get undefined or number') | ||
| expect(() => new Line({ n: [1, 2] })).toThrow('the n in constractor should get undefined or number') | ||
| expect(() => new Line({ n: true })).toThrow('the n in constractor should get undefined or number') | ||
| expect(() => new Line({ n: (f) => f })).toThrow('the n in constractor should get undefined or number') | ||
| expect(() => new Line({ n: () => f })).toThrow('the n in constractor should get undefined or number'); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| const point = require('../../modules/ecs6-class/point') | ||
| let point1 = new point({}) | ||
|
|
||
| describe('MOVE_VERTICAL', () => { | ||
| it('add a number of y', () => { | ||
| point1.moveVertical(5); | ||
| expect(point1).toEqual({ x: 0, y: 5 }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('MOVE_HORIZONTAL', () => { | ||
| it('add a number of x', () => { | ||
| point1.moveHorizontal(5); | ||
| expect(point1).toEqual({ x: 5, y: 5 }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('ERRORS', () => { | ||
| it('check errors of moveVertical function', () => { | ||
| expect(() => point1.moveVertical('c')).toThrow('the function should get a number') | ||
| expect(() => point1.moveVertical([1, 2])).toThrow('the function should get a number') | ||
| expect(() => point1.moveVertical(true)).toThrow('the function should get a number') | ||
| expect(() => point1.moveVertical()).toThrow('the function should get a number') | ||
| expect(() => point1.moveVertical((f) => f)).toThrow('the function should get a number') | ||
| }); | ||
| }); | ||
|
|
||
| describe('ERRORS', () => { | ||
| it('check errors of moveHorizontal function', () => { | ||
| expect(() => point1.moveHorizontal('c')).toThrow('the function should get a number') | ||
| expect(() => point1.moveHorizontal([1, 2])).toThrow('the function should get a number') | ||
| expect(() => point1.moveHorizontal(true)).toThrow('the function should get a number') | ||
| expect(() => point1.moveHorizontal()).toThrow('the function should get a number') | ||
| expect(() => point1.moveHorizontal((f) => f)).toThrow('the function should get a number') | ||
| }); | ||
| }); | ||
|
|
||
| describe('ERRORS', () => { | ||
| it('check errors of constactor', () => { | ||
| expect(() => new point({ x: 'c' })).toThrow('the constructor should get a number') | ||
| expect(() => new point({ y: [1, 2] })).toThrow('the constructor should get a number') | ||
| expect(() => new point({ x: true })).toThrow('the constructor should get a number') | ||
| expect(() => new point({ y: (f) => f })).toThrow('the constructor should get a number') | ||
| expect(() => new point({ x: () => f })).toThrow('the constructor should get a number') | ||
|
|
||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if point1.x - point2.x === 0??