-
Notifications
You must be signed in to change notification settings - Fork 41
build tests #21
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?
build tests #21
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,13 +1,45 @@ | ||
| 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('the function must get an arguments: point1 and point2!') | ||
| } | ||
|
|
||
| if((!(point1 instanceof Point))&&(!(point2 instanceof Point))){ | ||
| throw new Error("the arguments: point1 and point2 are not instance of 'Point'!") | ||
| } | ||
| if(!(point1 instanceof Point)){ | ||
| throw new Error("point1 is not instance of 'Point'!") | ||
| } | ||
| if(!(point2 instanceof Point)){ | ||
| throw new Error("point2 is not instance of '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('the function must get an arguments:line1 and line2!') | ||
| } | ||
| if((!(line1 instanceof Line))&&(!(line2 instanceof Line))){ | ||
| throw new Error("line1 and line2 are not instance of 'Line'!") | ||
| } | ||
| if(!(line1 instanceof Line)){ | ||
| throw new Error("line1 is not instance of 'Line'!") | ||
| } | ||
| if(!(line2 instanceof Line)){ | ||
| throw new Error("line2 is not instance of 'Line'!") | ||
| } | ||
| if(line1.slope===undefined || line2.slope===undefined){ | ||
| throw new Error('slope is undefined!') | ||
| } | ||
| if(line1.n===undefined || line2.n===undefined){ | ||
| throw new Error('n is undefined!') | ||
| } | ||
| 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. what happens if the slope is still undefined or the n is still undefined?
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 throw an error: You can calculate it |
||
| if (line1.n === line2.n) { | ||
| return true | ||
|
|
@@ -22,8 +54,19 @@ const calculateJunctionPoint = (line1, line2) => { | |
| return junctionPoint | ||
| } | ||
| } | ||
|
|
||
| const isPointOnLine = (line, point) => { | ||
| if(line===undefined || point===undefined){ | ||
| throw new Error('the function must get an arguments: line and point!') | ||
| } | ||
| if((!(line instanceof Line))&&(!(point instanceof Point))){ | ||
| throw new Error("line and point are not instance of 'Line' and 'Point!") | ||
| } | ||
| if(!(line instanceof Line)){ | ||
| throw new Error("line is not instance of 'Line'!") | ||
| } | ||
| if(!(point instanceof Point)){ | ||
| throw new Error("point is not instance of 'Point!") | ||
| } | ||
| const proxyLine = new Line({ point1: line.point1, point2: point }) | ||
| proxyLine.calculateSlope() | ||
| if (line.slope === proxyLine.slope) { | ||
|
|
@@ -39,4 +82,4 @@ module.exports = { | |
| calculateDistance, | ||
| calculateJunctionPoint, | ||
| isPointOnLine | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| const Line = require("../../../modules/ecs6-class/line") | ||
| const Point = require("../../../modules/ecs6-class/point") | ||
| let point1 = new Point({ x: 3, y: 4 }) | ||
| let point2 = new Point({ x: 1, y: 2 }) | ||
| let line = new Line({ point1, point2 }) | ||
| let line1 = new Line({ point1, point2 }) | ||
| const myline = new Line({}) | ||
|
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 all parameters before the tests |
||
|
|
||
| describe('LINE_CONSTRUCTOR', () => { | ||
|
|
||
| it('should check the line object', () => { | ||
| expect(line1.point1.x).toBe(3) | ||
| expect(line1.point1.y).toBe(4) | ||
| expect(line1.point2.x).toBe(1) | ||
| expect(line1.point2.y).toBe(2) | ||
| expect(line1.n).toBe(undefined) | ||
| expect(line1.slope).toBe(undefined) | ||
| }) | ||
|
|
||
| describe('ERROR_LINE_CONSTRUCTOR', () => { | ||
| it('should throw error if the line not valid', () => { | ||
| expect(() => new Line({ point1: new Line({}) })).toThrow("point1 not instance of 'Point'!") | ||
| expect(() => new Line({ point1: [1, 2, 3] })).toThrow("point1 not instance of 'Point'!") | ||
| expect(() => new Line({ point1: 'aaa' })).toThrow("point1 not instance of 'Point'!") | ||
| expect(() => new Line({ point2: new Line({}) })).toThrow("point2 not instance of 'Point'!") | ||
| expect(() => new Line({ point1: false })).toThrow("point1 not instance of 'Point'!") | ||
| expect(() => new Line({ n: 'hello' })).toThrow("n is not valid!") | ||
| expect(() => new Line({ slope: 'iii' })).toThrow("slope is not valid!") | ||
| expect(() => new Line({ point1: 'hello' , point2: 'hello' })).toThrow("point1 and point2 not instance of 'Point'!") | ||
| expect(() => new Line( {point1: 111 , point2: 222 })).toThrow("point1 and point2 not instance of 'Point'!") | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('CALCULATE_SLOPE', () => { | ||
| it('should calculate the slope', () => { | ||
| line.calculateSlope() | ||
| expect(line.slope).toBe(1) | ||
|
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. where are the errors? |
||
| }) | ||
| }) | ||
|
|
||
| describe('CALCULATE_N_LINE_FUNCTION', () => { | ||
| it('should calculate the n', () => { | ||
| line.calculateNOfLineFunction() | ||
| expect(line.n).toBe(1) | ||
| }) | ||
| }) | ||
|
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. where are the errors? |
||
|
|
||
| describe('RETURN_THE_POINT_ON_X_ASIS', () => { | ||
| it('should return a point on x axis', () => { | ||
| expect(line.getPointOnXAsis()).toEqual({ x: -1, y: 0 }) | ||
| }) | ||
| it('mock function on getPointOnXAsis function', () => { | ||
| jest.spyOn(line1, 'getPointByY').mockImplementation((y) => { | ||
| return { x: 0, y }; | ||
| }); | ||
| const points = line.getPointOnYAsis(); | ||
| expect(points).toEqual({ x: 0, y: 1 }); | ||
| }) | ||
| }) | ||
|
|
||
| describe('RETURN_THE_POINT_ON_Y_ASIS', () => { | ||
| it('should return a point on y axis', () => { | ||
| expect(line.getPointOnYAsis()).toEqual({ x: 0, y: 1 }) | ||
| }) | ||
| it('mock function on getPointOnYAsis function', () => { | ||
| jest.spyOn(line1, 'getPointByX').mockImplementation((x) => { | ||
| return { x, y: 0 }; | ||
| }); | ||
| const points = line.getPointOnXAsis(); | ||
| expect(points).toEqual({ x: -1, y: 0 }); | ||
| }) | ||
| }) | ||
|
|
||
| describe('GET_POINT_BY_X', () => { | ||
| it('should return y by x', () => { | ||
| expect(line.getPointByX(2)).toEqual({ x: 2, y: 3 }) | ||
gemtechd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }) | ||
| describe('ERROR', () => { | ||
| it('should throw error if the function:"getPointByX" not get/get not valid arguments', () => { | ||
| expect(() => line.getPointByX()).toThrow('x is undefined!') | ||
| expect(() => line.getPointByX(true)).toThrow('x is not a number!') | ||
| expect(() => line.getPointByX(() => false)).toThrow('x is not a number!') | ||
| expect(() => line.getPointByX([1, 2, 3, 4])).toThrow('x is not a number!') | ||
| expect(() => line.getPointByX({ x: 1, y: 2 })).toThrow('x is not a number!') | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('GET_POINT_BY_Y', () => { | ||
| it('should return x by y', () => { | ||
| expect(line.getPointByY(9)).toEqual({ x: 8, y: 9 }) | ||
| }) | ||
| describe('ERROR', () => { | ||
| it('should throw error if the function:"getPointByY" not get/get not valid arguments', () => { | ||
| expect(() => line.getPointByY()).toThrow('y is undefined!') | ||
| expect(() => line.getPointByY(true)).toThrow('y is not a number!') | ||
| expect(() => line.getPointByY(() => false)).toThrow('y is not a number!') | ||
| expect(() => line.getPointByY([1, 2, 3, 4])).toThrow('y is not a number!') | ||
| expect(() => line.getPointByY({ x: 1, y: 2 })).toThrow('y is not a number!') | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
|
|
||
|
|
||
|
|
||
gemtechd marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| const Point = require('../../../modules/ecs6-class/point') | ||
|
|
||
| describe('POINT_CONSTRUCTOR', () => { | ||
| let mypoint = new Point() | ||
| it('should check the point object', () => { | ||
| expect(mypoint.x).toBe(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. declare the parameters inside each test |
||
| expect(mypoint.y).toBe(0) | ||
| }) | ||
| describe('CONSTRUCTOR_POINT_ERROR', () => { | ||
| it('should throw error if the point not valid', () => { | ||
| expect(() => new Point({ x: 'x', y: 'y' })).toThrow('x is not a number!') | ||
| expect(() => new Point({ x: 'x' })).toThrow('x is not a number!') | ||
| expect(() => new Point({ y: 'y' })).toThrow('y is not a number!') | ||
| expect(() => new Point({ x: true })).toThrow('x is not a number!') | ||
| expect(() => new Point({ y: false })).toThrow('y is not a number!') | ||
| expect(() => new Point({ x: ['a', 'b'] })).toThrow('x is not a number!') | ||
| expect(() => new Point({ y: ['c', 'd'] })).toThrow('y is not a number!') | ||
| expect(() => new Point({ x: ['a', 'b'], y: ['c', 'd'] })).toThrow('x is not a number!') | ||
| expect(() => new Point({ x: [true, true, false] })).toThrow('x is not a number!') | ||
| expect(() => new Point({ y: [false, true] })).toThrow('y is not a number!') | ||
| expect(() => new Point({ x: [true, false], y: [false, true] })).toThrow('x is not a number!') | ||
| expect(() => new Point({ x: () => true })).toThrow('x is not a number!') | ||
| expect(() => new Point({ y: () => false })).toThrow('y is not a number!') | ||
| expect(() => new Point({ x: () => { }, y: () => true })).toThrow('x is not a number!') | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('MOVE_VERTICAL', () => { | ||
| it('should add to this.y the value', () => { | ||
| let point = new Point({}) | ||
| point.moveVertical(6) | ||
| expect(point).toEqual({ x: 0, y: 6 }); | ||
| }) | ||
|
|
||
| it('should add to this.y the value', () => { | ||
| let point = new Point({ x: 2, y: 3 }) | ||
| point.moveVertical(6) | ||
| expect(point).toEqual({ x: 2, y: 9 }); | ||
| }) | ||
|
|
||
| describe('ERROR', () => { | ||
| let mypoint = new Point() | ||
| it('should throw error if the function not get/get a valid argument', () => { | ||
| expect(() => mypoint.moveVertical(a => (a))).toThrow('the value is not a number!') | ||
| expect(() => mypoint.moveVertical(true)).toThrow('the value is not a number!') | ||
| expect(() => mypoint.moveVertical('aaa')).toThrow('the value is not a number!') | ||
| expect(() => mypoint.moveVertical(['aaa', 'bbb'])).toThrow('the value is not a number!') | ||
| expect(() => mypoint.moveVertical()).toThrow('the value is undefined!') | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('MOVE_HORIZONTAL', () => { | ||
|
|
||
| it('should add to this.x the value', () => { | ||
| let point = new Point({}) | ||
| point.moveHorizontal(6) | ||
| expect(point).toEqual({ x: 6, y: 0 }); | ||
| }) | ||
|
|
||
| it('should add to this.x the value', () => { | ||
| let point = new Point({ x: 5, y: 2 }) | ||
| point.moveHorizontal(6) | ||
| expect(point).toEqual({ x: 11, y: 2 }); | ||
| }) | ||
|
|
||
| describe('ERROR', () => { | ||
| let mypoint = new Point() | ||
| it('should throw error if the function not get/get a valid argument', () => { | ||
| expect(() => mypoint.moveHorizontal(v => (v))).toThrow('the value is not a number!') | ||
| expect(() => mypoint.moveHorizontal(true)).toThrow('the value is not a number!') | ||
| expect(() => mypoint.moveHorizontal('aaa')).toThrow('the value is not a number!') | ||
| expect(() => mypoint.moveHorizontal(['aaa', 'bbb'])).toThrow('the value is not a number!') | ||
| expect(() => mypoint.moveHorizontal()).toThrow('the value is undefined!') | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
|
|
||
|
|
||
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.
which argument?
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.
don't throw an error: You can calculate it