-
Notifications
You must be signed in to change notification settings - Fork 41
full coverage for the geometry-calculation project #11
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?
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 |
|---|---|---|
| @@ -1,14 +1,25 @@ | ||
| class Point { | ||
| constructor({x=0, y=0}={}) { | ||
| if (!(typeof (x) === "number") || !(typeof (y) === "number")) | ||
| throw new Error('x and y must be of the number type') | ||
| this.x = x; | ||
| this.y = y; | ||
| } | ||
| moveVertical(value) { | ||
| if (typeof value !== 'number') { | ||
| throw new Error('Invalid input. value should be a number.'); | ||
| } | ||
| this.y += value; | ||
| } | ||
| moveHorizontal(value) { | ||
| if (typeof value !== 'number') { | ||
| throw new Error('Invalid input. value should be a number.'); | ||
| } | ||
| this.x += value; | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| module.exports = Point | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,27 @@ | ||
| const Line = require('./ecs6-class/line') | ||
| const Point=require('./ecs6-class/point') | ||
|
|
||
| const calculateDistance = (point1, point2) => { | ||
| if(!(point1 instanceof Point)&&!(point2 instanceof Point)) | ||
| throw new Error('point1 and point2 must be of the Point type') | ||
| if (!(point1 instanceof Point)) | ||
| throw new Error('point1 must be of the Point type') | ||
| if (!(point2 instanceof Point)) | ||
| throw new Error('point2 must be of the Point type') | ||
| 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)&&!(line2 instanceof Line)) | ||
| throw new Error('line1 and line2 must be of the Line type') | ||
| if (!(line1 instanceof Line)) | ||
| throw new Error('line1 must be of the Line type') | ||
| if (!(line2 instanceof Line)) | ||
| throw new Error('line2 must be of the Line type') | ||
| if (line1.slope === line2.slope) { | ||
| if (line1.n === line2.n) { | ||
| return true | ||
|
|
@@ -23,18 +37,27 @@ const calculateJunctionPoint = (line1, line2) => { | |
| } | ||
| } | ||
|
|
||
|
|
||
| const isPointOnLine = (line, point) => { | ||
| const proxyLine = new Line(line.point1, point) | ||
| if (!(line instanceof Line)&&!(point instanceof Point)) { | ||
| throw new Error("The object line should be of the Line type and object point must be of the Point type") | ||
| } | ||
| if (!(line instanceof Line)) { | ||
| throw new Error("The object line should be of the Line type") | ||
| } | ||
| if (!(point instanceof Point)) { | ||
| throw new Error("The object point should be of the Point type") | ||
| } | ||
| const proxyLine = new Line({ point1: line.point1, point2: point }) | ||
| proxyLine.calculateSlope() | ||
| 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 of line calculated? |
||
| proxyLine.calculateNOfLineFunction() | ||
| if (line.n === proxyLine.n2) { | ||
| if (line.n === proxyLine.n) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| module.exports = { | ||
| calculateDistance, | ||
| calculateJunctionPoint, | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
|
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. jest is not in the dependecy section |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| const Line =require('../../../modules/ecs6-class/line') | ||
| const Point = require('../../../modules/ecs6-class/point') | ||
| const{calculateSlope}=require('../../../modules/ecs6-class/line') | ||
|
|
||
| const mockConstructor1=jest.fn(constructor) | ||
| const mockConstructor=jest.fn(constructor) | ||
| const mockCalculateSlope=jest.fn(calculateSlope) | ||
|
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 way of mocks is total wrong |
||
|
|
||
| describe('constructor tests',()=>{ | ||
|
|
||
| it('should create a Line instance with correct slope, and n values',()=>{ | ||
| const l=new Line({n:4,slope:7}) | ||
| expect(l.slope).toBe(7) | ||
| expect(l.n).toBe(4) | ||
|
|
||
| }) | ||
| it('should throw an error for invalid input types',()=>{ | ||
| const l=new Line({n:true,slope:7}) | ||
| expect(()=>l.toThrow('Invalid input should be numbers')) | ||
| }) | ||
| }) | ||
| describe('calculacalculateSlope function test',()=>{ | ||
| it('calculacalculateSlope should throw an error when dividing by zeroteSlope',()=>{ | ||
| const p1=mockConstructor(new Point({x:5,y:6})) | ||
| const p2=mockConstructor(new Point({x:5,y:3})) | ||
| const line=mockConstructor1(new Line({point1:p1,point2:p2})) | ||
| expect(()=>line.calculateSlope()).toThrowError('error division by 0') | ||
|
|
||
| }) | ||
| it('should return 2',()=>{ | ||
| const line=mockConstructor1(new Line({point1:new Point({x:4,y:8}),point2:new Point({x:5,y:10})})) | ||
| line.calculateSlope() | ||
| expect(line.slope).toBe(2) | ||
|
|
||
| }) | ||
| }) | ||
| describe(' getPointByY',()=>{ | ||
| it('should return new point acoording the y value',()=>{ | ||
| const line=mockConstructor1(new Line({point1:new Point({x:0,y:4}),n:6,slope:2})); | ||
| const l=line.getPointByY(line.point1.y) | ||
| expect(l).toEqual(new Point({x:-1,y:4})) | ||
| }) | ||
| it('shuold throw error when divison by zero',()=>{ | ||
| const line=mockConstructor1(new Line({point1:new Point({x:0,y:4}),point2:new Point({x:0,y:6}),slope:0})); | ||
| expect(()=>line.getPointByY(line.point1.y)).toThrow("don't divide by 0") | ||
| }) | ||
| }) | ||
| describe('getPointByX tests',()=>{ | ||
| it('check if the function return the currect value of y',()=>{ | ||
| const line=mockConstructor1(new Line({point1:new Point({x:8,y:0}),n:7,slope:5})) | ||
| const l=line.getPointByX(line.point1.x) | ||
| expect(l).toEqual(new Point({x:8,y:47})) | ||
| }) | ||
| }) | ||
|
|
||
| describe('getPointOnXAsis tests', () => { | ||
| it('should return the point on the x-axis with y-coordinate 0', () => { | ||
| const line =mockConstructor1(new Line({ point1: new Point({ x: 8, y: 0 }), n: 7, slope: 5 })) | ||
| const pointOnXAxis = line.getPointOnXAsis(); | ||
| expect(pointOnXAxis).toEqual(new Point({ x: -1.4, y: 0 })); | ||
| }); | ||
| }); | ||
| describe('getPointOnYAsis tests', () => { | ||
| it('should return the point on the y-axis with x-coordinate 0', () => { | ||
| const line =mockConstructor1(new Line({ point1: new Point({ x: 0, y: 6 }), n: 7, slope: 5 })) ; | ||
| const pointOnYAxis = line.getPointOnYAsis(); | ||
| expect(pointOnYAxis).toEqual(new Point({ x: 0, y: 7 })); | ||
| }); | ||
| }); | ||
|
|
||
| describe('calculateNOfLineFunction tests',()=>{ | ||
| it('should update the n with the corrent value',()=>{ | ||
| const l=mockConstructor1(new Line({point1:new Point({x:0,y:0}),point2:new Point({x:2,y:2}),slope:5})) | ||
| mockCalculateSlope(l.calculateSlope()) | ||
| l.calculateNOfLineFunction() | ||
| expect(l.n).toEqual(0) | ||
| }) | ||
| }) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
|
|
||
| const Point = require('../../../modules/ecs6-class/point'); | ||
|
|
||
|
|
||
| const mockConstructor = jest.fn(constructor); | ||
|
|
||
| describe('Constructor tests', () => { | ||
| it ('check if the constructor get variable' ,()=>{ | ||
| const point=new Point({x:1,y:2}) | ||
| expect(point.x).toBe(1) | ||
| expect(point.y).toBe(2) | ||
| }) | ||
|
|
||
| it('should throw an error for invalid input types', () => { | ||
| expect(() => new Point({ x:"rudi",y:'5'})).toThrowError('x and y must be of the number type'); | ||
| }) | ||
| it('should throw error when the values is not number type', () => { | ||
| expect(() => new Point({ x: "#", y: 4 })).toThrowError('x and y must be of the number type'); | ||
| }) | ||
| it('should throw error when the values is not number type', () => { | ||
| expect(() => new Point({ x: 2, y: false })).toThrowError('x and y must be of the number type'); | ||
| }) | ||
| it('Point constructor should set default values if no parameters are provided', () => { | ||
| const point=new Point() | ||
| expect(point.x).toBe(0) | ||
| expect(point.y).toBe(0) | ||
|
|
||
| }) | ||
|
|
||
| }); | ||
| describe('check the function moveVertical',()=>{ | ||
| it('moveVertical should update the y coordinate correctly',()=>{ | ||
| const point = mockConstructor(new Point({ x: 1, y: 2 })) | ||
| point.moveVertical(3); | ||
| expect(point.y).toBe(5); | ||
| }) | ||
| it('moveVertical should only accept a number as input',()=>{ | ||
| const point = mockConstructor(new Point({ x: 0, y: 8 })) | ||
| expect(() => point.moveVertical('5')).toThrow('Invalid input. value should be a number.'); | ||
| }) | ||
| }) | ||
| describe('check the function moveHorizontal',()=>{ | ||
| it('moveHorizontal should update the y coordinate correctly',()=>{ | ||
| const point=mockConstructor(new Point(0,0)); | ||
| point.moveHorizontal(6) | ||
| expect(point.x).toBe(6) | ||
|
|
||
| }) | ||
| it('moveHorizontal should only accept a number as input',()=>{ | ||
| const point=mockConstructor(new Point({x:4,y:5})) | ||
| expect(() => point.moveHorizontal('invalid')).toThrow('Invalid input. value should be 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 one is not a number?
I expect to get => if x is not a number : new Error(the parameter x must be a number) and if y is not a number: new Error(parameter y is not a number) if both are not numbers: new Error(both parameter must be type of number)